]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponentHandler.cxx
- changes according to coding conventions and documentation
[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 /** ROOT macro for the implementation of ROOT specific class methods */
41 ClassImp(AliHLTComponentHandler)
42
43 AliHLTComponentHandler::AliHLTComponentHandler()
44   :
45   fComponentList(),
46   fScheduleList(),
47   fLibraryList(),
48   fEnvironment()
49 {
50   memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
51 }
52
53 AliHLTComponentHandler::~AliHLTComponentHandler()
54 {
55   UnloadLibraries();
56 }
57
58 int AliHLTComponentHandler::AnnounceVersion()
59 {
60   int iResult=0;
61 #ifdef PACKAGE_STRING
62   void HLTbaseCompileInfo( char*& date, char*& time);
63   char* date="";
64   char* time="";
65   HLTbaseCompileInfo(date, time);
66   if (!date) date="unknown";
67   if (!time) time="unknown";
68   HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
69 #else
70   HLTInfo("ALICE High Level Trigger (embedded AliRoot build)");
71 #endif
72   return iResult;
73 }
74
75 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
76 {
77   Int_t iResult=0;
78   if (pSample) {
79     if (FindComponent(pSample->GetComponentID())==NULL) {
80       iResult=InsertComponent(pSample);
81       if (iResult>=0) {
82         HLTInfo("component %s registered", pSample->GetComponentID());
83       }
84     } else {
85       // component already registered
86       HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
87       iResult=-EEXIST;
88     }
89   } else {
90     iResult=-EINVAL;
91   }
92   return iResult;
93 }
94
95 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
96 {
97   int iResult=0;
98   if (componentID) {
99   } else {
100     iResult=-EINVAL;
101   }
102   return iResult;
103 }
104
105 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
106 {
107   Int_t iResult=0;
108   if (pSample) {
109     fScheduleList.push_back(pSample);
110   } else {
111     iResult=-EINVAL;
112   }
113   return iResult;
114 }
115
116 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
117 {
118   int iResult=0;
119   if (componentID) {
120     AliHLTComponent* pSample=FindComponent(componentID);
121     if (pSample!=NULL) {
122       component=pSample->Spawn();
123       if (component) {
124         HLTDebug("component \"%s\" created (%p)", componentID, component);
125         if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
126           HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
127           delete component;
128           component=NULL;
129         }
130       } else {
131         HLTError("can not spawn component \"%s\"", componentID);
132         iResult=-ENOENT;
133       }
134     } else {
135       HLTWarning("can not find component \"%s\"", componentID);
136       iResult=-ENOENT;
137     }
138   } else {
139     iResult=-EINVAL;
140   }
141   return iResult;
142 }
143
144 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
145 {
146   Int_t iResult=0;
147   if (componentID) {
148     vector<AliHLTComponent*>::iterator element=fComponentList.begin();
149     while (element!=fComponentList.end() && iResult>=0) {
150       if (strcmp(componentID, (*element)->GetComponentID())==0) {
151         break;
152       }
153       element++;
154       iResult++;
155     }
156     if (element==fComponentList.end()) iResult=-ENOENT;
157   } else {
158     iResult=-EINVAL;
159   }
160   return iResult;
161 }
162
163 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
164 {
165   AliHLTComponent* pSample=NULL;
166   Int_t index=FindComponentIndex(componentID);
167   if (index>=0) {
168     pSample=(AliHLTComponent*)fComponentList.at(index);
169   }
170   return pSample;
171 }
172
173 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
174 {
175   Int_t iResult=0;
176   if (pSample!=NULL) {
177     fComponentList.push_back(pSample);
178   } else {
179     iResult=-EINVAL;
180   }
181   return iResult;
182 }
183
184 void AliHLTComponentHandler::List() {
185   vector<AliHLTComponent*>::iterator element=fComponentList.begin();
186   int index=0;
187   while (element!=fComponentList.end()) {
188     HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
189   }
190 }
191
192 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) {
193   if (pEnv) {
194     memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
195     AliHLTLogging::Init(fEnvironment.fLoggingFunc);
196   }
197 }
198
199 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
200 {
201   int iResult=0;
202   if (libraryPath) {
203     AliHLTComponent::SetGlobalComponentHandler(this);
204     AliHLTLibHandle hLib=NULL;
205 #ifdef HAVE_DLFCN_H
206     // use interface to the dynamic linking loader
207     hLib=dlopen(libraryPath, RTLD_NOW);
208 #else
209     // use ROOT dynamic loader
210     if (gSystem->Load(libraryPath)==0) {
211       // create TString object to store library path and use pointer as handle 
212       hLib=reinterpret_cast<AliHLTLibHandle>(new TString(libraryPath));
213     }
214 #endif //HAVE_DLFCN_H
215     if (hLib) {
216       HLTInfo("library %s loaded", libraryPath);
217       fLibraryList.push_back(hLib);
218       vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
219       int iSize=fScheduleList.size();
220       int iLocalResult=0;
221       while (iSize-- > 0) {
222         element=fScheduleList.begin();
223         iLocalResult=RegisterComponent(*element);
224         if (iResult==0) iResult=iLocalResult;
225         fScheduleList.erase(element);
226       }
227     } else {
228       HLTError("can not load library %s", libraryPath);
229 #ifdef HAVE_DLFCN_H
230       HLTError("dlopen error: %s", dlerror());
231 #endif //HAVE_DLFCN_H
232 #ifdef __APPLE__
233       iResult=-EFTYPE;
234 #else
235       iResult=-ELIBACC;
236 #endif
237     }
238     AliHLTComponent::UnsetGlobalComponentHandler();
239   } else {
240     iResult=-EINVAL;
241   }
242   return iResult;
243 }
244
245 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
246 {
247   int iResult=0;
248   if (libraryPath) {
249   } else {
250     iResult=-EINVAL;
251   }
252   return iResult;
253 }
254
255 int AliHLTComponentHandler::UnloadLibraries()
256 {
257   int iResult=0;
258   vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
259   while (element!=fLibraryList.end()) {
260 #ifdef HAVE_DLFCN_H
261     dlclose(*element);
262 #else
263     TString* libraryPath=reinterpret_cast<TString*>(*element);
264     gSystem->Unload(libraryPath->Data());
265     delete libraryPath;
266 #endif //HAVE_DLFCN_H
267     element++;
268   }
269   return iResult;
270 }