JobController Class Reference

#include <JobController.h>

List of all members.

Public Member Functions

 JobController ()
 JobController (int argc, char **argv)
 ~JobController ()
JobControlleroperator<< (const char *)
void operator() (const char *c)
JobCEnvGetJobEnv () const
JobCPathRegistryGetPathRegistry () const
MomNavigatorGetMom ()
int Run ()

Static Public Member Functions

static JobControllerGetMotherController ()

Private Member Functions

void BuildSystemPath ()
bool ControllerCommand (JobCommand *cmd)
void HandleCommand (const char *cmd)
void HandleCommand (JobCommand *cmd)
void Help (JobCommand *cmd)
void Init ()
void ProcessCommands ()
void ReadMacroFile (const char *filename)

Private Attributes

JobCPathRegistryfPathRegistry
JobCInterpreterfInterp
MomNavigator fMom

Static Private Attributes

static JobControllerfMotherController = 0
static const char * fSystemPathName

Detailed Description

Definition at line 29 of file JobController.h.


Constructor & Destructor Documentation

JobController::JobController (  ) 

Definition at line 32 of file JobController.cxx.

References Init().

00032                              :
00033   fPathRegistry(new JobCPathRegistry()),
00034   fInterp(new JobCInterpreter())
00035 {
00036 //======================================================================
00037 // Purpose: Create a default job controller
00038 //======================================================================
00039   
00040   // Do a little work to set the initial state
00041   this->Init();
00042 }

JobController::JobController ( int  argc,
char **  argv 
)

Definition at line 46 of file JobController.cxx.

References Init(), and JobCEnv::Instance().

00046                                                   :
00047   fPathRegistry(new JobCPathRegistry()),
00048   fInterp(new JobCInterpreter())
00049 {
00050 //======================================================================
00051 // Purpose: Create the job controller
00052 //
00053 // Inputs: argc      - Number of command line arguments
00054 //         argv      - The array of command line arguments
00055 //======================================================================
00056   JobCEnv::Instance(argc, argv);
00057 
00058   // Perform some initializations
00059   this->Init();
00060 }

JobController::~JobController (  ) 

Definition at line 64 of file JobController.cxx.

References fInterp, fMotherController, fPathRegistry, Msg::kVerbose, and MSG.

00065 {
00066   MSG("JobC",Msg::kVerbose) << 
00067     "Shutting down JobController @ " << this << "\n";
00068   // Delete in order created
00069   if (fInterp)       {delete fInterp;       fInterp       = 0; }
00070   if (fPathRegistry) {delete fPathRegistry; fPathRegistry = 0; }
00071 
00072   if (this == fMotherController) fMotherController = 0;
00073 }


Member Function Documentation

void JobController::BuildSystemPath (  )  [private]

Definition at line 335 of file JobController.cxx.

References JobCModule::BeginJob(), fMom, fPathRegistry, JobCNode::GetModule(), JobCPathRegistry::MakePath(), JobCPath::PushBack(), JobCPathModule::SetMom(), and JobCPathModule::SetPathRegistry().

Referenced by Init().

00336 {
00337 //======================================================================
00338 // Purpose: Build the "system path" - the list of modules which are
00339 // provided by the framework to configure, querry, etc. the system.
00340 //======================================================================
00341   // Make the system path
00342   JobCPath* syspath = fPathRegistry->MakePath(".jobcontroller",&fMom,0);
00343   assert(syspath);
00344 
00345   // Add the framework modules to the system path
00346   JobCNode* dn = syspath->PushBack("Display","Void");
00347   JobCNode* mn = syspath->PushBack("Msg",    "Void");
00348   JobCNode* pn = syspath->PushBack("Path",   "Void");
00349   JobCNode* rn = syspath->PushBack("Root",   "Void");
00350   
00351   // Configure these nodes
00352   JobCPathModule* pm = static_cast<JobCPathModule*>(pn->GetModule());
00353   assert(pm);
00354   pm->SetPathRegistry(fPathRegistry);
00355   pm->SetMom(&fMom);
00356 
00357   dn->GetModule()->BeginJob();
00358   mn->GetModule()->BeginJob();
00359   pn->GetModule()->BeginJob();
00360   rn->GetModule()->BeginJob();
00361 }

bool JobController::ControllerCommand ( JobCommand cmd  )  [private]

