]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponentHandler.cxx
high-level component interface added
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponentHandler.cxx
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>                   *
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
19 /** @file   AliHLTComponentHandler.cxx
20     @author Matthias Richter, Timm Steinbeck
21     @date   
22     @brief  Implementation of HLT component handler. */
23
24 #if __GNUC__>= 3
25 using namespace std;
26 #endif
27 //#undef HAVE_DLFCN_H
28 #ifdef HAVE_DLFCN_H
29 #include <dlfcn.h>
30 #else
31 //#include <Riostream.h>
32 #include <TSystem.h>
33 #endif //HAVE_DLFCN_H
34 #include "AliHLTStdIncludes.h"
35 #include "AliHLTComponentHandler.h"
36 #include "AliHLTComponent.h"
37 #include "AliHLTDataTypes.h"
38 #include "AliHLTSystem.h"
39
40 // the standard components
41 #include "AliHLTFilePublisher.h"
42 #include "AliHLTFileWriter.h"
43 #include "AliHLTRootFilePublisherComponent.h"
44 #include "AliHLTRootFileWriterComponent.h"
45
46 /** ROOT macro for the implementation of ROOT specific class methods */
47 ClassImp(AliHLTComponentHandler)
48
49 AliHLTComponentHandler::AliHLTComponentHandler()
50   :
51   fComponentList(),
52   fScheduleList(),
53   fLibraryList(),
54   fEnvironment(),
55   fStandardList()
56 {
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
62   memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
63   AddStandardComponents();
64 }
65
66 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
67   :
68   fComponentList(),
69   fScheduleList(),
70   fLibraryList(),
71   fEnvironment(),
72   fStandardList()
73 {
74   // see header file for class documentation
75   if (pEnv) {
76     memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
77     AliHLTLogging::Init(pEnv->fLoggingFunc);
78   }  else
79     memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
80   AddStandardComponents();
81 }
82
83 AliHLTComponentHandler::~AliHLTComponentHandler()
84 {
85   // see header file for class documentation
86   UnloadLibraries();
87   DeleteStandardComponents();
88 }
89
90 int AliHLTComponentHandler::AnnounceVersion()
91 {
92   // see header file for class documentation
93   int iResult=0;
94 #ifdef PACKAGE_STRING
95   void HLTbaseCompileInfo( char*& date, char*& time);
96   char* date="";
97   char* time="";
98   HLTbaseCompileInfo(date, time);
99   if (!date) date="unknown";
100   if (!time) time="unknown";
101   HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
102 #else
103   HLTInfo("ALICE High Level Trigger (embedded AliRoot build)");
104 #endif
105   return iResult;
106 }
107
108 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
109 {
110   // see header file for class documentation
111   Int_t iResult=0;
112   if (pSample) {
113     if (FindComponent(pSample->GetComponentID())==NULL) {
114       iResult=InsertComponent(pSample);
115       if (iResult>=0) {
116         HLTInfo("component %s registered", pSample->GetComponentID());
117       }
118     } else {
119       // component already registered
120       HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
121       iResult=-EEXIST;
122     }
123   } else {
124     iResult=-EINVAL;
125   }
126   return iResult;
127 }
128
129 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
130 {
131   // see header file for class documentation
132   int iResult=0;
133   if (componentID) {
134   } else {
135     iResult=-EINVAL;
136   }
137   return iResult;
138 }
139
140 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
141 {
142   // see header file for class documentation
143   Int_t iResult=0;
144   if (pSample) {
145     fScheduleList.push_back(pSample);
146   } else {
147     iResult=-EINVAL;
148   }
149   return iResult;
150 }
151
152 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
153 {
154   // see header file for class documentation
155   int iResult=0;
156   if (componentID) {
157     AliHLTComponent* pSample=FindComponent(componentID);
158     if (pSample!=NULL) {
159       component=pSample->Spawn();
160       if (component) {
161         HLTDebug("component \"%s\" created (%p)", componentID, component);
162         if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
163           HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
164           delete component;
165           component=NULL;
166         }
167       } else {
168         HLTError("can not spawn component \"%s\"", componentID);
169         iResult=-ENOENT;
170       }
171     } else {
172       HLTWarning("can not find component \"%s\"", componentID);
173       iResult=-ENOENT;
174     }
175   } else {
176     iResult=-EINVAL;
177   }
178   return iResult;
179 }
180
181 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
182 {
183   // see header file for class documentation
184   Int_t iResult=0;
185   if (componentID) {
186     vector<AliHLTComponent*>::iterator element=fComponentList.begin();
187     while (element!=fComponentList.end() && iResult>=0) {
188       if (strcmp(componentID, (*element)->GetComponentID())==0) {
189         break;
190       }
191       element++;
192       iResult++;
193     }
194     if (element==fComponentList.end()) iResult=-ENOENT;
195   } else {
196     iResult=-EINVAL;
197   }
198   return iResult;
199 }
200
201 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
202 {
203   // see header file for class documentation
204   AliHLTComponent* pSample=NULL;
205   Int_t index=FindComponentIndex(componentID);
206   if (index>=0) {
207     pSample=(AliHLTComponent*)fComponentList.at(index);
208   }
209   return pSample;
210 }
211
212 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
213 {
214   // see header file for class documentation
215   Int_t iResult=0;
216   if (pSample!=NULL) {
217     fComponentList.push_back(pSample);
218   } else {
219     iResult=-EINVAL;
220   }
221   return iResult;
222 }
223
224 void AliHLTComponentHandler::List() 
225 {
226   // see header file for class documentation
227   vector<AliHLTComponent*>::iterator element=fComponentList.begin();
228   int index=0;
229   while (element!=fComponentList.end()) {
230     HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
231   }
232 }
233
234 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) 
235 {
236   // see header file for class documentation
237   if (pEnv) {
238     memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
239     AliHLTLogging::Init(fEnvironment.fLoggingFunc);
240   }
241 }
242
243 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
244 {
245   // see header file for class documentation
246   int iResult=0;
247   if (libraryPath) {
248     AliHLTComponent::SetGlobalComponentHandler(this);
249     AliHLTLibHandle hLib=NULL;
250 #ifdef HAVE_DLFCN_H
251     // use interface to the dynamic linking loader
252     hLib=dlopen(libraryPath, RTLD_NOW);
253 #else
254     // use ROOT dynamic loader
255     if (gSystem->Load(libraryPath)==0) {
256       // create TString object to store library path and use pointer as handle 
257       hLib=reinterpret_cast<AliHLTLibHandle>(new TString(libraryPath));
258     }
259 #endif //HAVE_DLFCN_H
260     if (hLib) {
261       HLTInfo("library %s loaded", libraryPath);
262       fLibraryList.push_back(hLib);
263       iResult=RegisterScheduledComponents();
264     } else {
265       HLTError("can not load library %s", libraryPath);
266 #ifdef HAVE_DLFCN_H
267       HLTError("dlopen error: %s", dlerror());
268 #endif //HAVE_DLFCN_H
269 #ifdef __APPLE__
270       iResult=-EFTYPE;
271 #else
272       iResult=-ELIBACC;
273 #endif
274     }
275     AliHLTComponent::UnsetGlobalComponentHandler();
276   } else {
277     iResult=-EINVAL;
278   }
279   return iResult;
280 }
281
282 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
283 {
284   // see header file for class documentation
285   int iResult=0;
286   if (libraryPath) {
287   } else {
288     iResult=-EINVAL;
289   }
290   return iResult;
291 }
292
293 int AliHLTComponentHandler::UnloadLibraries()
294 {
295   // see header file for class documentation
296   int iResult=0;
297   vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
298   while (element!=fLibraryList.end()) {
299 #ifdef HAVE_DLFCN_H
300     dlclose(*element);
301 #else
302     TString* libraryPath=reinterpret_cast<TString*>(*element);
303     gSystem->Unload(libraryPath->Data());
304     delete libraryPath;
305 #endif //HAVE_DLFCN_H
306     element++;
307   }
308   return iResult;
309 }
310
311 int AliHLTComponentHandler::AddStandardComponents()
312 {
313   // see header file for class documentation
314   int iResult=0;
315   AliHLTComponent::SetGlobalComponentHandler(this);
316   fStandardList.push_back(new AliHLTFilePublisher);
317   fStandardList.push_back(new AliHLTFileWriter);
318   fStandardList.push_back(new AliHLTRootFilePublisherComponent);
319   fStandardList.push_back(new AliHLTRootFileWriterComponent);
320   AliHLTComponent::UnsetGlobalComponentHandler();
321   iResult=RegisterScheduledComponents();
322   return iResult;
323 }
324
325 int AliHLTComponentHandler::RegisterScheduledComponents()
326 {
327   // see header file for class documentation
328   int iResult=0;
329   vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
330   int iLocalResult=0;
331   while (element!=fScheduleList.end()) {
332     iLocalResult=RegisterComponent(*element);
333     if (iResult==0) iResult=iLocalResult;
334     fScheduleList.erase(element);
335     element=fScheduleList.begin();
336   }
337   return iResult;
338 }
339
340 int AliHLTComponentHandler::DeleteStandardComponents()
341 {
342   // see header file for class documentation
343   int iResult=0;
344   vector<AliHLTComponent*>::iterator element=fStandardList.begin();
345   while (element!=fStandardList.end()) {
346     DeregisterComponent((*element)->GetComponentID());
347     delete(*element);
348     fStandardList.erase(element);
349     element=fStandardList.begin();
350   }
351   return iResult;
352 }