- added version announcement to base library
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponentHandler.cxx
CommitLineData
f23a6e1a 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> *
f23a6e1a 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
b22e91eb 19/** @file AliHLTComponentHandler.cxx
20 @author Matthias Richter, Timm Steinbeck
21 @date
22 @brief Implementation of HLT component handler. */
f23a6e1a 23
0c0c9d99 24#if __GNUC__>= 3
f23a6e1a 25using namespace std;
26#endif
27
f23a6e1a 28#include <dlfcn.h>
85869391 29#include "AliHLTStdIncludes.h"
f23a6e1a 30#include "AliHLTComponentHandler.h"
31#include "AliHLTComponent.h"
32#include "AliHLTDataTypes.h"
33#include "AliHLTSystem.h"
34
b22e91eb 35/** ROOT macro for the implementation of ROOT specific class methods */
f23a6e1a 36ClassImp(AliHLTComponentHandler)
37
38AliHLTComponentHandler::AliHLTComponentHandler()
85869391 39 :
40 fComponentList(),
41 fScheduleList(),
42 fLibraryList(),
43 fEnvironment()
f23a6e1a 44{
85869391 45 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
f23a6e1a 46}
47
f23a6e1a 48AliHLTComponentHandler::~AliHLTComponentHandler()
49{
50 UnloadLibraries();
51}
52
fa760045 53int 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
f23a6e1a 70Int_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) {
85465857 77 HLTInfo("component %s registered", pSample->GetComponentID());
f23a6e1a 78 }
79 } else {
80 // component already registered
fa760045 81 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
f23a6e1a 82 iResult=-EEXIST;
83 }
84 } else {
85 iResult=-EINVAL;
86 }
87 return iResult;
88}
89
90int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
91{
92 return 0;
93}
94
95Int_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
fa760045 106int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnv, int argc, const char** argv, AliHLTComponent*& component )
f23a6e1a 107{
108 int iResult=0;
109 if (componentID) {
110 AliHLTComponent* pSample=FindComponent(componentID);
111 if (pSample!=NULL) {
112 component=pSample->Spawn();
113 if (component) {
85465857 114 HLTDebug("component \"%s\" created (%p)", componentID, component);
fa760045 115 component->Init(&fEnvironment, pEnv, argc, argv);
5ec8e281 116 } else {
85465857 117 HLTError("can not spawn component \"%s\"", componentID);
5ec8e281 118 iResult=-ENOENT;
f23a6e1a 119 }
5ec8e281 120 } else {
85465857 121 HLTWarning("can not find component \"%s\"", componentID);
5ec8e281 122 iResult=-ENOENT;
f23a6e1a 123 }
124 } else {
125 iResult=-EINVAL;
126 }
127 return iResult;
128}
129
130Int_t AliHLTComponentHandler::FindComponentIndex(const Char_t* 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
149AliHLTComponent* AliHLTComponentHandler::FindComponent(const Char_t* 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
159Int_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
170void AliHLTComponentHandler::List() {
171 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
172 int index=0;
173 while (element!=fComponentList.end()) {
85465857 174 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
f23a6e1a 175 }
176}
177
178void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) {
179 if (pEnv) {
180 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
bb16cc41 181 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
f23a6e1a 182 }
183}
184
185int 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();
fa760045 193 HLTInfo("library %s loaded", libraryPath);
f23a6e1a 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 {
85465857 205 HLTError("can not load library %s", libraryPath);
206 HLTError("dlopen error: %s", dlerror());
f23a6e1a 207 iResult=-ELIBACC;
208 }
209 } else {
210 iResult=-EINVAL;
211 }
212 return iResult;
213}
214
215int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
216{
217 int iResult=0;
218 return iResult;
219}
220
221int 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}