Definition at line 148 of file JobController.cxx.

References JobCInterpreter::EchoOff(), JobCInterpreter::EchoOn(), fInterp, JobCommand::HaveOpt(), Help(), Msg::kWarning, MSG, JobCommand::PopCmd(), JobCommand::PopOpt(), JobCommand::PushCmd(), and ReadMacroFile().

Referenced by HandleCommand().

00149 {
00150 //======================================================================
00151 // Purpose: Handle commands intrinsic to the job controller
00152 //======================================================================
00153   string c1 = cmd->PopCmd();
00154   if (c1 == "echo" || c1 == "Echo" || c1 == "ECHO") {
00155     // Command "echo"
00156     if (cmd->HaveOpt()) {
00157       string c2 = cmd->PopOpt();
00158       if (c2 == "on" || c2 == "On" || c2 == "ON") {
00159         fInterp->EchoOn();
00160         return true;
00161       }
00162       if (c2 == "off" || c2 == "Off" || c2 == "OFF") {
00163         fInterp->EchoOff();
00164         return true;
00165       }
00166     }
00167     else {
00168       // No options -- default is to turn echoing on
00169       fInterp->EchoOn();
00170       return true;
00171     }
00172   }
00173   if (c1 == "help" || c1 == "Help" || c1 == "HELP") {
00174     // Command "help"
00175     this->Help(cmd);
00176     return true;
00177   }
00178   if (c1 == "Exec" || c1 == "exec" || c1 == "EXEC") {
00179     if (cmd->HaveOpt()) {
00180       string macrofile = cmd->PopOpt();
00181       this->ReadMacroFile(macrofile.c_str());
00182       return true;
00183     }
00184     else {
00185       MSG("JobC",Msg::kWarning) << "Need JobControl macro file.\n";
00186       return false;
00187     }
00188   }
00189 
00190   // Failed to handle this command -- push the command line back up
00191   cmd->PushCmd();
00192   return false;
00193 }

JobCEnv * JobController::GetJobEnv (  )  const

Definition at line 77 of file JobController.cxx.

References JobCEnv::Instance().

00078 {
00079   JobCEnv& env = JobCEnv::Instance();
00080   return &env;
00081 }

MomNavigator * JobController::GetMom (  ) 

Definition at line 85 of file JobController.cxx.

References fMom.

00086 {
00087   return &fMom;
00088 }

JobController * JobController::GetMotherController (  )  [static]

Definition at line 98 of file JobController.cxx.

References fMotherController.

Referenced by JobCROOT::GetMom(), and JobCROOT::operator()().

00099 { 
00100 //======================================================================
00101 // Purpose: Return a pointer to the "mother controller", the first of
00102 // a sequence of controllers instantiated in a job.
00103 //
00104 // Note: Use of this method only makes sense for interactive jobs
00105 // where it makes sense to assume that the user is talking to a single
00106 // job controller (ie. the mother controller). Batch jobs could in
00107 // general have multiple controllers so any code that you expect to
00108 // work in a batch system *should not* rely on this call.
00109 //======================================================================
00110   if (fMotherController) {
00111     return fMotherController;
00112   }
00113   else {
00114     // Don't have one -- so make one. This will only get allocated if
00115     // this block gets executed
00116     static JobController motherController;
00117     
00118     // Assert that the instantiation set the fMotherController variable
00119     // and return it
00120     assert(fMotherController);
00121     return fMotherController;
00122   }
00123   return 0; // keeps compilers quiet.
00124 }

JobCPathRegistry * JobController::GetPathRegistry (  )  const

Definition at line 92 of file JobController.cxx.

References fPathRegistry.

00092                                                        { 
00093   return fPathRegistry; 
00094 }

void JobController::HandleCommand ( JobCommand cmd  )  [private]

Definition at line 197 of file JobController.cxx.

References ControllerCommand(), fPathRegistry, JobCPath::GetModule(), JobCPathHandler::HandleCommand(), JobCModule::HandleCommand(), Msg::kWarning, JobCPathRegistry::LookUpModule(), JobCPathRegistry::LookUpPath(), MSG, JobCommand::PopCmd(), and JobCommand::PushCmd().

00198 {
00199 //======================================================================
00200 // Purpose: Allow commands to be fed into the JobController
00201 //
00202 //  Assumes that commands have the following format:
00203 //  /[path]/[module]/... 
00204 //  and then pass the rest on to the job module to handle
00205 //======================================================================
00206   const char *c1;
00207   JobCPath   *path  =0;
00208   JobCModule *module=0;
00209 
00210   // Commands are handled in the following way until a unique
00211   // destination for the command is found:
00212   //
00213   // 1) Check if this job controller knows how to process the command 
00214   //     yes -> done, no -> 2)
00215   // 2) Check if the command references an existing path
00216   //     yes->3, no->5
00217   // 3) Check if the command references a module in the path
00218   //     yes->done, no->4)
00219   // 4) Send the command to the path itself
00220   // 5) See if the command references a module in any path uniquely
00221   //    yes->done no->warning
00222 
00223   // 1) See if the command is an "instrinsic" JobController command
00224   if (this->ControllerCommand(cmd)) return;
00225 
00226   // 2) Check if this matches an existing path
00227   c1 = cmd->PopCmd();
00228   path = fPathRegistry->LookUpPath(c1);
00229   if (path) {
00230     // Matches an existing path
00231     const char *c2;
00232     c2 = cmd->PopCmd();
00233     // 3) Found the path, see if the next thing is a module in the path
00234     if (c2) {
00235       module = path->GetModule(c2);
00236       if (module == 0) {
00237         // 4) No module, send the back the command up and send it to the path
00238         cmd->PushCmd();
00239         JobCPathHandler ph(path);
00240         ph.HandleCommand(cmd);
00241         return;
00242       }
00243     }
00244   }
00245   else {
00246     // 5) See if the command references a module uniquely
00247     module = fPathRegistry->LookUpModule(c1);
00248     if ( module == 0 ) {
00249       // Failed to find unique module
00250       MSG("JobC",Msg::kWarning) 
00251         << "Failed to find '" << c1 << "'.\n";
00252       return;
00253     }
00254   }
00255 
00256   // To get here we must have uniquely identified a job module to
00257   // which to send the command
00258   module->HandleCommand(cmd);
00259 }

void JobController::HandleCommand ( const char *  cmd  )  [private]

Referenced by ProcessCommands().

void JobController::Help ( JobCommand cmd  )  [private]

Definition at line 263 of file JobController.cxx.

References JobCommand::HaveOpt(), JobCModuleProxy::Help(), JobCModuleRegistry::Instance(), Msg::kInfo, Msg::kWarning, JobCModuleRegistry::LookUp(), MSG, and JobCommand::PopOpt().

Referenced by ControllerCommand().

00264 {
00265   if (cmd->HaveOpt()) {
00266     while (cmd->HaveOpt()) {
00267       // Figure out what module the user wants help for
00268       const char *opt = cmd->PopOpt();
00269       JobCModuleProxy* mp = JobCModuleRegistry::Instance().LookUp(opt);
00270       if (mp) {
00271         mp->Help();
00272       }
00273       else {
00274         MSG("JobC", Msg::kWarning) << "Can not find module " << opt << endl;
00275       }
00276     }
00277   }
00278   else {
00279     MSG("JobC",Msg::kInfo) <<
00280       "===================================="
00281       "====================================\n" << 
00282       "                             JOBCONTROL HELP\n" <<
00283       "\n" <<
00284       " In general job control commands are of the format:\n" << 
00285       " /[path]/[module]/[command] [option1] [option2] ...\n" <<
00286       "\n" <<
00287       "  where [path]    is a name of a job path\n" <<
00288       "        [module]  is a name of a job module\n" <<
00289       "        [command] is the name of a command to send to the module\n" <<
00290       "        [option]  is a possible option for the command\n" <<
00291       "\n" << 
00292       " Commands are in general case sensitive.\n" <<
00293       "\n" << 
00294       " For help with a specific job module try:\n" <<
00295       "  Help [module1] [module2] ...\n" <<
00296       "\n" <<
00297       " The following is a list of " <<
00298       JobCModuleRegistry::Instance() <<
00299       "===================================="
00300       "====================================" << endl;
00301   }
00302 }

void JobController::Init ( void   )  [private]

Definition at line 306 of file JobController.cxx.

References BuildSystemPath(), exit(), fMotherController, JobCEnv::GetMacroFile(), JobCEnv::GetModuleHelpList(), JobCEnv::Instance(), ReadMacroFile(), and Run().

Referenced by JobController().

00307 {
00308 //======================================================================
00309 // Purpose: Set the initial state of the JobController
00310 //======================================================================
00311   // If the user requested help from the command line handle that
00312   if (JobCEnv::Instance().GetModuleHelpList()) {
00313     (*this) << "Help " << JobCEnv::Instance().GetModuleHelpList() << "\n";
00314     this->Run();
00315     exit(1);
00316   }
00317   
00318   // If this is the first controller, set it to be the mother controller
00319   if (fMotherController == 0) fMotherController = this;
00320 
00321   // Build the system path
00322   this->BuildSystemPath();
00323   
00324   // Process any macro files that have been loaded
00325   const char *macrofile;
00326   for (unsigned int i=0; 
00327        (macrofile=JobCEnv::Instance().GetMacroFile(i))!=0; 
00328        ++i) {
00329     this->ReadMacroFile(macrofile);
00330   }
00331 }

void JobController::operator() ( const char *  c  ) 

Definition at line 128 of file JobController.cxx.

00128 { (*this) << c << "\n"; }

JobController & JobController::operator<< ( const char *  cmd  ) 

Definition at line 132 of file JobController.cxx.

References JobCEnv::Instance(), and ProcessCommands().

00133 {
00134 //======================================================================
00135 // Purpose: Allow commands to be fed into the JobController
00136 //======================================================================
00137   // Feed the command to the interpreter
00138   (*fInterp) << cmd;
00139 
00140   // If we're in interactive mode act on the commands right away
00141   if (JobCEnv::Instance().IsBatch()==false) this->ProcessCommands();
00142 
00143   return *this;
00144 }

void JobController::ProcessCommands (  )  [private]

Definition at line 365 of file JobController.cxx.

References fInterp, HandleCommand(), jc, and JobCInterpreter::PopJobCommand().

Referenced by operator<<(), and Run().

00366 {
00367 //======================================================================
00368 // Purpose: Process any commands in the interpreter's stack
00369 //======================================================================
00370   while (JobCommand* jc = fInterp->PopJobCommand()) {
00371     this->HandleCommand(jc);
00372   }
00373 }

void JobController::ReadMacroFile ( const char *  filename  )  [private]

Definition at line 377 of file JobController.cxx.

References Msg::kWarning, and MSG.

Referenced by ControllerCommand(), and Init().

00378 {
00379 //======================================================================
00380 // Purpose: Read job control commands from a macro file
00381 //======================================================================
00382   ifstream ifs(filename);
00383   
00384   if (ifs.bad()) {
00385     MSG("JobC",Msg::kWarning) << 
00386       "Macro file '" << filename << "' does not exist.\n";
00387     return;
00388   }
00389   else {
00390     const unsigned int bufs = 2048;
00391     char c[bufs];
00392     string s;
00393     while (ifs.good()) {
00394       ifs.getline(c,bufs,'\n');
00395       s = c;
00396       s += '\n'; // Add the return character back on
00397       (*this) << s.c_str();
00398     }
00399   }
00400 }

int JobController::Run (  ) 

Definition at line 404 of file JobController.cxx.

References JobCEnv::Instance(), ProcessCommands(), and JobCEnv::RunRootApp().

Referenced by Init(), and main().

00405 {
00406 //======================================================================
00407 // Purpose: Run the job
00408 //======================================================================
00409   this->ProcessCommands();
00410   JobCEnv::Instance().RunRootApp();
00411   return 1;
00412 }


Member Data Documentation

Definition at line 62 of file JobController.h.

Referenced by ControllerCommand(), ProcessCommands(), and ~JobController().

Definition at line 63 of file JobController.h.

Referenced by BuildSystemPath(), and GetMom().

Definition at line 58 of file JobController.h.

Referenced by GetMotherController(), Init(), and ~JobController().

Definition at line 61 of file JobController.h.

Referenced by BuildSystemPath(), GetPathRegistry(), HandleCommand(), and ~JobController().

const char* JobController::fSystemPathName [static, private]

Definition at line 59 of file JobController.h.


The documentation for this class was generated from the following files:

Generated on 2 Nov 2017 for loon by  doxygen 1.6.1