]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTComponentHandler.cxx
Minor cleanup of code.
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponentHandler.cxx
index 28a829399f0169f756cf6479ed113d722ee588dd..c26cc14341108692bfe5c57c1fb2f63b9c24628e 100644 (file)
@@ -1,11 +1,12 @@
 // $Id$
 
 /**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ * This file is property of and copyright by the ALICE HLT Project        * 
+ * ALICE Experiment at CERN, All rights reserved.                         *
  *                                                                        *
- * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
- *          Timm Steinbeck <timm@kip.uni-heidelberg.de>                   *
- *          for The ALICE Off-line Project.                               *
+ * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
+ *                  Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
+ *                  for The ALICE HLT Project.                            *
  *                                                                        *
  * Permission to use, copy, modify and distribute this software and its   *
  * documentation strictly for non-commercial purposes is hereby granted   *
     @date   
     @brief  Implementation of HLT component handler. */
 
+// see header file for class documentation
+// or
+// refer to README to build package
+// or
+// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+
 #if __GNUC__>= 3
 using namespace std;
 #endif
@@ -31,17 +38,12 @@ using namespace std;
 //#include <Riostream.h>
 #include <TSystem.h>
 #endif //HAVE_DLFCN_H
-#include "AliHLTStdIncludes.h"
+//#include "AliHLTStdIncludes.h"
 #include "AliHLTComponentHandler.h"
 #include "AliHLTComponent.h"
 #include "AliHLTDataTypes.h"
-#include "AliHLTSystem.h"
-
-// the standard components
-// #include "AliHLTFilePublisher.h"
-// #include "AliHLTFileWriter.h"
-// #include "AliHLTRootFilePublisherComponent.h"
-// #include "AliHLTRootFileWriterComponent.h"
+#include "AliHLTModuleAgent.h"
+#include "TString.h"
 
 /** ROOT macro for the implementation of ROOT specific class methods */
 ClassImp(AliHLTComponentHandler)
@@ -52,7 +54,8 @@ AliHLTComponentHandler::AliHLTComponentHandler()
   fScheduleList(),
   fLibraryList(),
   fEnvironment(),
-  fStandardList()
+  fOwnedComponents(),
+  fLibraryMode(kDynamic)
 {
   // see header file for class documentation
   // or
@@ -65,11 +68,13 @@ AliHLTComponentHandler::AliHLTComponentHandler()
 
 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
   :
+  AliHLTLogging(),
   fComponentList(),
   fScheduleList(),
   fLibraryList(),
   fEnvironment(),
-  fStandardList()
+  fOwnedComponents(),
+  fLibraryMode(kDynamic)
 {
   // see header file for class documentation
   if (pEnv) {
@@ -81,15 +86,22 @@ AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
       // for redirection
       AliHLTLogging::Init(pEnv->fLoggingFunc);
     }
-  }  else
+  }  else {
     memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
+  }
+  //#ifndef __DEBUG
+  //SetLocalLoggingLevel(kHLTLogError);
+  //#else
+  //SetLocalLoggingLevel(kHLTLogInfo);
+  //#endif
+
   AddStandardComponents();
 }
 
 AliHLTComponentHandler::~AliHLTComponentHandler()
 {
   // see header file for class documentation
-  DeleteStandardComponents();
+  DeleteOwnedComponents();
   UnloadLibraries();
 }
 
@@ -104,13 +116,25 @@ int AliHLTComponentHandler::AnnounceVersion()
   HLTbaseCompileInfo(date, time);
   if (!date) date="unknown";
   if (!time) time="unknown";
-  HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
+  HLTImportant("%s build on %s (%s)", PACKAGE_STRING, date, time);
 #else
