3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
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 //**************************************************************************
19 /** @file AliHLTDimServer.cxx
20 @author Matthias Richter
22 @brief HLT DIM server implementation
25 #include "AliHLTDimServer.h"
26 #include "TObjArray.h"
30 /** ROOT macro for the implementation of ROOT specific class methods */
31 ClassImp(AliHLTDimServer)
33 AliHLTDimServer::AliHLTDimServer()
37 , fpServerThread(NULL)
38 , fUpdatePeriod(10000)
40 // see header file for class documentation
42 // refer to README to build package
44 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
47 AliHLTDimServer::AliHLTDimServer(const char* servername)
48 : TNamed(servername, "AliHLTDimServer")
51 , fpServerThread(NULL)
52 , fUpdatePeriod(10000)
54 // see header file for class documentation
57 AliHLTDimServer::AliHLTDimInterface* AliHLTDimServer::fgpInterface=NULL;
59 AliHLTDimServer::~AliHLTDimServer()
61 // see header file for class documentation
64 int AliHLTDimServer::RegisterService(AliHLTDimService* pService)
66 // see header file for class documentation
67 if (!pService) return -EINVAL;
68 if (fServices.FindObject(pService)) {
70 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "RegisterService" , __FILE__ , __LINE__ , "duplicate service name %s, skip ...", pService->GetName());
73 fServices.Add(pService);
77 AliHLTDimServer::AliHLTDimService* AliHLTDimServer::CreateService(enum AliHLTDimServer::AliHLTDimServiceDataType type, const char* name)
79 // see header file for class documentation
80 AliHLTDimService* service=new AliHLTDimService(type, name);
81 int iResult=RegisterService(service);
84 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "CreateService" , __FILE__ , __LINE__ , "failed to register service %s: %d", name, iResult);
85 if (service) delete service;
91 TObjArray* AliHLTDimServer::CreateServiceGroup(enum AliHLTDimServer::AliHLTDimServiceDataType type, const char* basename, int count)
94 TObjArray* pServices=new TObjArray;
97 if (basename && count>0) {
100 while ((i/=10)>0) digits++;
102 log.LoggingVarargs(kHLTLogDebug, "AliHLTDimServer", "CreateServiceGroup" , __FILE__ , __LINE__ , "basename=%s count=%d digits=%d\n", basename, count, digits);
103 int namelen=strlen(basename)+2+digits;
104 char* name=(char*)malloc(namelen);
105 char* format=(char*)malloc(namelen); // this has actually only indirect to do with namelen but its appropriate
106 if (name && format) {
107 const char* key=strchr(basename, '%');
108 strcpy(format, basename);
110 int iPos=(key-basename)+1;
112 sprintf(format+iPos, "0*d");
115 *(format+iPos++)='%';
116 *(format+iPos++)=key[1];
118 strcpy(format+iPos, &key[2]);
120 sprintf(format+strlen(basename), "_%%0*d");
122 for (i=0; i<count && iResult>=0; i++) {
123 sprintf(name, format, digits, i);
124 AliHLTDimService* service=new AliHLTDimService(type, name);
125 iResult=RegisterService(service);
130 if (name) free(name);
131 if (format) free(format);
138 int AliHLTDimServer::UpdateServices()
140 /// Update all services via the Dim channel
144 AliHLTDimServer::AliHLTDimInterface* AliHLTDimServer::Interface()
147 fgpInterface=new AliHLTDimInterface;
149 fgpInterface->Init();
155 int AliHLTDimServer::Init(const char* dimNameServer)
158 const char* myname=GetName();
159 if (myname==NULL || myname[0]==0) {
160 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Init" , __FILE__ , __LINE__ , "can not start without a server name, skipping initialization ...");
164 if (Interface()==NULL) return -ENODEV;
166 Interface()->DisSetDnsNode(dimNameServer);
170 int AliHLTDimServer::Reset()
175 int AliHLTDimServer::Start()
177 // start the server in a separate thread
180 if (GetState()!=kStateOff) {
181 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Start" , __FILE__ , __LINE__ , "server is not off, currently in state %d", GetState());
184 fpServerThread=new TThread(ServerLoop, (void*)this);
185 if (!fpServerThread) {
186 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Start" , __FILE__ , __LINE__ , "unable to create server thread");
189 SetState(kStateStarting);
190 fpServerThread->Run();
193 const int maxcount=100;
194 const int maxsleepms=1000;
195 while (GetState()==kStateStarting) {
196 if (count++==maxcount) break;
197 gSystem->Sleep(maxsleepms/maxcount);
200 if (GetState()!=kStateRunning) {
201 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Start" , __FILE__ , __LINE__ , "could not start server, currently in state %d", GetState());
208 int AliHLTDimServer::Stop()
210 // stop the server thread
213 if (GetState()!=kStateRunning) {
214 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Stop" , __FILE__ , __LINE__ , "server is not running, currently in state %d", GetState());
217 SetState(kStateStopping);
219 const int maxcount=100;
220 const int maxsleepms=2*fUpdatePeriod;
221 while (GetState()==kStateStopping) {
222 if (count++==maxcount) break;
223 gSystem->Sleep(maxsleepms/maxcount);
226 if (GetState()!=kStateOff) {
227 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Stop" , __FILE__ , __LINE__ , "could not stop server, currently in state %d", GetState());
234 void* AliHLTDimServer::ServerLoop(void* param)
236 // see header file for class documentation
237 if (!param) return (void*)0;
238 AliHLTDimServer* server=reinterpret_cast<AliHLTDimServer*>(param);
239 return server->ServerLoop();
242 void* AliHLTDimServer::ServerLoop()
244 // see header file for class documentation
245 if (!Interface()) return (void*)-ENODEV;
248 TIter next(&fServices);
250 while ((obj=next())!=NULL) {
251 AliHLTDimService* pService=dynamic_cast<AliHLTDimService*>(obj);
252 if (!pService) continue;
253 TString name=GetName(); name+="_"; name+=pService->GetName();
255 void* buffer=pService->GetLocation();
257 switch (pService->GetType()) {
266 case kDataTypeString:
267 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "ignoring dim service %s: type 'string' not yet implemented", name.Data());
270 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "ignoring dim service %s: unknown type %d", name.Data(), pService->GetType());
273 int id=Interface()->DisAddService(name.Data(), type, buffer, size);
275 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "failed to add dim service %s: error %d", name.Data(), id);
282 SetState(kStateRunning);
283 Interface()->DisStartServing(GetName());
284 while (GetState()==kStateRunning) {
285 gSystem->Sleep(fUpdatePeriod);
288 if (GetState()!=kStateStopping) return (void*)0;
291 Interface()->DisStopServing();
293 while ((obj=next())!=NULL) {
294 const AliHLTDimService* pService=dynamic_cast<const AliHLTDimService*>(obj);
295 if (!pService || pService->GetId()<0) continue;
296 // int iResult=Interface()->DisRemoveService(pService->GetId());
298 // log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "failed to remove dim service %s: error %d", pService->GetName(), iResult);
307 AliHLTDimServer::AliHLTDimService::AliHLTDimService()
310 , fType(kDataTypeUnknown)
313 // see header file for class documentation
316 AliHLTDimServer::AliHLTDimService::AliHLTDimService(enum AliHLTDimServiceDataType type, const char* servicename)
317 : TNamed(servicename, "AliHLTDimService")
322 // see header file for class documentation
325 case kDataTypeInt: break;
326 case kDataTypeFloat: break;
327 case kDataTypeString:
328 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "AliHLTDimService" , __FILE__ , __LINE__ , "dim service type 'string' not yet implemented");
331 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "AliHLTDimService" , __FILE__ , __LINE__ , "Unknown dim service type %d", fType);
335 void AliHLTDimServer::AliHLTDimService::Update(AliHLTDimServicePoint_t& sp)
337 // see header file for class documentation
338 static bool bWarning=true;
341 case kDataTypeInt: fData.iVal=sp.iVal; break;
342 case kDataTypeFloat: fData.fVal=sp.fVal; break;
343 case kDataTypeString:
344 if (bWarning) log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "Update" , __FILE__ , __LINE__ , "Failed to update dim service %s: 'string' not yet implemented", GetName());
348 if (bWarning) log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "Update" , __FILE__ , __LINE__ , "Failed to update dim service %s: unknown type %d", GetName(), fType);
352 AliHLTDimServer::Interface()->DisUpdateService(fId);
355 AliHLTDimServer::AliHLTDimInterface::AliHLTDimInterface()
357 , fpDisAddService(NULL)
358 , fpDisRemoveService(NULL)
359 , fpDisUpdateService(NULL)
360 , fpDisStartServing(NULL)
361 , fpDisStopServing(NULL)
362 , fpDisSetDnsNode(NULL)
366 AliHLTDimServer::AliHLTDimInterface::~AliHLTDimInterface()
371 const char* AliHLTDimServer::AliHLTDimInterface::fgkDimLibraryName="libdim.so";
372 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisAddServiceSymbol="dis_add_service";
373 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisRemoveServiceSymbol="dis_remove_service";
374 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisUpdateServiceSymbol="dis_update_service";
375 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisStartServingSymbol="dis_start_serving";
376 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisStopServingSymbol="dis_stop_serving";
377 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisSetDnsNodeSymbol="dis_set_dns_node";
379 int AliHLTDimServer::AliHLTDimInterface::Init()
381 /// load the dim library and function pointers
382 if (gSystem->Load(fgkDimLibraryName)) {
383 HLTFatal("failed to load dim library: %s", fgkDimLibraryName);
387 fpDisAddService=(fctDisAddService)FindSymbol(fgkDimLibraryName, fgkDisAddServiceSymbol);
388 if (!fpDisAddService) return -ENODEV;
390 fpDisRemoveService=(fctDisRemoveService)FindSymbol(fgkDimLibraryName, fgkDisRemoveServiceSymbol);
391 if (!fpDisRemoveService) return -ENODEV;
393 fpDisUpdateService=(fctDisUpdateService)FindSymbol(fgkDimLibraryName, fgkDisUpdateServiceSymbol);
394 if (!fpDisUpdateService) return -ENODEV;
396 fpDisStartServing=(fctDisCharArg)FindSymbol(fgkDimLibraryName, fgkDisStartServingSymbol);
397 if (!fpDisStartServing) return -ENODEV;
399 fpDisStopServing=(fctDisNoArg)FindSymbol(fgkDimLibraryName, fgkDisStopServingSymbol);
400 if (!fpDisStopServing) return -ENODEV;
402 fpDisSetDnsNode=(fctDisCharArg)FindSymbol(fgkDimLibraryName, fgkDisSetDnsNodeSymbol);
403 if (!fpDisSetDnsNode) return -ENODEV;
408 AliHLTDimServer::fctVoid AliHLTDimServer::AliHLTDimInterface::FindSymbol(const char* library, const char* symbol) const
410 /// Find symbol in the dim library
412 fctVoid fctptr=gSystem->DynFindSymbol(library, tmp.Data());
414 // do a 2nd try with appended '_'
416 fctptr=gSystem->DynFindSymbol(library, tmp.Data());
418 if (fctptr) return fctptr;
420 HLTError("can not find symbol '%s' in %s", symbol, library);