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