MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Essentials/pAntler/AntlerMain.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS Core Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00030 
00031 
00032 
00033 
00034 
00035 #include "Antler.h"
00036 #include <sstream>
00037 #include <string>
00038 #include <iterator>
00039 #include <algorithm>
00040 
00041 using namespace std;
00042 
00043 
00044 
00045 
00046 //this is our main object...everything after this is configuration...
00047 CAntler gAntler;
00048 
00049 #ifndef _WIN32
00050 //this is a signal handler
00051 void CatchMeBeforeDeath(int sig) 
00052 {
00053         gAntler.ShutDown();
00054         exit(0);
00055 }
00056 #endif
00057 
00058 
00059 int main(int argc ,char *argv[])
00060 {
00061         
00062 #ifndef _WIN32
00063         //register a handler for shutdown
00064         signal(SIGINT, CatchMeBeforeDeath);
00065         signal( SIGQUIT, CatchMeBeforeDeath);
00066         signal( SIGTERM, CatchMeBeforeDeath);
00067 #endif
00068         
00069       
00070     
00071     //here we look for overloading directoves which are of form --name=value
00072     std::vector<std::string> vArgv;
00073     std::map<std::string, std::string> OverLoads;
00074     for(int i = 0;i<argc;i++)
00075     {
00076         std::string t(argv[i]);
00077         if(t.find("--")==0)
00078         {
00079             //this looks like an overloading directive
00080             MOOSChomp(t,"--");
00081             std::string sVar = MOOSChomp(t,"=");
00082             if(t.empty())
00083             {
00084                                 if(MOOSStrCmp(sVar, "quiet"))
00085                                 {
00086                                         gAntler.SetVerbosity(CAntler::QUIET);
00087                                 }
00088                                 else if(MOOSStrCmp(sVar, "terse"))
00089                                 {
00090                                         gAntler.SetVerbosity(CAntler::TERSE);
00091                                 }
00092                                 else
00093                                 {
00094                                         MOOSTrace("error incomplete overloading of parameter  --%s=value (are there extraneous whitespaces?)\n",sVar.c_str());
00095                                         return -1;
00096                                 }
00097                         }
00098             else
00099             {
00100                 OverLoads[sVar] = t;
00101             }
00102         }
00103         else
00104         {
00105             //nope this is a regular parameter so it gets passed onto what is yet to come
00106             vArgv.push_back(t);
00107         }
00108     }
00109     
00110       
00111         MOOSTrace("*************************************\n");
00112     MOOSTrace("*  This is Antler, head of MOOS...  *\n");
00113     MOOSTrace("*  P. Newman 2008                   *\n");
00114     MOOSTrace("*************************************\n");
00115         
00116     
00117     switch(vArgv.size())
00118     {
00119         case 2:
00120         {
00121             //standard principal Antler
00122             std::string sMissionFile = vArgv[1];
00123             
00124             if((int)vArgv.size()!=argc)
00125             {
00126                 //we are overloading somehow...
00127                 CProcessConfigReader MR;
00128                 MR.SetFile(sMissionFile);
00129                 
00130                 sMissionFile+="++";
00131                 
00132                 //we need to take a copy of the missions file and fill in overloads
00133                 if(!MR.MakeOverloadedCopy(sMissionFile,OverLoads))
00134                     return MOOSFail("error making overloaded mission file\n");
00135                 
00136             }
00137             
00138                     
00139             return gAntler.Run(sMissionFile) ? 0 :-1;            
00140         }
00141         case 3:
00142         {
00143             //standard principal Antler but only run a subset of processes
00144             //arg 3 must be string quoted
00145             std::string sMissionFile = vArgv[1];
00146             
00147             
00148             if((int)vArgv.size()!=argc)
00149             {
00150                 //we are overloading somehow...
00151                 CProcessConfigReader MR;
00152                 MR.SetFile(sMissionFile);
00153                 
00154                 sMissionFile+="++";
00155                 
00156                 //we need to take a copy of the missions file and fill in overloads
00157                 if(!MR.MakeOverloadedCopy(sMissionFile,OverLoads))
00158                     return MOOSFail("error making overloaded mission file\n");
00159                 
00160             }
00161             
00162             
00163             
00164             //make a set of processes we want to launch
00165             std::stringstream S(vArgv[2]); 
00166             std::set<std::string> Filter;
00167             //this rather fancy looking bit f stl simply iterates over a list of strings
00168             std::copy(istream_iterator<std::string>(S), 
00169                       istream_iterator<string>(),
00170                       std::inserter(Filter,Filter.begin()));
00171                        
00172             return gAntler.Run(sMissionFile,Filter);
00173         }
00174         case 4:
00175         {            
00176             //headless MOOS - driven my another Antler somewhere else
00177             std::string sDBHost = vArgv[1]; //where is DB?
00178             int nPort = atoi(vArgv[2].c_str()); //what port
00179             std::string sName = vArgv[3]; //what is our Antler name?
00180             return gAntler.Run(sDBHost,nPort,sName) ? 0 :-1;
00181         }
00182         default:
00183         {
00184             MOOSTrace("usage:\n pAntler missionfile.moos\nor\n pAntler missionfile.moos \"P1,P2,P3...\"\nor pAntler DBHost DBPort AntlerName\n");
00185                         MOOSTrace("\n  --quiet to suppress console output, --terse for limited output\n");
00186                         
00187             return -1;
00188         }
00189     }
00190   
00191     
00192 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines