Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

JobCModuleRegistry.cxx

Go to the documentation of this file.
00001 
00002 // $Id: JobCModuleRegistry.cxx,v 1.15 2003/10/17 00:41:12 messier Exp $
00003 //
00004 // A place for the modules available to a given job to register
00005 // themselves
00006 //
00007 // messier@huhepl.harvard.edu
00009 #include "JobControl/JobCModuleRegistry.h"
00010 #include <cstdio>
00011 #include <cstring>
00012 #include "TSystem.h" // Load() library
00013 #include "MessageService/MsgService.h"
00014 
00015 CVSID("$Id: JobCModuleRegistry.cxx,v 1.15 2003/10/17 00:41:12 messier Exp $");
00016 
00017 JobCModuleRegistry* JobCModuleRegistry::fInstance = 0;
00018 
00019 //......................................................................
00020 
00021 ostream& operator<<(ostream& os, const JobCModuleRegistry& jcr) 
00022 {
00023   list<JobCModuleProxy*>::const_iterator itrModuleTable;
00024   os << "Registered job modules:\n";
00025   for (itrModuleTable  = jcr.fModuleTable.begin();
00026        itrModuleTable != jcr.fModuleTable.end();
00027        ++itrModuleTable) {
00028     os << " " << 
00029       (*itrModuleTable)->GetName() << " - " <<
00030       (*itrModuleTable)->GetFunc() << "\n";
00031   }
00032   return os;
00033 }
00034 
00035 //......................................................................
00036 
00037 JobCModuleRegistry::~JobCModuleRegistry() {}
00038 
00039 //......................................................................
00040 
00041 void JobCModuleRegistry::Register(JobCModuleProxy* proxy)
00042 {
00043   const char* modName = proxy->GetName();
00044   // Add the module to the table
00045   if (this->LookUp(modName,false)) {
00046     // MSG("JobC", Msg::kWarning)
00047     // << "A job module named " << modName << " is already registered!\n"
00048     // << "Duplicate module names are not allowed!\n";
00049     return;
00050   }
00051   else {
00052     // Do insertions in alphabetical order
00053     list<JobCModuleProxy*>::iterator itrModuleTable;
00054     for (itrModuleTable  = fModuleTable.begin();
00055          itrModuleTable != fModuleTable.end();
00056          ++itrModuleTable) {
00057       if (strcmp(modName,(*itrModuleTable)->GetName())<0) {
00058         break;
00059       }
00060     }
00061     fModuleTable.insert(itrModuleTable,proxy);
00062 #ifdef SITE_HAS_SIGC // temporary
00063     this->SigRegister(modName);
00064 #endif
00065     MSG("JobC", Msg::kDebug) 
00066       << "Registered job module " << modName << " " 
00067       << proxy << ".\n";
00068   }
00069 }
00070 
00071 //......................................................................
00072 
00073 JobCModuleProxy* JobCModuleRegistry::LookUp(const char *name, bool loadlib)
00074 {
00075   // Check if its in table
00076   list<JobCModuleProxy*>::iterator itrModuleTable;
00077   for (itrModuleTable  = fModuleTable.begin();
00078        itrModuleTable != fModuleTable.end();
00079        ++itrModuleTable) {
00080     if (strcmp(name,(*itrModuleTable)->GetName())==0) {
00081       return (*itrModuleTable);
00082     }
00083   }
00084   
00085   // Not in table... try loading libraries
00086   if (loadlib && this->CheckLibMap(name)) return this->LookUp(name);
00087 
00088   return 0;
00089 }
00090 
00091 //......................................................................
00092 
00093 JobCModuleRegistry::JobCModuleRegistry()
00094 {
00095 #ifdef DEBUG_JOBC
00096 #include <MessageService/MsgStream.h>
00097   // Setting this debug level at run time is too late in this case...
00098   MsgService::Instance()->GetStream("JobC")->SetLogLevel(Msg::kVerbose);
00099 #endif
00100   this->BuildModToLibMap();
00101 }
00102 
00103 //......................................................................
00104 
00105 JobCModuleRegistry& JobCModuleRegistry::Instance() 
00106 {
00107   static JobCModuleRegistry::Cleaner c; // end-of-run clean up
00108   if (fInstance==0) {
00109     c.ClassIsUsed(); // Keeps compiler's quite if I "use" this object
00110     fInstance = new JobCModuleRegistry;
00111   }
00112   return *fInstance;
00113 }
00114 
00115 //......................................................................
00116 
00117 int JobCModuleRegistry::BuildModToLibMap()
00118 {
00119 //======================================================================
00120 // Poke around looking for a "jobmodules.txt" file and use it to build
00121 // the map of which libraries provide which modules
00122 //======================================================================
00123 
00124   // Try to find a jobmodules file which lists which libraries provide
00125   // which modules
00126   FILE* fp = 0;
00127   // First try in the private release
00128   const char* tmp = getenv("SRT_PRIVATE_CONTEXT");
00129   if (!tmp) return 0;
00130   std::string modfile = tmp;
00131   modfile += "/tmp/jobmodules.txt";
00132   if ( (fp=fopen(modfile.c_str(),"r"))==0 ) {
00133     // Next, try the public release
00134     tmp = getenv("SRT_PUBLIC_CONTEXT");
00135     if (!tmp) return 0;
00136     std::string modfile = tmp;
00137     modfile += "/tmp/jobmodules.txt";
00138     if ( (fp=fopen(modfile.c_str(),"r"))==0 ) {
00139       // Finally try in /tmp
00140       modfile = "/tmp/jobmodules.txt";
00141       if ( (fp=fopen(modfile.c_str(),"r"))==0 ) {
00142         // Give up...
00143         return 0;
00144       }
00145     }
00146   }
00147   
00148   // To get here fp must be valid
00149   std::string lib = "";
00150   char buff[256];
00151   while (fgets(buff,255,fp)) {
00152     if (buff[0]=='#') continue;
00153 
00154     // Get rid of leading and training white space
00155     const char* b = buff;
00156     for (; *b==' ' && *b!='\0'; ++b);
00157     int len = strlen(buff)-1;
00158     for (; len>0 && (buff[len]==' '||buff[len]=='\n'); --len);
00159     buff[len+1] = '\0';
00160     
00161     // Figure out if entry is library or module
00162     len = strlen(b);
00163     if (len<2) continue;
00164     const char* tag = b + len - 3;
00165     if (strncmp(b,"lib",3)==0 && strncmp(tag, ".so",3)==0) {
00166       lib = b;
00167     }
00168     else {
00169       std::string module = b;
00170       if (fModLibMap[module]=="") {
00171         fModLibMap[module] = lib;
00172       }
00173       else {
00174         if (!(fModLibMap[module] == lib)) {
00175           MSG("JobC",Msg::kWarning) << 
00176             "Duplicate entry: " << 
00177             module << "->" << fModLibMap[module] << "," <<
00178             module << "->" << lib                << "." << 
00179             std::endl;
00180         }
00181       }
00182     }
00183   }
00184   fclose(fp);
00185   return 1;
00186 }
00187 
00188 //......................................................................
00189 
00190 int JobCModuleRegistry::CheckLibMap(const std::string& module) 
00191 {
00192   std::string lib = fModLibMap[module];
00193   if (lib=="") {
00194     MSG("JobC",Msg::kWarning) << 
00195       "\n" <<
00196       "Unable to find module '"<<module<<"' in job module list.\n" <<
00197       "Consider regenerating list with 'makemodulemap' command or\n" <<
00198       "edit the $SRT_PRIVATE_CONTEXT/tmp/jobmodules.txt file by hand.\n";
00199     return 0;
00200   }
00201   if (gSystem) {
00202     if (gSystem->Load(lib.c_str())==0) {
00203       // Loaded OK, we can remove entries pointing at this library
00204       std::map<std::string,std::string>::iterator itr(fModLibMap.begin());
00205       std::map<std::string,std::string>::iterator itrEnd(fModLibMap.end());
00206       for (;itr!=itrEnd;++itr) {
00207         if (itr->first == lib) fModLibMap.erase(itr);
00208       }
00209       return 1;
00210     }
00211   }
00212   return 0;
00213 }
00214 

Generated on Mon Feb 15 11:06:49 2010 for loon by  doxygen 1.3.9.1