]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponentHandler.cxx
- code version from TPC commissioning merged
[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         component->Init(&fEnvironment, pEnv, argc, argv);
116       } else {
117         HLTError("can not spawn component \"%s\"", componentID);
118         iResult=-ENOENT;
119       }
120     } else {
121       HLTWarning("can not find component \"%s\"", componentID);
122       iResult=-ENOENT;
123     }
124   } else {
125     iResult=-EINVAL;
126   }
127   return iResult;
128 }
129
130 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
131 {
132   Int_t iResult=0;
133   if (componentID) {
134     vector<AliHLTComponent*>::iterator element=fComponentList.begin();
135     while (element!=fComponentList.end() && iResult>=0) {
136       if (strcmp(componentID, (*element)->GetComponentID())==0) {
137         break;
138       }
139       element++;
140       iResult++;
141     }
142     if (element==fComponentList.end()) iResult=-ENOENT;
143   } else {
144     iResult=-EINVAL;
145   }
146   return iResult;
147 }
148
149 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
150 {
151   AliHLTComponent* pSample=NULL;
152   Int_t index=FindComponentIndex(componentID);
153   if (index>=0) {
154     pSample=(AliHLTComponent*)fComponentList.at(index);
155   }
156   return pSample;
157 }
158
159 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
160 {
161   Int_t iResult=0;
162   if (pSample!=NULL) {
163     fComponentList.push_back(pSample);
164   } else {
165     iResult=-EINVAL;
166   }
167   return iResult;
168 }
169
170 void AliHLTComponentHandler::List() {
171   vector<AliHLTComponent*>::iterator element=fComponentList.begin();
172   int index=0;
173   while (element!=fComponentList.end()) {
174     HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
175   }
176 }
177
178 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) {
179   if (pEnv) {
180     memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
181     AliHLTLogging::Init(fEnvironment.fLoggingFunc);
182   }
183 }
184
185 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
186 {
187   int iResult=0;
188   if (libraryPath) {
189     AliHLTComponent::SetGlobalComponentHandler(this);
190     AliHLTLibHandle hLib=dlopen(libraryPath, RTLD_NOW);
191     if (hLib) {
192       AliHLTComponent::UnsetGlobalComponentHandler();
193       HLTInfo("library %s loaded", libraryPath);
194       fLibraryList.push_back(hLib);
195       vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
196       int iSize=fScheduleList.size();
197       int iLocalResult=0;
198       while (iSize-- > 0) {
199         element=fScheduleList.begin();
200         iLocalResult=RegisterComponent(*element);
201         if (iResult==0) iResult=iLocalResult;
202         fScheduleList.erase(element);
203       }
204     } else {
205       HLTError("can not load library %s", libraryPath);
206       HLTError("dlopen error: %s", dlerror());
207       iResult=-ELIBACC;
208     }
209   } else {
210     iResult=-EINVAL;
211   }
212   return iResult;
213 }
214
215 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
216 {
217   int iResult=0;
218   return iResult;
219 }
220
221 int AliHLTComponentHandler::UnloadLibraries()
222 {
223   int iResult=0;
224   vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
225   while (element!=fLibraryList.end()) {
226     dlclose(*element);
227     element++;
228   }
229   return iResult;
230 }