]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTConfiguration.cxx
Major update required to handle old and new AliHLTEventDDL structures within HLT...
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTConfiguration.cxx
CommitLineData
3495cce2 1// $Id$
2
3/**************************************************************************
9be2600f 4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
3495cce2 6 * *
9be2600f 7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
3495cce2 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
030da833 19// @file AliHLTConfiguration.cxx
20// @author Matthias Richter
21// @date 2007
22// @brief HLT configuration description for a single component.
23// @note The class is used in Offline (AliRoot) context
3495cce2 24
3a7c0444 25// see header file for class documentation
26// or
27// refer to README to build package
28// or
29// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
0c0c9d99 31#if __GNUC__>= 3
3495cce2 32using namespace std;
33#endif
34
a5854ddd 35#include <cerrno>
3495cce2 36#include "AliHLTConfiguration.h"
c38ba6f9 37#include "AliHLTConfigurationHandler.h"
38#include "AliHLTTask.h"
3495cce2 39#include "AliHLTComponent.h"
40#include "AliHLTComponentHandler.h"
41#include <iostream>
a5854ddd 42#include <string>
70ed7d01 43#include "TList.h"
3495cce2 44
b22e91eb 45/** ROOT macro for the implementation of ROOT specific class methods */
3495cce2 46ClassImp(AliHLTConfiguration)
47
48AliHLTConfiguration::AliHLTConfiguration()
85869391 49 :
52c1c164 50 fID(""),
51 fComponent(""),
52 fStringSources(""),
85869391 53 fNofSources(-1),
53feaef5 54 fListSources(),
55 fListSrcElement(),
52c1c164 56 fArguments(""),
85869391 57 fArgc(-1),
032c5e5e 58 fArgv(NULL),
59 fBufferSize(-1)
3495cce2 60{
3a7c0444 61 // see header file for class documentation
62 // or
63 // refer to README to build package
64 // or
65 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
66
3495cce2 67 fListSrcElement=fListSources.begin();
68}
69
032c5e5e 70AliHLTConfiguration::AliHLTConfiguration(const char* id, const char* component, const char* sources,
71 const char* arguments, const char* bufsize)
85869391 72 :
73 fID(id),
74 fComponent(component),
75 fStringSources(sources),
76 fNofSources(-1),
53feaef5 77 fListSources(),
78 fListSrcElement(),
85869391 79 fArguments(arguments),
80 fArgc(-1),
032c5e5e 81 fArgv(NULL),
82 fBufferSize(-1)
3495cce2 83{
70ed7d01 84 // see header file for function documentation
032c5e5e 85 if (bufsize) fBufferSize=ConvertSizeString(bufsize);
85869391 86 fListSrcElement=fListSources.begin();
3495cce2 87 if (id && component) {
70ed7d01 88 if (fgConfigurationHandler) {
89 fgConfigurationHandler->RegisterConfiguration(this);
85465857 90 } else {
91 HLTError("no configuration handler set, abort registration");
92 }
3495cce2 93 }
94}
95
fc455fba 96AliHLTConfiguration::AliHLTConfiguration(const AliHLTConfiguration& src)
85869391 97 :
53feaef5 98 TObject(),
99 AliHLTLogging(),
fc455fba 100 fID(src.fID),
101 fComponent(src.fComponent),
102 fStringSources(src.fStringSources),
85869391 103 fNofSources(-1),
53feaef5 104 fListSources(),
105 fListSrcElement(),
fc455fba 106 fArguments(src.fArguments),
85869391 107 fArgc(-1),
032c5e5e 108 fArgv(NULL),
109 fBufferSize(src.fBufferSize)
85869391 110{
70ed7d01 111 // see header file for function documentation
85869391 112 fListSrcElement=fListSources.begin();
85869391 113}
114
fc455fba 115AliHLTConfiguration& AliHLTConfiguration::operator=(const AliHLTConfiguration& src)
85869391 116{
70ed7d01 117 // see header file for function documentation
fc455fba 118 fID=src.fID;
119 fComponent=src.fComponent;
120 fStringSources=src.fStringSources;
121 fNofSources=-1;
122 fArguments=src.fArguments;
123 fArgc=-1;
124 fArgv=NULL;
032c5e5e 125 fBufferSize=src.fBufferSize;
85869391 126 return *this;
127}
128
3495cce2 129AliHLTConfiguration::~AliHLTConfiguration()
130{
70ed7d01 131 // see header file for function documentation
132 if (fgConfigurationHandler) {
52c1c164 133 if (fgConfigurationHandler->FindConfiguration(fID.Data())!=NULL) {
df1e5419 134 // remove the configuration from the handler if it exists
135 // but DO NOT remove the clone configuration
136 fgConfigurationHandler->RemoveConfiguration(this);
85465857 137 }
3495cce2 138 }
139 if (fArgv != NULL) {
140 if (fArgc>0) {
141 for (int i=0; i<fArgc; i++) {
142 delete[] fArgv[i];
143 }
144 }
145 delete[] fArgv;
146 fArgv=NULL;
147 }
a742f6f8 148
149 vector<AliHLTConfiguration*>::iterator element=fListSources.begin();
150 while (element!=fListSources.end()) {
151 fListSources.erase(element);
152 element=fListSources.begin();
153 }
3495cce2 154}
155
b22e91eb 156/* the global configuration handler which is used to automatically register the configuration
157 */
70ed7d01 158AliHLTConfigurationHandler* AliHLTConfiguration::fgConfigurationHandler=NULL;
b22e91eb 159
85465857 160int AliHLTConfiguration::GlobalInit(AliHLTConfigurationHandler* pHandler)
161{
70ed7d01 162 // see header file for function documentation
85465857 163 int iResult=0;
95654878 164 if (fgConfigurationHandler!=NULL && fgConfigurationHandler!=pHandler) {
fc455fba 165 fgConfigurationHandler->Logging(kHLTLogWarning, "AliHLTConfiguration::GlobalInit", HLT_DEFAULT_LOG_KEYWORD, "configuration handler already initialized, overriding object %p with %p", fgConfigurationHandler, pHandler);
85465857 166 }
70ed7d01 167 fgConfigurationHandler=pHandler;
85465857 168 return iResult;
169}
170
fc455fba 171int AliHLTConfiguration::GlobalDeinit(AliHLTConfigurationHandler* pHandler)
85465857 172{
70ed7d01 173 // see header file for function documentation
85465857 174 int iResult=0;
20101b23 175 if (fgConfigurationHandler!=NULL && fgConfigurationHandler!=pHandler) {
fc455fba 176 fgConfigurationHandler->Logging(kHLTLogWarning, "AliHLTConfiguration::GlobalDeinit", HLT_DEFAULT_LOG_KEYWORD, "handler %p is not set, skip ...", pHandler);
177 return -EBADF;
178 }
70ed7d01 179 fgConfigurationHandler=NULL;
85465857 180 return iResult;
181}
182
70ed7d01 183const char* AliHLTConfiguration::GetName() const
184{
185 // see header file for function documentation
52c1c164 186 if (!fID.IsNull())
187 return fID.Data();
3495cce2 188 return TObject::GetName();
189}
190
191AliHLTConfiguration* AliHLTConfiguration::GetSource(const char* id)
192{
70ed7d01 193 // see header file for function documentation
3495cce2 194 AliHLTConfiguration* pSrc=NULL;
195 if (id) {
196 // first check the current element
197 if (fListSrcElement!=fListSources.end() && strcmp(id, (*fListSrcElement)->GetName())==0) {
198 pSrc=*fListSrcElement;
199 } else {
200 // check the list
201
202 pSrc=GetFirstSource();
203 while (pSrc) {
204 if (strcmp(id, pSrc->GetName())==0)
205 break;
206 pSrc=GetNextSource();
207 }
208 }
209 }
210 return pSrc;
211}
212
213AliHLTConfiguration* AliHLTConfiguration::GetFirstSource()
214{
70ed7d01 215 // see header file for function documentation
3495cce2 216 AliHLTConfiguration* pSrc=NULL;
95654878 217 if (fNofSources>=0 || ExtractSources()>=0) {
3495cce2 218 fListSrcElement=fListSources.begin();
219 if (fListSrcElement!=fListSources.end()) pSrc=*fListSrcElement;
220 }
221 return pSrc;
222}
223
224AliHLTConfiguration* AliHLTConfiguration::GetNextSource()
225{
70ed7d01 226 // see header file for function documentation
3495cce2 227 AliHLTConfiguration* pSrc=NULL;
228 if (fNofSources>0) {
229 if (fListSrcElement!=fListSources.end() && (++fListSrcElement)!=fListSources.end())
230 pSrc=*fListSrcElement;
231 }
232 return pSrc;
233}
234
235int AliHLTConfiguration::SourcesResolved(int bAuto)
236{
70ed7d01 237 // see header file for function documentation
3495cce2 238 int iResult=0;
95e36f3d 239 if (fNofSources>=0 || (bAuto && (iResult=ExtractSources()))>=0) {
85465857 240 //HLTDebug("fNofSources=%d", fNofSources);
241 //HLTDebug("list size = %d", fListSources.size());
3495cce2 242 iResult=fNofSources==(int)fListSources.size();
243 }
244 return iResult;
245}
246
247int AliHLTConfiguration::InvalidateSource(AliHLTConfiguration* pConf)
248{
70ed7d01 249 // see header file for function documentation
3495cce2 250 int iResult=0;
251 if (pConf) {
252 vector<AliHLTConfiguration*>::iterator element=fListSources.begin();
253 while (element!=fListSources.end()) {
254 if (*element==pConf) {
255 fListSources.erase(element);
256 fListSrcElement=fListSources.end();
257 // there is no need to re-evaluate until there was a new configuration registered
258 // -> postpone the invalidation, its done in AliHLTConfigurationHandler::RegisterConfiguration
259 //InvalidateSources();
260 break;
261 }
262 element++;
263 }
264 } else {
265 iResult=-EINVAL;
266 }
267 return iResult;
268}
269
270void AliHLTConfiguration::PrintStatus()
271{
70ed7d01 272 // see header file for function documentation
85465857 273 HLTLogKeyword("configuration status");
274 HLTMessage("status of configuration \"%s\" (%p)", GetName(), this);
52c1c164 275 if (!fComponent.IsNull()) HLTMessage(" - component: \"%s\"", fComponent.Data());
85465857 276 else HLTMessage(" - component string invalid");
52c1c164 277 if (!fStringSources.IsNull()) HLTMessage(" - sources: \"%s\"", fStringSources.Data());
85465857 278 else HLTMessage(" - no sources");
3495cce2 279 if (SourcesResolved(1)<=0)
85465857 280 HLTMessage(" there are unresolved sources");
3495cce2 281 AliHLTConfiguration* pSrc=GetFirstSource();
282 while (pSrc) {
85465857 283 HLTMessage(" source \"%s\" (%p) resolved", pSrc->GetName(), pSrc);
3495cce2 284 pSrc=GetNextSource();
285 }
286}
287
0c0c9d99 288int AliHLTConfiguration::GetArguments(const char*** pArgv)
3495cce2 289{
70ed7d01 290 // see header file for function documentation
3495cce2 291 int iResult=0;
0c0c9d99 292 if (pArgv) {
9ce4bf4a 293 if (fArgc==-1) {
294 if ((iResult=ExtractArguments())<0) {
295 HLTError("error extracting arguments for configuration %s", GetName());
296 fArgc=-EINVAL;
297 }
298 } else if (fArgc<0) {
299 HLTError("previous argument extraction failed");
300 }
301 //HLTDebug("%s fArgc %d", GetName(), fArgc);
0c0c9d99 302 iResult=fArgc;
3495cce2 303 *pArgv=(const char**)fArgv;
304 } else {
9ce4bf4a 305 HLTError("invalid parameter");
3495cce2 306 iResult=-EINVAL;
307 }
308 return iResult;
309}
310
311
312int AliHLTConfiguration::ExtractSources()
313{
70ed7d01 314 // see header file for function documentation
3495cce2 315 int iResult=0;
316 fNofSources=0;
52c1c164 317 if (!fStringSources.IsNull()) {
3495cce2 318 vector<char*> tgtList;
319 fListSources.clear();
52c1c164 320 if ((iResult=InterpreteString(fStringSources.Data(), tgtList))>=0) {
3495cce2 321 fNofSources=tgtList.size();
322 vector<char*>::iterator element=tgtList.begin();
85465857 323 while ((element=tgtList.begin())!=tgtList.end()) {
70ed7d01 324 if (fgConfigurationHandler) {
325 AliHLTConfiguration* pConf=fgConfigurationHandler->FindConfiguration(*element);
85465857 326 if (pConf) {
a742f6f8 327 //HLTDebug("configuration %s (%p): source \"%s\" (%p) inserted", GetName(), this, pConf->GetName(), pConf);
85465857 328 fListSources.push_back(pConf);
329 } else {
330 HLTError("can not find source \"%s\"", (*element));
331 iResult=-ENOENT;
332 }
333 } else if (iResult>=0) {
334 iResult=-EFAULT;
335 HLTFatal("global configuration handler not initialized, can not resolve sources");
3495cce2 336 }
337 delete[] (*element);
338 tgtList.erase(element);
339 }
340 fListSrcElement=fListSources.begin();
341 }
342 }
343 return iResult;
344}
345
346int AliHLTConfiguration::ExtractArguments()
347{
70ed7d01 348 // see header file for function documentation
3495cce2 349 int iResult=0;
52c1c164 350 if (!fArguments.IsNull()) {
3495cce2 351 vector<char*> tgtList;
352 if ((iResult=InterpreteString(fArguments, tgtList))>=0) {
353 fArgc=tgtList.size();
9ce4bf4a 354 //HLTDebug("configuration %s: extracted %d arguments from \"%s\"", GetName(), fArgc, fArguments);
3495cce2 355 if (fArgc>0) {
356 fArgv = new char*[fArgc];
357 if (fArgv) {
358 vector<char*>::iterator element=tgtList.begin();
359 int i=0;
360 while (element!=tgtList.end()) {
85465857 361 //HLTDebug("assign arguments %d (%s)", i, *element);
3495cce2 362 fArgv[i++]=(*element);
363 element++;
364 }
365 } else {
366 iResult=-ENOMEM;
367 }
368 }
369 }
9ce4bf4a 370 } else {
371 // there are zero arguments
372 fArgc=0;
3495cce2 373 }
374 return iResult;
375}
376
4b31e06b 377int AliHLTConfiguration::InterpreteString(const char* arg, vector<char*>& argList)
3495cce2 378{
70ed7d01 379 // see header file for function documentation
3495cce2 380 int iResult=0;
381 if (arg) {
85465857 382 //HLTDebug("interprete \"%s\"", arg);
3495cce2 383 int i=0;
384 int prec=-1;
5f5b708b 385 int bQuote=0;
3495cce2 386 do {
5f5b708b 387 //HLTDebug("%d %x", i, arg[i]);
388 if (arg[i]=='\'' && bQuote==0) {
389 bQuote=1;
390 } else if (arg[i]==0 ||
391 (arg[i]==' ' && bQuote==0) ||
392 (arg[i]=='\'' && bQuote==1)) {
393 bQuote=0;
3495cce2 394 if (prec>=0) {
395 char* pEntry= new char[i-prec+1];
396 if (pEntry) {
397 strncpy(pEntry, &arg[prec], i-prec);
398 pEntry[i-prec]=0; // terminate string
85465857 399 //HLTDebug("create string \"%s\", insert at %d", pEntry, argList.size());
3495cce2 400 argList.push_back(pEntry);
401 } else
402 iResult=-ENOMEM;
403 prec=-1;
404 }
405 } else if (prec==-1) prec=i;
406 } while (arg[i++]!=0 && iResult>=0);
407 } else {
408 iResult=-EINVAL;
409 }
410 return iResult;
411}
412
032c5e5e 413int AliHLTConfiguration::ConvertSizeString(const char* strSize) const
414{
415 // see header file for function documentation
416 int size=0;
417 if (!strSize) return -1;
418
419 char* endptr=NULL;
420 size=strtol(strSize, &endptr, 10);
421 if (size>=0) {
422 if (endptr) {
423 if (endptr==strSize) {
424 HLTWarning("ignoring unrecognized buffer size '%s'", strSize);
425 size=-1;
426 } else if (*endptr==0) {
427 // no unit specifier
428 } else if (*endptr=='k') {
429 size*=1014;
430 } else if (*endptr=='M') {
431 size*=1024*1024;
432 } else {
433 HLTWarning("ignoring buffer size of unknown unit '%c'", endptr[0]);
434 }
435 } else {
436 HLTWarning("ignoring negative buffer size specifier '%s'", strSize);
437 size=-1;
438 }
439 }
440 return size;
441}
442
3495cce2 443int AliHLTConfiguration::FollowDependency(const char* id, TList* pTgtList)
444{
70ed7d01 445 // see header file for function documentation
3495cce2 446 int iResult=0;
447 if (id) {
448 AliHLTConfiguration* pDep=NULL;
449 if ((pDep=GetSource(id))!=NULL) {
450 if (pTgtList) pTgtList->Add(pDep);
451 iResult++;
452 } else {
453 pDep=GetFirstSource();
454 while (pDep && iResult==0) {
455 if ((iResult=pDep->FollowDependency(id, pTgtList))>0) {
456 if (pTgtList) pTgtList->AddFirst(pDep);
457 iResult++;
458 }
459 pDep=GetNextSource();
460 }
461 }
462 } else {
463 iResult=-EINVAL;
464 }
465 return iResult;
466}
467
3495cce2 468