]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTDimServer.h
43c900fdd2a94ba8a484a72fbc5f73b6d11a0476
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDimServer.h
1 //-*- Mode: C++ -*-
2 // $Id$
3
4 #ifndef ALIHLTDIMSERVER_H
5 #define ALIHLTDIMSERVER_H
6
7 //* This file is property of and copyright by the ALICE HLT Project        * 
8 //* ALICE Experiment at CERN, All rights reserved.                         *
9 //* See cxx source for full Copyright notice                               *
10
11 /** @file   AliHLTDimServer.h
12     @author Matthias Richter
13     @date   20010-03-10
14     @brief  HLT DIM server
15 */
16 #include "AliHLTLogging.h"
17 #include "TNamed.h"
18 #include "TObjArray.h"
19
20 class TThread;
21
22 /**
23  * @class AliHLTDimServer
24  * Implementation of a DIM server for HLT and the dynamic access to the
25  * DIM  library.
26  */
27 class AliHLTDimServer : public TNamed {
28 public:
29   AliHLTDimServer();
30   AliHLTDimServer(const char* servername);
31   ~AliHLTDimServer();
32
33   /// Data type identifiers for services.
34   enum AliHLTDimServiceDataType{
35     kDataTypeUnknown = 0, /// initializer
36     kDataTypeInt,         /// Integer type
37     kDataTypeFloat,       /// Float type
38     kDataTypeString,      /// String type
39   };
40
41   /// The service data field.
42   struct AliHLTDimServicePoint_t {
43     union {
44       int iVal;     /// integer value
45       float fVal;   /// float value
46       void* strVal; /// string value, casted to string* before use
47     };
48   };
49
50   /** @class AliHLTDimService
51    * Base class for DIM services
52    */
53   class AliHLTDimService : public TNamed {
54   public:
55     AliHLTDimService();
56     AliHLTDimService(AliHLTDimServiceDataType type, const char* servicename);
57     
58     void Update(AliHLTDimServicePoint_t& sp);
59     AliHLTDimServiceDataType GetType() const {return fType;}
60     void* GetLocation() {return &fData.iVal;}
61     int GetId() const {return fId;}
62     int SetId(int id) {fId=id;return id;}
63
64   private:
65     AliHLTDimServicePoint_t fData; /// the data point
66     AliHLTDimServiceDataType fType; /// type of this service
67     int fId; /// id of the service
68   };
69
70   /** @class AliHLTDimServiceFloat
71    * DIM service for a float value
72    */
73   class AliHLTDimServiceFloat : public AliHLTDimService {
74   public:
75     AliHLTDimServiceFloat();
76     ~AliHLTDimServiceFloat();
77
78     void Update(float f) {
79       AliHLTDimServicePoint_t sp; sp.fVal=f; AliHLTDimService::Update(sp);
80     }
81   };
82
83   /**
84    * Register a service.
85    * @param pService    the service to be registered
86    * @ingroup rcu_ce_base_services
87    */
88   int RegisterService(AliHLTDimService* pService);
89
90   /**
91    * Create a service.
92    * @param type        type of the channel, see @ref ceServiceDataType
93    * @param name        unique name of the service
94    * @return dim service object, needs to be cleaned by the caller
95    * @ingroup rcu_ce_base_services
96    */
97   AliHLTDimService* CreateService(AliHLTDimServiceDataType type, const char* name);
98
99   /**
100    * Create a group of services.
101    * The names are built from the basename and the number of services.
102    * @param type        type of the channel
103    * @param basename    base name of the services, the name might contain a '%d' sequence which is then
104    *                    replaced by the number, number is appended if no '%d' provided
105    * @param count       number of services in this group, passed to the <i>update</i> and <i>set</i> function as parameter major
106    * @return            TObjArray of AliHLTDimService objects, the array needs to be cleaned by the caller
107    * @ingroup rcu_ce_base_services
108    */
109   TObjArray* CreateServiceGroup(AliHLTDimServiceDataType type, const char* basename, int count);
110
111   /// Update all services via the Dim channel
112   int UpdateServices();
113
114   /// init the server
115   /// load the dim library and function pointers
116   /// init dim (DNS and server name)
117   int Init(const char* dimNameServer);
118
119   /// Reset
120   int Reset();
121
122   /// start the server
123   int Start();
124
125   /// stop the server
126   int Stop();
127
128 protected:
129   enum AliHLTDimServerState_t {
130     // server is not started
131     kStateOff = 0,
132     // starting, will be changed by the server thread to kStateRunning
133     kStateStarting,
134     // server running
135     kStateRunning,
136     // set by the main thread and changed by the server thread before it terminates
137     kStateStopping,
138     // error
139     kStateError
140   };
141
142   int SetState(int state) {fState=state; return fState;}
143
144   int GetState() {return fState;}
145
146   typedef void (*fctVoid)();
147   typedef int (*fctDisServiceCallback)( const char*);
148   typedef int (*fctDisAddService)     ( const char* service, 
149                                         const char* type, 
150                                         void* buffer, 
151                                         int size, 
152                                         fctDisServiceCallback cb, 
153                                         long int tag);
154   typedef int (*fctDisRemoveService)  ( unsigned int id);
155   typedef int (*fctDisUpdateService)  ( unsigned int id);
156   typedef int (*fctDisCharArg)        ( const char*);
157   typedef int (*fctDisNoArg)          ( );
158
159   /** 
160    * @class AliHLTDimInterface
161    * Interface to the dim library
162    */
163   class AliHLTDimInterface : public AliHLTLogging {
164   public:
165     AliHLTDimInterface();
166     ~AliHLTDimInterface();
167
168     /// load the dim library and function pointers
169     int Init();
170
171     int DisAddService(const char* service, const char* type, void* buffer, 
172                       int size, fctDisServiceCallback cb, long int tag) {
173       if (fpDisAddService) return (*fpDisAddService)(service, type, buffer, size, cb, tag);
174       return -ENODEV;
175     }
176
177     int DisAddService(const char* service, const char* type, void* buffer, int size) {
178       if (fpDisAddService) return (*fpDisAddService)(service, type, buffer, size, NULL, 0);
179       return -ENODEV;
180     }
181
182     int DisRemoveService(unsigned int id) {
183       if (fpDisRemoveService) return (*fpDisRemoveService)(id);
184       return -ENODEV;
185     }
186
187     int DisUpdateService(unsigned int id) {
188       if (fpDisUpdateService) return (*fpDisUpdateService)(id);
189       return -ENODEV;
190     }
191
192     int DisStartServing(const char *server) {
193       if (fpDisStartServing) return (*fpDisStartServing)(server);
194       return -ENODEV;
195     }
196
197     int DisStopServing() {
198       if (fpDisStopServing) return (*fpDisStopServing)();
199       return -ENODEV;
200     }
201
202     int DisSetDnsNode(const char *server) {
203       if (fpDisSetDnsNode) return (*fpDisSetDnsNode)(server);
204       return -ENODEV;
205     }
206
207   private:
208     fctVoid FindSymbol(const char* library, const char* symbol) const;
209
210     fctDisAddService      fpDisAddService;      //! transient
211     fctDisRemoveService   fpDisRemoveService;   //! transient
212     fctDisUpdateService   fpDisUpdateService;   //! transient
213     fctDisCharArg         fpDisStartServing;    //! transient
214     fctDisNoArg           fpDisStopServing;     //! transient
215     fctDisCharArg         fpDisSetDnsNode;      //! transient
216     static const char*    fgkDimLibraryName  ;       //!
217     static const char*    fgkDisAddServiceSymbol;    //!
218     static const char*    fgkDisRemoveServiceSymbol;    //!
219     static const char*    fgkDisUpdateServiceSymbol; //!
220     static const char*    fgkDisStartServingSymbol;  //!
221     static const char*    fgkDisStopServingSymbol;  //!
222     static const char*    fgkDisSetDnsNodeSymbol;  //!
223   };
224
225   static AliHLTDimInterface* Interface();
226
227 private:
228   /// copy constructor not permitted
229   AliHLTDimServer(const AliHLTDimServer&);
230   /// assignment operator not permitted
231   AliHLTDimServer& operator=(const AliHLTDimServer&);
232
233   /// entry point for the thread, param is pointer to object
234   static void* ServerLoop(void* param);
235
236   /// the server loop
237   void* ServerLoop();
238
239   TObjArray fServices; //! list of services
240   int fState; //! state of the server
241   TThread* fpServerThread; //! thread
242   int fUpdatePeriod; //! update period for the DIM loop in ms
243
244   static AliHLTDimInterface* fgpInterface; //! the dim interface
245
246   ClassDef(AliHLTDimServer, 0)
247 };
248 #endif