]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTComponentHandler.cxx
HLT base
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponentHandler.cxx
CommitLineData
f23a6e1a 1// $Id$
2
3/**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
f23a6e1a 8 * for The ALICE Off-line Project. *
9 * *
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 **************************************************************************/
18
b22e91eb 19/** @file AliHLTComponentHandler.cxx
20 @author Matthias Richter, Timm Steinbeck
21 @date
22 @brief Implementation of HLT component handler. */
f23a6e1a 23
0c0c9d99 24#if __GNUC__>= 3
f23a6e1a 25using namespace std;
26#endif
53feaef5 27//#undef HAVE_DLFCN_H
28#ifdef HAVE_DLFCN_H
f23a6e1a 29#include <dlfcn.h>
53feaef5 30#else
31//#include <Riostream.h>
32#include <TSystem.h>
33#endif //HAVE_DLFCN_H
85869391 34#include "AliHLTStdIncludes.h"
f23a6e1a 35#include "AliHLTComponentHandler.h"
36#include "AliHLTComponent.h"
37#include "AliHLTDataTypes.h"
38#include "AliHLTSystem.h"
39
9ce4bf4a 40// the standard components
242bb794 41// #include "AliHLTFilePublisher.h"
a742f6f8 42// #include "AliHLTFileWriter.h"
242bb794 43// #include "AliHLTRootFilePublisherComponent.h"
44// #include "AliHLTRootFileWriterComponent.h"
9ce4bf4a 45
b22e91eb 46/** ROOT macro for the implementation of ROOT specific class methods */
f23a6e1a 47ClassImp(AliHLTComponentHandler)
48
49AliHLTComponentHandler::AliHLTComponentHandler()
85869391 50 :
51 fComponentList(),
52 fScheduleList(),
53 fLibraryList(),
9ce4bf4a 54 fEnvironment(),
55 fStandardList()
f23a6e1a 56{
70ed7d01 57 // see header file for class documentation
58 // or
59 // refer to README to build package
60 // or
61 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85869391 62 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
9ce4bf4a 63 AddStandardComponents();
f23a6e1a 64}
65
3cde846d 66AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
67 :
68 fComponentList(),
69 fScheduleList(),
70 fLibraryList(),
71 fEnvironment(),
72 fStandardList()
73{
70ed7d01 74 // see header file for class documentation
3cde846d 75 if (pEnv) {
76 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
fa274626 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
81 // for redirection
82 AliHLTLogging::Init(pEnv->fLoggingFunc);
83 }
3cde846d 84 } else
85 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
86 AddStandardComponents();
87}
88
f23a6e1a 89AliHLTComponentHandler::~AliHLTComponentHandler()
90{
70ed7d01 91 // see header file for class documentation
9ce4bf4a 92 DeleteStandardComponents();
a742f6f8 93 UnloadLibraries();
f23a6e1a 94}
95
fa760045 96int AliHLTComponentHandler::AnnounceVersion()
97{
70ed7d01 98 // see header file for class documentation
fa760045 99 int iResult=0;
100#ifdef PACKAGE_STRING
101 void HLTbaseCompileInfo( char*& date, char*& time);
102 char* date="";
103 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);
108#else
a742f6f8 109 HLTInfo("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
fa760045 110#endif
111 return iResult;
112}
113
f23a6e1a 114Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
115{
70ed7d01 116 // see header file for class documentation
f23a6e1a 117 Int_t iResult=0;
118 if (pSample) {
119 if (FindComponent(pSample->GetComponentID())==NULL) {
120 iResult=InsertComponent(pSample);
121 if (iResult>=0) {
85465857 122 HLTInfo("component %s registered", pSample->GetComponentID());
f23a6e1a 123 }
124 } else {
125 // component already registered
fa760045 126 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
f23a6e1a 127 iResult=-EEXIST;
128 }
129 } else {
130 iResult=-EINVAL;
131 }
132 return iResult;
133}
134
135int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
136{
70ed7d01 137 // see header file for class documentation
53feaef5 138 int iResult=0;
139 if (componentID) {
140 } else {
141 iResult=-EINVAL;
142 }
143 return iResult;
f23a6e1a 144}
145
146Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
147{
70ed7d01 148 // see header file for class documentation
f23a6e1a 149 Int_t iResult=0;
150 if (pSample) {
151 fScheduleList.push_back(pSample);
152 } else {
153 iResult=-EINVAL;
154 }
155 return iResult;
156}
157
2d7ff710 158int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
f23a6e1a 159{
70ed7d01 160 // see header file for class documentation
f23a6e1a 161 int iResult=0;
162 if (componentID) {
163 AliHLTComponent* pSample=FindComponent(componentID);
164 if (pSample!=NULL) {
165 component=pSample->Spawn();
166 if (component) {
85465857 167 HLTDebug("component \"%s\" created (%p)", componentID, component);
2d7ff710 168 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
84645eb0 169 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
170 delete component;
171 component=NULL;
172 }
5ec8e281 173 } else {
85465857 174 HLTError("can not spawn component \"%s\"", componentID);
5ec8e281 175 iResult=-ENOENT;
f23a6e1a 176 }
5ec8e281 177 } else {
85465857 178 HLTWarning("can not find component \"%s\"", componentID);
5ec8e281 179 iResult=-ENOENT;
f23a6e1a 180 }
181 } else {
182 iResult=-EINVAL;
183 }
184 return iResult;
185}
186
db16520a 187Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
f23a6e1a 188{
70ed7d01 189 // see header file for class documentation
f23a6e1a 190 Int_t iResult=0;
191 if (componentID) {
192 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
193 while (element!=fComponentList.end() && iResult>=0) {
194 if (strcmp(componentID, (*element)->GetComponentID())==0) {
195 break;
196 }
197 element++;
198 iResult++;
199 }
200 if (element==fComponentList.end()) iResult=-ENOENT;
201 } else {
202 iResult=-EINVAL;
203 }
204 return iResult;
205}
206
db16520a 207AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
f23a6e1a 208{
70ed7d01 209 // see header file for class documentation
f23a6e1a 210 AliHLTComponent* pSample=NULL;
211 Int_t index=FindComponentIndex(componentID);
212 if (index>=0) {
213 pSample=(AliHLTComponent*)fComponentList.at(index);
214 }
215 return pSample;
216}
217
218Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
219{
70ed7d01 220 // see header file for class documentation
f23a6e1a 221 Int_t iResult=0;
222 if (pSample!=NULL) {
223 fComponentList.push_back(pSample);
224 } else {
225 iResult=-EINVAL;
226 }
227 return iResult;
228}
229
70ed7d01 230void AliHLTComponentHandler::List()
231{
232 // see header file for class documentation
f23a6e1a 233 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
234 int index=0;
235 while (element!=fComponentList.end()) {
85465857 236 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
f23a6e1a 237 }
238}
239
70ed7d01 240void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
241{
242 // see header file for class documentation
f23a6e1a 243 if (pEnv) {
244 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
fa274626 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
249 // for redirection
250 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
251 }
f23a6e1a 252 }
253}
254
255int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
256{
70ed7d01 257 // see header file for class documentation
f23a6e1a 258 int iResult=0;
259 if (libraryPath) {
260 AliHLTComponent::SetGlobalComponentHandler(this);
c215072c 261 AliHLTLibHandle hLib;
85f0cede 262 const char* loadtype="";
53feaef5 263#ifdef HAVE_DLFCN_H
264 // use interface to the dynamic linking loader
c215072c 265 hLib.handle=dlopen(libraryPath, RTLD_NOW);
85f0cede 266 loadtype="dlopen";
53feaef5 267#else
268 // use ROOT dynamic loader
c215072c 269 // check if the library was already loaded, as Load returns
270 // 'failure' if the library was already loaded
85f0cede 271 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
272 if (pLib) {
273 int* pRootHandle=reinterpret_cast<int*>(pLib->handle);
c215072c 274 (*pRootHandle)++;
275 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
276 hLib.handle=pRootHandle;
c215072c 277 }
278
279 if (hLib.handle==NULL && gSystem->Load(libraryPath)==0) {
280 int* pRootHandle=new int;
281 if (pRootHandle) *pRootHandle=1;
282 hLib.handle=pRootHandle;
85f0cede 283 //HLTDebug("library %s loaded via gSystem", libraryPath);
53feaef5 284 }
85f0cede 285 loadtype="gSystem";
53feaef5 286#endif //HAVE_DLFCN_H
c215072c 287 if (hLib.handle!=NULL) {
288 // create TString object to store library path and use pointer as handle
289 hLib.name=new TString(libraryPath);
85f0cede 290 HLTInfo("library %s loaded (%s)", libraryPath, loadtype);
c215072c 291 fLibraryList.insert(fLibraryList.begin(), hLib);
9ce4bf4a 292 iResult=RegisterScheduledComponents();
f23a6e1a 293 } else {
85465857 294 HLTError("can not load library %s", libraryPath);
53feaef5 295#ifdef HAVE_DLFCN_H
85465857 296 HLTError("dlopen error: %s", dlerror());
53feaef5 297#endif //HAVE_DLFCN_H
0fe88043 298#ifdef __APPLE__
299 iResult=-EFTYPE;
300#else
f23a6e1a 301 iResult=-ELIBACC;
0fe88043 302#endif
f23a6e1a 303 }
2bbbadd1 304 AliHLTComponent::UnsetGlobalComponentHandler();
f23a6e1a 305 } else {
306 iResult=-EINVAL;
307 }
308 return iResult;
309}
310
311int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
312{
70ed7d01 313 // see header file for class documentation
f23a6e1a 314 int iResult=0;
53feaef5 315 if (libraryPath) {
a742f6f8 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);
322 break;
323 }
324 element++;
325 }
53feaef5 326 } else {
327 iResult=-EINVAL;
328 }
f23a6e1a 329 return iResult;
330}
331
a742f6f8 332int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
f23a6e1a 333{
70ed7d01 334 // see header file for class documentation
f23a6e1a 335 int iResult=0;
a742f6f8 336 fgAliLoggingFunc=NULL;
337 TString* pName=reinterpret_cast<TString*>(handle.name);
53feaef5 338#ifdef HAVE_DLFCN_H
a742f6f8 339 dlclose(handle.handle);
53feaef5 340#else
a742f6f8 341 int* pCount=reinterpret_cast<int*>(handle.handle);
342 if (--(*pCount)==0) {
c215072c 343 if (pName) {
a742f6f8 344 gSystem->Unload(pName->Data());
345 }
346 else {
347 HLTError("missing library name, can not unload");
c215072c 348 }
a742f6f8 349 delete pCount;
350 }
351#endif //HAVE_DLFCN_H
352 handle.name=NULL;
353 handle.handle=NULL;
354 if (pName) {
355 HLTDebug("unload library %s", pName->Data());
356 delete pName;
357 } else {
358 HLTWarning("missing name for unloaded library");
359 }
360 pName=NULL;
361 return iResult;
362}
363
364int AliHLTComponentHandler::UnloadLibraries()
365{
366 // see header file for class documentation
367 int iResult=0;
368 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
369 while (element!=fLibraryList.end()) {
370 UnloadLibrary(*element);
c215072c 371 fLibraryList.erase(element);
372 element=fLibraryList.begin();
f23a6e1a 373 }
374 return iResult;
375}
9ce4bf4a 376
85f0cede 377void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
378{
379 // see header file for class documentation
380 AliHLTLibHandle* hLib=FindLibrary(library);
381 if (hLib==NULL) return NULL;
382 void* pFunc=NULL;
383#ifdef HAVE_DLFCN_H
384 pFunc=dlsym(hLib->handle, symbol);
385#else
386 TString* name=reinterpret_cast<TString*>(hLib->name);
387 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
388#endif
389 return pFunc;
390}
391
392AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
393{
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) {
400 hLib=&(*element);
401 break;
402 }
403 element++;
404 }
405 return hLib;
406}
407
9ce4bf4a 408int AliHLTComponentHandler::AddStandardComponents()
409{
70ed7d01 410 // see header file for class documentation
9ce4bf4a 411 int iResult=0;
412 AliHLTComponent::SetGlobalComponentHandler(this);
242bb794 413// fStandardList.push_back(new AliHLTFilePublisher);
a742f6f8 414// fStandardList.push_back(new AliHLTFileWriter);
242bb794 415// fStandardList.push_back(new AliHLTRootFilePublisherComponent);
416// fStandardList.push_back(new AliHLTRootFileWriterComponent);
9ce4bf4a 417 AliHLTComponent::UnsetGlobalComponentHandler();
418 iResult=RegisterScheduledComponents();
419 return iResult;
420}
421
422int AliHLTComponentHandler::RegisterScheduledComponents()
423{
70ed7d01 424 // see header file for class documentation
9ce4bf4a 425 int iResult=0;
426 vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
427 int iLocalResult=0;
428 while (element!=fScheduleList.end()) {
429 iLocalResult=RegisterComponent(*element);
430 if (iResult==0) iResult=iLocalResult;
431 fScheduleList.erase(element);
432 element=fScheduleList.begin();
433 }
434 return iResult;
435}
436
437int AliHLTComponentHandler::DeleteStandardComponents()
438{
70ed7d01 439 // see header file for class documentation
9ce4bf4a 440 int iResult=0;
441 vector<AliHLTComponent*>::iterator element=fStandardList.begin();
442 while (element!=fStandardList.end()) {
443 DeregisterComponent((*element)->GetComponentID());
444 delete(*element);
445 fStandardList.erase(element);
446 element=fStandardList.begin();
447 }
448 return iResult;
449}