]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTDimServer.h
- start DIS serving added to server loop
[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   /** @class AliHLTDimServiceInt
84    * DIM service for a int value
85    */
86   class AliHLTDimServiceInt : public AliHLTDimService {
87   public:
88     AliHLTDimServiceInt();
89     ~AliHLTDimServiceInt();
90
91     void Update(int i) {
92       AliHLTDimServicePoint_t sp; sp.iVal=i; AliHLTDimService::Update(sp);
93     }
94   };
95
96   /**
97    * Register a service.
98    * @param pService    the service to be registered
99    * @ingroup rcu_ce_base_services
100    */
101   int RegisterService(AliHLTDimService* pService);
102
103   /**
104    * Create a service.
105    * @param type        type of the channel, see @ref ceServiceDataType
106    * @param name        unique name of the service
107    * @return dim service object, needs to be cleaned by the caller
108    * @ingroup rcu_ce_base_services
109    */
110   AliHLTDimService* CreateService(AliHLTDimServiceDataType type, const char* name);
111
112   /**
113    * Create a group of services.
114    * The names are built from the basename and the number of services.
115    * @param type        type of the channel
116    * @param basename    base name of the services, the name might contain a '%d' sequence which is then
117    *                    replaced by the number, number is appended if no '%d' provided
118    * @param count       number of services in this group, passed to the <i>update</i> and <i>set</i> function as parameter major
119    * @return            TObjArray of AliHLTDimService objects, the array needs to be cleaned by the caller
120    * @ingroup rcu_ce_base_services
121    */
122   TObjArray* CreateServiceGroup(AliHLTDimServiceDataType type, const char* basename, int count);
123
124   /// Update all services via the Dim channel
125   int UpdateServices();
126
127   /// init the server
128   /// load the dim library and function pointers
129   /// init dim (DNS and server name)
130   int Init(const char* dimNameServer);
131
132   /// Reset
133   int Reset();
134
135   /// start the server
136   int Start();
137
138   /// stop the server
139   int Stop();
140
141 protected:
142   enum AliHLTDimServerState_t {
143     // server is not started
144     kStateOff = 0,
145     // starting, will be changed by the server thread to kStateRunning
146     kStateStarting,
147     // server running
148     kStateRunning,
149     // set by the main thread and changed by the server thread before it terminates
150     kStateStopping,
151     // error
152     kStateError
153   };
154
155   int SetState(int state) {fState=state; return fState;}
156
157   int GetState() {return fState;}
158
159   typedef void (*fctVoid)();
160   typedef int (*fctDisServiceCallback)( const char*);
161   typedef int (*fctDisAddService)     ( const char* service, 
162                                         const char* type, 
163                                         void* buffer, 
164                                         int size, 
165                                         fctDisServiceCallback cb, 
166                                         long int tag);
167   typedef int (*fctDisRemoveService)  ( unsigned int id);
168   typedef int (*fctDisUpdateService)  ( unsigned int id);
169   typedef int (*fctDisCharArg)        ( const char*);
170   typedef int (*fctDisNoArg)          ( );
171
172   /** 
173    * @class AliHLTDimInterface
174    * Interface to the dim library
175    */
176   class AliHLTDimInterface : public AliHLTLogging {
177   public:
178     AliHLTDimInterface();
179     ~AliHLTDimInterface();
180
181     /// load the dim library and function pointers
182     int Init();
183
184     int DisAddService(const char* service, const char* type, void* buffer, 
185                       int size, fctDisServiceCallback cb, long int tag) {
186       if (fpDisAddService) return (*fpDisAddService)(service, type, buffer, size, cb, tag);
187       return -ENODEV;
188     }
189
190     int DisAddService(const char* service, const char* type, void* buffer, int size) {
191       if (fpDisAddService) return (*fpDisAddService)(service, type, buffer, size, NULL, 0);
192       return -ENODEV;
193     }
194
195     int DisRemoveService(unsigned int id) {
196       if (fpDisRemoveService) return (*fpDisRemoveService)(id);
197       return -ENODEV;
198     }
199
200     int DisUpdateService(unsigned int id) {
201       if (fpDisUpdateService) return (*fpDisUpdateService)(id);
202       return -ENODEV;
203     }
204
205     int DisStartServing(const char *server) {
206       if (fpDisStartServing) return (*fpDisStartServing)(server);
207       return -ENODEV;
208     }
209
210     int DisStopServing() {
211       if (fpDisStopServing) return (*fpDisStopServing)();
212       return -ENODEV;
213     }
214
215     int DisSetDnsNode(const char *server) {
216       if (fpDisSetDnsNode) return (*fpDisSetDnsNode)(server);
217       return -ENODEV;
218     }
219
220   private:
221     fctVoid FindSymbol(const char* library, const char* symbol) const;
222
223     fctDisAddService      fpDisAddService;      //! transient
224     fctDisRemoveService   fpDisRemoveService;   //! transient
225     fctDisUpdateService   fpDisUpdateService;   //! transient
226     fctDisCharArg         fpDisStartServing;    //! transient
227     fctDisNoArg           fpDisStopServing;     //! transient
228     fctDisCharArg         fpDisSetDnsNode;      //! transient
229     static const char*    fgkDimLibraryName  ;       //!
230     static const char*    fgkDisAddServiceSymbol;    //!
231     static const char*    fgkDisRemoveServiceSymbol;    //!
232     static const char*    fgkDisUpdateServiceSymbol; //!
233     static const char*    fgkDisStartServingSymbol;  //!
234     static const char*    fgkDisStopServingSymbol;  //!
235     static const char*    fgkDisSetDnsNodeSymbol;  //!
236   };
237
238   static AliHLTDimInterface* Interface();
239
240 private:
241   /// copy constructor not permitted
242   AliHLTDimServer(const AliHLTDimServer&);
243   /// assignment operator not permitted
244   AliHLTDimServer& operator=(const AliHLTDimServer&);
245
246   /// entry point for the thread, param is pointer to object
247   static void* ServerLoop(void* param);
248
249   /// the server loop
250   void* ServerLoop();
251
252   TObjArray fServices; //! list of services
253   int fState; //! state of the server
254   TThread* fpServerThread; //! thread
255   int fUpdatePeriod; //! update period for the DIM loop in ms
256
257   static AliHLTDimInterface* fgpInterface; //! the dim interface
258
259   ClassDef(AliHLTDimServer, 0)
260 };
261 #endif