3 /**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
8 * for The ALICE Off-line Project. *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
19 /** @file AliHLTComponentHandler.cxx
20 @author Matthias Richter, Timm Steinbeck
22 @brief Implementation of HLT component handler. */
31 //#include <Riostream.h>
34 #include "AliHLTStdIncludes.h"
35 #include "AliHLTComponentHandler.h"
36 #include "AliHLTComponent.h"
37 #include "AliHLTDataTypes.h"
38 #include "AliHLTSystem.h"
40 // the standard components
41 // #include "AliHLTFilePublisher.h"
42 // #include "AliHLTFileWriter.h"
43 // #include "AliHLTRootFilePublisherComponent.h"
44 // #include "AliHLTRootFileWriterComponent.h"
46 /** ROOT macro for the implementation of ROOT specific class methods */
47 ClassImp(AliHLTComponentHandler)
49 AliHLTComponentHandler::AliHLTComponentHandler()
57 // see header file for class documentation
59 // refer to README to build package
61 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
62 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
63 AddStandardComponents();
66 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
74 // see header file for class documentation
76 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
77 if (pEnv->fLoggingFunc) {
78 // the AliHLTLogging::Init method also sets the stream output
79 // and notification handler to AliLog. This should only be done
80 // if the logging environment contains a logging function
82 AliHLTLogging::Init(pEnv->fLoggingFunc);
85 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
86 AddStandardComponents();
89 AliHLTComponentHandler::~AliHLTComponentHandler()
91 // see header file for class documentation
92 DeleteStandardComponents();
96 int AliHLTComponentHandler::AnnounceVersion()
98 // see header file for class documentation
100 #ifdef PACKAGE_STRING
101 void HLTbaseCompileInfo( char*& date, char*& time);
104 HLTbaseCompileInfo(date, time);
105 if (!date) date="unknown";
106 if (!time) time="unknown";
107 HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
109 HLTInfo("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
114 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
116 // see header file for class documentation
119 if (FindComponent(pSample->GetComponentID())==NULL) {
120 iResult=InsertComponent(pSample);
122 HLTInfo("component %s registered", pSample->GetComponentID());
125 // component already registered
126 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
135 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
137 // see header file for class documentation
146 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
148 // see header file for class documentation
151 fScheduleList.push_back(pSample);
158 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
160 // see header file for class documentation
163 AliHLTComponent* pSample=FindComponent(componentID);
165 component=pSample->Spawn();
167 HLTDebug("component \"%s\" created (%p)", componentID, component);
168 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
169 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
174 HLTError("can not spawn component \"%s\"", componentID);
178 HLTWarning("can not find component \"%s\"", componentID);
187 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
189 // see header file for class documentation
192 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
193 while (element!=fComponentList.end() && iResult>=0) {
194 if (strcmp(componentID, (*element)->GetComponentID())==0) {
200 if (element==fComponentList.end()) iResult=-ENOENT;
207 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
209 // see header file for class documentation
210 AliHLTComponent* pSample=NULL;
211 Int_t index=FindComponentIndex(componentID);
213 pSample=(AliHLTComponent*)fComponentList.at(index);
218 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
220 // see header file for class documentation
223 fComponentList.push_back(pSample);
230 void AliHLTComponentHandler::List()
232 // see header file for class documentation
233 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
235 while (element!=fComponentList.end()) {
236 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
240 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
242 // see header file for class documentation
244 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
245 if (fEnvironment.fLoggingFunc) {
246 // the AliHLTLogging::Init method also sets the stream output
247 // and notification handler to AliLog. This should only be done
248 // if the logging environment contains a logging function
250 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
255 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
257 // see header file for class documentation
260 AliHLTComponent::SetGlobalComponentHandler(this);
261 AliHLTLibHandle hLib;
262 const char* loadtype="";
264 // use interface to the dynamic linking loader
265 hLib.handle=dlopen(libraryPath, RTLD_NOW);
268 // use ROOT dynamic loader
269 // check if the library was already loaded, as Load returns
270 // 'failure' if the library was already loaded
271 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
273 int* pRootHandle=reinterpret_cast<int*>(pLib->handle);
275 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
276 hLib.handle=pRootHandle;
279 if (hLib.handle==NULL && gSystem->Load(libraryPath)==0) {
280 int* pRootHandle=new int;
281 if (pRootHandle) *pRootHandle=1;
282 hLib.handle=pRootHandle;
283 //HLTDebug("library %s loaded via gSystem", libraryPath);
286 #endif //HAVE_DLFCN_H
287 if (hLib.handle!=NULL) {
288 // create TString object to store library path and use pointer as handle
289 hLib.name=new TString(libraryPath);
290 HLTInfo("library %s loaded (%s)", libraryPath, loadtype);
291 fLibraryList.insert(fLibraryList.begin(), hLib);
292 iResult=RegisterScheduledComponents();
294 HLTError("can not load library %s", libraryPath);
296 HLTError("dlopen error: %s", dlerror());
297 #endif //HAVE_DLFCN_H
304 AliHLTComponent::UnsetGlobalComponentHandler();
311 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
313 // see header file for class documentation
316 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
317 while (element!=fLibraryList.end()) {
318 TString* pName=reinterpret_cast<TString*>((*element).name);
319 if (pName->CompareTo(libraryPath)==0) {
320 UnloadLibrary(*element);
321 fLibraryList.erase(element);
332 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
334 // see header file for class documentation
336 fgAliLoggingFunc=NULL;
337 TString* pName=reinterpret_cast<TString*>(handle.name);
339 dlclose(handle.handle);
341 int* pCount=reinterpret_cast<int*>(handle.handle);
342 if (--(*pCount)==0) {
344 gSystem->Unload(pName->Data());
347 HLTError("missing library name, can not unload");
351 #endif //HAVE_DLFCN_H
355 HLTDebug("unload library %s", pName->Data());
358 HLTWarning("missing name for unloaded library");
364 int AliHLTComponentHandler::UnloadLibraries()
366 // see header file for class documentation
368 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
369 while (element!=fLibraryList.end()) {
370 UnloadLibrary(*element);
371 fLibraryList.erase(element);
372 element=fLibraryList.begin();
377 void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
379 // see header file for class documentation
380 AliHLTLibHandle* hLib=FindLibrary(library);
381 if (hLib==NULL) return NULL;
384 pFunc=dlsym(hLib->handle, symbol);
386 TString* name=reinterpret_cast<TString*>(hLib->name);
387 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
392 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
394 // see header file for class documentation
395 AliHLTLibHandle* hLib=NULL;
396 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
397 while (element!=fLibraryList.end()) {
398 TString* name=reinterpret_cast<TString*>((*element).name);
399 if (name->CompareTo(library)==0) {
408 int AliHLTComponentHandler::AddStandardComponents()
410 // see header file for class documentation
412 AliHLTComponent::SetGlobalComponentHandler(this);
413 // fStandardList.push_back(new AliHLTFilePublisher);
414 // fStandardList.push_back(new AliHLTFileWriter);
415 // fStandardList.push_back(new AliHLTRootFilePublisherComponent);
416 // fStandardList.push_back(new AliHLTRootFileWriterComponent);
417 AliHLTComponent::UnsetGlobalComponentHandler();
418 iResult=RegisterScheduledComponents();
422 int AliHLTComponentHandler::RegisterScheduledComponents()
424 // see header file for class documentation
426 vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
428 while (element!=fScheduleList.end()) {
429 iLocalResult=RegisterComponent(*element);
430 if (iResult==0) iResult=iLocalResult;
431 fScheduleList.erase(element);
432 element=fScheduleList.begin();
437 int AliHLTComponentHandler::DeleteStandardComponents()
439 // see header file for class documentation
441 vector<AliHLTComponent*>::iterator element=fStandardList.begin();
442 while (element!=fStandardList.end()) {
443 DeregisterComponent((*element)->GetComponentID());
445 fStandardList.erase(element);
446 element=fStandardList.begin();