-  HLTInfo("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
+  HLTImportant("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
 #endif
   return iResult;
 }
 
+Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
+{
+  // see header file for class documentation
+  Int_t iResult=0;
+  if (pSample==NULL) return -EINVAL;
+  if ((iResult=RegisterComponent(pSample))>=0) {
+    //HLTDebug("sample %s (%p) managed by handler", pSample->GetComponentID(), pSample);
+    fOwnedComponents.push_back(pSample);
+  }
+  return iResult;
+}
+
 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
 {
   // see header file for class documentation
@@ -135,8 +159,10 @@ Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
 {
   // see header file for class documentation
+
   int iResult=0;
   if (componentID) {
+    HLTWarning("not yet implemented, please notify the developers if you need this function");
   } else {
     iResult=-EINVAL;
   }
@@ -155,7 +181,7 @@ Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
   return iResult;
 }
 
-int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
+int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component, const char* cdbPath )
 {
   // see header file for class documentation
   int iResult=0;
@@ -165,6 +191,9 @@ int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvP
       component=pSample->Spawn();
       if (component) {
        HLTDebug("component \"%s\" created (%p)", componentID, component);
+       if (cdbPath) {
+         component->InitCDB(cdbPath, this);
+       }
        if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
          HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
          delete component;
@@ -189,7 +218,7 @@ Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
   // see header file for class documentation
   Int_t iResult=0;
   if (componentID) {
-    vector<AliHLTComponent*>::iterator element=fComponentList.begin();
+    AliHLTComponentPList::iterator element=fComponentList.begin();
     while (element!=fComponentList.end() && iResult>=0) {
       if (strcmp(componentID, (*element)->GetComponentID())==0) {
        break;
@@ -230,13 +259,28 @@ Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
 void AliHLTComponentHandler::List() 
 {
   // see header file for class documentation
-  vector<AliHLTComponent*>::iterator element=fComponentList.begin();
+  AliHLTComponentPList::iterator element=fComponentList.begin();
   int index=0;
   while (element!=fComponentList.end()) {
     HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
   }
 }
 
+int AliHLTComponentHandler::HasOutputData( const char* componentID)
+{
+  // see header file for class documentation
+  int iResult=0;
+  AliHLTComponent* pSample=FindComponent(componentID);
+  if (pSample) {
+    AliHLTComponent::TComponentType ct=AliHLTComponent::kUnknown;
+    ct=pSample->GetComponentType();
+    iResult=(ct==AliHLTComponent::kSource || ct==AliHLTComponent::kProcessor);
+  } else {
+    iResult=-ENOENT;
+  }
+  return iResult;
+}
+
 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) 
 {
   // see header file for class documentation
@@ -252,46 +296,95 @@ void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
   }
 }
 
-int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
+AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
+{
+  // see header file for class documentation
+  TLibraryMode old=fLibraryMode;
+  fLibraryMode=mode;
+  return old;
+}
+
+int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
 {
   // see header file for class documentation
   int iResult=0;
   if (libraryPath) {
+    // first activate all agents which are already loaded
+    if (bActivateAgents) ActivateAgents();
+
+    // set the global component handler for static component registration
     AliHLTComponent::SetGlobalComponentHandler(this);
+
     AliHLTLibHandle hLib;
     const char* loadtype="";
 #ifdef HAVE_DLFCN_H
     // use interface to the dynamic linking loader
-    hLib.handle=dlopen(libraryPath, RTLD_NOW);
-    loadtype="dlopen";
+
+    // exeption does not help in Root context, the Root exeption
+    // handler always catches the exeption before. Have to find out
+    // how exeptions can be used in Root
+    /*try*/ {
+      hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
+      loadtype="dlopen";
+    }
+    /*
+    catch (...) {
+      // error message printed further down
+      loadtype="dlopen exeption";
+    }
+    */
 #else
     // use ROOT dynamic loader
     // check if the library was already loaded, as Load returns
     // 'failure' if the library was already loaded
+    /*try*/ {
     AliHLTLibHandle* pLib=FindLibrary(libraryPath);
     if (pLib) {
-       int* pRootHandle=reinterpret_cast<int*>(pLib->handle);
+       int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
        (*pRootHandle)++;
        HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
-       hLib.handle=pRootHandle;
+       hLib.fHandle=pRootHandle;
     }
     
-    if (hLib.handle==NULL && gSystem->Load(libraryPath)==0) {
+    if (hLib.fHandle==NULL && gSystem->Load(libraryPath)>=0) {
       int* pRootHandle=new int;
       if (pRootHandle) *pRootHandle=1;
-      hLib.handle=pRootHandle;
+      hLib.fHandle=pRootHandle;
       //HLTDebug("library %s loaded via gSystem", libraryPath);
     }
     loadtype="gSystem";
+    }
+    /*
+    catch (...) {
+      // error message printed further down
+      loadtype="gSystem exeption";
+    }
+    */
 #endif //HAVE_DLFCN_H
-    if (hLib.handle!=NULL) {
+    if (hLib.fHandle!=NULL) {
       // create TString object to store library path and use pointer as handle 
-      hLib.name=new TString(libraryPath);
-      HLTInfo("library %s loaded (%s)", libraryPath, loadtype);
+      hLib.fName=new TString(libraryPath);
+      hLib.fMode=fLibraryMode;
+      HLTImportant("library %s loaded (%s%s)", libraryPath, hLib.fMode==kStatic?"persistent, ":"", loadtype);
       fLibraryList.insert(fLibraryList.begin(), hLib);
+      typedef void (*CompileInfo)( char*& date, char*& time);
+      CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
+      if (fctInfo) {
+       char* date="";
+       char* time="";
+       (*fctInfo)(date, time);
+       if (!date) date="unknown";
+       if (!time) time="unknown";
+       HLTImportant("build on %s (%s)", date, time);
+      } else {
+       HLTImportant("no build info available (possible AliRoot embedded build)");
+      }
+
+      // static registration of components when library is loaded
       iResult=RegisterScheduledComponents();
+
     } else {
-      HLTError("can not load library %s", libraryPath);
+      HLTError("can not load library %s (%s)", libraryPath, loadtype);
 #ifdef HAVE_DLFCN_H
       HLTError("dlopen error: %s", dlerror());
 #endif //HAVE_DLFCN_H
@@ -302,6 +395,13 @@ int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
 #endif
     }
     AliHLTComponent::UnsetGlobalComponentHandler();
+    
+    if (iResult>=0) {
+      // alternative dynamic registration by library agents
+      // !!! has to be done after UnsetGlobalComponentHandler
+      if (bActivateAgents) ActivateAgents();
+    }
+
   } else {
     iResult=-EINVAL;
   }
@@ -315,7 +415,7 @@ int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
   if (libraryPath) {
     vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
     while (element!=fLibraryList.end()) {
-      TString* pName=reinterpret_cast<TString*>((*element).name);
+      TString* pName=reinterpret_cast<TString*>((*element).fName);
       if (pName->CompareTo(libraryPath)==0) {
        UnloadLibrary(*element);
        fLibraryList.erase(element);
@@ -334,11 +434,23 @@ int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandl
   // see header file for class documentation
   int iResult=0;
   fgAliLoggingFunc=NULL;
-  TString* pName=reinterpret_cast<TString*>(handle.name);
+  TString* pName=reinterpret_cast<TString*>(handle.fName);
+  if (handle.fMode!=kStatic) {
 #ifdef HAVE_DLFCN_H
-  dlclose(handle.handle);
+  // exeption does not help in Root context, the Root exeption
+  // handler always catches the exeption before. Have to find out
+  // how exeptions can be used in Root
+
+  /*try*/ {
+    dlclose(handle.fHandle);
+  }
+  /*
+  catch (...) {
+    HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
+  }
+  */
 #else
-  int* pCount=reinterpret_cast<int*>(handle.handle);
+  int* pCount=reinterpret_cast<int*>(handle.fHandle);
   if (--(*pCount)==0) {
     if (pName) {
       /** Matthias 26.04.2007
@@ -366,14 +478,17 @@ int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandl
     delete pCount;
   }
 #endif //HAVE_DLFCN_H
-  handle.name=NULL;
-  handle.handle=NULL;
   if (pName) {
     HLTDebug("unload library %s", pName->Data());
-    delete pName;
   } else {
     HLTWarning("missing name for unloaded library");
   }
+  }
+  handle.fName=NULL;
+  handle.fHandle=NULL;
+  if (pName) {
+    delete pName;
+  }
   pName=NULL;
   return iResult;
 }
@@ -398,9 +513,9 @@ void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol
   if (hLib==NULL) return NULL;
   void* pFunc=NULL;
 #ifdef HAVE_DLFCN_H
-  pFunc=dlsym(hLib->handle, symbol);
+  pFunc=dlsym(hLib->fHandle, symbol);
 #else
-  TString* name=reinterpret_cast<TString*>(hLib->name);
+  TString* name=reinterpret_cast<TString*>(hLib->fName);
   pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
 #endif
   return pFunc;
@@ -412,7 +527,7 @@ AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(con
   AliHLTLibHandle* hLib=NULL;
   vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
   while (element!=fLibraryList.end()) {
-    TString* name=reinterpret_cast<TString*>((*element).name);
+    TString* name=reinterpret_cast<TString*>((*element).fName);
     if (name->CompareTo(library)==0) {
       hLib=&(*element);
       break;
@@ -427,10 +542,6 @@ int AliHLTComponentHandler::AddStandardComponents()
   // see header file for class documentation
   int iResult=0;
   AliHLTComponent::SetGlobalComponentHandler(this);
-//   fStandardList.push_back(new AliHLTFilePublisher);
-//   fStandardList.push_back(new AliHLTFileWriter);
-//   fStandardList.push_back(new AliHLTRootFilePublisherComponent);
-//   fStandardList.push_back(new AliHLTRootFileWriterComponent);
   AliHLTComponent::UnsetGlobalComponentHandler();
   iResult=RegisterScheduledComponents();
   return iResult;
@@ -440,7 +551,7 @@ int AliHLTComponentHandler::RegisterScheduledComponents()
 {
   // see header file for class documentation
   int iResult=0;
-  vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
+  AliHLTComponentPList::iterator element=fScheduleList.begin();
   int iLocalResult=0;
   while (element!=fScheduleList.end()) {
     iLocalResult=RegisterComponent(*element);
@@ -451,16 +562,50 @@ int AliHLTComponentHandler::RegisterScheduledComponents()
   return iResult;
 }
 
-int AliHLTComponentHandler::DeleteStandardComponents()
+int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
 {
   // see header file for class documentation
   int iResult=0;
-  vector<AliHLTComponent*>::iterator element=fStandardList.begin();
-  while (element!=fStandardList.end()) {
-    DeregisterComponent((*element)->GetComponentID());
-    delete(*element);
-    fStandardList.erase(element);
-    element=fStandardList.begin();
+  AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
+  while (pAgent && iResult>=0) {
+    if (blackList) {
+      int i=0;
+      for (; i<size; i++) {
+       if (blackList[i]==pAgent) break;
+      }
+      if (i<size) {
+       // this agent was in the list
+       pAgent=AliHLTModuleAgent::GetNextAgent();
+       continue;
+      }
+    }
+
+    pAgent->ActivateComponentHandler(this);
+    pAgent=AliHLTModuleAgent::GetNextAgent();
+  }
+  return iResult;
+}
+
+int AliHLTComponentHandler::DeleteOwnedComponents()
+{
+  // see header file for class documentation
+  int iResult=0;
+  AliHLTComponentPList::iterator element=fOwnedComponents.begin();
+  while (element!=fOwnedComponents.end()) {
+    //DeregisterComponent((*element)->GetComponentID());
+    // exeption does not help in Root context, the Root exeption
+    // handler always catches the exeption before. Have to find out
+    // how exeptions can be used in Root
+    /*try*/ {
+      delete *element;
+    }
+    /*
+    catch (...) {
+      HLTError("delete managed sample %p", *element);
+    }
+    */
+    fOwnedComponents.erase(element);
+    element=fOwnedComponents.begin();
   }
   return iResult;
 }