From 52dc7a61acd12845c8d34c6a7c989846237e235d Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 25 Oct 2013 22:19:57 -0400 Subject: [PATCH 01/25] indentation fix, no logic edits --- Essentials/pAntler/Antler.cpp | 316 +++++++++++++++++----------------- 1 file changed, 158 insertions(+), 158 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index 8f9b80f..e5f58c7 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -16,13 +16,13 @@ CAntler::CAntler() { - m_JobLock.UnLock(); + m_JobLock.UnLock(); m_bNewJob = false; m_sAntlerName = "Monarch"; m_sDBHost = "localhost"; m_nDBPort = 9000; m_bQuitCurrentJob = false; - m_eVerbosity=CHATTY; + m_eVerbosity=CHATTY; } //this is the vanilla version of Run - called to run from a single mission file @@ -51,7 +51,7 @@ bool CAntler::Run(const std::string & sHost, int nPort, const std::string & sAn MOOSTrace(" This is headless Antler called \"%s\"\n Waiting for mission file from %s:%d\n",m_sAntlerName.c_str(),m_sDBHost.c_str(),m_nDBPort); - const char * sSpin = "-\\|/"; + const char * sSpin = "-\\|/"; while(1) { //wait to be signalled that there is work to do... @@ -114,7 +114,7 @@ bool CAntler::DoRemoteControl() //suck out the Antler filter line std::string sFilter; std::getline(ss, sFilter); - MOOSTrace("%s\n", sFilter.c_str()); + MOOSTrace("%s\n", sFilter.c_str()); MOOSChomp(sFilter,"ANTLERFILTER:", true); std::stringstream ssF(sFilter); @@ -170,21 +170,21 @@ bool CAntler::DoRemoteControl() bool CAntler::SetVerbosity(VERBOSITY_LEVEL eLevel) { - m_eVerbosity = eLevel; - switch(eLevel) - { - case CHATTY: - break; - case TERSE: - InhibitMOOSTraceInThisThread(true); - break; - case QUIET: - InhibitMOOSTraceInThisThread(true); - if(m_pMOOSComms!=NULL) - m_pMOOSComms->SetQuiet(true); + m_eVerbosity = eLevel; + switch(eLevel) + { + case CHATTY: + break; + case TERSE: + InhibitMOOSTraceInThisThread(true); + break; + case QUIET: + InhibitMOOSTraceInThisThread(true); + if(m_pMOOSComms!=NULL) + m_pMOOSComms->SetQuiet(true); - } - return true; + } + return true; } bool CAntler::ConfigureMOOSComms() @@ -199,7 +199,7 @@ bool CAntler::ConfigureMOOSComms() m_pMOOSComms = new CMOOSCommClient; m_pMOOSComms->SetOnConnectCallBack(_MOOSConnectCB,this); - m_pMOOSComms->SetOnDisconnectCallBack(_MOOSDisconnectCB,this); + m_pMOOSComms->SetOnDisconnectCallBack(_MOOSDisconnectCB,this); m_pMOOSComms->SetQuiet(true); std::string sMe =MOOSFormat("Antler{%s}",m_sAntlerName.c_str()); @@ -279,7 +279,7 @@ bool CAntler::OnMOOSDisconnect() bool CAntler::PublishProcessQuit(const std::string & sProc) { - if(!m_bHeadless) + if(!m_bHeadless) return false; if(!m_pMOOSComms->IsConnected()) @@ -293,7 +293,7 @@ bool CAntler::PublishProcessQuit(const std::string & sProc) bool CAntler::PublishProcessLaunch(const std::string & sProc) { - if(!m_bHeadless) + if(!m_bHeadless) return false; if(!m_pMOOSComms->IsConnected()) @@ -309,58 +309,58 @@ bool CAntler::KillNicely(MOOSProc* pProc) { #ifndef _WIN32 - if(m_bSupportGentleKill) - { - if(pProc->m_bNewConsole) - { - //we need to be crafty.... - std::string sCmd = "ps -e -o ppid= -o pid="; + if(m_bSupportGentleKill) + { + if(pProc->m_bNewConsole) + { + //we need to be crafty.... + std::string sCmd = "ps -e -o ppid= -o pid="; - FILE* In = popen(sCmd.c_str(),"r"); + FILE* In = popen(sCmd.c_str(),"r"); - if(In!=NULL) - { - bool bFound = false; - char Line[256]; - while(fgets(Line,sizeof(Line),In)) - { - std::stringstream L(Line); - int ppid,pid; - L>>ppid; - L>>pid; + if(In!=NULL) + { + bool bFound = false; + char Line[256]; + while(fgets(Line,sizeof(Line),In)) + { + std::stringstream L(Line); + int ppid,pid; + L>>ppid; + L>>pid; - if(pProc->m_ChildPID==ppid) - { - kill(pid,SIGTERM); - bFound = true; - } - } - pclose(In); - return bFound; - } - else - { - return false; - } - } - else - { - //the proc ID we have is of the actuall MOOSProcess and not its parent - //we can just kill it... - kill(pProc->m_ChildPID,SIGTERM); - } - } - else - { - kill(pProc->m_ChildPID,SIGTERM); - } + if(pProc->m_ChildPID==ppid) + { + kill(pid,SIGTERM); + bFound = true; + } + } + pclose(In); + return bFound; + } + else + { + return false; + } + } + else + { + //the proc ID we have is of the actuall MOOSProcess and not its parent + //we can just kill it... + kill(pProc->m_ChildPID,SIGTERM); + } + } + else + { + kill(pProc->m_ChildPID,SIGTERM); + } #else - //MOOSTrace("Warning - gentle killing of win32 processes is not implemented\n"); - pProc->pWin32Proc->vTerminate(); + //MOOSTrace("Warning - gentle killing of win32 processes is not implemented\n"); + pProc->pWin32Proc->vTerminate(); return true; #endif - return true; + return true; } @@ -412,17 +412,17 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) } - //are we being ask to support gentle process killing? + //are we being ask to support gentle process killing? #ifdef _WIN32 - m_bSupportGentleKill= false; + m_bSupportGentleKill= false; #else - m_bSupportGentleKill = true; - m_MissionReader.GetConfigurationParam("GentleKill",m_bSupportGentleKill); + m_bSupportGentleKill = true; + m_MissionReader.GetConfigurationParam("GentleKill",m_bSupportGentleKill); #endif - //no cycle through each line in the configuration block. If it begins with run then it means launch + //no cycle through each line in the configuration block. If it begins with run then it means launch for(p = sParams.begin();p!=sParams.end();p++) { std::string sLine = *p; @@ -438,22 +438,22 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) if(pNew!=NULL) { - //this a really important bit of text most folk will want to see it... - InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); - { - MOOSTrace(" [%.3d] Process: %-15s ~ %-15s launched successfully\n", - m_nCurrentLaunch, - pNew->m_sApp.c_str(), - pNew->m_sMOOSName.c_str()); - } - InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); + //this a really important bit of text most folk will want to see it... + InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); + { + MOOSTrace(" [%.3d] Process: %-15s ~ %-15s launched successfully\n", + m_nCurrentLaunch, + pNew->m_sApp.c_str(), + pNew->m_sMOOSName.c_str()); + } + InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); m_ProcList.push_front(pNew); - m_nCurrentLaunch++; + m_nCurrentLaunch++; PublishProcessLaunch(pNew->m_sApp); } - //wait a while + //wait a while MOOSPause(nTimeMSBetweenSpawn); } @@ -497,7 +497,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) #ifdef _WIN32 if(m_bQuitCurrentJob) { - KillNicely(pMOOSProc); + KillNicely(pMOOSProc); } @@ -505,14 +505,14 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) if( pMOOSProc->pWin32Proc->dwGetExitCode()!=STILL_ACTIVE) { - //this a really important bit of text most folk will want to see it... - InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); - { - MOOSTrace(" [%.3d] Process: %-15s has quit\n", - --m_nCurrentLaunch, - pMOOSProc->m_sApp.c_str()); - } - InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); + //this a really important bit of text most folk will want to see it... + InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); + { + MOOSTrace(" [%.3d] Process: %-15s has quit\n", + --m_nCurrentLaunch, + pMOOSProc->m_sApp.c_str()); + } + InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); PublishProcessQuit(pMOOSProc->m_sApp); @@ -527,7 +527,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) if(m_bQuitCurrentJob) { MOOSTrace(" actively killing running child %s\n",pMOOSProc->m_sApp.c_str()); - KillNicely(pMOOSProc); + KillNicely(pMOOSProc); //just give it a little time - for pities sake - no need for this pause MOOSPause(300); } @@ -537,13 +537,13 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) if(waitpid(pMOOSProc->m_ChildPID,&nStatus,WNOHANG)>0) { - InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); - { - MOOSTrace(" [%.3d] Process: %-15s has quit\n", - --m_nCurrentLaunch, - pMOOSProc->m_sApp.c_str()); - } - InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); + InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); + { + MOOSTrace(" [%.3d] Process: %-15s has quit\n", + --m_nCurrentLaunch, + pMOOSProc->m_sApp.c_str()); + } + InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); PublishProcessQuit(pMOOSProc->m_sApp); @@ -595,49 +595,49 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchLis std::string sLaunchConfigurationName; std::string sLaunchConfiguration; #ifdef _WIN32 - bool bNIX = false; + bool bNIX = false; #else - bool bNIX= true; + bool bNIX= true; #endif - std::string sLaunchKey = bNIX?"XConfig":"Win32Config"; + std::string sLaunchKey = bNIX?"XConfig":"Win32Config"; if(!MOOSValFromString(sLaunchConfigurationName, sParam, sLaunchKey)) { //some applications are v.important in MOOS if not told otherwise they get special colours - string sBgColor = bNIX? "#DDDDFF" : ""; - string sFgColor = bNIX? "#000000" : ""; + string sBgColor = bNIX? "#DDDDFF" : ""; + string sFgColor = bNIX? "#000000" : ""; if(MOOSStrCmp(sProcName,"iRemote")) { - sBgColor = bNIX ? "#CC0000" : "RED"; - sFgColor = bNIX ? "#FFFFFF" : ""; + sBgColor = bNIX ? "#CC0000" : "RED"; + sFgColor = bNIX ? "#FFFFFF" : ""; } if(MOOSStrCmp(sProcName,"MOOSDB")) { - sBgColor = bNIX? "#003300" : "BLUE"; - sFgColor = bNIX? "#FFFFFF" : ""; + sBgColor = bNIX? "#003300" : "BLUE"; + sFgColor = bNIX? "#FFFFFF" : ""; } if(bNIX) - { - sLaunchConfiguration = "-geometry,"+MOOSFormat("80x12+2+%d",(m_nCurrentLaunch++)*50)+ - ",+sb,"+ - ",-fg,"+sFgColor+ - ",-bg,"+sBgColor+ - ",-T,"+MOOSFormat("%s %s",sProcName.c_str(),sMOOSName.empty()?"":MOOSFormat("as MOOSName \"%s\"",sMOOSName.c_str()).c_str()); - } - else - { - sLaunchConfiguration = sBgColor; - } + { + sLaunchConfiguration = "-geometry,"+MOOSFormat("80x12+2+%d",(m_nCurrentLaunch++)*50)+ + ",+sb,"+ + ",-fg,"+sFgColor+ + ",-bg,"+sBgColor+ + ",-T,"+MOOSFormat("%s %s",sProcName.c_str(),sMOOSName.empty()?"":MOOSFormat("as MOOSName \"%s\"",sMOOSName.c_str()).c_str()); + } + else + { + sLaunchConfiguration = sBgColor; + } } else { //OK look for this configuration string if(!m_MissionReader.GetConfigurationParam(sLaunchConfigurationName,sLaunchConfiguration)) - return MOOSFail(" warning: could not find resource string called \"%s\"",sLaunchConfigurationName.c_str()) ; + return MOOSFail(" warning: could not find resource string called \"%s\"",sLaunchConfigurationName.c_str()) ; } //OK now simply chomp our way through a space delimited list... @@ -647,7 +647,7 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchLis std::string sP = MOOSChomp(sLaunchConfiguration,","); MOOSTrimWhiteSpace(sP); if(!sP.empty()) - LaunchList.push_back(sP); + LaunchList.push_back(sP); } return !LaunchList.empty(); @@ -678,7 +678,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //if things go well this will eventually point to a new Process MOOSProc * pNewProc = NULL; - //look for tilde demarking end of param=val block + //look for tilde demarking end of param=val block string sOption = MOOSChomp(sParam,"~"); @@ -720,7 +720,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //do we want a new console? bool bNewConsole = false; - MOOSValFromString(bNewConsole, sOption, "NEWCONSOLE",true); + MOOSValFromString(bNewConsole, sOption, "NEWCONSOLE",true); //do we want to inhibit the passing of MOOS parameters (mission file and MOOSName) bool bInhibitMOOSParams = false; @@ -729,7 +729,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //by default process are assumed to be on the system path //users can specify an alternative path for all process by setting "ExectutablePath=" in the mission file //configuration block. - //user has the option of specifying paths individually process by process. + //user has the option of specifying paths individually process by process. //alternativelt they can specify that a particular process shouod be located by system wide and not //in the default executable path std::string sSpecifiedPath; @@ -738,7 +738,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) { if(MOOSStrCmp(sSpecifiedPath,"SYSTEM")) { - //do nothing - the system should know where to look + //do nothing - the system should know where to look } else { @@ -757,7 +757,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //here we figure out what MOOS name is implied if none is given (thanks to N.P and F.A) if(sMOOSName.empty()) - { + { std::string sTmp = sProcName; if(sTmp.rfind('/') != string::npos) { @@ -770,7 +770,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) return NULL; } sMOOSName = sTmp; - } + } //here we bail according to our filters @@ -800,7 +800,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) pNewProc->m_ExtraCommandLineParameters =ExtraCommandLineParameters; pNewProc->m_ConsoleLaunchParameters = sLaunchParams; pNewProc->m_bInhibitMOOSParams = bInhibitMOOSParams; - pNewProc->m_sMOOSName = sMOOSName; + pNewProc->m_sMOOSName = sMOOSName; pNewProc->m_bNewConsole = bNewConsole; pNewProc->m_sMissionFile = m_MissionReader.GetFileName(); @@ -824,34 +824,34 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) bool CAntler::ShutDown() { - MOOSPROC_LIST::iterator q; + MOOSPROC_LIST::iterator q; - MOOSTrace("\n\n|***** Shutdown *****|\n\n"); + MOOSTrace("\n\n|***** Shutdown *****|\n\n"); - for(q = m_ProcList.begin();q!=m_ProcList.end();q++) - { - MOOSProc * pMOOSProc = *q; + for(q = m_ProcList.begin();q!=m_ProcList.end();q++) + { + MOOSProc * pMOOSProc = *q; - if(KillNicely(pMOOSProc)) - { + if(KillNicely(pMOOSProc)) + { - int nStatus = 0; + int nStatus = 0; #ifndef _WIN32 - MOOSTrace("\n Signalling %-15s ",pMOOSProc->m_sApp.c_str()); - if(waitpid(pMOOSProc->m_ChildPID,&nStatus,0)>0) - { - MOOSTrace("[OK]"); - } + MOOSTrace("\n Signalling %-15s ",pMOOSProc->m_sApp.c_str()); + if(waitpid(pMOOSProc->m_ChildPID,&nStatus,0)>0) + { + MOOSTrace("[OK]"); + } #endif - } + } - } + } - MOOSTrace("\n\n All spawned processes shutdown.\n\n That was the MOOS \n\n"); + MOOSTrace("\n\n All spawned processes shutdown.\n\n That was the MOOS \n\n"); - return true; + return true; } @@ -870,7 +870,7 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) sCmd+=pNewProc->m_sMissionFile+" "+pNewProc->m_sMOOSName+" "; //continuing this task, here we pass extra parameters to the MOOS process if required - for(STRING_LIST::iterator p = pNewProc->m_ExtraCommandLineParameters.begin();p!=pNewProc->m_ExtraCommandLineParameters.end();p++) + for(STRING_LIST::iterator p = pNewProc->m_ExtraCommandLineParameters.begin();p!=pNewProc->m_ExtraCommandLineParameters.end();p++) { sCmd+=*p+" "; } @@ -899,14 +899,14 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) //set up white text StartUp.dwFillAttribute = - FOREGROUND_INTENSITY| - FOREGROUND_BLUE | - FOREGROUND_RED | - FOREGROUND_GREEN| - BACKGROUND_INTENSITY ; + FOREGROUND_INTENSITY| + FOREGROUND_BLUE | + FOREGROUND_RED | + FOREGROUND_GREEN| + BACKGROUND_INTENSITY ; - //give users basic control over backgroun as an RGB combination + //give users basic control over backgroun as an RGB combination for(STRING_LIST::iterator q=pNewProc->m_ConsoleLaunchParameters.begin();q!=pNewProc->m_ConsoleLaunchParameters.end();q++) { if(MOOSStrCmp(*q, "BACKGROUND_BLUE")) @@ -1027,12 +1027,12 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) } //Parent execution stream continues here... - //we need to change the process group of the spawned process so that ctrl-C - //does get sent to all the launched xterms. Instead we will catch ctrl-C ourself - //and find the MOOS process running within the sub process, signal it to die - //so subprocesses can clean up nicely - if(m_bSupportGentleKill) - setpgid(pNewProc->m_ChildPID,0); + //we need to change the process group of the spawned process so that ctrl-C + //does get sent to all the launched xterms. Instead we will catch ctrl-C ourself + //and find the MOOS process running within the sub process, signal it to die + //so subprocesses can clean up nicely + if(m_bSupportGentleKill) + setpgid(pNewProc->m_ChildPID,0); return true; } From 4410f2bd53a34660f57eac3e1c678d71a9506286 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 25 Oct 2013 22:36:18 -0400 Subject: [PATCH 02/25] some whitespace style fixes and long lines tamed. no logic changes --- Essentials/pAntler/Antler.cpp | 317 +++++++++++++++++++--------------- 1 file changed, 176 insertions(+), 141 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index e5f58c7..2c62757 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -14,8 +14,6 @@ using namespace std; #define DEBUG_LAUNCH 0 CAntler::CAntler() { - - m_JobLock.UnLock(); m_bNewJob = false; m_sAntlerName = "Monarch"; @@ -44,24 +42,27 @@ bool CAntler::Run(const std::string & sHost, int nPort, const std::string & sAn m_bHeadless = true; - if(!ConfigureMOOSComms()) + if (!ConfigureMOOSComms()) return true; - MOOSTrace(" This is headless Antler called \"%s\"\n Waiting for mission file from %s:%d\n",m_sAntlerName.c_str(),m_sDBHost.c_str(),m_nDBPort); + MOOSTrace(" This is headless Antler called \"%s\"\n" + "Waiting for mission file from %s:%d\n", + m_sAntlerName.c_str(), + m_sDBHost.c_str(), + m_nDBPort); const char * sSpin = "-\\|/"; - while(1) + while (1) { //wait to be signalled that there is work to do... int i = 0; - while(!m_bNewJob) + while (!m_bNewJob) { MOOSPause(500); MOOSTrace(" Speak to me Monarch....%c\r",sSpin[i++%3]); - } - + } //no more launching until this community is complete m_JobLock.Lock(); Spawn(m_sReceivedMissionFile,true); @@ -75,31 +76,31 @@ bool CAntler::Run(const std::string & sHost, int nPort, const std::string & sAn bool CAntler::DoRemoteControl() { - - - - while(1) + while (1) { MOOSPause(100); - if(!m_pMOOSComms->IsConnected()) + if (!m_pMOOSComms->IsConnected()) continue; //better check mail MOOSMSG_LIST NewMail; - if(m_pMOOSComms->Fetch(NewMail)) + if (m_pMOOSComms->Fetch(NewMail)) { CMOOSMsg Msg; - if(m_bHeadless) + if (m_bHeadless) { - if(m_pMOOSComms->PeekMail(NewMail,"MISSION_FILE",Msg)) + if (m_pMOOSComms->PeekMail(NewMail,"MISSION_FILE",Msg)) { MOOSTrace("\n|***** Dynamic Brief *****|\n\n"); //make a new file name - m_sReceivedMissionFile = MOOSFormat("runtime_%s.moos",MOOSGetTimeStampString().c_str()); + m_sReceivedMissionFile = MOOSFormat("runtime_%s.moos", + MOOSGetTimeStampString().c_str()); - MOOSTrace(" %s received [%d bytes]\n",m_sReceivedMissionFile.c_str(),Msg.GetString().size()); + MOOSTrace(" %s received [%d bytes]\n", + m_sReceivedMissionFile.c_str(), + Msg.GetString().size()); MOOSTrace(" shutting down all current spawned processes:\n"); //tell the current job to quit @@ -126,7 +127,7 @@ bool CAntler::DoRemoteControl() //write out the whole file std::ofstream Out(m_sReceivedMissionFile.c_str()); - if(!Out.is_open()) + if (!Out.is_open()) { m_JobLock.UnLock(); return MOOSFail("failed to open mission file for writing"); @@ -151,7 +152,7 @@ bool CAntler::DoRemoteControl() } else { - if(m_pMOOSComms->PeekAndCheckMail(NewMail, "ANTLER_STATUS", Msg)) + if (m_pMOOSComms->PeekAndCheckMail(NewMail, "ANTLER_STATUS", Msg)) { std::string sWhat; MOOSValFromString(sWhat, Msg.GetString(),"Action"); @@ -160,10 +161,12 @@ bool CAntler::DoRemoteControl() std::string sID; MOOSValFromString(sID, Msg.GetString(), "AntlerID"); - MOOSTrace(" [rmt] Process %-15s has %s (by %s)\n",sProc.c_str(),sWhat.c_str(),sID.c_str()); + MOOSTrace(" [rmt] Process %-15s has %s (by %s)\n", + sProc.c_str(), + sWhat.c_str(), + sID.c_str()); } } - } } } @@ -180,7 +183,7 @@ bool CAntler::SetVerbosity(VERBOSITY_LEVEL eLevel) break; case QUIET: InhibitMOOSTraceInThisThread(true); - if(m_pMOOSComms!=NULL) + if (m_pMOOSComms!=NULL) m_pMOOSComms->SetQuiet(true); } @@ -190,31 +193,27 @@ bool CAntler::SetVerbosity(VERBOSITY_LEVEL eLevel) bool CAntler::ConfigureMOOSComms() { - - //start a monitoring thread m_RemoteControlThread.Initialise(_RemoteControlCB, this); m_RemoteControlThread.Start(); - m_pMOOSComms = new CMOOSCommClient; m_pMOOSComms->SetOnConnectCallBack(_MOOSConnectCB,this); m_pMOOSComms->SetOnDisconnectCallBack(_MOOSDisconnectCB,this); m_pMOOSComms->SetQuiet(true); - std::string sMe =MOOSFormat("Antler{%s}",m_sAntlerName.c_str()); + std::string sMe = MOOSFormat("Antler{%s}",m_sAntlerName.c_str()); //try and connect to a DB - if(!m_pMOOSComms->Run(m_sDBHost.c_str(), (long int)m_nDBPort, sMe.c_str(), 1)) + if (!m_pMOOSComms->Run(m_sDBHost.c_str(), (long int)m_nDBPort, sMe.c_str(), 1)) return MOOSFail("could not set up MOOSComms\n"); - return true; } -bool CAntler::SendMissionFile( ) +bool CAntler::SendMissionFile() { MOOSTrace("\n\n|***** Propagate *****|\n\n"); CMOOSFileReader FR; @@ -223,10 +222,12 @@ bool CAntler::SendMissionFile( ) //copy the filters in ss<<"ANTLERFILTER:"; - std::copy (m_Filter.begin(), m_Filter.end(), ostream_iterator (ss, " ")); + std::copy (m_Filter.begin(), + m_Filter.end(), + ostream_iterator (ss, " ")); ss<Notify("MISSION_FILE",ss.str()); - MOOSTrace(" Monarch published thinned mission file [%d bytes]\n\n",ss.str().size()); + MOOSTrace(" Monarch published thinned mission file [%d bytes]\n\n", + ss.str().size()); return true; } + + bool CAntler::OnMOOSConnect() { - if(m_bHeadless) + if (m_bHeadless) { MOOSTrace(" Connecting to a DB\n"); m_pMOOSComms->Register("MISSION_FILE",0); @@ -253,14 +257,16 @@ bool CAntler::OnMOOSConnect() } return true; } + + bool CAntler::OnMOOSDisconnect() { - if(m_bHeadless) + if (m_bHeadless) { MOOSTrace(" DB Connection Lost\n"); - if(m_bKillOnDBDisconnect) + if (m_bKillOnDBDisconnect) { //look likes the monarch is dead..... MOOSTrace(" shutting down all current spawned processes:\n"); @@ -279,27 +285,32 @@ bool CAntler::OnMOOSDisconnect() bool CAntler::PublishProcessQuit(const std::string & sProc) { - if(!m_bHeadless) + if (!m_bHeadless) return false; - if(!m_pMOOSComms->IsConnected()) + if (!m_pMOOSComms->IsConnected()) return false; - m_pMOOSComms->Notify("ANTLER_STATUS",MOOSFormat("Action=Quit,Process=%s,AntlerID=%s",sProc.c_str(),m_sAntlerName.c_str())); - + m_pMOOSComms->Notify("ANTLER_STATUS", + MOOSFormat("Action=Quit,Process=%s,AntlerID=%s", + sProc.c_str(), + m_sAntlerName.c_str())); return true; } + bool CAntler::PublishProcessLaunch(const std::string & sProc) { - if(!m_bHeadless) + if (!m_bHeadless) return false; - if(!m_pMOOSComms->IsConnected()) + if (!m_pMOOSComms->IsConnected()) return false; - m_pMOOSComms->Notify("ANTLER_STATUS",MOOSFormat("Action=Launched,Process=%s,AntlerID=%s",sProc.c_str(),m_sAntlerName.c_str())); + m_pMOOSComms->Notify("ANTLER_STATUS", + MOOSFormat("Action=Launched,Process=%s,AntlerID=%s", + sProc.c_str(),m_sAntlerName.c_str())); return true; } @@ -309,27 +320,27 @@ bool CAntler::KillNicely(MOOSProc* pProc) { #ifndef _WIN32 - if(m_bSupportGentleKill) + if (m_bSupportGentleKill) { - if(pProc->m_bNewConsole) + if (pProc->m_bNewConsole) { //we need to be crafty.... std::string sCmd = "ps -e -o ppid= -o pid="; FILE* In = popen(sCmd.c_str(),"r"); - if(In!=NULL) + if (In!=NULL) { bool bFound = false; char Line[256]; - while(fgets(Line,sizeof(Line),In)) + while (fgets(Line,sizeof(Line),In)) { std::stringstream L(Line); int ppid,pid; L>>ppid; L>>pid; - if(pProc->m_ChildPID==ppid) + if (pProc->m_ChildPID==ppid) { kill(pid,SIGTERM); bFound = true; @@ -372,15 +383,16 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) //set up the mission file reader - if(!m_MissionReader.SetFile(sMissionFile)) + if (!m_MissionReader.SetFile(sMissionFile)) return MOOSFail("error reading mission file\n"); - m_MissionReader.SetAppName("ANTLER"); //NB no point in running under another name...(I guess Anter1 could launch Antler2 though...) + m_MissionReader.SetAppName("ANTLER"); //NB no point in running under another name... + // (I guess Antler1 could launch Antler2 though...) STRING_LIST sParams; - if(!m_MissionReader.GetConfiguration( m_MissionReader.GetAppName(),sParams)) + if (!m_MissionReader.GetConfiguration( m_MissionReader.GetAppName(),sParams)) return MOOSFail("error reading antler config block from mission file\n"); @@ -395,10 +407,10 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) m_sDefaultExecutablePath = "SYSTEMPATH"; m_MissionReader.GetConfigurationParam("ExecutablePath",m_sDefaultExecutablePath); - if(!MOOSStrCmp("SYSTEMPATH",m_sDefaultExecutablePath)) + if (!MOOSStrCmp("SYSTEMPATH",m_sDefaultExecutablePath)) { //MOOSTrace("\"ExecutablePath\" is %s\n",m_sDefaultExecutablePath.c_str()); - if(*m_sDefaultExecutablePath.rbegin()!='/') + if (*m_sDefaultExecutablePath.rbegin()!='/') { //look to add extra / if needed m_sDefaultExecutablePath+='/'; @@ -422,21 +434,22 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) - //no cycle through each line in the configuration block. If it begins with run then it means launch - for(p = sParams.begin();p!=sParams.end();p++) + //now cycle through each line in the configuration block. + // If it begins with run then it means launch + for (p = sParams.begin();p!=sParams.end();p++) { std::string sLine = *p; std::string sWhat = MOOSChomp(sLine,"="); - if(MOOSStrCmp(sWhat,"RUN")) + if (MOOSStrCmp(sWhat,"RUN")) { //OK we are being asked to run a process //try to create a process MOOSProc* pNew = CreateMOOSProcess(sLine); - if(pNew!=NULL) + if (pNew!=NULL) { //this a really important bit of text most folk will want to see it... InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); @@ -461,18 +474,18 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) - if(bHeadless==false) + if (bHeadless==false) { bool bMaster=false; m_MissionReader.GetConfigurationParam("EnableDistributed",bMaster); - if(bMaster) + if (bMaster) { m_MissionReader.GetValue("ServerHost",m_sDBHost); m_MissionReader.GetValue("ServerPort",m_nDBPort); - if(!ConfigureMOOSComms()) + if (!ConfigureMOOSComms()) return MOOSFail("failed to start MOOS comms"); } @@ -486,23 +499,23 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) //now wait on all our processes to close.... - while(m_ProcList.size()!=0) + while (m_ProcList.size()!=0) { MOOSPROC_LIST::iterator q; - for(q = m_ProcList.begin();q!=m_ProcList.end();q++) + for (q = m_ProcList.begin();q!=m_ProcList.end();q++) { MOOSProc * pMOOSProc = *q; #ifdef _WIN32 - if(m_bQuitCurrentJob) + if (m_bQuitCurrentJob) { KillNicely(pMOOSProc); } pMOOSProc->pWin32Proc->vWaitForTerminate(100); - if( pMOOSProc->pWin32Proc->dwGetExitCode()!=STILL_ACTIVE) + if ( pMOOSProc->pWin32Proc->dwGetExitCode()!=STILL_ACTIVE) { //this a really important bit of text most folk will want to see it... @@ -524,9 +537,10 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) break; } #else - if(m_bQuitCurrentJob) + if (m_bQuitCurrentJob) { - MOOSTrace(" actively killing running child %s\n",pMOOSProc->m_sApp.c_str()); + MOOSTrace(" actively killing running child %s\n", + pMOOSProc->m_sApp.c_str()); KillNicely(pMOOSProc); //just give it a little time - for pities sake - no need for this pause MOOSPause(300); @@ -534,7 +548,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) int nStatus = 0; - if(waitpid(pMOOSProc->m_ChildPID,&nStatus,WNOHANG)>0) + if (waitpid(pMOOSProc->m_ChildPID,&nStatus,WNOHANG)>0) { InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); @@ -558,38 +572,40 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) } - - return 0; - } -bool CAntler::MakeExtraExecutableParameters(std::string sParam,STRING_LIST & ExtraCommandLineParameters,std::string sProcName,std::string sMOOSName) +bool CAntler::MakeExtraExecutableParameters(std::string sParam, + STRING_LIST & ExtraCommandLineParameters, + std::string sProcName,std::string sMOOSName) { ExtraCommandLineParameters.clear(); std::string sExtraParamsName; - if(!MOOSValFromString(sExtraParamsName, sParam, "ExtraProcessParams", true)) + if (!MOOSValFromString(sExtraParamsName, sParam, "ExtraProcessParams", true)) return true;//nothing to do std::string sExtraParams; //OK look for this configuration string - if(!m_MissionReader.GetConfigurationParam(sExtraParamsName,sExtraParams)) - return MOOSFail(" warning cannot find extra parameters named \"%s\"\n",sExtraParamsName.c_str()); + if (!m_MissionReader.GetConfigurationParam(sExtraParamsName,sExtraParams)) + return MOOSFail(" warning cannot find extra parameters named \"%s\"\n", + sExtraParamsName.c_str()); - while(!sExtraParams.empty()) + while (!sExtraParams.empty()) ExtraCommandLineParameters.push_back(MOOSChomp(sExtraParams,",")); return true; } -bool CAntler::MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchList,std::string sProcName,std::string sMOOSName) +bool CAntler::MakeConsoleLaunchParams(std::string sParam, + STRING_LIST & LaunchList,std::string sProcName, + std::string sMOOSName) { //sParam is a string in the Run=ProcName @ sParam ~ MOOSName std::string sLaunchConfigurationName; @@ -603,29 +619,36 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchLis std::string sLaunchKey = bNIX?"XConfig":"Win32Config"; - if(!MOOSValFromString(sLaunchConfigurationName, sParam, sLaunchKey)) + if (!MOOSValFromString(sLaunchConfigurationName, sParam, sLaunchKey)) { - //some applications are v.important in MOOS if not told otherwise they get special colours + //some applications are v.important in MOOS + // -- if not told otherwise they get special colours string sBgColor = bNIX? "#DDDDFF" : ""; string sFgColor = bNIX? "#000000" : ""; - if(MOOSStrCmp(sProcName,"iRemote")) + if (MOOSStrCmp(sProcName,"iRemote")) { sBgColor = bNIX ? "#CC0000" : "RED"; sFgColor = bNIX ? "#FFFFFF" : ""; } - if(MOOSStrCmp(sProcName,"MOOSDB")) + if (MOOSStrCmp(sProcName,"MOOSDB")) { sBgColor = bNIX? "#003300" : "BLUE"; sFgColor = bNIX? "#FFFFFF" : ""; } - if(bNIX) + if (bNIX) { - sLaunchConfiguration = "-geometry,"+MOOSFormat("80x12+2+%d",(m_nCurrentLaunch++)*50)+ - ",+sb,"+ - ",-fg,"+sFgColor+ - ",-bg,"+sBgColor+ - ",-T,"+MOOSFormat("%s %s",sProcName.c_str(),sMOOSName.empty()?"":MOOSFormat("as MOOSName \"%s\"",sMOOSName.c_str()).c_str()); + sLaunchConfiguration = "-geometry," + + MOOSFormat("80x12+2+%d", (m_nCurrentLaunch++)*50) + + ",+sb," + + ",-fg," + sFgColor + + ",-bg," + sBgColor + + ",-T," + MOOSFormat("%s %s", + sProcName.c_str(), + sMOOSName.empty() + ? "" + : MOOSFormat("as MOOSName \"%s\"", + sMOOSName.c_str()).c_str()); } else { @@ -636,17 +659,19 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchLis { //OK look for this configuration string - if(!m_MissionReader.GetConfigurationParam(sLaunchConfigurationName,sLaunchConfiguration)) - return MOOSFail(" warning: could not find resource string called \"%s\"",sLaunchConfigurationName.c_str()) ; + if (!m_MissionReader.GetConfigurationParam(sLaunchConfigurationName, + sLaunchConfiguration)) + return MOOSFail(" warning: could not find resource string called \"%s\"", + sLaunchConfigurationName.c_str()) ; } //OK now simply chomp our way through a space delimited list... - while(!sLaunchConfiguration.empty()) + while (!sLaunchConfiguration.empty()) { MOOSTrimWhiteSpace(sLaunchConfiguration); std::string sP = MOOSChomp(sLaunchConfiguration,","); MOOSTrimWhiteSpace(sP); - if(!sP.empty()) + if (!sP.empty()) LaunchList.push_back(sP); } @@ -667,7 +692,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //further parameters are to left left of @ string sParam = sConfiguration; - if(sProcName.empty()) + if (sProcName.empty()) { MOOSTrace("no process specified - RUN=???\n"); return NULL; @@ -686,19 +711,19 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) bool bDistributed=false; m_MissionReader.GetConfigurationParam("EnableDistributed",bDistributed); - if(bDistributed) + if (bDistributed) { - if(m_bHeadless) + if (m_bHeadless) { //we are a drone std::string sAntlerRequired; - if(!MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) + if (!MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) return NULL; //this is for primary Antler - if(!MOOSStrCmp(sAntlerRequired, m_sAntlerName)) + if (!MOOSStrCmp(sAntlerRequired, m_sAntlerName)) return NULL; //for some other Antler //OK it is for us... @@ -708,7 +733,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) { //we are a TopMOOS std::string sAntlerRequired; - if(MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) + if (MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) return NULL; //this is for a drone } @@ -727,16 +752,16 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) MOOSValFromString(bInhibitMOOSParams, sOption, "InhibitMOOSParams", true); //by default process are assumed to be on the system path - //users can specify an alternative path for all process by setting "ExectutablePath=" in the mission file - //configuration block. + //users can specify an alternative path for all process by setting + // "ExectutablePath=" in the mission file configuration block. //user has the option of specifying paths individually process by process. - //alternativelt they can specify that a particular process shouod be located by system wide and not - //in the default executable path + //alternativelt they can specify that a particular process should be located by + // system wide and not in the default executable path std::string sSpecifiedPath; std::string sFullProcName=sProcName; - if(MOOSValFromString(sSpecifiedPath, sOption, "PATH",true)) + if (MOOSValFromString(sSpecifiedPath, sOption, "PATH",true)) { - if(MOOSStrCmp(sSpecifiedPath,"SYSTEM")) + if (MOOSStrCmp(sSpecifiedPath,"SYSTEM")) { //do nothing - the system should know where to look } @@ -756,17 +781,18 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) string sMOOSName = MOOSChomp(sParam); //here we figure out what MOOS name is implied if none is given (thanks to N.P and F.A) - if(sMOOSName.empty()) + if (sMOOSName.empty()) { std::string sTmp = sProcName; - if(sTmp.rfind('/') != string::npos) + if (sTmp.rfind('/') != string::npos) { sTmp = sTmp.substr(sTmp.rfind('/')+1); } - if(sTmp.empty()) + if (sTmp.empty()) { // ended with a / ? - MOOSTrace("Error in configuration -MOOS Name cannot end in \" / \" : %s\n",sProcName.c_str()); + MOOSTrace("Error in configuration -MOOS Name cannot end in \" / \" : %s\n", + sProcName.c_str()); return NULL; } sMOOSName = sTmp; @@ -774,23 +800,27 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //here we bail according to our filters - if(!m_Filter.empty() && m_Filter.find(sMOOSName)==m_Filter.end()) + if (!m_Filter.empty() && m_Filter.find(sMOOSName)==m_Filter.end()) { return NULL; } - //it is pssible to specifiy complicated parameters to the process being launched. (For example - //xterm being passed a whole load of configurations and then the name of the MOOS process it should - //itself launch. This next call fills in a string list of such parameters + //it is pssible to specifiy complicated parameters to the process being launched. + // (For example xterm being passed a whole load of configurations and then the name + // of the MOOS process it should itself launch. + // This next call fills in a string list of such parameters STRING_LIST sLaunchParams; - if(bNewConsole) + if (bNewConsole) MakeConsoleLaunchParams(sOption, sLaunchParams,sProcName,sMOOSName); //here we figure what extra command line parameters should be given to the MOOS Process //being launched. STRING_LIST ExtraCommandLineParameters; - MakeExtraExecutableParameters(sOption,ExtraCommandLineParameters,sProcName,sMOOSName); + MakeExtraExecutableParameters(sOption, + ExtraCommandLineParameters, + sProcName, + sMOOSName); @@ -806,9 +836,9 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //finally spawn each according to his own #ifndef _WIN32 - if(DoNixOSLaunch(pNewProc)) + if (DoNixOSLaunch(pNewProc)) #else - if(DoWin32Launch(pNewProc)) + if (DoWin32Launch(pNewProc)) #endif { return pNewProc; @@ -828,18 +858,18 @@ bool CAntler::ShutDown() MOOSTrace("\n\n|***** Shutdown *****|\n\n"); - for(q = m_ProcList.begin();q!=m_ProcList.end();q++) + for (q = m_ProcList.begin();q!=m_ProcList.end();q++) { MOOSProc * pMOOSProc = *q; - if(KillNicely(pMOOSProc)) + if (KillNicely(pMOOSProc)) { int nStatus = 0; #ifndef _WIN32 MOOSTrace("\n Signalling %-15s ",pMOOSProc->m_sApp.c_str()); - if(waitpid(pMOOSProc->m_ChildPID,&nStatus,0)>0) + if (waitpid(pMOOSProc->m_ChildPID,&nStatus,0)>0) { MOOSTrace("[OK]"); } @@ -859,18 +889,17 @@ bool CAntler::ShutDown() #ifdef _WIN32 bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) { - - try { - // make the command line ProcessBinaryName MissionFile ProcessMOOSName string sCmd = pNewProc->m_sApp+" "; - if(pNewProc->m_bInhibitMOOSParams==false) + if (pNewProc->m_bInhibitMOOSParams==false) sCmd+=pNewProc->m_sMissionFile+" "+pNewProc->m_sMOOSName+" "; //continuing this task, here we pass extra parameters to the MOOS process if required - for(STRING_LIST::iterator p = pNewProc->m_ExtraCommandLineParameters.begin();p!=pNewProc->m_ExtraCommandLineParameters.end();p++) + for (STRING_LIST::iterator p = pNewProc->m_ExtraCommandLineParameters.begin(); + p != pNewProc->m_ExtraCommandLineParameters.end(); + p++) { sCmd+=*p+" "; } @@ -880,9 +909,10 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) pNewProc->pWin32Attrib = NULL; pNewProc->pWin32Attrib = new XPCProcessAttrib(NULL,(char *)sCmd.c_str()); - if(pNewProc->m_bNewConsole) + if (pNewProc->m_bNewConsole) { - pNewProc->pWin32Attrib->vSetCreationFlag(CREATE_NEW_CONSOLE | CREATE_SUSPENDED); + pNewProc->pWin32Attrib->vSetCreationFlag(CREATE_NEW_CONSOLE + | CREATE_SUSPENDED); } else { @@ -907,19 +937,21 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) //give users basic control over backgroun as an RGB combination - for(STRING_LIST::iterator q=pNewProc->m_ConsoleLaunchParameters.begin();q!=pNewProc->m_ConsoleLaunchParameters.end();q++) + for (STRING_LIST::iterator q = pNewProc->m_ConsoleLaunchParameters.begin(); + q!=pNewProc->m_ConsoleLaunchParameters.end(); + q++) { - if(MOOSStrCmp(*q, "BACKGROUND_BLUE")) + if (MOOSStrCmp(*q, "BACKGROUND_BLUE")) { StartUp.dwFillAttribute|=BACKGROUND_BLUE; StartUp.dwFlags|=STARTF_USEFILLATTRIBUTE; } - if(MOOSStrCmp(*q, "BACKGROUND_GREEN")) + if (MOOSStrCmp(*q, "BACKGROUND_GREEN")) { StartUp.dwFillAttribute|=BACKGROUND_GREEN; StartUp.dwFlags|=STARTF_USEFILLATTRIBUTE; } - if(MOOSStrCmp(*q, "BACKGROUND_RED")) + if (MOOSStrCmp(*q, "BACKGROUND_RED")) { StartUp.dwFillAttribute|=BACKGROUND_RED; StartUp.dwFlags|=STARTF_USEFILLATTRIBUTE; @@ -939,11 +971,11 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) } catch (XPCException & e) { - if(pNewProc->pWin32Attrib!=NULL) + if (pNewProc->pWin32Attrib!=NULL) { delete pNewProc->pWin32Attrib; } - if(pNewProc->pWin32Proc!=NULL) + if (pNewProc->pWin32Proc!=NULL) { delete pNewProc->pWin32Proc; } @@ -961,28 +993,29 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) { //make a child process - if((pNewProc->m_ChildPID = fork())<0) + if ((pNewProc->m_ChildPID = fork())<0) { //hell! MOOSTrace("fork failed, not good\n"); return false; } - else if(pNewProc->m_ChildPID ==0) + else if (pNewProc->m_ChildPID ==0) { //I'm the child now.. STRING_LIST::iterator p = pNewProc->m_ConsoleLaunchParameters.begin(); - unsigned int nExecParams = pNewProc->m_ConsoleLaunchParameters.size()+pNewProc->m_ExtraCommandLineParameters.size()+ 6; + unsigned int nExecParams = pNewProc->m_ConsoleLaunchParameters.size() + + pNewProc->m_ExtraCommandLineParameters.size() + 6; const char ** pExecVParams = new const char* [nExecParams]; int i = 0; //do we need to configure an xterm? - if(pNewProc->m_bNewConsole) + if (pNewProc->m_bNewConsole) { pExecVParams[i++] = DEFAULT_NIX_TERMINAL; - if(!pNewProc->m_ConsoleLaunchParameters.empty()) + if (!pNewProc->m_ConsoleLaunchParameters.empty()) { - while(p!=pNewProc->m_ConsoleLaunchParameters.end()) + while (p!=pNewProc->m_ConsoleLaunchParameters.end()) { pExecVParams[i++] = (p++->c_str()); } @@ -994,7 +1027,7 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) //here we fill in the process name we really care about pExecVParams[i++] = (pNewProc->m_sApp.c_str()) ; - if(!pNewProc->m_bInhibitMOOSParams) + if (!pNewProc->m_bInhibitMOOSParams) { //we do the usual thing of supplying Mission file and MOOSName pExecVParams[i++] = pNewProc->m_sMissionFile.c_str(); @@ -1003,7 +1036,9 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) //here we pass extra parameters to the MOOS process if required - for(p = pNewProc->m_ExtraCommandLineParameters.begin();p!=pNewProc->m_ExtraCommandLineParameters.end();p++) + for (p = pNewProc->m_ExtraCommandLineParameters.begin(); + p != pNewProc->m_ExtraCommandLineParameters.end(); + p++) { pExecVParams[i++] = (p->c_str()); } @@ -1011,13 +1046,13 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) //terminate list pExecVParams[i++] = NULL; #if(DEBUG_LAUNCH) - for(int j = 0;j (pExecVParams); - if(execvp(pExecVParams[0], pParamList)==-1) + if (execvp(pExecVParams[0], pParamList)==-1) { MOOSTrace("Failed exec - not good. Called exec as follows:\n"); exit(EXIT_FAILURE); @@ -1031,7 +1066,7 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) //does get sent to all the launched xterms. Instead we will catch ctrl-C ourself //and find the MOOS process running within the sub process, signal it to die //so subprocesses can clean up nicely - if(m_bSupportGentleKill) + if (m_bSupportGentleKill) setpgid(pNewProc->m_ChildPID,0); return true; From 711733ad3cbf143fc09bd5440f931856fccd5eb5 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 25 Oct 2013 23:03:20 -0400 Subject: [PATCH 03/25] readability fixes -- if/else blocks swapped to keep shorter block on top. no logic changes. --- Essentials/pAntler/Antler.cpp | 260 ++++++++++++++++------------------ 1 file changed, 123 insertions(+), 137 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index 2c62757..5d9ba92 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -87,7 +87,24 @@ bool CAntler::DoRemoteControl() if (m_pMOOSComms->Fetch(NewMail)) { CMOOSMsg Msg; - if (m_bHeadless) + if (!m_bHeadless) + { + if (m_pMOOSComms->PeekAndCheckMail(NewMail, "ANTLER_STATUS", Msg)) + { + std::string sWhat; + MOOSValFromString(sWhat, Msg.GetString(),"Action"); + std::string sProc; + MOOSValFromString(sProc, Msg.GetString(),"Process"); + std::string sID; + MOOSValFromString(sID, Msg.GetString(), "AntlerID"); + + MOOSTrace(" [rmt] Process %-15s has %s (by %s)\n", + sProc.c_str(), + sWhat.c_str(), + sID.c_str()); + } + } + else // headless { if (m_pMOOSComms->PeekMail(NewMail,"MISSION_FILE",Msg)) @@ -103,10 +120,8 @@ bool CAntler::DoRemoteControl() Msg.GetString().size()); MOOSTrace(" shutting down all current spawned processes:\n"); - //tell the current job to quit + //tell the current job to quit, and wait for it to happen m_bQuitCurrentJob = true; - - //wait for that to happen m_JobLock.Lock(); //here we copy the mission file contained in the message to @@ -124,7 +139,6 @@ bool CAntler::DoRemoteControl() istream_iterator(), std::inserter(m_Filter,m_Filter.begin())); - //write out the whole file std::ofstream Out(m_sReceivedMissionFile.c_str()); if (!Out.is_open()) @@ -133,44 +147,19 @@ bool CAntler::DoRemoteControl() return MOOSFail("failed to open mission file for writing"); } - //you've gotta lurve C++ ... - Out<PeekAndCheckMail(NewMail, "ANTLER_STATUS", Msg)) - { - std::string sWhat; - MOOSValFromString(sWhat, Msg.GetString(),"Action"); - std::string sProc; - MOOSValFromString(sProc, Msg.GetString(),"Process"); - std::string sID; - MOOSValFromString(sID, Msg.GetString(), "AntlerID"); - - MOOSTrace(" [rmt] Process %-15s has %s (by %s)\n", - sProc.c_str(), - sWhat.c_str(), - sID.c_str()); - } - } } } } + bool CAntler::SetVerbosity(VERBOSITY_LEVEL eLevel) { m_eVerbosity = eLevel; @@ -190,6 +179,7 @@ bool CAntler::SetVerbosity(VERBOSITY_LEVEL eLevel) return true; } + bool CAntler::ConfigureMOOSComms() { @@ -263,7 +253,6 @@ bool CAntler::OnMOOSDisconnect() { if (m_bHeadless) { - MOOSTrace(" DB Connection Lost\n"); if (m_bKillOnDBDisconnect) @@ -283,6 +272,7 @@ bool CAntler::OnMOOSDisconnect() return true; } + bool CAntler::PublishProcessQuit(const std::string & sProc) { if (!m_bHeadless) @@ -318,59 +308,56 @@ bool CAntler::PublishProcessLaunch(const std::string & sProc) bool CAntler::KillNicely(MOOSProc* pProc) { -#ifndef _WIN32 - - if (m_bSupportGentleKill) +#ifdef _WIN32 + + //MOOSTrace("Warning - gentle killing of win32 processes is not implemented\n"); + pProc->pWin32Proc->vTerminate(); + +#else + + if (!m_bSupportGentleKill) + { + kill(pProc->m_ChildPID,SIGTERM); + } + else { - if (pProc->m_bNewConsole) + if (!pProc->m_bNewConsole) + { + //the proc ID we have is of the actuall MOOSProcess and not its parent + //we can just kill it... + kill(pProc->m_ChildPID,SIGTERM); + } + else { //we need to be crafty.... std::string sCmd = "ps -e -o ppid= -o pid="; FILE* In = popen(sCmd.c_str(),"r"); - if (In!=NULL) + if (In == NULL) return false; + + bool bFound = false; + char Line[256]; + while (fgets(Line,sizeof(Line),In)) { - bool bFound = false; - char Line[256]; - while (fgets(Line,sizeof(Line),In)) + std::stringstream L(Line); + int ppid,pid; + L>>ppid; + L>>pid; + + if (pProc->m_ChildPID==ppid) { - std::stringstream L(Line); - int ppid,pid; - L>>ppid; - L>>pid; - - if (pProc->m_ChildPID==ppid) - { - kill(pid,SIGTERM); - bFound = true; - } - } - pclose(In); - return bFound; - } - else - { - return false; - } - } - else - { - //the proc ID we have is of the actuall MOOSProcess and not its parent - //we can just kill it... - kill(pProc->m_ChildPID,SIGTERM); + kill(pid,SIGTERM); + bFound = true; + } + } + pclose(In); + return bFound; } } - else - { - kill(pProc->m_ChildPID,SIGTERM); - } -#else - //MOOSTrace("Warning - gentle killing of win32 processes is not implemented\n"); - pProc->pWin32Proc->vTerminate(); - return true; #endif + return true; } @@ -407,7 +394,12 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) m_sDefaultExecutablePath = "SYSTEMPATH"; m_MissionReader.GetConfigurationParam("ExecutablePath",m_sDefaultExecutablePath); - if (!MOOSStrCmp("SYSTEMPATH",m_sDefaultExecutablePath)) + if (MOOSStrCmp("SYSTEMPATH",m_sDefaultExecutablePath)) + { + MOOSTrace("Unless directed otherwise using system path to locate binaries \n"); + m_sDefaultExecutablePath=""; + } + else { //MOOSTrace("\"ExecutablePath\" is %s\n",m_sDefaultExecutablePath.c_str()); if (*m_sDefaultExecutablePath.rbegin()!='/') @@ -417,11 +409,6 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) } } - else - { - MOOSTrace("Unless directed otherwise using system path to locate binaries \n"); - m_sDefaultExecutablePath=""; - } //are we being ask to support gentle process killing? @@ -429,7 +416,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) m_bSupportGentleKill= false; #else m_bSupportGentleKill = true; - m_MissionReader.GetConfigurationParam("GentleKill",m_bSupportGentleKill); + m_MissionReader.GetConfigurationParam("GentleKill", m_bSupportGentleKill); #endif @@ -440,26 +427,26 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) { std::string sLine = *p; - std::string sWhat = MOOSChomp(sLine,"="); + std::string sWhat = MOOSChomp(sLine, "="); - if (MOOSStrCmp(sWhat,"RUN")) + if (MOOSStrCmp(sWhat, "RUN")) { //OK we are being asked to run a process //try to create a process MOOSProc* pNew = CreateMOOSProcess(sLine); - if (pNew!=NULL) + if (pNew != NULL) { //this a really important bit of text most folk will want to see it... - InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); + InhibitMOOSTraceInThisThread(m_eVerbosity == QUIET); { MOOSTrace(" [%.3d] Process: %-15s ~ %-15s launched successfully\n", m_nCurrentLaunch, pNew->m_sApp.c_str(), pNew->m_sMOOSName.c_str()); } - InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); + InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); m_ProcList.push_front(pNew); m_nCurrentLaunch++; @@ -474,9 +461,14 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) - if (bHeadless==false) + if (bHeadless) { - bool bMaster=false; + m_bKillOnDBDisconnect = true; + m_MissionReader.GetConfigurationParam("KillOnDBDisconnect",m_bKillOnDBDisconnect); + } + else + { + bool bMaster = false; m_MissionReader.GetConfigurationParam("EnableDistributed",bMaster); if (bMaster) { @@ -490,22 +482,17 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) } } - else - { - m_bKillOnDBDisconnect = true; - m_MissionReader.GetConfigurationParam("KillOnDBDisconnect",m_bKillOnDBDisconnect); - } //now wait on all our processes to close.... - while (m_ProcList.size()!=0) + while (m_ProcList.size() != 0) { MOOSPROC_LIST::iterator q; - for (q = m_ProcList.begin();q!=m_ProcList.end();q++) + for (q = m_ProcList.begin(); q != m_ProcList.end(); q++) { - MOOSProc * pMOOSProc = *q; + MOOSProc* pMOOSProc = *q; #ifdef _WIN32 if (m_bQuitCurrentJob) @@ -619,7 +606,16 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, std::string sLaunchKey = bNIX?"XConfig":"Win32Config"; - if (!MOOSValFromString(sLaunchConfigurationName, sParam, sLaunchKey)) + if (MOOSValFromString(sLaunchConfigurationName, sParam, sLaunchKey)) + { + + //OK look for this configuration string + if (!m_MissionReader.GetConfigurationParam(sLaunchConfigurationName, + sLaunchConfiguration)) + return MOOSFail(" warning: could not find resource string called \"%s\"", + sLaunchConfigurationName.c_str()) ; + } + else { //some applications are v.important in MOOS // -- if not told otherwise they get special colours @@ -631,12 +627,18 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, sBgColor = bNIX ? "#CC0000" : "RED"; sFgColor = bNIX ? "#FFFFFF" : ""; } + if (MOOSStrCmp(sProcName,"MOOSDB")) { sBgColor = bNIX? "#003300" : "BLUE"; sFgColor = bNIX? "#FFFFFF" : ""; } - if (bNIX) + + if (!bNIX) + { + sLaunchConfiguration = sBgColor; + } + else { sLaunchConfiguration = "-geometry," + MOOSFormat("80x12+2+%d", (m_nCurrentLaunch++)*50) @@ -650,19 +652,6 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, : MOOSFormat("as MOOSName \"%s\"", sMOOSName.c_str()).c_str()); } - else - { - sLaunchConfiguration = sBgColor; - } - } - else - { - - //OK look for this configuration string - if (!m_MissionReader.GetConfigurationParam(sLaunchConfigurationName, - sLaunchConfiguration)) - return MOOSFail(" warning: could not find resource string called \"%s\"", - sLaunchConfigurationName.c_str()) ; } //OK now simply chomp our way through a space delimited list... @@ -711,11 +700,21 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) bool bDistributed=false; m_MissionReader.GetConfigurationParam("EnableDistributed",bDistributed); - if (bDistributed) + if (!bDistributed) { - - - if (m_bHeadless) + //we run everything + } + else + { + if (!m_bHeadless) + { + //we are a TopMOOS + std::string sAntlerRequired; + if (MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) + return NULL; //this is for a drone + + } + else { //we are a drone std::string sAntlerRequired; @@ -729,18 +728,6 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //OK it is for us... //MOOSTrace("Headless Antler found a RUN directive...\n"); } - else - { - //we are a TopMOOS - std::string sAntlerRequired; - if (MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) - return NULL; //this is for a drone - - } - } - else - { - //we run everything } //do we want a new console? @@ -759,7 +746,12 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) // system wide and not in the default executable path std::string sSpecifiedPath; std::string sFullProcName=sProcName; - if (MOOSValFromString(sSpecifiedPath, sOption, "PATH",true)) + if (!MOOSValFromString(sSpecifiedPath, sOption, "PATH", true)) + { + //we just use the Anter-wide Exepath + sFullProcName = m_sDefaultExecutablePath+sProcName; + } + else { if (MOOSStrCmp(sSpecifiedPath,"SYSTEM")) { @@ -768,14 +760,9 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) else { //ok we are being told to look in a special place - sFullProcName=sSpecifiedPath+"/"+sProcName; + sFullProcName = sSpecifiedPath + "/" + sProcName; } } - else - { - //we just use the Anter-wide Exepath - sFullProcName = m_sDefaultExecutablePath+sProcName; - } //name of process as registered...is the first param after "~" string sMOOSName = MOOSChomp(sParam); @@ -823,7 +810,6 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) sMOOSName); - //All good up to here, now make a new process info holder stucture... pNewProc = new MOOSProc; pNewProc->m_sApp = sFullProcName; From 0517713a8df469a16ec58449f8efd1e9f67bbb36 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 25 Oct 2013 23:05:30 -0400 Subject: [PATCH 04/25] indentation fix --- Essentials/pAntler/Antler.h | 228 ++++++++++++++++++------------------ 1 file changed, 114 insertions(+), 114 deletions(-) diff --git a/Essentials/pAntler/Antler.h b/Essentials/pAntler/Antler.h index 46679fb..4465cd0 100644 --- a/Essentials/pAntler/Antler.h +++ b/Essentials/pAntler/Antler.h @@ -21,14 +21,14 @@ #include #ifdef _WIN32 - #include "XPCProcess.h" - #include "XPCProcessAttrib.h" - #include "XPCException.h" +#include "XPCProcess.h" +#include "XPCProcessAttrib.h" +#include "XPCException.h" #else - #include - #include - #include - #include +#include +#include +#include +#include #endif #define DEFAULT_NIX_TERMINAL "xterm" @@ -36,136 +36,136 @@ class CAntler - { - public: +{ +public: - //something to hold alient process information for the Antler class - struct MOOSProc - { + //something to hold alient process information for the Antler class + struct MOOSProc + { #ifdef _WIN32 - XPCProcess * pWin32Proc; - XPCProcessAttrib * pWin32Attrib; + XPCProcess * pWin32Proc; + XPCProcessAttrib * pWin32Attrib; #else - pid_t m_ChildPID; + pid_t m_ChildPID; #endif - std::string m_sApp; - std::string m_sMOOSName; - std::string m_sMissionFile; - bool m_bInhibitMOOSParams; - bool m_bNewConsole; - STRING_LIST m_ExtraCommandLineParameters; - STRING_LIST m_ConsoleLaunchParameters; + std::string m_sApp; + std::string m_sMOOSName; + std::string m_sMissionFile; + bool m_bInhibitMOOSParams; + bool m_bNewConsole; + STRING_LIST m_ExtraCommandLineParameters; + STRING_LIST m_ConsoleLaunchParameters; - }; + }; - public: +public: - //constructor - CAntler(); - //this is the only public function. Call it to have Antler do its thing. - bool Run(const std::string & sMissionFile,std::set Filter = std::set() ); + //constructor + CAntler(); + //this is the only public function. Call it to have Antler do its thing. + bool Run(const std::string & sMissionFile,std::set Filter = std::set() ); - //run in a headless fashion - instructions will be recieved via MOOSComms - bool Run(const std::string & sHost, int lPort, const std::string & sAntlerName); + //run in a headless fashion - instructions will be recieved via MOOSComms + bool Run(const std::string & sHost, int lPort, const std::string & sAntlerName); - enum VERBOSITY_LEVEL - { - QUIET, - TERSE, - CHATTY, - }; - bool SetVerbosity(VERBOSITY_LEVEL eLevel); + enum VERBOSITY_LEVEL + { + QUIET, + TERSE, + CHATTY, + }; + bool SetVerbosity(VERBOSITY_LEVEL eLevel); - //call this to cause a clean shut down - bool ShutDown(); + //call this to cause a clean shut down + bool ShutDown(); - protected: +protected: - //top level spawn - all comes from here - bool Spawn(const std::string & sMissionFile, bool bHeadless = false); + //top level spawn - all comes from here + bool Spawn(const std::string & sMissionFile, bool bHeadless = false); - //create, configure and launch a process - MOOSProc* CreateMOOSProcess(std:: string sProcName); + //create, configure and launch a process + MOOSProc* CreateMOOSProcess(std:: string sProcName); - // called to figure out what xterm parameters should be used with launch (ie where should the xterm be and how should it look) - bool MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchList,std::string sProcName,std::string sMOOSName); + // called to figure out what xterm parameters should be used with launch (ie where should the xterm be and how should it look) + bool MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchList,std::string sProcName,std::string sMOOSName); - //caled to figure out what if any additional parameters should be passed to the process being launched - bool MakeExtraExecutableParameters(std::string sParam,STRING_LIST & ExtraCommandLineParameters,std::string sProcName,std::string sMOOSName); + //caled to figure out what if any additional parameters should be passed to the process being launched + bool MakeExtraExecutableParameters(std::string sParam,STRING_LIST & ExtraCommandLineParameters,std::string sProcName,std::string sMOOSName); - //Functions responsible for actually start new processes according to OS + //Functions responsible for actually start new processes according to OS #ifndef _WIN32 - bool DoNixOSLaunch(MOOSProc * pNewProc); + bool DoNixOSLaunch(MOOSProc * pNewProc); #else - bool DoWin32Launch(MOOSProc *pNewProc); + bool DoWin32Launch(MOOSProc *pNewProc); #endif - bool ConfigureMOOSComms(); - bool SendMissionFile(); - - //tell a Monarch what is goinon remotely - bool PublishProcessQuit(const std::string & sProc); - bool PublishProcessLaunch(const std::string & sProc); - - typedef std::list MOOSPROC_LIST; - MOOSPROC_LIST m_ProcList; - std::string m_sDefaultExecutablePath; - CProcessConfigReader m_MissionReader; - - //if this set is non empty then only procs listed here will be run.. - std::set m_Filter; - - int m_nCurrentLaunch; - - //this is used to communicate with the BD and ultimately other instantiations of - //pAntler on different machines... - CMOOSThread m_RemoteControlThread; - CMOOSCommClient * m_pMOOSComms; - /**method to allow Listen thread to be launched with a MOOSThread.*/ - static bool _RemoteControlCB(void* pParam) - { - CAntler* pMe = (CAntler*) pParam; - return pMe->DoRemoteControl(); - } - /** internal MOOS client callbacks*/ - static bool _MOOSConnectCB(void *pParam) - { - CAntler* pMe = (CAntler*) pParam; - return pMe->OnMOOSConnect(); - } - static bool _MOOSDisconnectCB(void *pParam) - { - CAntler* pMe = (CAntler*) pParam; - return pMe->OnMOOSDisconnect(); - } - /** main comms handling thread*/ - bool DoRemoteControl(); - /** hellos MOOSDB!*/ - bool OnMOOSConnect(); - /** goodby MOOSDB*/ - bool OnMOOSDisconnect(); - - /** Kill a process gently */ - bool KillNicely(MOOSProc* pProc); + bool ConfigureMOOSComms(); + bool SendMissionFile(); - CMOOSLock m_JobLock; - std::string m_sMissionFile; - bool m_bHeadless; - bool m_bQuitCurrentJob; - bool m_bSupportGentleKill; - bool m_bRunning; - bool m_bNewJob; - std::string m_sMonarchAntler; - bool m_bKillOnDBDisconnect; - std::string m_sReceivedMissionFile; - std::string m_sAntlerName; - std::string m_sDBHost; - int m_nDBPort; - - VERBOSITY_LEVEL m_eVerbosity; + //tell a Monarch what is goinon remotely + bool PublishProcessQuit(const std::string & sProc); + bool PublishProcessLaunch(const std::string & sProc); + + typedef std::list MOOSPROC_LIST; + MOOSPROC_LIST m_ProcList; + std::string m_sDefaultExecutablePath; + CProcessConfigReader m_MissionReader; + + //if this set is non empty then only procs listed here will be run.. + std::set m_Filter; + + int m_nCurrentLaunch; + + //this is used to communicate with the BD and ultimately other instantiations of + //pAntler on different machines... + CMOOSThread m_RemoteControlThread; + CMOOSCommClient * m_pMOOSComms; + /**method to allow Listen thread to be launched with a MOOSThread.*/ + static bool _RemoteControlCB(void* pParam) + { + CAntler* pMe = (CAntler*) pParam; + return pMe->DoRemoteControl(); + } + /** internal MOOS client callbacks*/ + static bool _MOOSConnectCB(void *pParam) + { + CAntler* pMe = (CAntler*) pParam; + return pMe->OnMOOSConnect(); + } + static bool _MOOSDisconnectCB(void *pParam) + { + CAntler* pMe = (CAntler*) pParam; + return pMe->OnMOOSDisconnect(); + } + /** main comms handling thread*/ + bool DoRemoteControl(); + /** hellos MOOSDB!*/ + bool OnMOOSConnect(); + /** goodby MOOSDB*/ + bool OnMOOSDisconnect(); + + /** Kill a process gently */ + bool KillNicely(MOOSProc* pProc); + + CMOOSLock m_JobLock; + std::string m_sMissionFile; + bool m_bHeadless; + bool m_bQuitCurrentJob; + bool m_bSupportGentleKill; + bool m_bRunning; + bool m_bNewJob; + std::string m_sMonarchAntler; + bool m_bKillOnDBDisconnect; + std::string m_sReceivedMissionFile; + std::string m_sAntlerName; + std::string m_sDBHost; + int m_nDBPort; + + VERBOSITY_LEVEL m_eVerbosity; - }; +}; #endif From dde5d791fedd83f3a4ceacf6cec14a5862ea1018 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 25 Oct 2013 23:08:12 -0400 Subject: [PATCH 05/25] long lines tamed, no logic changes --- Essentials/pAntler/Antler.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Essentials/pAntler/Antler.h b/Essentials/pAntler/Antler.h index 4465cd0..8563628 100644 --- a/Essentials/pAntler/Antler.h +++ b/Essentials/pAntler/Antler.h @@ -63,7 +63,8 @@ class CAntler //constructor CAntler(); //this is the only public function. Call it to have Antler do its thing. - bool Run(const std::string & sMissionFile,std::set Filter = std::set() ); + bool Run(const std::string & sMissionFile, + std::set Filter = std::set() ); //run in a headless fashion - instructions will be recieved via MOOSComms bool Run(const std::string & sHost, int lPort, const std::string & sAntlerName); @@ -88,11 +89,19 @@ class CAntler //create, configure and launch a process MOOSProc* CreateMOOSProcess(std:: string sProcName); - // called to figure out what xterm parameters should be used with launch (ie where should the xterm be and how should it look) - bool MakeConsoleLaunchParams(std::string sParam,STRING_LIST & LaunchList,std::string sProcName,std::string sMOOSName); - - //caled to figure out what if any additional parameters should be passed to the process being launched - bool MakeExtraExecutableParameters(std::string sParam,STRING_LIST & ExtraCommandLineParameters,std::string sProcName,std::string sMOOSName); + // called to figure out what xterm parameters should be used with launch + // (ie where should the xterm be and how should it look) + bool MakeConsoleLaunchParams(std::string sParam, + STRING_LIST & LaunchList, + std::string sProcName, + std::string sMOOSName); + + //caled to figure out what if any additional parameters + //should be passed to the process being launched + bool MakeExtraExecutableParameters(std::string sParam, + STRING_LIST & ExtraCommandLineParameters, + std::string sProcName, + std::string sMOOSName); //Functions responsible for actually start new processes according to OS #ifndef _WIN32 From 9f6ab4d33b067b9634aff1f031d2ab2d07062cd7 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 25 Oct 2013 23:21:12 -0400 Subject: [PATCH 06/25] more readability changes, no logic changes --- Essentials/pAntler/Antler.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index 5d9ba92..f368940 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -619,8 +619,8 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, { //some applications are v.important in MOOS // -- if not told otherwise they get special colours - string sBgColor = bNIX? "#DDDDFF" : ""; - string sFgColor = bNIX? "#000000" : ""; + string sBgColor = bNIX ? "#DDDDFF" : ""; + string sFgColor = bNIX ? "#000000" : ""; if (MOOSStrCmp(sProcName,"iRemote")) { @@ -630,8 +630,8 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, if (MOOSStrCmp(sProcName,"MOOSDB")) { - sBgColor = bNIX? "#003300" : "BLUE"; - sFgColor = bNIX? "#FFFFFF" : ""; + sBgColor = bNIX ? "#003300" : "BLUE"; + sFgColor = bNIX ? "#FFFFFF" : ""; } if (!bNIX) @@ -640,17 +640,16 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, } else { + std::string sLabel = sMOOSName.empty() + ? "" + : MOOSFormat("as MOOSName \"%s\"", sMOOSName.c_str()).c_str()); + sLaunchConfiguration = "-geometry," + MOOSFormat("80x12+2+%d", (m_nCurrentLaunch++)*50) + ",+sb," + ",-fg," + sFgColor + ",-bg," + sBgColor - + ",-T," + MOOSFormat("%s %s", - sProcName.c_str(), - sMOOSName.empty() - ? "" - : MOOSFormat("as MOOSName \"%s\"", - sMOOSName.c_str()).c_str()); + + ",-T," + MOOSFormat("%s %s", sProcName.c_str(), sLabel); } } From 52c4b2c1c48f742970b46115f54a1de34779829c Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 25 Oct 2013 23:50:21 -0400 Subject: [PATCH 07/25] pedantic readability changes --- Essentials/pAntler/Antler.cpp | 184 +++++++++++++++++----------------- 1 file changed, 94 insertions(+), 90 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index f368940..978f557 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -20,11 +20,11 @@ CAntler::CAntler() m_sDBHost = "localhost"; m_nDBPort = 9000; m_bQuitCurrentJob = false; - m_eVerbosity=CHATTY; + m_eVerbosity = CHATTY; } //this is the vanilla version of Run - called to run from a single mission file -bool CAntler::Run(const std::string & sMissionFile,std::set Filter ) +bool CAntler::Run(const std::string & sMissionFile, std::set Filter) { m_bHeadless = false; m_sMissionFile = sMissionFile; @@ -33,7 +33,7 @@ bool CAntler::Run(const std::string & sMissionFile,std::set Filter } //this version will wait for a mission fiel to be sent via a DB -bool CAntler::Run(const std::string & sHost, int nPort, const std::string & sAntlerName) +bool CAntler::Run(const std::string & sHost, int nPort, const std::string & sAntlerName) { //this is more interesting... m_sAntlerName = sAntlerName; @@ -61,11 +61,11 @@ bool CAntler::Run(const std::string & sHost, int nPort, const std::string & sAn while (!m_bNewJob) { MOOSPause(500); - MOOSTrace(" Speak to me Monarch....%c\r",sSpin[i++%3]); + MOOSTrace(" Speak to me Monarch....%c\r", sSpin[i++%3]); } //no more launching until this community is complete m_JobLock.Lock(); - Spawn(m_sReceivedMissionFile,true); + Spawn(m_sReceivedMissionFile, true); m_bNewJob = false; m_JobLock.UnLock(); @@ -92,9 +92,9 @@ bool CAntler::DoRemoteControl() if (m_pMOOSComms->PeekAndCheckMail(NewMail, "ANTLER_STATUS", Msg)) { std::string sWhat; - MOOSValFromString(sWhat, Msg.GetString(),"Action"); + MOOSValFromString(sWhat, Msg.GetString(), "Action"); std::string sProc; - MOOSValFromString(sProc, Msg.GetString(),"Process"); + MOOSValFromString(sProc, Msg.GetString(), "Process"); std::string sID; MOOSValFromString(sID, Msg.GetString(), "AntlerID"); @@ -107,7 +107,7 @@ bool CAntler::DoRemoteControl() else // headless { - if (m_pMOOSComms->PeekMail(NewMail,"MISSION_FILE",Msg)) + if (m_pMOOSComms->PeekMail(NewMail, "MISSION_FILE", Msg)) { MOOSTrace("\n|***** Dynamic Brief *****|\n\n"); @@ -131,13 +131,13 @@ bool CAntler::DoRemoteControl() std::string sFilter; std::getline(ss, sFilter); MOOSTrace("%s\n", sFilter.c_str()); - MOOSChomp(sFilter,"ANTLERFILTER:", true); + MOOSChomp(sFilter, "ANTLERFILTER:", true); std::stringstream ssF(sFilter); //fill in the filter set std::copy(istream_iterator(ssF), istream_iterator(), - std::inserter(m_Filter,m_Filter.begin())); + std::inserter(m_Filter, m_Filter.begin())); //write out the whole file std::ofstream Out(m_sReceivedMissionFile.c_str()); @@ -172,7 +172,7 @@ bool CAntler::SetVerbosity(VERBOSITY_LEVEL eLevel) break; case QUIET: InhibitMOOSTraceInThisThread(true); - if (m_pMOOSComms!=NULL) + if (m_pMOOSComms != NULL) m_pMOOSComms->SetQuiet(true); } @@ -188,11 +188,11 @@ bool CAntler::ConfigureMOOSComms() m_RemoteControlThread.Start(); m_pMOOSComms = new CMOOSCommClient; - m_pMOOSComms->SetOnConnectCallBack(_MOOSConnectCB,this); - m_pMOOSComms->SetOnDisconnectCallBack(_MOOSDisconnectCB,this); + m_pMOOSComms->SetOnConnectCallBack(_MOOSConnectCB, this); + m_pMOOSComms->SetOnDisconnectCallBack(_MOOSDisconnectCB, this); m_pMOOSComms->SetQuiet(true); - std::string sMe = MOOSFormat("Antler{%s}",m_sAntlerName.c_str()); + std::string sMe = MOOSFormat("Antler{%s}", m_sAntlerName.c_str()); //try and connect to a DB if (!m_pMOOSComms->Run(m_sDBHost.c_str(), (long int)m_nDBPort, sMe.c_str(), 1)) @@ -222,7 +222,7 @@ bool CAntler::SendMissionFile() std::string sL = FR.GetNextValidLine()+"\n"; ss<Notify("MISSION_FILE",ss.str()); + m_pMOOSComms->Notify("MISSION_FILE", ss.str()); MOOSTrace(" Monarch published thinned mission file [%d bytes]\n\n", @@ -237,12 +237,12 @@ bool CAntler::OnMOOSConnect() if (m_bHeadless) { MOOSTrace(" Connecting to a DB\n"); - m_pMOOSComms->Register("MISSION_FILE",0); + m_pMOOSComms->Register("MISSION_FILE", 0); } else { - m_pMOOSComms->Register("ANTLER_STATUS",0); + m_pMOOSComms->Register("ANTLER_STATUS", 0); SendMissionFile(); } return true; @@ -300,7 +300,7 @@ bool CAntler::PublishProcessLaunch(const std::string & sProc) m_pMOOSComms->Notify("ANTLER_STATUS", MOOSFormat("Action=Launched,Process=%s,AntlerID=%s", - sProc.c_str(),m_sAntlerName.c_str())); + sProc.c_str(), m_sAntlerName.c_str())); return true; } @@ -317,7 +317,7 @@ bool CAntler::KillNicely(MOOSProc* pProc) if (!m_bSupportGentleKill) { - kill(pProc->m_ChildPID,SIGTERM); + kill(pProc->m_ChildPID, SIGTERM); } else { @@ -325,29 +325,29 @@ bool CAntler::KillNicely(MOOSProc* pProc) { //the proc ID we have is of the actuall MOOSProcess and not its parent //we can just kill it... - kill(pProc->m_ChildPID,SIGTERM); + kill(pProc->m_ChildPID, SIGTERM); } else { //we need to be crafty.... std::string sCmd = "ps -e -o ppid= -o pid="; - FILE* In = popen(sCmd.c_str(),"r"); + FILE* In = popen(sCmd.c_str(), "r"); if (In == NULL) return false; bool bFound = false; char Line[256]; - while (fgets(Line,sizeof(Line),In)) + while (fgets(Line, sizeof(Line), In)) { std::stringstream L(Line); - int ppid,pid; - L>>ppid; - L>>pid; + int ppid, pid; + L >> ppid; + L >> pid; - if (pProc->m_ChildPID==ppid) + if (pProc->m_ChildPID == ppid) { - kill(pid,SIGTERM); + kill(pid, SIGTERM); bFound = true; } } @@ -379,7 +379,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) STRING_LIST sParams; - if (!m_MissionReader.GetConfiguration( m_MissionReader.GetAppName(),sParams)) + if (!m_MissionReader.GetConfiguration(m_MissionReader.GetAppName(), sParams)) return MOOSFail("error reading antler config block from mission file\n"); @@ -387,25 +387,25 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) STRING_LIST::iterator p; sParams.reverse(); - int nTimeMSBetweenSpawn=DEFAULTTIMEBETWEENSPAWN; - m_MissionReader.GetConfigurationParam("MSBetweenLaunches",nTimeMSBetweenSpawn); + int nTimeMSBetweenSpawn = DEFAULTTIMEBETWEENSPAWN; + m_MissionReader.GetConfigurationParam("MSBetweenLaunches", nTimeMSBetweenSpawn); //here we'll figure out a what paths to use when looking for executables m_sDefaultExecutablePath = "SYSTEMPATH"; - m_MissionReader.GetConfigurationParam("ExecutablePath",m_sDefaultExecutablePath); + m_MissionReader.GetConfigurationParam("ExecutablePath", m_sDefaultExecutablePath); - if (MOOSStrCmp("SYSTEMPATH",m_sDefaultExecutablePath)) + if (MOOSStrCmp("SYSTEMPATH", m_sDefaultExecutablePath)) { MOOSTrace("Unless directed otherwise using system path to locate binaries \n"); - m_sDefaultExecutablePath=""; + m_sDefaultExecutablePath = ""; } else { - //MOOSTrace("\"ExecutablePath\" is %s\n",m_sDefaultExecutablePath.c_str()); - if (*m_sDefaultExecutablePath.rbegin()!='/') + //MOOSTrace("\"ExecutablePath\" is %s\n", m_sDefaultExecutablePath.c_str()); + if (*m_sDefaultExecutablePath.rbegin() != '/') { //look to add extra / if needed - m_sDefaultExecutablePath+='/'; + m_sDefaultExecutablePath += '/'; } } @@ -423,7 +423,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) //now cycle through each line in the configuration block. // If it begins with run then it means launch - for (p = sParams.begin();p!=sParams.end();p++) + for (p = sParams.begin(); p != sParams.end(); p++) { std::string sLine = *p; @@ -464,17 +464,17 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) if (bHeadless) { m_bKillOnDBDisconnect = true; - m_MissionReader.GetConfigurationParam("KillOnDBDisconnect",m_bKillOnDBDisconnect); + m_MissionReader.GetConfigurationParam("KillOnDBDisconnect", m_bKillOnDBDisconnect); } else { bool bMaster = false; - m_MissionReader.GetConfigurationParam("EnableDistributed",bMaster); + m_MissionReader.GetConfigurationParam("EnableDistributed", bMaster); if (bMaster) { - m_MissionReader.GetValue("ServerHost",m_sDBHost); - m_MissionReader.GetValue("ServerPort",m_nDBPort); + m_MissionReader.GetValue("ServerHost", m_sDBHost); + m_MissionReader.GetValue("ServerPort", m_nDBPort); if (!ConfigureMOOSComms()) @@ -502,17 +502,17 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) pMOOSProc->pWin32Proc->vWaitForTerminate(100); - if ( pMOOSProc->pWin32Proc->dwGetExitCode()!=STILL_ACTIVE) + if (pMOOSProc->pWin32Proc->dwGetExitCode() != STILL_ACTIVE) { //this a really important bit of text most folk will want to see it... - InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); + InhibitMOOSTraceInThisThread(m_eVerbosity == QUIET); { MOOSTrace(" [%.3d] Process: %-15s has quit\n", --m_nCurrentLaunch, pMOOSProc->m_sApp.c_str()); } - InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); + InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); PublishProcessQuit(pMOOSProc->m_sApp); @@ -535,16 +535,16 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) int nStatus = 0; - if (waitpid(pMOOSProc->m_ChildPID,&nStatus,WNOHANG)>0) + if (waitpid(pMOOSProc->m_ChildPID, &nStatus, WNOHANG)>0) { - InhibitMOOSTraceInThisThread(m_eVerbosity==QUIET); + InhibitMOOSTraceInThisThread(m_eVerbosity == QUIET); { MOOSTrace(" [%.3d] Process: %-15s has quit\n", --m_nCurrentLaunch, pMOOSProc->m_sApp.c_str()); } - InhibitMOOSTraceInThisThread(m_eVerbosity!=CHATTY); + InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); PublishProcessQuit(pMOOSProc->m_sApp); @@ -567,7 +567,8 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) bool CAntler::MakeExtraExecutableParameters(std::string sParam, STRING_LIST & ExtraCommandLineParameters, - std::string sProcName,std::string sMOOSName) + std::string sProcName, + std::string sMOOSName) { ExtraCommandLineParameters.clear(); @@ -579,19 +580,20 @@ bool CAntler::MakeExtraExecutableParameters(std::string sParam, std::string sExtraParams; //OK look for this configuration string - if (!m_MissionReader.GetConfigurationParam(sExtraParamsName,sExtraParams)) + if (!m_MissionReader.GetConfigurationParam(sExtraParamsName, sExtraParams)) return MOOSFail(" warning cannot find extra parameters named \"%s\"\n", sExtraParamsName.c_str()); while (!sExtraParams.empty()) - ExtraCommandLineParameters.push_back(MOOSChomp(sExtraParams,",")); + ExtraCommandLineParameters.push_back(MOOSChomp(sExtraParams, ",")); return true; } bool CAntler::MakeConsoleLaunchParams(std::string sParam, - STRING_LIST & LaunchList,std::string sProcName, + STRING_LIST & LaunchList, + std::string sProcName, std::string sMOOSName) { //sParam is a string in the Run=ProcName @ sParam ~ MOOSName @@ -622,13 +624,13 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, string sBgColor = bNIX ? "#DDDDFF" : ""; string sFgColor = bNIX ? "#000000" : ""; - if (MOOSStrCmp(sProcName,"iRemote")) + if (MOOSStrCmp(sProcName, "iRemote")) { sBgColor = bNIX ? "#CC0000" : "RED"; sFgColor = bNIX ? "#FFFFFF" : ""; } - if (MOOSStrCmp(sProcName,"MOOSDB")) + if (MOOSStrCmp(sProcName, "MOOSDB")) { sBgColor = bNIX ? "#003300" : "BLUE"; sFgColor = bNIX ? "#FFFFFF" : ""; @@ -657,7 +659,7 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, while (!sLaunchConfiguration.empty()) { MOOSTrimWhiteSpace(sLaunchConfiguration); - std::string sP = MOOSChomp(sLaunchConfiguration,","); + std::string sP = MOOSChomp(sLaunchConfiguration, ","); MOOSTrimWhiteSpace(sP); if (!sP.empty()) LaunchList.push_back(sP); @@ -675,7 +677,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //what tis its name? (if no @ symbol we just get the name and no cmdline) - string sProcName = MOOSChomp(sConfiguration,"@"); + string sProcName = MOOSChomp(sConfiguration, "@"); //further parameters are to left left of @ string sParam = sConfiguration; @@ -692,12 +694,12 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) MOOSProc * pNewProc = NULL; //look for tilde demarking end of param=val block - string sOption = MOOSChomp(sParam,"~"); + string sOption = MOOSChomp(sParam, "~"); - bool bDistributed=false; - m_MissionReader.GetConfigurationParam("EnableDistributed",bDistributed); + bool bDistributed = false; + m_MissionReader.GetConfigurationParam("EnableDistributed", bDistributed); if (!bDistributed) { @@ -731,7 +733,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //do we want a new console? bool bNewConsole = false; - MOOSValFromString(bNewConsole, sOption, "NEWCONSOLE",true); + MOOSValFromString(bNewConsole, sOption, "NEWCONSOLE", true); //do we want to inhibit the passing of MOOS parameters (mission file and MOOSName) bool bInhibitMOOSParams = false; @@ -744,7 +746,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //alternativelt they can specify that a particular process should be located by // system wide and not in the default executable path std::string sSpecifiedPath; - std::string sFullProcName=sProcName; + std::string sFullProcName = sProcName; if (!MOOSValFromString(sSpecifiedPath, sOption, "PATH", true)) { //we just use the Anter-wide Exepath @@ -752,7 +754,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) } else { - if (MOOSStrCmp(sSpecifiedPath,"SYSTEM")) + if (MOOSStrCmp(sSpecifiedPath, "SYSTEM")) { //do nothing - the system should know where to look } @@ -786,7 +788,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //here we bail according to our filters - if (!m_Filter.empty() && m_Filter.find(sMOOSName)==m_Filter.end()) + if (!m_Filter.empty() && m_Filter.find(sMOOSName) == m_Filter.end()) { return NULL; } @@ -797,7 +799,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) // This next call fills in a string list of such parameters STRING_LIST sLaunchParams; if (bNewConsole) - MakeConsoleLaunchParams(sOption, sLaunchParams,sProcName,sMOOSName); + MakeConsoleLaunchParams(sOption, sLaunchParams, sProcName, sMOOSName); //here we figure what extra command line parameters should be given to the MOOS Process @@ -812,7 +814,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //All good up to here, now make a new process info holder stucture... pNewProc = new MOOSProc; pNewProc->m_sApp = sFullProcName; - pNewProc->m_ExtraCommandLineParameters =ExtraCommandLineParameters; + pNewProc->m_ExtraCommandLineParameters = ExtraCommandLineParameters; pNewProc->m_ConsoleLaunchParameters = sLaunchParams; pNewProc->m_bInhibitMOOSParams = bInhibitMOOSParams; pNewProc->m_sMOOSName = sMOOSName; @@ -843,7 +845,7 @@ bool CAntler::ShutDown() MOOSTrace("\n\n|***** Shutdown *****|\n\n"); - for (q = m_ProcList.begin();q!=m_ProcList.end();q++) + for (q = m_ProcList.begin();q != m_ProcList.end(); q++) { MOOSProc * pMOOSProc = *q; @@ -853,8 +855,8 @@ bool CAntler::ShutDown() int nStatus = 0; #ifndef _WIN32 - MOOSTrace("\n Signalling %-15s ",pMOOSProc->m_sApp.c_str()); - if (waitpid(pMOOSProc->m_ChildPID,&nStatus,0)>0) + MOOSTrace("\n Signalling %-15s ", pMOOSProc->m_sApp.c_str()); + if (waitpid(pMOOSProc->m_ChildPID, &nStatus, 0)>0) { MOOSTrace("[OK]"); } @@ -877,22 +879,22 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) try { // make the command line ProcessBinaryName MissionFile ProcessMOOSName - string sCmd = pNewProc->m_sApp+" "; - if (pNewProc->m_bInhibitMOOSParams==false) - sCmd+=pNewProc->m_sMissionFile+" "+pNewProc->m_sMOOSName+" "; + string sCmd = pNewProc->m_sApp + " "; + if (pNewProc->m_bInhibitMOOSParams == false) + sCmd += pNewProc->m_sMissionFile + " " + pNewProc->m_sMOOSName + " "; //continuing this task, here we pass extra parameters to the MOOS process if required for (STRING_LIST::iterator p = pNewProc->m_ExtraCommandLineParameters.begin(); p != pNewProc->m_ExtraCommandLineParameters.end(); p++) { - sCmd+=*p+" "; + sCmd += *p + " "; } //make a new process attributes class pNewProc->pWin32Attrib = NULL; - pNewProc->pWin32Attrib = new XPCProcessAttrib(NULL,(char *)sCmd.c_str()); + pNewProc->pWin32Attrib = new XPCProcessAttrib(NULL, (char *)sCmd.c_str()); if (pNewProc->m_bNewConsole) { @@ -923,23 +925,23 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) //give users basic control over backgroun as an RGB combination for (STRING_LIST::iterator q = pNewProc->m_ConsoleLaunchParameters.begin(); - q!=pNewProc->m_ConsoleLaunchParameters.end(); + q != pNewProc->m_ConsoleLaunchParameters.end(); q++) { if (MOOSStrCmp(*q, "BACKGROUND_BLUE")) { - StartUp.dwFillAttribute|=BACKGROUND_BLUE; - StartUp.dwFlags|=STARTF_USEFILLATTRIBUTE; + StartUp.dwFillAttribute |= BACKGROUND_BLUE; + StartUp.dwFlags |= STARTF_USEFILLATTRIBUTE; } if (MOOSStrCmp(*q, "BACKGROUND_GREEN")) { - StartUp.dwFillAttribute|=BACKGROUND_GREEN; - StartUp.dwFlags|=STARTF_USEFILLATTRIBUTE; + StartUp.dwFillAttribute |= BACKGROUND_GREEN; + StartUp.dwFlags |= STARTF_USEFILLATTRIBUTE; } if (MOOSStrCmp(*q, "BACKGROUND_RED")) { - StartUp.dwFillAttribute|=BACKGROUND_RED; - StartUp.dwFlags|=STARTF_USEFILLATTRIBUTE; + StartUp.dwFillAttribute |= BACKGROUND_RED; + StartUp.dwFlags |= STARTF_USEFILLATTRIBUTE; } } @@ -948,7 +950,7 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) //no create an object capable of laucnhing win32 processes pNewProc->pWin32Proc = NULL; - pNewProc->pWin32Proc =new XPCProcess(* pNewProc->pWin32Attrib); + pNewProc->pWin32Proc = new XPCProcess(* pNewProc->pWin32Attrib); //go! pNewProc->pWin32Proc->vResume(); @@ -956,16 +958,16 @@ bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) } catch (XPCException & e) { - if (pNewProc->pWin32Attrib!=NULL) + if (pNewProc->pWin32Attrib != NULL) { delete pNewProc->pWin32Attrib; } - if (pNewProc->pWin32Proc!=NULL) + if (pNewProc->pWin32Proc != NULL) { delete pNewProc->pWin32Proc; } - MOOSTrace("*** %s Launch Failed:***\n\a\a",pNewProc->m_sApp.c_str()); - MOOSTrace("%s\n",e.sGetException()); + MOOSTrace("*** %s Launch Failed:***\n\a\a", pNewProc->m_sApp.c_str()); + MOOSTrace("%s\n", e.sGetException()); return false; } @@ -978,13 +980,15 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) { //make a child process - if ((pNewProc->m_ChildPID = fork())<0) + pNewProc->m_ChildPID = fork(); + if (pNewProc->m_ChildPID < 0) { //hell! MOOSTrace("fork failed, not good\n"); return false; } - else if (pNewProc->m_ChildPID ==0) + + if (pNewProc->m_ChildPID == 0) { //I'm the child now.. @@ -1000,7 +1004,7 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) pExecVParams[i++] = DEFAULT_NIX_TERMINAL; if (!pNewProc->m_ConsoleLaunchParameters.empty()) { - while (p!=pNewProc->m_ConsoleLaunchParameters.end()) + while (p != pNewProc->m_ConsoleLaunchParameters.end()) { pExecVParams[i++] = (p++->c_str()); } @@ -1032,12 +1036,12 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) pExecVParams[i++] = NULL; #if(DEBUG_LAUNCH) for (int j = 0; j < i; j++) - MOOSTrace("argv[%d]:\"%s\"\n",j,pExecVParams[j]); + MOOSTrace("argv[%d]:\"%s\"\n", j, pExecVParams[j]); #endif //and finally replace ourselves with a new xterm process image char * const * pParamList = const_cast (pExecVParams); - if (execvp(pExecVParams[0], pParamList)==-1) + if (execvp(pExecVParams[0], pParamList) == -1) { MOOSTrace("Failed exec - not good. Called exec as follows:\n"); exit(EXIT_FAILURE); @@ -1052,7 +1056,7 @@ bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) //and find the MOOS process running within the sub process, signal it to die //so subprocesses can clean up nicely if (m_bSupportGentleKill) - setpgid(pNewProc->m_ChildPID,0); + setpgid(pNewProc->m_ChildPID, 0); return true; } From 2171615379436f074647ce65a5032712e4bc5834 Mon Sep 17 00:00:00 2001 From: Ian Date: Sun, 27 Oct 2013 15:08:26 -0400 Subject: [PATCH 08/25] typos fixed --- Essentials/pAntler/Antler.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index 978f557..06adad3 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -644,14 +644,14 @@ bool CAntler::MakeConsoleLaunchParams(std::string sParam, { std::string sLabel = sMOOSName.empty() ? "" - : MOOSFormat("as MOOSName \"%s\"", sMOOSName.c_str()).c_str()); + : MOOSFormat("as MOOSName \"%s\"", sMOOSName.c_str()).c_str(); sLaunchConfiguration = "-geometry," + MOOSFormat("80x12+2+%d", (m_nCurrentLaunch++)*50) + ",+sb," + ",-fg," + sFgColor + ",-bg," + sBgColor - + ",-T," + MOOSFormat("%s %s", sProcName.c_str(), sLabel); + + ",-T," + MOOSFormat("%s %s", sProcName.c_str(), sLabel.c_str()); } } @@ -676,7 +676,7 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) { - //what tis its name? (if no @ symbol we just get the name and no cmdline) + //what is its name? (if no @ symbol we just get the name and no cmdline) string sProcName = MOOSChomp(sConfiguration, "@"); //further parameters are to left left of @ From ef5effc62a2cbca90aca1071cff83e901a3a4369 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 12:05:47 -0400 Subject: [PATCH 09/25] object oriented approach to process launching, placeholders for screen launch --- Essentials/pAntler/MOOSProc.cpp | 522 ++++++++++++++++++++++++++++++++ Essentials/pAntler/MOOSProc.h | 229 ++++++++++++++ 2 files changed, 751 insertions(+) create mode 100644 Essentials/pAntler/MOOSProc.cpp create mode 100644 Essentials/pAntler/MOOSProc.h diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp new file mode 100644 index 0000000..2450e70 --- /dev/null +++ b/Essentials/pAntler/MOOSProc.cpp @@ -0,0 +1,522 @@ +#include "MOOSProc.h" + +#define DEBUG_LAUNCH 0 + + +using namespace std; + +// read the bare minimum of text to begin processing +bool CProcCfg::ParseConfig(string sConfig) +{ + + //what is its name? (if no @ symbol we just get the name and no cmdline) + m_sAppName = MOOSChomp(sConfig, "@"); + + if (m_sAppName.empty()) + { + MOOSTrace("no process specified - RUN=???\n"); + return false; + } + + //further parameters are to right of @ + string sTmp = sConfig; + + //look for tilde demarking end of param=val block + m_sParams = MOOSChomp(sTmp, "~"); + + //read the remainder + m_sMOOSName = MOOSChomp(sTmp); + + return true; + +} + + +bool CProcCfg::GetParamValue(string sParam, string &sValue) +{ + return MOOSValFromString(sValue, m_sParams, sParam, true); +} + + +// fully parse the config, looking up all references to alternate configs +bool CMOOSProc::BuildConfig() +{ + + // Look for a line containing additional command line params (ExtraProcessParams) + m_CommandLineParameters.clear(); + string sExtraParamsName; + if (m_cfg.GetParamValue("ExtraProcessParams", sExtraParamsName)) + { + std::string sExtraParams; + + //OK look for this configuration string + if (!m_MissionReader->GetConfigurationParam(sExtraParamsName, sExtraParams)) + return MOOSFail(" warning cannot find extra parameters named \"%s\"\n", + sExtraParamsName.c_str()); + + while (!sExtraParams.empty()) + m_CommandLineParameters.push_back(MOOSChomp(sExtraParams, ",")); + } + + // Inhibit MOOS params? + MOOSValFromString(m_bInhibitMOOSParams, m_cfg.m_sParams, "InhibitMOOSParams", true); + + //by default process are assumed to be on the system path + //users can specify an alternative path for all process by setting + // "ExectutablePath=" in the mission file configuration block. + //user has the option of specifying paths individually process by process. + //alternativelt they can specify that a particular process should be located by + // system wide and not in the default executable path + string sSpecifiedPath; + m_cfg.m_sAppPath = m_cfg.m_sAppName; + if (!m_cfg.GetParamValue("PATH", sSpecifiedPath)) + { + //we just use the Antler-wide Exepath + m_cfg.m_sAppPath = m_cfg.m_sExecPath + m_cfg.m_sAppName; + } + else + { + if (!MOOSStrCmp(sSpecifiedPath, "SYSTEM")) + { + //we are being told to look in a special place + m_cfg.m_sAppPath = sSpecifiedPath + "/" + m_cfg.m_sAppName; + } + // otherwise it's up to the SYSTEM, so do nothing + } + + + //here we figure out what MOOS name is implied if none is given (thanks to N.P and F.A) + if (m_cfg.m_sMOOSName.empty()) + { + std::string sTmp = m_cfg.m_sAppName; + while (sTmp.rfind('/') != string::npos) + { + sTmp = sTmp.substr(sTmp.rfind('/') + 1); + } + + if (sTmp.empty()) + { + // ended with a / ? + MOOSTrace("Error in configuration -MOOS Name cannot end in \" / \" : %s\n", + m_cfg.m_sAppName.c_str()); + return false; + } + m_cfg.m_sMOOSName = sTmp; + } + + + + // other stuff + + return this->BuildConfigSpecific(); +} + + + +#ifdef _WIN32 + +CMOOSProcWin32::~CMOOSProcWin32() +{ + delete m_pWin32Attrib; + delete m_pWin32Proc; +} + + +bool CMOOSProcWin32::BuildConfigSpecific() +{ + + + return this->BuildConfigSpecific2(); +} + +bool CMOOSProcConsoleWin32::BuildConfigSpecific2() +{ + if (MOOSValFromString(sLaunchConfigurationName, sParam, "Win32Config")) + { + //OK look for this configuration string + if (!m_MissionReader->GetConfigurationParam(sLaunchConfigurationName, + sLaunchConfiguration)) + return MOOSFail(" warning: could not find resource string called \"%s\"", + sLaunchConfigurationName.c_str()) ; + + // which is ridiculous, because we can't do anything with it + } + else + { + if (IsMOOSDB(sAppName)) + { + this->m_ConsoleFlags.push_back(BACKGROUND_BLUE); + } + + if (IsIRemote(sAppName)) + { + this->m_ConsoleFlags.push_back(BACKGROUND_RED); + } + } + + return true; +} + +bool CMOOSProcWin32::Start(bool bGentle) +{ + try + { + // make the command line ProcessBinaryName MissionFile ProcessMOOSName + string sCmd = this->m_sApp + " "; + if (!this->m_bInhibitMOOSParams) + sCmd += this->MissionFile() + " " + this->m_sMOOSName + " "; + + //continuing this task, here we pass extra parameters to the MOOS process if required + for (STRING_LIST::iterator p = this->m_CommandLineParameters.begin(); + p != this->m_ExtraCommandLineParameters.end(); + p++) + { + sCmd += *p + " "; + } + + //make a new process attributes class + this->m_pWin32Attrib = NULL; + this->m_pWin32Attrib = new XPCProcessAttrib(NULL, (char *)sCmd.c_str()); + this->m_pWin32Attrib->vSetCreationFlag(this->CreationFlags()); + + //we shall use our own start up image as a starting point + STARTUPINFO StartUp; + GetStartupInfo (&StartUp); + + //set up the title + StartUp.lpTitle = (char*)this->m_sApp.c_str(); + + //set up white text + StartUp.dwFillAttribute = + FOREGROUND_INTENSITY | + FOREGROUND_BLUE | + FOREGROUND_RED | + FOREGROUND_GREEN | + BACKGROUND_INTENSITY ; + + + //give users basic control over backgroun as an RGB combination + for (DWORD_LIST::iterator q = this->m_ConsoleFlags.begin(); + q != this->m_ConsoleFlags.end(); + q++) + { + StartUp.dwFillAttribute |= *q; + StartUp.dwFlags |= STARTF_USEFILLATTRIBUTE; + } + + this->m_pWin32Attrib->vSetStartupInfo(&StartUp); + + //no create an object capable of laucnhing win32 processes + this->m_pWin32Proc = NULL; + this->m_pWin32Proc = new XPCProcess(* this->m_pWin32Attrib); + + //go! + this->m_pWin32Proc->vResume(); + + } + catch (XPCException & e) + { + if (this->m_pWin32Attrib != NULL) + { + delete this->m_pWin32Attrib; + } + if (this->m_pWin32Proc != NULL) + { + delete this->m_pWin32Proc; + } + MOOSTrace("*** %s Launch Failed:***\n\a\a", this->m_sApp.c_str()); + MOOSTrace("%s\n", e.sGetException()); + return false; + } + + return true; + +} + + +bool CMOOSProcWin32::Stop(bool bGentle) +{ + m_pWin32Proc->vTerminate(); + m_pWin32Proc->vWaitForTerminate(100); + return true; +} + +bool CMOOSProcWin32::IsStopped() +{ + return m_pWin32Proc->dwGetExitCode() != STILL_ACTIVE; +} + + + +#else // NIX DEFINITIONS + + + + +bool CMOOSProcNix::BuildConfigSpecific() +{ + return true; +} + +bool CMOOSProcConsoleNix::BuildConfigSpecific() +{ + string sLaunchConfigurationName; + string sLaunchConfiguration; + + + if (m_cfg.GetParamValue("XConfig", sLaunchConfigurationName)) + { + //OK look for this configuration string + if (!m_MissionReader->GetConfigurationParam(sLaunchConfigurationName, + sLaunchConfiguration)) + return MOOSFail(" warning: could not find resource string called \"%s\"", + sLaunchConfigurationName.c_str()) ; + } + else + { + //some applications are v.important in MOOS + // -- if not told otherwise they get special colours + string sBgColor = "#DDDDFF"; + string sFgColor = "#000000"; + + if (IsIRemote()) + { + sBgColor = "#CC0000"; + sFgColor = "#FFFFFF"; + } + + if (IsMOOSDB()) + { + sBgColor = "#003300"; + sFgColor = "#FFFFFF"; + } + + string sLabel = m_cfg.m_sMOOSName.empty() + ? "" + : MOOSFormat("as MOOSName \"%s\"", m_cfg.m_sMOOSName.c_str()).c_str(); + + sLaunchConfiguration = "-geometry," + + MOOSFormat("80x12+2+%d", (*(m_cfg.m_nCurrentLaunch)) * 50) + + ",+sb," + + ",-fg," + sFgColor + + ",-bg," + sBgColor + + ",-T," + MOOSFormat("%s %s", GetAppName().c_str(), sLabel.c_str()); + + } + + //OK now simply chomp our way through a space delimited list... + while (!sLaunchConfiguration.empty()) + { + MOOSTrimWhiteSpace(sLaunchConfiguration); + string sP = MOOSChomp(sLaunchConfiguration, ","); + MOOSTrimWhiteSpace(sP); + if (!sP.empty()) + m_ConsoleLaunchParameters.push_back(sP); + } + + return true; + +} + +bool CMOOSProcScreenNix::BuildConfigSpecific() +{ + return true; +} + + +bool CMOOSProcNixBase::Start(bool bGentle) +{ + + //make a child process + this->m_ChildPID = fork(); + if (this->m_ChildPID < 0) + { + //hell! + MOOSTrace("fork failed, not good\n"); + return false; + } + + if (this->m_ChildPID == 0) + { + //I'm the child now.. + + // build m_FullCommandLine according to launch type + m_FullCommandLine.clear(); + this->StartSpecific(); + + if (!m_FullCommandLine.size()) + { + MOOSTrace("pAntler author error: no elements in m_FullCommandLine!"); + return false; + } + + // CONVERT STRING LIST TO ARRAY + const char ** pExecVParams = new const char* [m_FullCommandLine.size() + 1]; + int i = 0; + for (STRING_LIST::iterator s = this->m_FullCommandLine.begin(); + s != this->m_FullCommandLine.end(); + ++s) + { + pExecVParams[i++] = s->c_str(); + } + + //terminate list + pExecVParams[i++] = NULL; + if(DEBUG_LAUNCH) + { + for (int j = 0; j < i; j++) + MOOSTrace("argv[%d]:\"%s\"\n", j, pExecVParams[j]); + } + + //and finally replace ourselves with a new xterm process image + char * const * pParamList = const_cast (pExecVParams); + if (execvp(pExecVParams[0], pParamList) == -1) + { + MOOSTrace("Failed exec - not good. Called exec as follows:\n"); + exit(EXIT_FAILURE); + + } + + } + + //Parent execution stream continues here... + //we need to change the process group of the spawned process so that ctrl-C + //does get sent to all the launched xterms. Instead we will catch ctrl-C ourself + //and find the MOOS process running within the sub process, signal it to die + //so subprocesses can clean up nicely + if (bGentle) + setpgid(this->m_ChildPID, 0); + + return true; + +} + +bool CMOOSProcNix::StartSpecific() +{ + + m_FullCommandLine.push_back(m_cfg.m_sAppPath); + + if (!this->m_bInhibitMOOSParams) + { + //we do the usual thing of supplying Mission file and MOOSName + + m_FullCommandLine.push_back(this->MissionFile()); + m_FullCommandLine.push_back(m_cfg.m_sMOOSName); + } + + //here we pass extra parameters to the MOOS process if required + m_FullCommandLine.splice(m_FullCommandLine.end(), m_CommandLineParameters); + + + return true; +} + +bool CMOOSProcConsoleNix::StartSpecific() +{ + + // xterm -e + m_FullCommandLine.push_back(DEFAULT_NIX_TERMINAL); + m_FullCommandLine.splice(m_FullCommandLine.end(), m_ConsoleLaunchParameters); + m_FullCommandLine.push_back("-e"); + + + // this is the same as the plain-vanilla nix launch + m_FullCommandLine.push_back(m_cfg.m_sAppPath); + + if (!this->m_bInhibitMOOSParams) + { + //we do the usual thing of supplying Mission file and MOOSName + + m_FullCommandLine.push_back(this->MissionFile()); + m_FullCommandLine.push_back(m_cfg.m_sMOOSName); + } + + //here we pass extra parameters to the MOOS process if required + m_FullCommandLine.splice(m_FullCommandLine.end(), m_CommandLineParameters); + + return true; +} + +bool CMOOSProcScreenNix::StartSpecific() +{ + return true; +} + + +bool CMOOSProcNixBase::Stop(bool bGentle) +{ + bool ret = this->StopSpecific(bGentle); + + //just give it a little time - for pities sake - no need for this pause + MOOSPause(300); + + return ret; +} + +bool CMOOSProcNix::StopSpecific(bool bGentle) +{ + kill(m_ChildPID, SIGTERM); + + return true; +} + +bool CMOOSProcConsoleNix::StopSpecific(bool bGentle) +{ + if (bGentle) + { + kill(m_ChildPID, SIGTERM); + } + else + { + //we need to be crafty.... + string sCmd = "ps -e -o ppid= -o pid="; + + FILE* In = popen(sCmd.c_str(), "r"); + + if (In == NULL) return false; + + bool bFound = false; + char Line[256]; + while (fgets(Line, sizeof(Line), In)) + { + stringstream L(Line); + int ppid, pid; + L >> ppid; + L >> pid; + + if (m_ChildPID == ppid) + { + kill(pid, SIGTERM); + bFound = true; + } + } + pclose(In); + return bFound; + } + + return true; +} + +bool CMOOSProcScreenNix::StopSpecific(bool bGentle) +{ + return true; +} + + +bool CMOOSProcNixBase::IsStopped() +{ + int nStatus = 0; + return waitpid(this->m_ChildPID, &nStatus, WNOHANG) > 0; +} + + +#endif + + + + + + + + + diff --git a/Essentials/pAntler/MOOSProc.h b/Essentials/pAntler/MOOSProc.h new file mode 100644 index 0000000..f37676f --- /dev/null +++ b/Essentials/pAntler/MOOSProc.h @@ -0,0 +1,229 @@ +/* + * MOOSProc.h + * MOOS + * + * Created by Ian Katz on 2013-10-27 + * Based on code from pnewman on 18/04/2008. + * + */ + + +#ifdef _WIN32 +#pragma warning(disable : 4786) +#endif + +#ifndef MOOSPROCH +#define MOOSPROCH + +#include "MOOS/libMOOS/MOOSLib.h" +#include + + +#ifdef _WIN32 +#include "XPCProcess.h" +#include "XPCProcessAttrib.h" +#include "XPCException.h" +#else +#include +#include +#include +#include +#endif + +using namespace std; + +#define DEFAULT_NIX_TERMINAL "xterm" + +// data structure for the "RUN = ..." line in a pAntler config +class CProcCfg +{ +public: + CProcCfg() {}; + CProcCfg(string sConfig) { ParseConfig(sConfig); }; + ~CProcCfg() {}; + + // keep attributes public + string m_sAppName; // executable name + string m_sAppPath; // full path to app + string m_sMOOSName; // run-as name of app + string m_sMissionFile; // path to mission file + string m_sParams; // key-val parameters from config line + + // SPECIAL USE + string m_sExecPath; // "SYSTEM", or prefix to executables + int* m_nCurrentLaunch; // pointer to number of launches + + // read in all data + bool ParseConfig(string sConfig); + + // get a value from the parsed data + bool GetParamValue(string sParam, string &sValue); + +}; + + + +// data structure for a process instantiated from RUN line +class CMOOSProc +{ +public: + CMOOSProc(CProcCfg config, CProcessConfigReader* MissionReader) + : m_cfg(config), m_MissionReader(MissionReader) {}; + virtual ~CMOOSProc() {}; + + bool BuildConfig(); + + virtual bool BuildConfigSpecific() = 0; + virtual bool Start(bool bGentle) = 0; + virtual bool Stop(bool bGentle) = 0; + virtual bool IsStopped() = 0; + + string MissionFile() { return m_MissionReader->GetFileName(); }; + bool IsMOOSDB() { return MOOSStrCmp("MOOSDB", m_cfg.m_sAppName); }; + bool IsIRemote() { return MOOSStrCmp("iRemote", m_cfg.m_sAppName); }; + + string GetAppName() { return m_cfg.m_sAppName; }; + string GetMOOSName() { return m_cfg.m_sMOOSName; }; + +protected: + + CProcCfg m_cfg; + + CProcessConfigReader* m_MissionReader; + + // settings from more in-depth parsing + bool m_bInhibitMOOSParams; + STRING_LIST m_CommandLineParameters; + + + // conditional compilation to hold OS-specific program launch info +#ifdef _WIN32 + XPCProcess * m_pWin32Proc; + XPCProcessAttrib * m_pWin32Attrib; +#else + pid_t m_ChildPID; + +public: + pid_t GetChildPID() { return m_ChildPID; }; + +#endif + +}; + + + +#ifdef _WIN32 + +typedef std::list DWORD_LIST; + +class CMOOSProcWin32 : public CMOOSProc +{ +public: + CMOOSProcWin32(CProcCfg config, CProcessConfigReader* MissionReader) + : CMOOSProc(config, MissionReader) {}; + virtual ~CMOOSProcWin32(); + + // value passed to vSetCreationFlag + virtual DWORD CreationFlags() { return CREATE_SUSPENDED; }; + + virtual bool BuildConfigSpecific(); + virtual bool BuildConfigSpecific2() + { return true; }; // for console, we ignore it in this instance + + virtual bool Start(bool bGentle); + virtual bool Stop(bool bGentle); + virtual bool IsStopped(); + +protected: + // unused in this instance, but placed here to cut down on duplicate code + DWORD_LIST m_ConsoleFlags; + +}; + + +class CMOOSProcConsoleWin32 : public CMOOSProcWin32 +{ +public: + CMOOSProc(CProcCfg config, CProcessConfigReader* MissionReader) + : CMOOSProcProcWin32Base(config, MissionReader) {}; + virtual ~CMOOSProc() {}; + + virtual bool BuildConfigSpecific2(string sConfig); + virtual DWORD CreationFlags() { return CREATE_NEW_CONSOLE | CREATE_SUSPENDED; }; + +}; + + +#else + +class CMOOSProcNixBase : public CMOOSProc +{ +public: + CMOOSProcNixBase(CProcCfg config, CProcessConfigReader* MissionReader) + : CMOOSProc(config, MissionReader) {}; + virtual ~CMOOSProcNixBase() {}; + + // we add a 300ms pause after the "stop" signal on *nix + virtual bool Stop(bool bGentle); + virtual bool StopSpecific(bool bGentle) = 0; + + // we need to do more sophisticated things with the command line + virtual bool Start(bool bGentle); + virtual bool StartSpecific() = 0; + + virtual bool IsStopped(); + +protected: + STRING_LIST m_FullCommandLine; +}; + + +class CMOOSProcNix : public CMOOSProcNixBase +{ +public: + CMOOSProcNix(CProcCfg config, CProcessConfigReader* MissionReader) + : CMOOSProcNixBase(config, MissionReader) {}; + virtual ~CMOOSProcNix() {}; + + virtual bool BuildConfigSpecific(); + virtual bool StartSpecific(); + virtual bool StopSpecific(bool bGentle); +}; + + +class CMOOSProcConsoleNix : public CMOOSProcNixBase +{ +public: + CMOOSProcConsoleNix(CProcCfg config, CProcessConfigReader* MissionReader) + : CMOOSProcNixBase(config, MissionReader) {}; + virtual ~CMOOSProcConsoleNix() {}; + + virtual bool BuildConfigSpecific(); + virtual bool StartSpecific(); + virtual bool StopSpecific(bool bGentle); + +protected: + STRING_LIST m_ConsoleLaunchParameters; +}; + + +class CMOOSProcScreenNix : public CMOOSProcNixBase +{ +public: + CMOOSProcScreenNix(CProcCfg config, CProcessConfigReader* MissionReader) + : CMOOSProcNixBase(config, MissionReader) {}; + virtual ~CMOOSProcScreenNix() {}; + + virtual bool BuildConfigSpecific(); + virtual bool StartSpecific(); + virtual bool StopSpecific(bool bGentle); + +protected: + STRING_LIST m_ScreenLaunchParameters; +}; + + +#endif // _WIN32 + + +#endif // ifndef MOOSPROC From 77e011013a16acfa8e290fee506af3307bb8cf70 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 12:17:07 -0400 Subject: [PATCH 10/25] Refactoring of pAntler, functionality should be identical excepting an added LAUNCHTYPE param to override NEWCONSOLE. stubs for LaunchType=Screen --- Essentials/pAntler/Antler.cpp | 641 ++++++------------------------ Essentials/pAntler/Antler.h | 71 +--- Essentials/pAntler/CMakeLists.txt | 2 +- 3 files changed, 133 insertions(+), 581 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index 06adad3..ae978d1 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -1,4 +1,6 @@ + #include "Antler.h" +#include "MOOSProc.h" using namespace std; #include @@ -11,7 +13,6 @@ using namespace std; -#define DEBUG_LAUNCH 0 CAntler::CAntler() { m_JobLock.UnLock(); @@ -306,61 +307,6 @@ bool CAntler::PublishProcessLaunch(const std::string & sProc) } -bool CAntler::KillNicely(MOOSProc* pProc) -{ -#ifdef _WIN32 - - //MOOSTrace("Warning - gentle killing of win32 processes is not implemented\n"); - pProc->pWin32Proc->vTerminate(); - -#else - - if (!m_bSupportGentleKill) - { - kill(pProc->m_ChildPID, SIGTERM); - } - else - { - if (!pProc->m_bNewConsole) - { - //the proc ID we have is of the actuall MOOSProcess and not its parent - //we can just kill it... - kill(pProc->m_ChildPID, SIGTERM); - } - else - { - //we need to be crafty.... - std::string sCmd = "ps -e -o ppid= -o pid="; - - FILE* In = popen(sCmd.c_str(), "r"); - - if (In == NULL) return false; - - bool bFound = false; - char Line[256]; - while (fgets(Line, sizeof(Line), In)) - { - std::stringstream L(Line); - int ppid, pid; - L >> ppid; - L >> pid; - - if (pProc->m_ChildPID == ppid) - { - kill(pid, SIGTERM); - bFound = true; - } - } - pclose(In); - return bFound; - } - } - -#endif - - return true; -} - bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) { @@ -412,13 +358,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) //are we being ask to support gentle process killing? -#ifdef _WIN32 - m_bSupportGentleKill= false; -#else - m_bSupportGentleKill = true; m_MissionReader.GetConfigurationParam("GentleKill", m_bSupportGentleKill); -#endif - //now cycle through each line in the configuration block. @@ -434,7 +374,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) //OK we are being asked to run a process //try to create a process - MOOSProc* pNew = CreateMOOSProcess(sLine); + CMOOSProc* pNew = CreateMOOSProcess(sLine); if (pNew != NULL) { @@ -443,14 +383,14 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) { MOOSTrace(" [%.3d] Process: %-15s ~ %-15s launched successfully\n", m_nCurrentLaunch, - pNew->m_sApp.c_str(), - pNew->m_sMOOSName.c_str()); + pNew->GetAppName().c_str(), + pNew->GetMOOSName().c_str()); } InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); m_ProcList.push_front(pNew); m_nCurrentLaunch++; - PublishProcessLaunch(pNew->m_sApp); + PublishProcessLaunch(pNew->GetAppName()); } //wait a while @@ -486,23 +426,22 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) //now wait on all our processes to close.... - while (m_ProcList.size() != 0) + while (m_ProcList.size()) { MOOSPROC_LIST::iterator q; for (q = m_ProcList.begin(); q != m_ProcList.end(); q++) { - MOOSProc* pMOOSProc = *q; + CMOOSProc* pMOOSProc = *q; -#ifdef _WIN32 if (m_bQuitCurrentJob) { - KillNicely(pMOOSProc); + MOOSTrace(" actively killing running child %s\n", + pMOOSProc->GetAppName().c_str()); + pMOOSProc->Stop(m_bSupportGentleKill); } - - - pMOOSProc->pWin32Proc->vWaitForTerminate(100); - if (pMOOSProc->pWin32Proc->dwGetExitCode() != STILL_ACTIVE) + + if (pMOOSProc->IsStopped()) { //this a really important bit of text most folk will want to see it... @@ -510,50 +449,19 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) { MOOSTrace(" [%.3d] Process: %-15s has quit\n", --m_nCurrentLaunch, - pMOOSProc->m_sApp.c_str()); + pMOOSProc->GetAppName().c_str()); } InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); - PublishProcessQuit(pMOOSProc->m_sApp); - delete pMOOSProc->pWin32Attrib; - delete pMOOSProc->pWin32Proc; + PublishProcessQuit(pMOOSProc->GetAppName()); delete pMOOSProc; m_ProcList.erase(q); break; } -#else - if (m_bQuitCurrentJob) - { - MOOSTrace(" actively killing running child %s\n", - pMOOSProc->m_sApp.c_str()); - KillNicely(pMOOSProc); - //just give it a little time - for pities sake - no need for this pause - MOOSPause(300); - } - - - int nStatus = 0; - if (waitpid(pMOOSProc->m_ChildPID, &nStatus, WNOHANG)>0) - { - - InhibitMOOSTraceInThisThread(m_eVerbosity == QUIET); - { - MOOSTrace(" [%.3d] Process: %-15s has quit\n", - --m_nCurrentLaunch, - pMOOSProc->m_sApp.c_str()); - } - InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); - - - PublishProcessQuit(pMOOSProc->m_sApp); - - m_ProcList.erase(q); - break; - } + MOOSPause(100); -#endif } @@ -565,139 +473,19 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) -bool CAntler::MakeExtraExecutableParameters(std::string sParam, - STRING_LIST & ExtraCommandLineParameters, - std::string sProcName, - std::string sMOOSName) -{ - - ExtraCommandLineParameters.clear(); - - std::string sExtraParamsName; - if (!MOOSValFromString(sExtraParamsName, sParam, "ExtraProcessParams", true)) - return true;//nothing to do - - std::string sExtraParams; - - //OK look for this configuration string - if (!m_MissionReader.GetConfigurationParam(sExtraParamsName, sExtraParams)) - return MOOSFail(" warning cannot find extra parameters named \"%s\"\n", - sExtraParamsName.c_str()); - - while (!sExtraParams.empty()) - ExtraCommandLineParameters.push_back(MOOSChomp(sExtraParams, ",")); - - return true; - -} -bool CAntler::MakeConsoleLaunchParams(std::string sParam, - STRING_LIST & LaunchList, - std::string sProcName, - std::string sMOOSName) +CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) { - //sParam is a string in the Run=ProcName @ sParam ~ MOOSName - std::string sLaunchConfigurationName; - std::string sLaunchConfiguration; -#ifdef _WIN32 - bool bNIX = false; - -#else - bool bNIX= true; -#endif + CProcCfg config; - - std::string sLaunchKey = bNIX?"XConfig":"Win32Config"; - if (MOOSValFromString(sLaunchConfigurationName, sParam, sLaunchKey)) + if (!config.ParseConfig(sConfiguration)) { - - //OK look for this configuration string - if (!m_MissionReader.GetConfigurationParam(sLaunchConfigurationName, - sLaunchConfiguration)) - return MOOSFail(" warning: could not find resource string called \"%s\"", - sLaunchConfigurationName.c_str()) ; - } - else - { - //some applications are v.important in MOOS - // -- if not told otherwise they get special colours - string sBgColor = bNIX ? "#DDDDFF" : ""; - string sFgColor = bNIX ? "#000000" : ""; - - if (MOOSStrCmp(sProcName, "iRemote")) - { - sBgColor = bNIX ? "#CC0000" : "RED"; - sFgColor = bNIX ? "#FFFFFF" : ""; - } - - if (MOOSStrCmp(sProcName, "MOOSDB")) - { - sBgColor = bNIX ? "#003300" : "BLUE"; - sFgColor = bNIX ? "#FFFFFF" : ""; - } - - if (!bNIX) - { - sLaunchConfiguration = sBgColor; - } - else - { - std::string sLabel = sMOOSName.empty() - ? "" - : MOOSFormat("as MOOSName \"%s\"", sMOOSName.c_str()).c_str(); - - sLaunchConfiguration = "-geometry," - + MOOSFormat("80x12+2+%d", (m_nCurrentLaunch++)*50) - + ",+sb," - + ",-fg," + sFgColor - + ",-bg," + sBgColor - + ",-T," + MOOSFormat("%s %s", sProcName.c_str(), sLabel.c_str()); - } - } - - //OK now simply chomp our way through a space delimited list... - while (!sLaunchConfiguration.empty()) - { - MOOSTrimWhiteSpace(sLaunchConfiguration); - std::string sP = MOOSChomp(sLaunchConfiguration, ","); - MOOSTrimWhiteSpace(sP); - if (!sP.empty()) - LaunchList.push_back(sP); + return NULL; } - - return !LaunchList.empty(); - - -} + // set pointer to current launch number + config.m_nCurrentLaunch = &m_nCurrentLaunch; - -CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) -{ - - - //what is its name? (if no @ symbol we just get the name and no cmdline) - string sProcName = MOOSChomp(sConfiguration, "@"); - - //further parameters are to left left of @ - string sParam = sConfiguration; - - if (sProcName.empty()) - { - MOOSTrace("no process specified - RUN=???\n"); - return NULL; - } - - //std::string sFullProcName = m_sDefaultExecutablePath+sProcName; - - //if things go well this will eventually point to a new Process - MOOSProc * pNewProc = NULL; - - //look for tilde demarking end of param=val block - string sOption = MOOSChomp(sParam, "~"); - - - bool bDistributed = false; m_MissionReader.GetConfigurationParam("EnableDistributed", bDistributed); @@ -707,19 +495,18 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) } else { + std::string sAntlerRequired; if (!m_bHeadless) { //we are a TopMOOS - std::string sAntlerRequired; - if (MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) + if (config.GetParamValue("AntlerID", sAntlerRequired)) return NULL; //this is for a drone } else { //we are a drone - std::string sAntlerRequired; - if (!MOOSValFromString(sAntlerRequired, sOption, "AntlerID", true)) + if (config.GetParamValue("AntlerID", sAntlerRequired)) return NULL; //this is for primary Antler @@ -731,110 +518,112 @@ CAntler::MOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) } } - //do we want a new console? - bool bNewConsole = false; - MOOSValFromString(bNewConsole, sOption, "NEWCONSOLE", true); - - //do we want to inhibit the passing of MOOS parameters (mission file and MOOSName) - bool bInhibitMOOSParams = false; - MOOSValFromString(bInhibitMOOSParams, sOption, "InhibitMOOSParams", true); - - //by default process are assumed to be on the system path - //users can specify an alternative path for all process by setting - // "ExectutablePath=" in the mission file configuration block. - //user has the option of specifying paths individually process by process. - //alternativelt they can specify that a particular process should be located by - // system wide and not in the default executable path - std::string sSpecifiedPath; - std::string sFullProcName = sProcName; - if (!MOOSValFromString(sSpecifiedPath, sOption, "PATH", true)) - { - //we just use the Anter-wide Exepath - sFullProcName = m_sDefaultExecutablePath+sProcName; - } - else + + //NEW option: launch (process, console, screen) + LAUNCH_TYPE lt = L_UNDEF; + string sLaunchType; + if (config.GetParamValue("LAUNCHTYPE", sLaunchType)) { - if (MOOSStrCmp(sSpecifiedPath, "SYSTEM")) - { - //do nothing - the system should know where to look - } + if (MOOSStrCmp("PROCESS", sLaunchType)) + lt = L_PROCESS; + else if (MOOSStrCmp("CONSOLE", sLaunchType)) + lt = L_CONSOLE; + else if (MOOSStrCmp("SCREEN", sLaunchType)) + lt = L_SCREEN; else { - //ok we are being told to look in a special place - sFullProcName = sSpecifiedPath + "/" + sProcName; + MOOSTrace("Error: LaunchType was not one of (Process, Console, Screen)"); + return NULL; } } - //name of process as registered...is the first param after "~" - string sMOOSName = MOOSChomp(sParam); - - //here we figure out what MOOS name is implied if none is given (thanks to N.P and F.A) - if (sMOOSName.empty()) + // backwards compatibility with NEWCONSOLE + bool bNewConsole = false; + bool bGotConsoleParam = MOOSValFromString(bNewConsole, + config.m_sParams, + "NEWCONSOLE", + true); + + // fill in the value if we need it. otherwise, warn if we have both specifiers + if (L_UNDEF == lt) { - std::string sTmp = sProcName; - if (sTmp.rfind('/') != string::npos) - { - sTmp = sTmp.substr(sTmp.rfind('/')+1); - } - if (sTmp.empty()) - { - // ended with a / ? - MOOSTrace("Error in configuration -MOOS Name cannot end in \" / \" : %s\n", - sProcName.c_str()); - return NULL; - } - sMOOSName = sTmp; + MOOSTrace("Warning: NEWCONSOLE is deprecated. Use LAUNCHTYPE."); + lt = bNewConsole ? L_CONSOLE : L_PROCESS; + } + else if (bGotConsoleParam) + { + MOOSTrace("Warning: LAUNCHTYPE overrides NEWCONSOLE, so ignoring this value"); } //here we bail according to our filters - if (!m_Filter.empty() && m_Filter.find(sMOOSName) == m_Filter.end()) + if (!m_Filter.empty() && m_Filter.find(config.m_sMOOSName) == m_Filter.end()) { return NULL; } - - //it is pssible to specifiy complicated parameters to the process being launched. - // (For example xterm being passed a whole load of configurations and then the name - // of the MOOS process it should itself launch. - // This next call fills in a string list of such parameters - STRING_LIST sLaunchParams; - if (bNewConsole) - MakeConsoleLaunchParams(sOption, sLaunchParams, sProcName, sMOOSName); - - - //here we figure what extra command line parameters should be given to the MOOS Process - //being launched. - STRING_LIST ExtraCommandLineParameters; - MakeExtraExecutableParameters(sOption, - ExtraCommandLineParameters, - sProcName, - sMOOSName); - + + + //All good up to here, now make a new process holder stucture... + CMOOSProc* pNewProc = NULL; + +#ifdef _WIN32 + + switch (lt) + { + case L_PROCESS: + pNewProc = new CMOOSProcWin32(config, &m_MissionReader); + break; + case L_CONSOLE: + pNewProc = new CMOOSProcConsoleWin32(config, &m_MissionReader); + break; + case L_SCREEN: + MOOSTrace("GNU screen launch is not yet supported under Win32"); + return NULL; + default: + MOOSTrace("LAUNCHTYPE and NEWCONSOLE didn't reveal the launch type"); + return NULL; + } + +#else + + switch (lt) + { + case L_PROCESS: + pNewProc = new CMOOSProcNix(config, &m_MissionReader); + break; + case L_CONSOLE: + pNewProc = new CMOOSProcConsoleNix(config, &m_MissionReader); + break; + case L_SCREEN: + pNewProc = new CMOOSProcScreenNix(config, &m_MissionReader); + break; + default: + MOOSTrace("LAUNCHTYPE and NEWCONSOLE didn't reveal the launch type"); + return NULL; + } + +#endif - //All good up to here, now make a new process info holder stucture... - pNewProc = new MOOSProc; - pNewProc->m_sApp = sFullProcName; - pNewProc->m_ExtraCommandLineParameters = ExtraCommandLineParameters; - pNewProc->m_ConsoleLaunchParameters = sLaunchParams; - pNewProc->m_bInhibitMOOSParams = bInhibitMOOSParams; - pNewProc->m_sMOOSName = sMOOSName; - pNewProc->m_bNewConsole = bNewConsole; - pNewProc->m_sMissionFile = m_MissionReader.GetFileName(); + + if (!pNewProc->BuildConfig()) + { + MOOSTrace("App %s failed BuildConfig()", config.m_sAppName.c_str()); + delete pNewProc; + return NULL; + } + //finally spawn each according to his own -#ifndef _WIN32 - if (DoNixOSLaunch(pNewProc)) -#else - if (DoWin32Launch(pNewProc)) -#endif - { - return pNewProc; - } - else - { - delete pNewProc; - return NULL; - } + if (pNewProc->Start(m_bSupportGentleKill)) + { + ++m_nCurrentLaunch; + return pNewProc; + } + else + { + delete pNewProc; + return NULL; + } } @@ -847,16 +636,16 @@ bool CAntler::ShutDown() for (q = m_ProcList.begin();q != m_ProcList.end(); q++) { - MOOSProc * pMOOSProc = *q; + CMOOSProc * pMOOSProc = *q; - if (KillNicely(pMOOSProc)) + if (pMOOSProc->Stop(m_bSupportGentleKill)) { - int nStatus = 0; #ifndef _WIN32 - MOOSTrace("\n Signalling %-15s ", pMOOSProc->m_sApp.c_str()); - if (waitpid(pMOOSProc->m_ChildPID, &nStatus, 0)>0) + int nStatus = 0; + MOOSTrace("\n Signalling %-15s ", pMOOSProc->GetAppName().c_str()); + if (0 < waitpid(pMOOSProc->GetChildPID(), &nStatus, 0)) { MOOSTrace("[OK]"); } @@ -871,195 +660,3 @@ bool CAntler::ShutDown() return true; } - - -#ifdef _WIN32 -bool CAntler::DoWin32Launch(CAntler::MOOSProc * pNewProc) -{ - try - { - // make the command line ProcessBinaryName MissionFile ProcessMOOSName - string sCmd = pNewProc->m_sApp + " "; - if (pNewProc->m_bInhibitMOOSParams == false) - sCmd += pNewProc->m_sMissionFile + " " + pNewProc->m_sMOOSName + " "; - - //continuing this task, here we pass extra parameters to the MOOS process if required - for (STRING_LIST::iterator p = pNewProc->m_ExtraCommandLineParameters.begin(); - p != pNewProc->m_ExtraCommandLineParameters.end(); - p++) - { - sCmd += *p + " "; - } - - - //make a new process attributes class - pNewProc->pWin32Attrib = NULL; - pNewProc->pWin32Attrib = new XPCProcessAttrib(NULL, (char *)sCmd.c_str()); - - if (pNewProc->m_bNewConsole) - { - pNewProc->pWin32Attrib->vSetCreationFlag(CREATE_NEW_CONSOLE - | CREATE_SUSPENDED); - } - else - { - pNewProc->pWin32Attrib->vSetCreationFlag(CREATE_SUSPENDED); - } - - - //we shall use our own start up image as a starting point - STARTUPINFO StartUp; - GetStartupInfo (&StartUp); - - //set up the title - StartUp.lpTitle = (char*)pNewProc->m_sApp.c_str(); - - //set up white text - StartUp.dwFillAttribute = - FOREGROUND_INTENSITY| - FOREGROUND_BLUE | - FOREGROUND_RED | - FOREGROUND_GREEN| - BACKGROUND_INTENSITY ; - - - //give users basic control over backgroun as an RGB combination - for (STRING_LIST::iterator q = pNewProc->m_ConsoleLaunchParameters.begin(); - q != pNewProc->m_ConsoleLaunchParameters.end(); - q++) - { - if (MOOSStrCmp(*q, "BACKGROUND_BLUE")) - { - StartUp.dwFillAttribute |= BACKGROUND_BLUE; - StartUp.dwFlags |= STARTF_USEFILLATTRIBUTE; - } - if (MOOSStrCmp(*q, "BACKGROUND_GREEN")) - { - StartUp.dwFillAttribute |= BACKGROUND_GREEN; - StartUp.dwFlags |= STARTF_USEFILLATTRIBUTE; - } - if (MOOSStrCmp(*q, "BACKGROUND_RED")) - { - StartUp.dwFillAttribute |= BACKGROUND_RED; - StartUp.dwFlags |= STARTF_USEFILLATTRIBUTE; - } - - } - - pNewProc->pWin32Attrib->vSetStartupInfo(&StartUp); - - //no create an object capable of laucnhing win32 processes - pNewProc->pWin32Proc = NULL; - pNewProc->pWin32Proc = new XPCProcess(* pNewProc->pWin32Attrib); - - //go! - pNewProc->pWin32Proc->vResume(); - - } - catch (XPCException & e) - { - if (pNewProc->pWin32Attrib != NULL) - { - delete pNewProc->pWin32Attrib; - } - if (pNewProc->pWin32Proc != NULL) - { - delete pNewProc->pWin32Proc; - } - MOOSTrace("*** %s Launch Failed:***\n\a\a", pNewProc->m_sApp.c_str()); - MOOSTrace("%s\n", e.sGetException()); - return false; - } - - return true; - -} -#else - -bool CAntler::DoNixOSLaunch(CAntler::MOOSProc * pNewProc) -{ - - //make a child process - pNewProc->m_ChildPID = fork(); - if (pNewProc->m_ChildPID < 0) - { - //hell! - MOOSTrace("fork failed, not good\n"); - return false; - } - - if (pNewProc->m_ChildPID == 0) - { - //I'm the child now.. - - STRING_LIST::iterator p = pNewProc->m_ConsoleLaunchParameters.begin(); - unsigned int nExecParams = pNewProc->m_ConsoleLaunchParameters.size() - + pNewProc->m_ExtraCommandLineParameters.size() + 6; - const char ** pExecVParams = new const char* [nExecParams]; - int i = 0; - - //do we need to configure an xterm? - if (pNewProc->m_bNewConsole) - { - pExecVParams[i++] = DEFAULT_NIX_TERMINAL; - if (!pNewProc->m_ConsoleLaunchParameters.empty()) - { - while (p != pNewProc->m_ConsoleLaunchParameters.end()) - { - pExecVParams[i++] = (p++->c_str()); - } - } - - pExecVParams[i++] = "-e"; - - } - - //here we fill in the process name we really care about - pExecVParams[i++] = (pNewProc->m_sApp.c_str()) ; - if (!pNewProc->m_bInhibitMOOSParams) - { - //we do the usual thing of supplying Mission file and MOOSName - pExecVParams[i++] = pNewProc->m_sMissionFile.c_str(); - pExecVParams[i++] = pNewProc->m_sMOOSName.c_str(); - } - - - //here we pass extra parameters to the MOOS process if required - for (p = pNewProc->m_ExtraCommandLineParameters.begin(); - p != pNewProc->m_ExtraCommandLineParameters.end(); - p++) - { - pExecVParams[i++] = (p->c_str()); - } - - //terminate list - pExecVParams[i++] = NULL; -#if(DEBUG_LAUNCH) - for (int j = 0; j < i; j++) - MOOSTrace("argv[%d]:\"%s\"\n", j, pExecVParams[j]); -#endif - - //and finally replace ourselves with a new xterm process image - char * const * pParamList = const_cast (pExecVParams); - if (execvp(pExecVParams[0], pParamList) == -1) - { - MOOSTrace("Failed exec - not good. Called exec as follows:\n"); - exit(EXIT_FAILURE); - - } - - } - - //Parent execution stream continues here... - //we need to change the process group of the spawned process so that ctrl-C - //does get sent to all the launched xterms. Instead we will catch ctrl-C ourself - //and find the MOOS process running within the sub process, signal it to die - //so subprocesses can clean up nicely - if (m_bSupportGentleKill) - setpgid(pNewProc->m_ChildPID, 0); - - return true; -} - -#endif - diff --git a/Essentials/pAntler/Antler.h b/Essentials/pAntler/Antler.h index 8563628..8370c8f 100644 --- a/Essentials/pAntler/Antler.h +++ b/Essentials/pAntler/Antler.h @@ -20,44 +20,12 @@ #include #include -#ifdef _WIN32 -#include "XPCProcess.h" -#include "XPCProcessAttrib.h" -#include "XPCException.h" -#else -#include -#include -#include -#include -#endif +#include "MOOSProc.h" -#define DEFAULT_NIX_TERMINAL "xterm" #define DEFAULTTIMEBETWEENSPAWN 1000 - class CAntler { -public: - - //something to hold alient process information for the Antler class - struct MOOSProc - { -#ifdef _WIN32 - XPCProcess * pWin32Proc; - XPCProcessAttrib * pWin32Attrib; -#else - pid_t m_ChildPID; -#endif - std::string m_sApp; - std::string m_sMOOSName; - std::string m_sMissionFile; - bool m_bInhibitMOOSParams; - bool m_bNewConsole; - STRING_LIST m_ExtraCommandLineParameters; - STRING_LIST m_ConsoleLaunchParameters; - - }; - public: //constructor @@ -75,6 +43,15 @@ class CAntler TERSE, CHATTY, }; + + enum LAUNCH_TYPE + { + L_UNDEF, + L_PROCESS, + L_CONSOLE, + L_SCREEN // GNU screen, not X11 + }; + bool SetVerbosity(VERBOSITY_LEVEL eLevel); //call this to cause a clean shut down @@ -87,28 +64,9 @@ class CAntler //create, configure and launch a process - MOOSProc* CreateMOOSProcess(std:: string sProcName); - - // called to figure out what xterm parameters should be used with launch - // (ie where should the xterm be and how should it look) - bool MakeConsoleLaunchParams(std::string sParam, - STRING_LIST & LaunchList, - std::string sProcName, - std::string sMOOSName); + CMOOSProc* CreateMOOSProcess(std:: string sProcName); - //caled to figure out what if any additional parameters - //should be passed to the process being launched - bool MakeExtraExecutableParameters(std::string sParam, - STRING_LIST & ExtraCommandLineParameters, - std::string sProcName, - std::string sMOOSName); - - //Functions responsible for actually start new processes according to OS -#ifndef _WIN32 - bool DoNixOSLaunch(MOOSProc * pNewProc); -#else - bool DoWin32Launch(MOOSProc *pNewProc); -#endif + bool ConfigureMOOSComms(); bool SendMissionFile(); @@ -116,7 +74,7 @@ class CAntler bool PublishProcessQuit(const std::string & sProc); bool PublishProcessLaunch(const std::string & sProc); - typedef std::list MOOSPROC_LIST; + typedef std::list MOOSPROC_LIST; MOOSPROC_LIST m_ProcList; std::string m_sDefaultExecutablePath; CProcessConfigReader m_MissionReader; @@ -154,9 +112,6 @@ class CAntler /** goodby MOOSDB*/ bool OnMOOSDisconnect(); - /** Kill a process gently */ - bool KillNicely(MOOSProc* pProc); - CMOOSLock m_JobLock; std::string m_sMissionFile; bool m_bHeadless; diff --git a/Essentials/pAntler/CMakeLists.txt b/Essentials/pAntler/CMakeLists.txt index 926018d..ec2e7d7 100644 --- a/Essentials/pAntler/CMakeLists.txt +++ b/Essentials/pAntler/CMakeLists.txt @@ -4,7 +4,7 @@ set(EXECNAME pAntler) find_package(MOOS 10) #what files are needed? -SET(SRCS AntlerMain.cpp Antler.cpp) +SET(SRCS AntlerMain.cpp Antler.cpp MOOSProc.cpp) include_directories( ${${EXECNAME}_INCLUDE_DIRS} ${MOOS_INCLUDE_DIRS} ${MOOS_DEPEND_INCLUDE_DIRS}) add_executable(${EXECNAME} ${SRCS} ) From 2b3120e80534614585f5fce8ace53fd695387313 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 12:23:09 -0400 Subject: [PATCH 11/25] fix typo --- Essentials/pAntler/MOOSProc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/pAntler/MOOSProc.h b/Essentials/pAntler/MOOSProc.h index f37676f..49f77e8 100644 --- a/Essentials/pAntler/MOOSProc.h +++ b/Essentials/pAntler/MOOSProc.h @@ -226,4 +226,4 @@ class CMOOSProcScreenNix : public CMOOSProcNixBase #endif // _WIN32 -#endif // ifndef MOOSPROC +#endif // ifndef MOOSPROCH From 9fc042d7e16346518a598eb3ca57dad1b2642d36 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 12:26:39 -0400 Subject: [PATCH 12/25] whitespace --- Essentials/pAntler/MOOSProc.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index 2450e70..1e4356f 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -511,12 +511,3 @@ bool CMOOSProcNixBase::IsStopped() #endif - - - - - - - - - From 417005ac75f93aeb4a5de3ab8c96f6f56b3742cc Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 12:52:24 -0400 Subject: [PATCH 13/25] comments added --- Essentials/pAntler/MOOSProc.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index 1e4356f..ca2c827 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -54,6 +54,7 @@ bool CMOOSProc::BuildConfig() return MOOSFail(" warning cannot find extra parameters named \"%s\"\n", sExtraParamsName.c_str()); + // convert commas to spaces while (!sExtraParams.empty()) m_CommandLineParameters.push_back(MOOSChomp(sExtraParams, ",")); } @@ -308,6 +309,7 @@ bool CMOOSProcConsoleNix::BuildConfigSpecific() while (!sLaunchConfiguration.empty()) { MOOSTrimWhiteSpace(sLaunchConfiguration); + // convert commas to spaces string sP = MOOSChomp(sLaunchConfiguration, ","); MOOSTrimWhiteSpace(sP); if (!sP.empty()) From e6766cb7c224f5a65d8df2ee68fbfeb14f55f86f Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 16:49:39 -0400 Subject: [PATCH 14/25] newlines --- Essentials/pAntler/Antler.cpp | 14 +++++++------- Essentials/pAntler/MOOSProc.cpp | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index ae978d1..42e7bf4 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -532,7 +532,7 @@ CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) lt = L_SCREEN; else { - MOOSTrace("Error: LaunchType was not one of (Process, Console, Screen)"); + MOOSTrace("Error: LaunchType was not one of (Process, Console, Screen)\n"); return NULL; } } @@ -547,12 +547,12 @@ CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) // fill in the value if we need it. otherwise, warn if we have both specifiers if (L_UNDEF == lt) { - MOOSTrace("Warning: NEWCONSOLE is deprecated. Use LAUNCHTYPE."); + //MOOSTrace("Warning: NEWCONSOLE is deprecated. Use LAUNCHTYPE.\n"); lt = bNewConsole ? L_CONSOLE : L_PROCESS; } else if (bGotConsoleParam) { - MOOSTrace("Warning: LAUNCHTYPE overrides NEWCONSOLE, so ignoring this value"); + MOOSTrace("Warning: LAUNCHTYPE overrides NEWCONSOLE, so ignoring this value\n"); } @@ -577,10 +577,10 @@ CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) pNewProc = new CMOOSProcConsoleWin32(config, &m_MissionReader); break; case L_SCREEN: - MOOSTrace("GNU screen launch is not yet supported under Win32"); + MOOSTrace("GNU screen launch is not yet supported under Win32\n"); return NULL; default: - MOOSTrace("LAUNCHTYPE and NEWCONSOLE didn't reveal the launch type"); + MOOSTrace("LAUNCHTYPE and NEWCONSOLE didn't reveal the launch type\n"); return NULL; } @@ -598,7 +598,7 @@ CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) pNewProc = new CMOOSProcScreenNix(config, &m_MissionReader); break; default: - MOOSTrace("LAUNCHTYPE and NEWCONSOLE didn't reveal the launch type"); + MOOSTrace("LAUNCHTYPE and NEWCONSOLE didn't reveal the launch type\n"); return NULL; } @@ -607,7 +607,7 @@ CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) if (!pNewProc->BuildConfig()) { - MOOSTrace("App %s failed BuildConfig()", config.m_sAppName.c_str()); + MOOSTrace("App %s failed BuildConfig()\n", config.m_sAppName.c_str()); delete pNewProc; return NULL; } diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index ca2c827..c5c91a6 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -348,7 +348,7 @@ bool CMOOSProcNixBase::Start(bool bGentle) if (!m_FullCommandLine.size()) { - MOOSTrace("pAntler author error: no elements in m_FullCommandLine!"); + MOOSTrace("pAntler author error: no elements in m_FullCommandLine!\n"); return false; } From 801fee6998fadf7ff907aba6705a4716823b3c4f Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 16:51:46 -0400 Subject: [PATCH 15/25] don't double-count procs --- Essentials/pAntler/Antler.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index 42e7bf4..004ad39 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -616,7 +616,6 @@ CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) //finally spawn each according to his own if (pNewProc->Start(m_bSupportGentleKill)) { - ++m_nCurrentLaunch; return pNewProc; } else From 693ee42c631a19bbcb7291d450941d6a365653d9 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 17:12:06 -0400 Subject: [PATCH 16/25] support for LaunchType=Screen TESTED WORKING IN UBUNTU --- Essentials/pAntler/MOOSProc.cpp | 82 ++++++++++++++++++++++++++++++++- Essentials/pAntler/MOOSProc.h | 4 ++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index c5c91a6..2c60aa6 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -137,7 +137,7 @@ bool CMOOSProcConsoleWin32::BuildConfigSpecific2() //OK look for this configuration string if (!m_MissionReader->GetConfigurationParam(sLaunchConfigurationName, sLaunchConfiguration)) - return MOOSFail(" warning: could not find resource string called \"%s\"", + return MOOSFail(" warning: could not find resource string called \"%s\"\n", sLaunchConfigurationName.c_str()) ; // which is ridiculous, because we can't do anything with it @@ -270,7 +270,7 @@ bool CMOOSProcConsoleNix::BuildConfigSpecific() //OK look for this configuration string if (!m_MissionReader->GetConfigurationParam(sLaunchConfigurationName, sLaunchConfiguration)) - return MOOSFail(" warning: could not find resource string called \"%s\"", + return MOOSFail(" warning: could not find resource string called \"%s\"\n", sLaunchConfigurationName.c_str()) ; } else @@ -322,6 +322,30 @@ bool CMOOSProcConsoleNix::BuildConfigSpecific() bool CMOOSProcScreenNix::BuildConfigSpecific() { + string sLaunchConfigurationName; + string sLaunchConfiguration; + + + if (m_cfg.GetParamValue("ScreenConfig", sLaunchConfigurationName)) + { + //OK look for this configuration string + // likely they would add -L for loggging, or -l/-ln for shell login options + if (!m_MissionReader->GetConfigurationParam(sLaunchConfigurationName, + sLaunchConfiguration)) + return MOOSFail(" warning: could not find resource string called \"%s\"\n", + sLaunchConfigurationName.c_str()) ; + } + + //OK now simply chomp our way through a space delimited list... + while (!sLaunchConfiguration.empty()) + { + MOOSTrimWhiteSpace(sLaunchConfiguration); + // convert commas to spaces + string sP = MOOSChomp(sLaunchConfiguration, ","); + MOOSTrimWhiteSpace(sP); + if (!sP.empty()) + m_ScreenLaunchParameters.push_back(sP); + } return true; } @@ -441,6 +465,28 @@ bool CMOOSProcConsoleNix::StartSpecific() bool CMOOSProcScreenNix::StartSpecific() { + // screen -dmS + m_FullCommandLine.push_back("screen"); + m_FullCommandLine.push_back("-dmS"); + m_FullCommandLine.push_back(m_cfg.m_sMOOSName); + + m_FullCommandLine.splice(m_FullCommandLine.end(), m_ScreenLaunchParameters); + + + // this is the same as the plain-vanilla nix launch + m_FullCommandLine.push_back(m_cfg.m_sAppPath); + + if (!this->m_bInhibitMOOSParams) + { + //we do the usual thing of supplying Mission file and MOOSName + + m_FullCommandLine.push_back(this->MissionFile()); + m_FullCommandLine.push_back(m_cfg.m_sMOOSName); + } + + //here we pass extra parameters to the MOOS process if required + m_FullCommandLine.splice(m_FullCommandLine.end(), m_CommandLineParameters); + return true; } @@ -501,6 +547,38 @@ bool CMOOSProcConsoleNix::StopSpecific(bool bGentle) bool CMOOSProcScreenNix::StopSpecific(bool bGentle) { + if (bGentle) + { + kill(m_ChildPID, SIGTERM); + } + else + { + //we need to be crafty.... + string sCmd = "ps -e -o ppid= -o pid="; + + FILE* In = popen(sCmd.c_str(), "r"); + + if (In == NULL) return false; + + bool bFound = false; + char Line[256]; + while (fgets(Line, sizeof(Line), In)) + { + stringstream L(Line); + int ppid, pid; + L >> ppid; + L >> pid; + + if (m_ChildPID == ppid) + { + kill(pid, SIGTERM); + bFound = true; + } + } + pclose(In); + return bFound; + } + return true; } diff --git a/Essentials/pAntler/MOOSProc.h b/Essentials/pAntler/MOOSProc.h index 49f77e8..944aea5 100644 --- a/Essentials/pAntler/MOOSProc.h +++ b/Essentials/pAntler/MOOSProc.h @@ -174,6 +174,8 @@ class CMOOSProcNixBase : public CMOOSProc virtual bool IsStopped(); protected: + // for all *nix processes, this will be the master arg list + // that gets converted into the execvp arg list STRING_LIST m_FullCommandLine; }; @@ -203,6 +205,7 @@ class CMOOSProcConsoleNix : public CMOOSProcNixBase virtual bool StopSpecific(bool bGentle); protected: + // parameters specifically for an xterm launch, preceding the "-e" STRING_LIST m_ConsoleLaunchParameters; }; @@ -219,6 +222,7 @@ class CMOOSProcScreenNix : public CMOOSProcNixBase virtual bool StopSpecific(bool bGentle); protected: + // parameters specifically for a screen launch, preceding the command (and args) STRING_LIST m_ScreenLaunchParameters; }; From b4089173c89d688fbba2953f2a50d85e4436b451 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 28 Oct 2013 17:30:21 -0400 Subject: [PATCH 17/25] prevent forking, so that antler doesn't think the process died --- Essentials/pAntler/MOOSProc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index 2c60aa6..934db96 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -467,7 +467,7 @@ bool CMOOSProcScreenNix::StartSpecific() { // screen -dmS m_FullCommandLine.push_back("screen"); - m_FullCommandLine.push_back("-dmS"); + m_FullCommandLine.push_back("-DmS"); m_FullCommandLine.push_back(m_cfg.m_sMOOSName); m_FullCommandLine.splice(m_FullCommandLine.end(), m_ScreenLaunchParameters); From 49c3954c7d4a741be278bd003f56560052e93955 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 1 Nov 2013 10:09:53 -0400 Subject: [PATCH 18/25] use namespace std instead of prefixing std:: --- Essentials/pAntler/Antler.h | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Essentials/pAntler/Antler.h b/Essentials/pAntler/Antler.h index 8370c8f..a568c37 100644 --- a/Essentials/pAntler/Antler.h +++ b/Essentials/pAntler/Antler.h @@ -24,6 +24,8 @@ #define DEFAULTTIMEBETWEENSPAWN 1000 +using namespace std; + class CAntler { public: @@ -31,11 +33,11 @@ class CAntler //constructor CAntler(); //this is the only public function. Call it to have Antler do its thing. - bool Run(const std::string & sMissionFile, - std::set Filter = std::set() ); + bool Run(const string & sMissionFile, + set Filter = set() ); //run in a headless fashion - instructions will be recieved via MOOSComms - bool Run(const std::string & sHost, int lPort, const std::string & sAntlerName); + bool Run(const string & sHost, int lPort, const string & sAntlerName); enum VERBOSITY_LEVEL { @@ -60,27 +62,27 @@ class CAntler protected: //top level spawn - all comes from here - bool Spawn(const std::string & sMissionFile, bool bHeadless = false); + bool Spawn(const string & sMissionFile, bool bHeadless = false); //create, configure and launch a process - CMOOSProc* CreateMOOSProcess(std:: string sProcName); + CMOOSProc* CreateMOOSProcess( string sProcName); bool ConfigureMOOSComms(); bool SendMissionFile(); //tell a Monarch what is goinon remotely - bool PublishProcessQuit(const std::string & sProc); - bool PublishProcessLaunch(const std::string & sProc); + bool PublishProcessQuit(const string & sProc); + bool PublishProcessLaunch(const string & sProc); - typedef std::list MOOSPROC_LIST; + typedef list MOOSPROC_LIST; MOOSPROC_LIST m_ProcList; - std::string m_sDefaultExecutablePath; + string m_sDefaultExecutablePath; CProcessConfigReader m_MissionReader; //if this set is non empty then only procs listed here will be run.. - std::set m_Filter; + set m_Filter; int m_nCurrentLaunch; @@ -113,17 +115,17 @@ class CAntler bool OnMOOSDisconnect(); CMOOSLock m_JobLock; - std::string m_sMissionFile; + string m_sMissionFile; bool m_bHeadless; bool m_bQuitCurrentJob; bool m_bSupportGentleKill; bool m_bRunning; bool m_bNewJob; - std::string m_sMonarchAntler; + string m_sMonarchAntler; bool m_bKillOnDBDisconnect; - std::string m_sReceivedMissionFile; - std::string m_sAntlerName; - std::string m_sDBHost; + string m_sReceivedMissionFile; + string m_sAntlerName; + string m_sDBHost; int m_nDBPort; VERBOSITY_LEVEL m_eVerbosity; From 12394056c66ecaf5fb09c3a353776dd40a821f80 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 1 Nov 2013 10:38:42 -0400 Subject: [PATCH 19/25] print MOOSName on quit messages to be consistent with launch messages --- Essentials/pAntler/Antler.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index 004ad39..edcfcc6 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -447,9 +447,10 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) //this a really important bit of text most folk will want to see it... InhibitMOOSTraceInThisThread(m_eVerbosity == QUIET); { - MOOSTrace(" [%.3d] Process: %-15s has quit\n", + MOOSTrace(" [%.3d] Process: %-15s ~ %-15s has quit\n", --m_nCurrentLaunch, - pMOOSProc->GetAppName().c_str()); + pMOOSProc->GetAppName().c_str(), + pMOOSProc->GetMOOSName().c_str()); } InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); @@ -643,7 +644,9 @@ bool CAntler::ShutDown() #ifndef _WIN32 int nStatus = 0; - MOOSTrace("\n Signalling %-15s ", pMOOSProc->GetAppName().c_str()); + MOOSTrace("\n Signalling %-15s ~ %-15s", + pMOOSProc->GetAppName().c_str(), + pMOOSProc->GetMOOSName().c_str()); if (0 < waitpid(pMOOSProc->GetChildPID(), &nStatus, 0)) { MOOSTrace("[OK]"); From b225dcc1bf8c8237a29ac7b5ccfc2215e5ec3640 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 1 Nov 2013 10:39:11 -0400 Subject: [PATCH 20/25] read 'debug launch' from moos file instead of compile-time option --- Essentials/pAntler/MOOSProc.cpp | 8 +++++--- Essentials/pAntler/MOOSProc.h | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index 934db96..bd5c29b 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -1,7 +1,5 @@ #include "MOOSProc.h" -#define DEBUG_LAUNCH 0 - using namespace std; @@ -41,6 +39,10 @@ bool CProcCfg::GetParamValue(string sParam, string &sValue) // fully parse the config, looking up all references to alternate configs bool CMOOSProc::BuildConfig() { + // whether to debug the launch + m_bDebugLaunch = false; + m_MissionReader->GetConfigurationParam("DebugLaunch", m_bDebugLaunch); + // Look for a line containing additional command line params (ExtraProcessParams) m_CommandLineParameters.clear(); @@ -388,7 +390,7 @@ bool CMOOSProcNixBase::Start(bool bGentle) //terminate list pExecVParams[i++] = NULL; - if(DEBUG_LAUNCH) + if(m_bDebugLaunch) { for (int j = 0; j < i; j++) MOOSTrace("argv[%d]:\"%s\"\n", j, pExecVParams[j]); diff --git a/Essentials/pAntler/MOOSProc.h b/Essentials/pAntler/MOOSProc.h index 944aea5..10b0cd5 100644 --- a/Essentials/pAntler/MOOSProc.h +++ b/Essentials/pAntler/MOOSProc.h @@ -92,6 +92,7 @@ class CMOOSProc CProcessConfigReader* m_MissionReader; // settings from more in-depth parsing + bool m_bDebugLaunch; bool m_bInhibitMOOSParams; STRING_LIST m_CommandLineParameters; From 08fcd7733f4417104d06932ce953a58dc1540aea Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 1 Nov 2013 10:39:43 -0400 Subject: [PATCH 21/25] sample file to test different launch modes --- Essentials/pAntler/test-antler-nix.moos | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Essentials/pAntler/test-antler-nix.moos diff --git a/Essentials/pAntler/test-antler-nix.moos b/Essentials/pAntler/test-antler-nix.moos new file mode 100644 index 0000000..5e3b610 --- /dev/null +++ b/Essentials/pAntler/test-antler-nix.moos @@ -0,0 +1,35 @@ +ProcessConfig = ANTLER +{ + MSBetweenLaunches = 100 + + DebugLaunch = false + + Run = MOOSDB @ LaunchType=Screen + + Run = xmessage @ NewConsole = false,XConfig=xconfig1 ~ xmessage-moosfile-moosname + + param_oldplaunch = launched,with,NewConsole=False + param_oldclaunch = launched,with,NewConsole=True + param_newplaunch = launched,with,LaunchType=Process + param_newclaunch = launched,with,LaunchType=Console + param_newslaunch = launched,with,LaunchType=Screen + + Run = xmessage @ NewConsole = true, InhibitMOOSParams=true,ExtraProcessParams=param_oldplaunch ~ xmessage-oldlaunch-process + Run = xmessage @ NewConsole = false, InhibitMOOSParams=true,ExtraProcessParams=param_oldclaunch ~ xmessage-oldlaunch-console + Run = xmessage @ LaunchType = Process, InhibitMOOSParams=true,ExtraProcessParams=param_newplaunch ~ xmessage-newlaunch-process + Run = xmessage @ LaunchType = Console, InhibitMOOSParams=true,ExtraProcessParams=param_newclaunch ~ xmessage-newlaunch-console + Run = xmessage @ LaunchType = Screen, InhibitMOOSParams=true,ExtraProcessParams=param_newslaunch ~ xmessage-newlaunch-screen + + param_xconfig = launched,with,xconfig + xconfig1 = -geometry,40x40+0+20 + Run = xmessage @ LaunchType=Console,XConfig=xconfig1,ExtraProcessParams=param_xconfig ~ xmessage-xconfig + + + Run = /usr/bin/xmessage @ InhibitMOOSParams=true, ExtraProcessParams=fullpath,NewConsole = false ~ xmessage-fullpath + fullpath = launched,with,full,path,to,executable + + Run = screen @ InhibitMOOSParams=true, ExtraProcessParams=screensummary,Xconfig=screenconsole,LaunchType=Console ~ screen-summary + screensummary = -ls + screenconsole = -hold + +} From d7a1b9d87c76b1dd1b61aaecb1d86651ca1c3d23 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 1 Nov 2013 10:41:20 -0400 Subject: [PATCH 22/25] more time between launches in test script --- Essentials/pAntler/test-antler-nix.moos | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/pAntler/test-antler-nix.moos b/Essentials/pAntler/test-antler-nix.moos index 5e3b610..98675e3 100644 --- a/Essentials/pAntler/test-antler-nix.moos +++ b/Essentials/pAntler/test-antler-nix.moos @@ -1,6 +1,6 @@ ProcessConfig = ANTLER { - MSBetweenLaunches = 100 + MSBetweenLaunches = 1000 DebugLaunch = false From 6283c62e41c2a5327cf71f78b02afbf728188ad9 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 1 Nov 2013 10:42:57 -0400 Subject: [PATCH 23/25] make xterm wide enough to view screen -ls --- Essentials/pAntler/test-antler-nix.moos | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/pAntler/test-antler-nix.moos b/Essentials/pAntler/test-antler-nix.moos index 98675e3..cc94e08 100644 --- a/Essentials/pAntler/test-antler-nix.moos +++ b/Essentials/pAntler/test-antler-nix.moos @@ -30,6 +30,6 @@ ProcessConfig = ANTLER Run = screen @ InhibitMOOSParams=true, ExtraProcessParams=screensummary,Xconfig=screenconsole,LaunchType=Console ~ screen-summary screensummary = -ls - screenconsole = -hold + screenconsole = -hold,-geometry,120x20+0+20 } From 981947a44f240a694381beb3b343bb46e6ff09a6 Mon Sep 17 00:00:00 2001 From: Ian Katz Date: Wed, 4 Dec 2013 17:15:08 -0500 Subject: [PATCH 24/25] properly initialize m_bInhibitMOOSParams --- Essentials/pAntler/MOOSProc.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index bd5c29b..ddfe9af 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -62,6 +62,7 @@ bool CMOOSProc::BuildConfig() } // Inhibit MOOS params? + m_bInhibitMOOSParams = false; MOOSValFromString(m_bInhibitMOOSParams, m_cfg.m_sParams, "InhibitMOOSParams", true); //by default process are assumed to be on the system path From 9c20b39fd867465bd6f7759080e0171bdefbfdb0 Mon Sep 17 00:00:00 2001 From: Ian Katz Date: Wed, 4 Dec 2013 17:37:06 -0500 Subject: [PATCH 25/25] tabs purged --- Essentials/pAntler/Antler.cpp | 22 ++-- Essentials/pAntler/Antler.h | 8 +- Essentials/pAntler/AntlerMain.cpp | 161 ++++++++++++++---------------- Essentials/pAntler/MOOSProc.cpp | 18 ++-- 4 files changed, 97 insertions(+), 112 deletions(-) diff --git a/Essentials/pAntler/Antler.cpp b/Essentials/pAntler/Antler.cpp index edcfcc6..8ccebe8 100644 --- a/Essentials/pAntler/Antler.cpp +++ b/Essentials/pAntler/Antler.cpp @@ -175,7 +175,6 @@ bool CAntler::SetVerbosity(VERBOSITY_LEVEL eLevel) InhibitMOOSTraceInThisThread(true); if (m_pMOOSComms != NULL) m_pMOOSComms->SetQuiet(true); - } return true; } @@ -356,11 +355,10 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) } - //are we being ask to support gentle process killing? m_MissionReader.GetConfigurationParam("GentleKill", m_bSupportGentleKill); - - + + //now cycle through each line in the configuration block. // If it begins with run then it means launch for (p = sParams.begin(); p != sParams.end(); p++) @@ -387,7 +385,7 @@ bool CAntler::Spawn(const std::string & sMissionFile, bool bHeadless) pNew->GetMOOSName().c_str()); } InhibitMOOSTraceInThisThread(m_eVerbosity != CHATTY); - + m_ProcList.push_front(pNew); m_nCurrentLaunch++; PublishProcessLaunch(pNew->GetAppName()); @@ -629,7 +627,7 @@ CMOOSProc* CAntler::CreateMOOSProcess(string sConfiguration) bool CAntler::ShutDown() { - + MOOSPROC_LIST::iterator q; MOOSTrace("\n\n|***** Shutdown *****|\n\n"); @@ -640,9 +638,9 @@ bool CAntler::ShutDown() if (pMOOSProc->Stop(m_bSupportGentleKill)) { - + #ifndef _WIN32 - + int nStatus = 0; MOOSTrace("\n Signalling %-15s ~ %-15s", pMOOSProc->GetAppName().c_str(), @@ -650,15 +648,13 @@ bool CAntler::ShutDown() if (0 < waitpid(pMOOSProc->GetChildPID(), &nStatus, 0)) { MOOSTrace("[OK]"); - } + } #endif } } - + MOOSTrace("\n\n All spawned processes shutdown.\n\n That was the MOOS \n\n"); - - + return true; } - diff --git a/Essentials/pAntler/Antler.h b/Essentials/pAntler/Antler.h index a568c37..a983ff7 100644 --- a/Essentials/pAntler/Antler.h +++ b/Essentials/pAntler/Antler.h @@ -38,7 +38,7 @@ class CAntler //run in a headless fashion - instructions will be recieved via MOOSComms bool Run(const string & sHost, int lPort, const string & sAntlerName); - + enum VERBOSITY_LEVEL { QUIET, @@ -55,10 +55,10 @@ class CAntler }; bool SetVerbosity(VERBOSITY_LEVEL eLevel); - + //call this to cause a clean shut down bool ShutDown(); - + protected: //top level spawn - all comes from here @@ -113,7 +113,7 @@ class CAntler bool OnMOOSConnect(); /** goodby MOOSDB*/ bool OnMOOSDisconnect(); - + CMOOSLock m_JobLock; string m_sMissionFile; bool m_bHeadless; diff --git a/Essentials/pAntler/AntlerMain.cpp b/Essentials/pAntler/AntlerMain.cpp index 62228e2..d88387a 100644 --- a/Essentials/pAntler/AntlerMain.cpp +++ b/Essentials/pAntler/AntlerMain.cpp @@ -50,24 +50,22 @@ CAntler gAntler; //this is a signal handler void CatchMeBeforeDeath(int sig) { - gAntler.ShutDown(); - exit(0); + gAntler.ShutDown(); + exit(0); } #endif int main(int argc ,char *argv[]) { - + #ifndef _WIN32 - //register a handler for shutdown - signal(SIGINT, CatchMeBeforeDeath); - signal( SIGQUIT, CatchMeBeforeDeath); - signal( SIGTERM, CatchMeBeforeDeath); + //register a handler for shutdown + signal(SIGINT, CatchMeBeforeDeath); + signal(SIGQUIT, CatchMeBeforeDeath); + signal(SIGTERM, CatchMeBeforeDeath); #endif - - - + //here we look for overloading directoves which are of form --name=value std::vector vArgv; std::map OverLoads; @@ -81,23 +79,23 @@ int main(int argc ,char *argv[]) std::string sVar = MOOSChomp(t,"="); if(t.empty()) { - if(MOOSStrCmp(sVar, "quiet")) - { - gAntler.SetVerbosity(CAntler::QUIET); - } - else if(MOOSStrCmp(sVar, "terse")) - { - gAntler.SetVerbosity(CAntler::TERSE); - } - else - { - MOOSTrace("error incomplete overloading of parameter --%s=value (are there extraneous whitespaces?)\n",sVar.c_str()); - return -1; - } - } + if(MOOSStrCmp(sVar, "quiet")) + { + gAntler.SetVerbosity(CAntler::QUIET); + } + else if(MOOSStrCmp(sVar, "terse")) + { + gAntler.SetVerbosity(CAntler::TERSE); + } + else + { + MOOSTrace("error incomplete overloading of parameter --%s=value (are there extraneous whitespaces?)\n",sVar.c_str()); + return -1; + } + } else { - OverLoads[sVar] = t; + OverLoads[sVar] = t; } } else @@ -108,85 +106,76 @@ int main(int argc ,char *argv[]) } - MOOSTrace("*************************************\n"); + MOOSTrace("*************************************\n"); MOOSTrace("* This is Antler, head of MOOS... *\n"); MOOSTrace("* P. Newman 2008 *\n"); MOOSTrace("*************************************\n"); - + switch(vArgv.size()) { - case 2: + case 2: + //standard principal Antler + std::string sMissionFile = vArgv[1]; + + if((int)vArgv.size()!=argc) { - //standard principal Antler - std::string sMissionFile = vArgv[1]; + //we are overloading somehow... + CProcessConfigReader MR; + MR.SetFile(sMissionFile); - if((int)vArgv.size()!=argc) - { - //we are overloading somehow... - CProcessConfigReader MR; - MR.SetFile(sMissionFile); - - sMissionFile+="++"; - - //we need to take a copy of the missions file and fill in overloads - if(!MR.MakeOverloadedCopy(sMissionFile,OverLoads)) - return MOOSFail("error making overloaded mission file\n"); - - } + sMissionFile+="++"; + + //we need to take a copy of the missions file and fill in overloads + if(!MR.MakeOverloadedCopy(sMissionFile,OverLoads)) + return MOOSFail("error making overloaded mission file\n"); - - return gAntler.Run(sMissionFile) ? 0 :-1; } - case 3: + + + return gAntler.Run(sMissionFile) ? 0 :-1; + case 3: + //standard principal Antler but only run a subset of processes + //arg 3 must be string quoted + std::string sMissionFile = vArgv[1]; + + + if((int)vArgv.size()!=argc) { - //standard principal Antler but only run a subset of processes - //arg 3 must be string quoted - std::string sMissionFile = vArgv[1]; - - - if((int)vArgv.size()!=argc) - { - //we are overloading somehow... - CProcessConfigReader MR; - MR.SetFile(sMissionFile); + //we are overloading somehow... + CProcessConfigReader MR; + MR.SetFile(sMissionFile); - sMissionFile+="++"; + sMissionFile+="++"; - //we need to take a copy of the missions file and fill in overloads - if(!MR.MakeOverloadedCopy(sMissionFile,OverLoads)) - return MOOSFail("error making overloaded mission file\n"); + //we need to take a copy of the missions file and fill in overloads + if(!MR.MakeOverloadedCopy(sMissionFile,OverLoads)) + return MOOSFail("error making overloaded mission file\n"); - } + } - //make a set of processes we want to launch - std::stringstream S(vArgv[2]); - std::set Filter; - //this rather fancy looking bit f stl simply iterates over a list of strings - std::copy(istream_iterator(S), - istream_iterator(), - std::inserter(Filter,Filter.begin())); + //make a set of processes we want to launch + std::stringstream S(vArgv[2]); + std::set Filter; + //this rather fancy looking bit f stl simply iterates over a list of strings + std::copy(istream_iterator(S), + istream_iterator(), + std::inserter(Filter,Filter.begin())); - return gAntler.Run(sMissionFile,Filter); - } - case 4: - { - //headless MOOS - driven my another Antler somewhere else - std::string sDBHost = vArgv[1]; //where is DB? - int nPort = atoi(vArgv[2].c_str()); //what port - std::string sName = vArgv[3]; //what is our Antler name? - return gAntler.Run(sDBHost,nPort,sName) ? 0 :-1; - } - default: - { - MOOSTrace("usage:\n pAntler missionfile.moos\nor\n pAntler missionfile.moos \"P1,P2,P3...\"\nor pAntler DBHost DBPort AntlerName\n"); - MOOSTrace("\n --quiet to suppress console output, --terse for limited output\n"); - - return -1; - } + return gAntler.Run(sMissionFile,Filter); + case 4: + //headless MOOS - driven my another Antler somewhere else + std::string sDBHost = vArgv[1]; //where is DB? + int nPort = atoi(vArgv[2].c_str()); //what port + std::string sName = vArgv[3]; //what is our Antler name? + return gAntler.Run(sDBHost,nPort,sName) ? 0 :-1; + default: + MOOSTrace("usage:\n pAntler missionfile.moos\nor\n pAntler missionfile.moos \"P1,P2,P3...\"\nor pAntler DBHost DBPort AntlerName\n"); + MOOSTrace("\n --quiet to suppress console output, --terse for limited output\n"); + + return -1; } - } diff --git a/Essentials/pAntler/MOOSProc.cpp b/Essentials/pAntler/MOOSProc.cpp index ddfe9af..08d46de 100644 --- a/Essentials/pAntler/MOOSProc.cpp +++ b/Essentials/pAntler/MOOSProc.cpp @@ -228,8 +228,8 @@ bool CMOOSProcWin32::Start(bool bGentle) { delete this->m_pWin32Proc; } - MOOSTrace("*** %s Launch Failed:***\n\a\a", this->m_sApp.c_str()); - MOOSTrace("%s\n", e.sGetException()); + MOOSTrace("*** %s Launch Failed:***\n\a\a", this->m_sApp.c_str()); + MOOSTrace("%s\n", e.sGetException()); return false; } @@ -398,7 +398,7 @@ bool CMOOSProcNixBase::Start(bool bGentle) } //and finally replace ourselves with a new xterm process image - char * const * pParamList = const_cast (pExecVParams); + char * const * pParamList = const_cast (pExecVParams); if (execvp(pExecVParams[0], pParamList) == -1) { MOOSTrace("Failed exec - not good. Called exec as follows:\n"); @@ -521,9 +521,9 @@ bool CMOOSProcConsoleNix::StopSpecific(bool bGentle) { //we need to be crafty.... string sCmd = "ps -e -o ppid= -o pid="; - + FILE* In = popen(sCmd.c_str(), "r"); - + if (In == NULL) return false; bool bFound = false; @@ -540,7 +540,7 @@ bool CMOOSProcConsoleNix::StopSpecific(bool bGentle) kill(pid, SIGTERM); bFound = true; } - } + } pclose(In); return bFound; } @@ -558,9 +558,9 @@ bool CMOOSProcScreenNix::StopSpecific(bool bGentle) { //we need to be crafty.... string sCmd = "ps -e -o ppid= -o pid="; - + FILE* In = popen(sCmd.c_str(), "r"); - + if (In == NULL) return false; bool bFound = false; @@ -577,7 +577,7 @@ bool CMOOSProcScreenNix::StopSpecific(bool bGentle) kill(pid, SIGTERM); bFound = true; } - } + } pclose(In); return bFound; }