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 and dynamic access
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)
93 // see header file for class documentation
95 TObjArray* pServices=new TObjArray;
98 if (basename && count>0) {
101 while ((i/=10)>0) digits++;
103 log.LoggingVarargs(kHLTLogDebug, "AliHLTDimServer", "CreateServiceGroup" , __FILE__ , __LINE__ , "basename=%s count=%d digits=%d\n", basename, count, digits);
104 int namelen=strlen(basename)+2+digits;
105 char* name=(char*)malloc(namelen);
106 char* format=(char*)malloc(namelen); // this has actually only indirect to do with namelen but its appropriate
107 if (name && format) {
108 const char* key=strchr(basename, '%');
109 strcpy(format, basename);
111 int iPos=(key-basename)+1;
113 sprintf(format+iPos, "0*d");
116 *(format+iPos++)='%';
117 *(format+iPos++)=key[1];
119 strcpy(format+iPos, &key[2]);
121 sprintf(format+strlen(basename), "_%%0*d");
123 for (i=0; i<count && iResult>=0; i++) {
124 sprintf(name, format, digits, i);
125 AliHLTDimService* service=new AliHLTDimService(type, name);
126 iResult=RegisterService(service);
131 if (name) free(name);
132 if (format) free(format);
139 int AliHLTDimServer::UpdateServices()
141 /// Update all services via the Dim channel
145 AliHLTDimServer::AliHLTDimInterface* AliHLTDimServer::Interface()
147 // get instance of the interface
149 fgpInterface=new AliHLTDimInterface;
151 fgpInterface->Init();
157 int AliHLTDimServer::Init(const char* dimNameServer)
159 // init the dim server, check if the interface is available and set the
162 const char* myname=GetName();
163 if (myname==NULL || myname[0]==0) {
164 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Init" , __FILE__ , __LINE__ , "can not start without a server name, skipping initialization ...");
168 if (Interface()==NULL) return -ENODEV;
170 Interface()->DisSetDnsNode(dimNameServer);
174 int AliHLTDimServer::Reset()
176 // reset the DIM server, functionality needs to be clarified
180 int AliHLTDimServer::Start()
182 // start the server in a separate thread
185 if (GetState()!=kStateOff) {
186 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Start" , __FILE__ , __LINE__ , "server is not off, currently in state %d", GetState());
189 fpServerThread=new TThread(ServerLoop, (void*)this);
190 if (!fpServerThread) {
191 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Start" , __FILE__ , __LINE__ , "unable to create server thread");
194 SetState(kStateStarting);
195 fpServerThread->Run();
198 const int maxcount=100;
199 const int maxsleepms=1000;
200 while (GetState()==kStateStarting) {
201 if (count++==maxcount) break;
202 gSystem->Sleep(maxsleepms/maxcount);
205 if (GetState()!=kStateRunning) {
206 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Start" , __FILE__ , __LINE__ , "could not start server, currently in state %d", GetState());
213 int AliHLTDimServer::Stop()
215 // stop the server thread
218 if (GetState()!=kStateRunning) {
219 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Stop" , __FILE__ , __LINE__ , "server is not running, currently in state %d", GetState());
222 SetState(kStateStopping);
224 const int maxcount=100;
225 const int maxsleepms=2*fUpdatePeriod;
226 while (GetState()==kStateStopping) {
227 if (count++==maxcount) break;
228 gSystem->Sleep(maxsleepms/maxcount);
231 if (GetState()!=kStateOff) {
232 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "Stop" , __FILE__ , __LINE__ , "could not stop server, currently in state %d", GetState());
239 void* AliHLTDimServer::ServerLoop(void* param)
241 // see header file for class documentation
242 if (!param) return (void*)0;
243 AliHLTDimServer* server=reinterpret_cast<AliHLTDimServer*>(param);
244 return server->ServerLoop();
247 void* AliHLTDimServer::ServerLoop()
249 // see header file for class documentation
250 if (!Interface()) return (void*)-ENODEV;
253 TIter next(&fServices);
255 while ((obj=next())!=NULL) {
256 AliHLTDimService* pService=dynamic_cast<AliHLTDimService*>(obj);
257 if (!pService) continue;
258 TString name=GetName(); name+="_"; name+=pService->GetName();
260 void* buffer=pService->GetLocation();
262 switch (pService->GetType()) {
271 case kDataTypeString:
272 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "ignoring dim service %s: type 'string' not yet implemented", name.Data());
275 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "ignoring dim service %s: unknown type %d", name.Data(), pService->GetType());
278 int id=Interface()->DisAddService(name.Data(), type, buffer, size);
280 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "failed to add dim service %s: error %d", name.Data(), id);
287 SetState(kStateRunning);
288 Interface()->DisStartServing(GetName());
289 while (GetState()==kStateRunning) {
290 gSystem->Sleep(fUpdatePeriod);
293 if (GetState()!=kStateStopping) return (void*)0;
296 Interface()->DisStopServing();
298 while ((obj=next())!=NULL) {
299 const AliHLTDimService* pService=dynamic_cast<const AliHLTDimService*>(obj);
300 if (!pService || pService->GetId()<0) continue;
301 // int iResult=Interface()->DisRemoveService(pService->GetId());
303 // log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "ServerLoop" , __FILE__ , __LINE__ , "failed to remove dim service %s: error %d", pService->GetName(), iResult);
312 AliHLTDimServer::AliHLTDimService::AliHLTDimService()
315 , fType(kDataTypeUnknown)
318 // see header file for class documentation
321 AliHLTDimServer::AliHLTDimService::AliHLTDimService(enum AliHLTDimServiceDataType type, const char* servicename)
322 : TNamed(servicename, "AliHLTDimService")
327 // see header file for class documentation
330 case kDataTypeInt: break;
331 case kDataTypeFloat: break;
332 case kDataTypeString:
333 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "AliHLTDimService" , __FILE__ , __LINE__ , "dim service type 'string' not yet implemented");
336 log.LoggingVarargs(kHLTLogError, "AliHLTDimServer", "AliHLTDimService" , __FILE__ , __LINE__ , "Unknown dim service type %d", fType);
340 void AliHLTDimServer::AliHLTDimService::Update(const AliHLTDimServicePoint_t& sp)
342 // see header file for class documentation
343 static bool bWarning=true;
346 case kDataTypeInt: fData.iVal=sp.iVal; break;
347 case kDataTypeFloat: fData.fVal=sp.fVal; break;
348 case kDataTypeString:
349 if (bWarning) log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "Update" , __FILE__ , __LINE__ , "Failed to update dim service %s: 'string' not yet implemented", GetName());
353 if (bWarning) log.LoggingVarargs(kHLTLogError, "AliHLTDimServer::AliHLTDimService", "Update" , __FILE__ , __LINE__ , "Failed to update dim service %s: unknown type %d", GetName(), fType);
357 AliHLTDimServer::Interface()->DisUpdateService(fId);
360 AliHLTDimServer::AliHLTDimInterface::AliHLTDimInterface()
362 , fpDisAddService(NULL)
363 , fpDisRemoveService(NULL)
364 , fpDisUpdateService(NULL)
365 , fpDisStartServing(NULL)
366 , fpDisStopServing(NULL)
367 , fpDisSetDnsNode(NULL)
371 AliHLTDimServer::AliHLTDimInterface::~AliHLTDimInterface()
376 const char* AliHLTDimServer::AliHLTDimInterface::fgkDimLibraryName="libdim.so";
377 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisAddServiceSymbol="dis_add_service";
378 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisRemoveServiceSymbol="dis_remove_service";
379 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisUpdateServiceSymbol="dis_update_service";
380 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisStartServingSymbol="dis_start_serving";
381 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisStopServingSymbol="dis_stop_serving";
382 const char* AliHLTDimServer::AliHLTDimInterface::fgkDisSetDnsNodeSymbol="dis_set_dns_node";
384 int AliHLTDimServer::AliHLTDimInterface::Init()
386 /// load the dim library and function pointers
387 if (gSystem->Load(fgkDimLibraryName)) {
388 HLTFatal("failed to load dim library: %s", fgkDimLibraryName);
392 fpDisAddService=(fctDisAddService)FindSymbol(fgkDimLibraryName, fgkDisAddServiceSymbol);
393 if (!fpDisAddService) return -ENODEV;
395 fpDisRemoveService=(fctDisRemoveService)FindSymbol(fgkDimLibraryName, fgkDisRemoveServiceSymbol);
396 if (!fpDisRemoveService) return -ENODEV;
398 fpDisUpdateService=(fctDisUpdateService)FindSymbol(fgkDimLibraryName, fgkDisUpdateServiceSymbol);
399 if (!fpDisUpdateService) return -ENODEV;
401 fpDisStartServing=(fctDisCharArg)FindSymbol(fgkDimLibraryName, fgkDisStartServingSymbol);
402 if (!fpDisStartServing) return -ENODEV;
404 fpDisStopServing=(fctDisNoArg)FindSymbol(fgkDimLibraryName, fgkDisStopServingSymbol);
405 if (!fpDisStopServing) return -ENODEV;
407 fpDisSetDnsNode=(fctDisCharArg)FindSymbol(fgkDimLibraryName, fgkDisSetDnsNodeSymbol);
408 if (!fpDisSetDnsNode) return -ENODEV;
413 AliHLTDimServer::fctVoid AliHLTDimServer::AliHLTDimInterface::FindSymbol(const char* library, const char* symbol) const
415 /// Find symbol in the dim library
417 fctVoid fctptr=gSystem->DynFindSymbol(library, tmp.Data());
419 // do a 2nd try with appended '_'
421 fctptr=gSystem->DynFindSymbol(library, tmp.Data());
423 if (fctptr) return fctptr;
425 HLTError("can not find symbol '%s' in %s", symbol, library);