]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTSystem.cxx
168956f4b4e3c3a13ca54ed1333cacf88c1f7cfd
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.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  *          for The ALICE Off-line Project.                               *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          *
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18 /** @file   AliHLTSystem.cxx
19     @author Matthias Richter
20     @date   
21     @brief  Implementation of HLT module management.
22 */
23
24 #if __GNUC__>= 3
25 using namespace std;
26 #endif
27
28 #include <cerrno>
29 #include <string>
30 #include "AliL3StandardIncludes.h"
31 #include "AliHLTSystem.h"
32 #include "AliHLTComponentHandler.h"
33 #include "AliHLTComponent.h"
34 #include "AliHLTConfiguration.h"
35 #include "AliHLTConfigurationHandler.h"
36 #include "AliHLTTask.h"
37
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTSystem)
40
41 AliHLTSystem::AliHLTSystem()
42 {
43   fpComponentHandler=new AliHLTComponentHandler();
44   if (fpComponentHandler) {
45     AliHLTComponentEnvironment env;
46     memset(&env, 0, sizeof(AliHLTComponentEnvironment));
47     env.fLoggingFunc=AliHLTLogging::Message;
48     fpComponentHandler->SetEnvironment(&env);
49
50     // init logging function in AliHLTLogging
51     Init(AliHLTLogging::Message);
52   } else {
53     HLTFatal("can not create Component Handler");
54   }
55   fpConfigurationHandler=new AliHLTConfigurationHandler();
56   if (fpConfigurationHandler) {
57     AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
58   } else {
59     HLTFatal("can not create Configuration Handler");
60   }
61 }
62
63
64 AliHLTSystem::~AliHLTSystem()
65 {
66     AliHLTConfiguration::GlobalDeinit();
67     if (fpConfigurationHandler) {
68       delete fpConfigurationHandler;
69     }
70     fpConfigurationHandler=NULL;
71
72     if (fpComponentHandler) {
73       delete fpComponentHandler;
74     }
75     fpComponentHandler=NULL;
76 }
77
78 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
79 {
80   int iResult=0;
81   return iResult;
82 }
83
84 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
85 {
86   int iResult=0;
87   return iResult;
88 }
89
90 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
91 {
92   int iResult=0;
93   return iResult;
94 }
95
96 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
97 {
98   int iResult=0;
99   if (pConf) {
100     AliHLTTask* pTask=NULL;
101     if ((pTask=FindTask(pConf->GetName()))!=NULL) {
102       if (pTask->GetConf()!=pConf) {
103         HLTError("configuration missmatch, there is already a task with configuration name \"%s\", but it is different. Most likely configuration %p is not registered properly", pConf->GetName(), pConf);
104         iResult=-EEXIST;
105         pTask=NULL;
106       }
107     } else if (pConf->SourcesResolved(1)!=1) {
108         HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
109         iResult=-ENOLINK;
110     } else {
111       pTask=new AliHLTTask(pConf, NULL);
112       if (pTask==NULL) {
113         iResult=-ENOMEM;
114       }
115     }
116     if (pTask) {
117       // check for circular dependencies
118       if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
119         HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
120         pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
121         HLTError("aborted ...");
122         iResult=-ELOOP;
123       }
124       if (iResult>=0) {
125         // check whether all dependencies are already in the task list
126         // create the missing ones
127         // this step is an iterative process which calls this function again for the missing
128         // configurations, in order to avoid the currently processed task to be created
129         // again it is added to the list temporarily and removed afterwards
130         // This is of high importance to preserve the order of the tasks. Furthermore, the
131         // InsertTask method has to be used in order to set all the cross links right 
132         fTaskList.Add(pTask);
133         AliHLTConfiguration* pDep=pConf->GetFirstSource();
134         while (pDep!=NULL && iResult>=0) {
135           if (FindTask(pDep->GetName())==NULL) {
136             iResult=BuildTaskList(pDep);
137           }
138           pDep=pConf->GetNextSource();
139         }
140         // remove the temporarily added task
141         fTaskList.Remove(pTask);
142
143         // insert the task and set the cross-links
144         if (iResult>=0) {
145           iResult=InsertTask(pTask);
146         }
147       } else {
148         delete pTask;
149         pTask=NULL;
150       }
151     }
152   } else {
153     iResult=-EINVAL;
154   }
155   return iResult;
156 }
157
158 int AliHLTSystem::CleanTaskList()
159 {
160   int iResult=0;
161   TObjLink* lnk=NULL;
162   while ((lnk=fTaskList.FirstLink())!=NULL) {
163     fTaskList.Remove(lnk);
164     delete (lnk->GetObject());
165   }
166   return iResult;
167 }
168
169 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
170 {
171   int iResult=0;
172   TObjLink *lnk = NULL;
173   if ((iResult=pTask->CheckDependencies())>0)
174     lnk=fTaskList.FirstLink();
175   while (lnk && iResult>0) {
176     AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
177     //HLTDebug("checking  \"%s\"", pCurr->GetName());
178     iResult=pTask->Depends(pCurr);
179     if (iResult>0) {
180       iResult=pTask->SetDependency(pCurr);
181       pCurr->SetTarget(pTask);
182       HLTDebug("set dependency  \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
183     }
184     if (pCurr->Depends(pTask)) {
185       // circular dependency
186       HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
187       iResult=-ELOOP;
188     } else if ((iResult=pTask->CheckDependencies())>0) {
189       lnk = lnk->Next();
190     }
191   }
192   if (iResult==0) {
193       if (lnk) {
194         fTaskList.AddAfter(lnk, pTask);
195       } else {
196         fTaskList.AddFirst(pTask);
197       }
198       HLTDebug("task \"%s\" inserted", pTask->GetName());
199   } else if (iResult>0) {
200     HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
201     iResult=-ENOLINK;
202   }
203   return iResult;
204 }
205
206 AliHLTTask* AliHLTSystem::FindTask(const char* id)
207 {
208   AliHLTTask* pTask=NULL;
209   if (id) {
210     pTask=(AliHLTTask*)fTaskList.FindObject(id); 
211   }
212   return pTask;
213 }
214
215 void AliHLTSystem::PrintTaskList()
216 {
217   HLTLogKeyword("task list");
218   TObjLink *lnk = NULL;
219   HLTMessage("Task List");
220   lnk=fTaskList.FirstLink();
221   while (lnk) {
222     TObject* obj=lnk->GetObject();
223     if (obj) {
224       HLTMessage("  %s - status:", obj->GetName());
225       AliHLTTask* pTask=(AliHLTTask*)obj;
226       pTask->PrintStatus();
227     } else {
228     }
229     lnk = lnk->Next();
230   }
231 }
232
233 int AliHLTSystem::Run() 
234 {
235   int iResult=0;
236   HLTError("function not yet implemented");
237   iResult=-ENOSYS;
238   return iResult;
239 }