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