started the new framework module supporting PubSub and AliRoot
[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  *          Artur Szostak <artursz@iafrica.com>                           *
9  *          for The ALICE Off-line Project.                               *
10  *                                                                        *
11  * Permission to use, copy, modify and distribute this software and its   *
12  * documentation strictly for non-commercial purposes is hereby granted   *
13  * without fee, provided that the above copyright notice appears in all   *
14  * copies and that both the copyright notice and this permission notice   *
15  * appear in the supporting documentation. The authors make no claims     *
16  * about the suitability of this software for any purpose. It is          *
17  * provided "as is" without express or implied warranty.                  *
18  **************************************************************************/
19
20 ///////////////////////////////////////////////////////////////////////////////
21 //                                                                           //
22 // handler class for HLT analysis components                                 //
23 //                                                                           //
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #if __GNUC__== 3
27 using namespace std;
28 #endif
29
30 #include <errno.h>
31 #include <string.h>
32 #include <dlfcn.h>
33 #include "AliL3StandardIncludes.h"
34 #include "AliHLTComponentHandler.h"
35 #include "AliHLTComponent.h"
36 #include "AliHLTDataTypes.h"
37 #include "AliHLTSystem.h"
38
39 ClassImp(AliHLTComponentHandler)
40
41 AliHLTComponentHandler::AliHLTComponentHandler()
42 {
43 }
44
45
46 AliHLTComponentHandler::~AliHLTComponentHandler()
47 {
48   UnloadLibraries();
49 }
50
51 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
52 {
53   Int_t iResult=0;
54   if (pSample) {
55     if (FindComponent(pSample->GetComponentID())==NULL) {
56       iResult=InsertComponent(pSample);
57       if (iResult>=0) {
58         Logging(kHLTLogInfo, "BASE", "Component Handler", "component %s registered", pSample->GetComponentID());
59       }
60     } else {
61       // component already registered
62       Logging(kHLTLogInfo, "BASE", "Component Handler", "component %s already registered, skipped", pSample->GetComponentID());
63       iResult=-EEXIST;
64     }
65   } else {
66     iResult=-EINVAL;
67   }
68   return iResult;
69 }
70
71 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
72 {
73   return 0;
74 }
75
76 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
77 {
78   Int_t iResult=0;
79   if (pSample) {
80     fScheduleList.push_back(pSample);
81   } else {
82     iResult=-EINVAL;
83   }
84   return iResult;
85 }
86
87 int AliHLTComponentHandler::CreateComponent(const Char_t* componentID, void* environ_param, int argc, const char** argv, AliHLTComponent*& component )
88 {
89   int iResult=0;
90   if (componentID) {
91     AliHLTComponent* pSample=FindComponent(componentID);
92     if (pSample!=NULL) {
93       component=pSample->Spawn();
94       if (component) {
95         component->Init(&fEnvironment, environ_param, argc, argv);
96       }
97     }
98   } else {
99     iResult=-EINVAL;
100   }
101   return iResult;
102 }
103
104 Int_t AliHLTComponentHandler::FindComponentIndex(const Char_t* componentID)
105 {
106   Int_t iResult=0;
107   if (componentID) {
108     vector<AliHLTComponent*>::iterator element=fComponentList.begin();
109     while (element!=fComponentList.end() && iResult>=0) {
110       if (strcmp(componentID, (*element)->GetComponentID())==0) {
111         break;
112       }
113       element++;
114       iResult++;
115     }
116     if (element==fComponentList.end()) iResult=-ENOENT;
117   } else {
118     iResult=-EINVAL;
119   }
120   return iResult;
121 }
122
123 AliHLTComponent* AliHLTComponentHandler::FindComponent(const Char_t* componentID)
124 {
125   AliHLTComponent* pSample=NULL;
126   Int_t index=FindComponentIndex(componentID);
127   if (index>=0) {
128     pSample=(AliHLTComponent*)fComponentList.at(index);
129   }
130   return pSample;
131 }
132
133 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
134 {
135   Int_t iResult=0;
136   if (pSample!=NULL) {
137     fComponentList.push_back(pSample);
138   } else {
139     iResult=-EINVAL;
140   }
141   return iResult;
142 }
143
144 void AliHLTComponentHandler::List() {
145   vector<AliHLTComponent*>::iterator element=fComponentList.begin();
146   int index=0;
147   while (element!=fComponentList.end()) {
148     Logging(kHLTLogInfo, "BASE", "Component Handler", "%d. %s", index++, (*element++)->GetComponentID());
149   }
150 }
151
152 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) {
153   if (pEnv) {
154     memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
155   }
156 }
157
158 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
159 {
160   int iResult=0;
161   if (libraryPath) {
162     AliHLTComponent::SetGlobalComponentHandler(this);
163     AliHLTLibHandle hLib=dlopen(libraryPath, RTLD_NOW);
164     if (hLib) {
165       AliHLTComponent::UnsetGlobalComponentHandler();
166       Logging(kHLTLogDebug, "BASE", "Component Handler", "library %s loaded", libraryPath);
167       fLibraryList.push_back(hLib);
168       vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
169       int iSize=fScheduleList.size();
170       int iLocalResult=0;
171       while (iSize-- > 0) {
172         element=fScheduleList.begin();
173         iLocalResult=RegisterComponent(*element);
174         if (iResult==0) iResult=iLocalResult;
175         fScheduleList.erase(element);
176       }
177     } else {
178       Logging(kHLTLogError, "BASE", "Component Handler", "can not load library %s", libraryPath);
179       Logging(kHLTLogError, "BASE", "Component Handler", "dlopen error: %s", dlerror());
180       iResult=-ELIBACC;
181     }
182   } else {
183     iResult=-EINVAL;
184   }
185   return iResult;
186 }
187
188 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
189 {
190   int iResult=0;
191   return iResult;
192 }
193
194 int AliHLTComponentHandler::UnloadLibraries()
195 {
196   int iResult=0;
197   vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
198   while (element!=fLibraryList.end()) {
199     dlclose(*element);
200     element++;
201   }
202   return iResult;
203 }
204
205 int AliHLTComponentHandler::Logging(AliHLTComponent_LogSeverity severity, const char* origin, const char* keyword, const char* format, ... ) {
206   if (fEnvironment.fLoggingFunc) {
207     va_list args;
208     va_start(args, format);
209     return (*fEnvironment.fLoggingFunc)( fEnvironment.fParam, severity, origin, keyword, AliHLTSystem::BuildLogString(format, args));
210   }
211   return -ENOSYS;
212 }