Changes required to publish custom data structures over DIM.
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDimServer.h
CommitLineData
8ecd252f 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
c7bdcc06 11// @file AliHLTDimServer.h
12// @author Matthias Richter
13// @date 20010-03-10
14// @brief HLT DIM server implementation and dynamic access
15// to DIM library
16
8ecd252f 17#include "AliHLTLogging.h"
18#include "TNamed.h"
19#include "TObjArray.h"
20
21class TThread;
22
23/**
24 * @class AliHLTDimServer
25 * Implementation of a DIM server for HLT and the dynamic access to the
26 * DIM library.
27 */
28class AliHLTDimServer : public TNamed {
29public:
30 AliHLTDimServer();
31 AliHLTDimServer(const char* servername);
32 ~AliHLTDimServer();
33
34 /// Data type identifiers for services.
35 enum AliHLTDimServiceDataType{
36 kDataTypeUnknown = 0, /// initializer
01cd4452 37 kDataTypeCustom, /// Custom format maintained by the user.
8ecd252f 38 kDataTypeInt, /// Integer type
39 kDataTypeFloat, /// Float type
40 kDataTypeString, /// String type
41 };
42
43 /// The service data field.
44 struct AliHLTDimServicePoint_t {
45 union {
46 int iVal; /// integer value
47 float fVal; /// float value
48 void* strVal; /// string value, casted to string* before use
49 };
50 };
51
52 /** @class AliHLTDimService
53 * Base class for DIM services
54 */
55 class AliHLTDimService : public TNamed {
56 public:
57 AliHLTDimService();
01cd4452 58
59 /**
60 * Create a new service with a particular predefined type and name.
61 * \param type The type of the service
62 * \param servicename The name of the service.
63 */
8ecd252f 64 AliHLTDimService(AliHLTDimServiceDataType type, const char* servicename);
65
01cd4452 66 /**
67 * Create a new service with a particular custom type.
68 * \param type The type of the service as a string.
69 * The format parameter specifies the contents of the structure in the
70 * form T:N[;T:N]*[;T] where T is the item type: (I)nteger, (C)arachter,
71 * (L)ong, (S)hort, (F)loat, (D)ouble, X(tra long) and N is the number
72 * of such items. The type alone at the end means all following items
73 * are of the same type. Example: "I:3;F:2;C" means 3 Integers, 2 Floats
74 * and Characters until the end. The format parameter is used for
75 * communicating between different platforms.
76 * \param data Points to a buffer maintained by the user which stores the
77 * data to publish. This buffer must exist as long as the DIM service
78 * is registered and active.
79 * \param size The size of the data structure pointed to by data.
80 * \param servicename The name of the service.
81 */
82 AliHLTDimService(const char* type, void* data, int size, const char* servicename);
83
84 /**
85 * Updates the DIM data point for custom data structures.
86 * i.e. This method should be used if the service was created with:
87 * AliHLTDimService(const char* type, void* data, int size, const char* servicename)
88 */
89 void Update();
90
91 /**
92 * Updates the DIM data point.
93 * This method should be used if the service was created with:
94 * AliHLTDimService(AliHLTDimServiceDataType type, const char* servicename)
95 * \param sp The new data point to publish via DIM.
96 */
c7bdcc06 97 void Update(const AliHLTDimServicePoint_t& sp);
01cd4452 98
8ecd252f 99 AliHLTDimServiceDataType GetType() const {return fType;}
01cd4452 100 const char* GetTypeString() const { return fTypeString.Data(); }
101 void* GetLocation() {return fDataBuffer;}
8ecd252f 102 int GetId() const {return fId;}
103 int SetId(int id) {fId=id;return id;}
01cd4452 104 void* GetDataBuffer() const { return fDataBuffer; }
105 int GetDataSize() const { return fDataSize; }
8ecd252f 106
107 private:
01cd4452 108
109 // Do not allow copying of this class
110 AliHLTDimService(const AliHLTDimService&);
111 AliHLTDimService& operator = (const AliHLTDimService&);
112
8ecd252f 113 AliHLTDimServicePoint_t fData; /// the data point
114 AliHLTDimServiceDataType fType; /// type of this service
01cd4452 115 TString fTypeString; /// The string representing the service type.
116 void* fDataBuffer; /// Pointer to the data buffer.
117 int fDataSize; /// The size of the data buffer.
8ecd252f 118 int fId; /// id of the service
119 };
120
121 /** @class AliHLTDimServiceFloat
122 * DIM service for a float value
123 */
124 class AliHLTDimServiceFloat : public AliHLTDimService {
125 public:
126 AliHLTDimServiceFloat();
127 ~AliHLTDimServiceFloat();
128
129 void Update(float f) {
130 AliHLTDimServicePoint_t sp; sp.fVal=f; AliHLTDimService::Update(sp);
131 }
132 };
133
bdd7f6fd 134 /** @class AliHLTDimServiceInt
135 * DIM service for a int value
136 */
137 class AliHLTDimServiceInt : public AliHLTDimService {
138 public:
139 AliHLTDimServiceInt();
140 ~AliHLTDimServiceInt();
141
142 void Update(int i) {
143 AliHLTDimServicePoint_t sp; sp.iVal=i; AliHLTDimService::Update(sp);
144 }
145 };
146
8ecd252f 147 /**
148 * Register a service.
149 * @param pService the service to be registered
8ecd252f 150 */
151 int RegisterService(AliHLTDimService* pService);
152
153 /**
154 * Create a service.
155 * @param type type of the channel, see @ref ceServiceDataType
156 * @param name unique name of the service
157 * @return dim service object, needs to be cleaned by the caller
8ecd252f 158 */
159 AliHLTDimService* CreateService(AliHLTDimServiceDataType type, const char* name);
160
161 /**
162 * Create a group of services.
163 * The names are built from the basename and the number of services.
164 * @param type type of the channel
165 * @param basename base name of the services, the name might contain a '%d' sequence which is then
166 * replaced by the number, number is appended if no '%d' provided
167 * @param count number of services in this group, passed to the <i>update</i> and <i>set</i> function as parameter major
168 * @return TObjArray of AliHLTDimService objects, the array needs to be cleaned by the caller
8ecd252f 169 */
170 TObjArray* CreateServiceGroup(AliHLTDimServiceDataType type, const char* basename, int count);
171
172 /// Update all services via the Dim channel
173 int UpdateServices();
174
175 /// init the server
176 /// load the dim library and function pointers
177 /// init dim (DNS and server name)
178 int Init(const char* dimNameServer);
179
180 /// Reset
181 int Reset();
182
183 /// start the server
184 int Start();
185
186 /// stop the server
187 int Stop();
188
189protected:
190 enum AliHLTDimServerState_t {
191 // server is not started
192 kStateOff = 0,
193 // starting, will be changed by the server thread to kStateRunning
194 kStateStarting,
195 // server running
196 kStateRunning,
197 // set by the main thread and changed by the server thread before it terminates
198 kStateStopping,
199 // error
200 kStateError
201 };
202
203 int SetState(int state) {fState=state; return fState;}
204
c7bdcc06 205 int GetState() const {return fState;}
8ecd252f 206
207 typedef void (*fctVoid)();
208 typedef int (*fctDisServiceCallback)( const char*);
209 typedef int (*fctDisAddService) ( const char* service,
210 const char* type,
211 void* buffer,
212 int size,
213 fctDisServiceCallback cb,
214 long int tag);
215 typedef int (*fctDisRemoveService) ( unsigned int id);
216 typedef int (*fctDisUpdateService) ( unsigned int id);
217 typedef int (*fctDisCharArg) ( const char*);
218 typedef int (*fctDisNoArg) ( );
219
030da833 220 /**
221 * @class AliHLTDimInterface
222 * Interface to the dim library
223 */
8ecd252f 224 class AliHLTDimInterface : public AliHLTLogging {
225 public:
226 AliHLTDimInterface();
227 ~AliHLTDimInterface();
228
229 /// load the dim library and function pointers
230 int Init();
231
232 int DisAddService(const char* service, const char* type, void* buffer,
c7bdcc06 233 int size, fctDisServiceCallback cb, long int tag) const {
8ecd252f 234 if (fpDisAddService) return (*fpDisAddService)(service, type, buffer, size, cb, tag);
235 return -ENODEV;
236 }
237
c7bdcc06 238 int DisAddService(const char* service, const char* type, void* buffer, int size) const {
8ecd252f 239 if (fpDisAddService) return (*fpDisAddService)(service, type, buffer, size, NULL, 0);
240 return -ENODEV;
241 }
242
c7bdcc06 243 int DisRemoveService(unsigned int id) const {
8ecd252f 244 if (fpDisRemoveService) return (*fpDisRemoveService)(id);
245 return -ENODEV;
246 }
247
c7bdcc06 248 int DisUpdateService(unsigned int id) const {
8ecd252f 249 if (fpDisUpdateService) return (*fpDisUpdateService)(id);
250 return -ENODEV;
251 }
252
c7bdcc06 253 int DisStartServing(const char *server) const {
8ecd252f 254 if (fpDisStartServing) return (*fpDisStartServing)(server);
255 return -ENODEV;
256 }
257
c7bdcc06 258 int DisStopServing() const {
8ecd252f 259 if (fpDisStopServing) return (*fpDisStopServing)();
260 return -ENODEV;
261 }
262
c7bdcc06 263 int DisSetDnsNode(const char *server) const {
8ecd252f 264 if (fpDisSetDnsNode) return (*fpDisSetDnsNode)(server);
265 return -ENODEV;
266 }
267
268 private:
269 fctVoid FindSymbol(const char* library, const char* symbol) const;
270
271 fctDisAddService fpDisAddService; //! transient
272 fctDisRemoveService fpDisRemoveService; //! transient
273 fctDisUpdateService fpDisUpdateService; //! transient
274 fctDisCharArg fpDisStartServing; //! transient
275 fctDisNoArg fpDisStopServing; //! transient
276 fctDisCharArg fpDisSetDnsNode; //! transient
277 static const char* fgkDimLibraryName ; //!
278 static const char* fgkDisAddServiceSymbol; //!
279 static const char* fgkDisRemoveServiceSymbol; //!
280 static const char* fgkDisUpdateServiceSymbol; //!
281 static const char* fgkDisStartServingSymbol; //!
282 static const char* fgkDisStopServingSymbol; //!
283 static const char* fgkDisSetDnsNodeSymbol; //!
284 };
285
286 static AliHLTDimInterface* Interface();
287
288private:
289 /// copy constructor not permitted
290 AliHLTDimServer(const AliHLTDimServer&);
291 /// assignment operator not permitted
292 AliHLTDimServer& operator=(const AliHLTDimServer&);
293
294 /// entry point for the thread, param is pointer to object
295 static void* ServerLoop(void* param);
296
297 /// the server loop
298 void* ServerLoop();
299
300 TObjArray fServices; //! list of services
301 int fState; //! state of the server
302 TThread* fpServerThread; //! thread
303 int fUpdatePeriod; //! update period for the DIM loop in ms
304
305 static AliHLTDimInterface* fgpInterface; //! the dim interface
306
307 ClassDef(AliHLTDimServer, 0)
308};
309#endif