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