]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/HOMER/AliHLTHOMERReader.h
coding conventions
[u/mrichter/AliRoot.git] / HLT / BASE / HOMER / AliHLTHOMERReader.h
1 // XEMacs -*-C++-*-
2 #ifndef AliHLTHOMERREADER_H
3 #define AliHLTHOMERREADER_H
4 /* This file is property of and copyright by the ALICE HLT Project        * 
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  * See cxx source for full Copyright notice                               */
7
8 /** @file   AliHLTHOMERReader.h
9     @author Timm Steinbeck
10     @date   Sep 14 2007
11     @brief  HLT Online Monitoring Environment including ROOT - Reader
12     @note   migrated from PubSub HLT-stable-20070905.141318 (rev 2375)    */
13
14 // see below for class documentation
15 // or
16 // refer to README to build package
17 // or
18 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
19
20 #include <limits.h>
21 #include <sys/ipc.h>
22 #include <sys/shm.h>
23 #include "AliHLTHOMERData.h"
24
25
26
27
28 /**
29  * @class AliHLTMonitoringReader
30  * The class provides a virtual interface for the HOMER reader.
31  * Used for dynamic generation of HOMER readers and dynamic loading of
32  * the libAliHLTHOMER library.
33  * @see AliHLTHOMERLibManager
34  */
35 class AliHLTMonitoringReader
36     {
37     public:
38
39         AliHLTMonitoringReader() {};
40         virtual ~AliHLTMonitoringReader() {};
41         
42         /* Read in the next available event */
43         virtual int ReadNextEvent() = 0;
44         /* Read in the next available event, wait max. timeout microsecs. */
45         virtual int ReadNextEvent( unsigned long timeout ) = 0;
46         
47         /* Return the type of the current event */
48         virtual homer_uint64 GetEventType() const = 0;
49
50         /* Return the ID of the current event */
51         virtual homer_uint64 GetEventID() const = 0;
52         
53         /* Return the number of data blocks in the current event */
54         virtual unsigned long GetBlockCnt() const = 0;
55         
56         /* Return the size (in bytes) of the current event's data
57            block with the given block index (starting at 0). */
58         virtual unsigned long GetBlockDataLength( unsigned long ndx ) const = 0;
59         /* Return a pointer to the start of the current event's data
60            block with the given block index (starting at 0). */
61         virtual const void* GetBlockData( unsigned long ndx ) const = 0;
62         /* Return IP address or hostname of node which sent the 
63            current event's data block with the given block index 
64            (starting at 0). */
65         virtual const char* GetBlockSendNodeID( unsigned long ndx ) const = 0;
66         /* Return byte order of the data stored in the 
67            current event's data block with the given block 
68            index (starting at 0). 
69            0 is unknown alignment, 
70            1 ist little endian, 
71            2 is big endian. */
72         virtual homer_uint8 GetBlockByteOrder( unsigned long ndx ) const = 0;
73         /* Return the alignment (in bytes) of the given datatype 
74            in the data stored in the current event's data block
75            with the given block index (starting at 0). 
76            Possible values for the data type are
77            0: homer_uint64
78            1: homer_uint32
79            2: uin16
80            3: homer_uint8
81            4: double
82            5: float
83         */
84         virtual homer_uint8 GetBlockTypeAlignment( unsigned long ndx, homer_uint8 dataType ) const = 0;
85
86         virtual homer_uint64 GetBlockStatusFlags( unsigned long ndx ) const = 0;
87
88         /* Return the type of the data in the current event's data
89            block with the given block index (starting at 0). */
90         virtual homer_uint64 GetBlockDataType( unsigned long ndx ) const = 0;
91         /* Return the origin of the data in the current event's data
92            block with the given block index (starting at 0). */
93         virtual homer_uint32 GetBlockDataOrigin( unsigned long ndx ) const = 0;
94         /* Return a specification of the data in the current event's data
95            block with the given block index (starting at 0). */
96         virtual homer_uint32 GetBlockDataSpec( unsigned long ndx ) const = 0;
97
98         /* Find the next data block in the current event with the given
99            data type, origin, and specification. Returns the block's 
100            index. */
101         virtual unsigned long FindBlockNdx( homer_uint64 type, homer_uint32 origin, 
102                                     homer_uint32 spec, unsigned long startNdx=0 ) const = 0;
103
104         /* Find the next data block in the current event with the given
105            data type, origin, and specification. Returns the block's 
106            index. */
107         virtual unsigned long FindBlockNdx( char type[8], char origin[4], 
108                                     homer_uint32 spec, unsigned long startNdx=0 ) const = 0;
109 #ifdef USE_ROOT
110         ClassDef(AliHLTMonitoringReader,1);
111 #endif
112     };
113
114
115
116 class AliHLTHOMERReader: public AliHLTMonitoringReader
117     {
118     public:
119 #ifdef USE_ROOT
120         AliHLTHOMERReader();
121 #endif
122
123         /* Constructors & destructors, HOMER specific */
124         /* For reading from a TCP port */
125         AliHLTHOMERReader( const char* hostname, unsigned short port );
126         /* For reading from multiple TCP ports */
127         AliHLTHOMERReader( unsigned int tcpCnt, const char** hostnames, unsigned short* ports );
128         /* For reading from a System V shared memory segment */
129         AliHLTHOMERReader( key_t shmKey, int shmSize );
130         /* For reading from multiple System V shared memory segments */
131         AliHLTHOMERReader( unsigned int shmCnt, key_t* shmKey, int* shmSize );
132         /* For reading from multiple TCP ports and multiple System V shared memory segments */
133         AliHLTHOMERReader( unsigned int tcpCnt, const char** hostnames, unsigned short* ports, 
134                      unsigned int shmCnt, key_t* shmKey, int* shmSize );
135         /* For reading from a buffer */
136         AliHLTHOMERReader( const void* pBuffer, int size );
137         virtual ~AliHLTHOMERReader();
138
139         /* Return the status of the connection as established by one of the constructors.
140            0 means connection is ok, non-zero specifies the type of error that occured. */
141         int GetConnectionStatus() const
142                 {
143                 return fConnectionStatus;
144                 }
145
146         /* Return the index of the connection for which an error given by the above
147            function occured. */
148         unsigned int GetErrorConnectionNdx() const
149                 {
150                 return fErrorConnection;
151                 }
152
153         void SetEventRequestAdvanceTime( unsigned long time )
154                 {
155                 // advance time in us
156                 fEventRequestAdvanceTime = time;
157                 }
158
159         /* Defined in AliHLTMonitoringReader */
160         /* Read in the next available event */
161         virtual int  ReadNextEvent();
162         /* Read in the next available event */
163         virtual int ReadNextEvent( unsigned long timeout );
164
165         /* Return the type of the current event */
166         virtual homer_uint64 GetEventType() const
167                 {
168                 return fCurrentEventType;
169                 }
170
171         /* Return the ID of the current event */
172         virtual homer_uint64 GetEventID() const
173                 {
174                 return fCurrentEventID;
175                 }
176
177         /* Return the number of data blocks in the current event */
178         virtual unsigned long GetBlockCnt() const
179                 {
180                 return fBlockCnt;
181                 }
182
183         /* Return the size (in bytes) of the current event's data
184            block with the given block index (starting at 0). */
185         virtual const void* GetBlockData( unsigned long ndx ) const;
186         /* Return a pointer to the start of the current event's data
187            block with the given block index (starting at 0). */
188         virtual unsigned long GetBlockDataLength( unsigned long ndx ) const;
189         /* Return IP address or hostname of node which sent the 
190            current event's data block with the given block index 
191            (starting at 0).
192            For HOMER this is the ID of the node on which the subscriber 
193            that provided this data runs/ran. */
194         virtual const char* GetBlockSendNodeID( unsigned long ndx ) const;
195         /* Return byte order of the data stored in the 
196            current event's data block with the given block 
197            index (starting at 0). 
198            0 is unknown alignment, 
199            1 ist little endian, 
200            2 is big endian. */
201         virtual homer_uint8 GetBlockByteOrder( unsigned long ndx ) const;
202         /* Return the alignment (in bytes) of the given datatype 
203            in the data stored in the current event's data block
204            with the given block index (starting at 0). 
205            Possible values for the data type are
206            0: homer_uint64
207            1: homer_uint32
208            2: uin16
209            3: homer_uint8
210            4: double
211            5: float
212         */
213         virtual homer_uint8 GetBlockTypeAlignment( unsigned long ndx, homer_uint8 dataType ) const;
214
215         virtual homer_uint64 GetBlockStatusFlags( unsigned long ndx ) const;
216
217         /* HOMER specific */
218         /* Return the type of the data in the current event's data
219            block with the given block index (starting at 0). */
220         homer_uint64 GetBlockDataType( unsigned long ndx ) const;
221         /* Return the origin of the data in the current event's data
222            block with the given block index (starting at 0). */
223         homer_uint32 GetBlockDataOrigin( unsigned long ndx ) const;
224         /* Return a specification of the data in the current event's data
225            block with the given block index (starting at 0). */
226         homer_uint32 GetBlockDataSpec( unsigned long ndx ) const;
227
228         /* Find the next data block in the current event with the given
229            data type, origin, and specification. Returns the block's 
230            index. */
231         unsigned long FindBlockNdx( homer_uint64 type, homer_uint32 origin, 
232                                     homer_uint32 spec, unsigned long startNdx=0 ) const;
233
234         /* Find the next data block in the current event with the given
235            data type, origin, and specification. Returns the block's 
236            index. */
237         unsigned long FindBlockNdx( char type[8], char origin[4], 
238                                     homer_uint32 spec, unsigned long startNdx=0 ) const;
239         
240         /* Return the ID of the node that actually produced this data block.
241            This may be different from the node which sent the data to this
242            monitoring object as returned by GetBlockSendNodeID. */
243         const char* GetBlockCreateNodeID( unsigned long ndx ) const;
244
245     protected:
246
247       enum DataSourceType { kUndef=0, kTCP, kShm, kBuf};
248         struct DataSource
249             {
250                 DataSourceType fType; // source type (TCP or Shm)
251                 unsigned fNdx; // This source's index
252                 const char* fHostname; // Filled for both Shm and TCP
253                 unsigned short fTCPPort; // port if type TCP
254                 key_t fShmKey; // shm key if type Shm
255                 int fShmSize; // shm size if type Shm
256                 int fTCPConnection; // File descriptor for the TCP connection
257                 int fShmID; // ID of the shared memory area
258                 void* fShmPtr; // Pointer to shared memory area
259                 void* fData; // Pointer to data read in for current event from this source
260                 unsigned long fDataSize; // Size of data (to be) read in for current event from this source
261                 unsigned long fDataRead; // Data actually read for current event
262             };
263
264         void Init();
265         
266         bool AllocDataSources( unsigned int sourceCnt );
267         int AddDataSource( const char* hostname, unsigned short port, DataSource& source );
268         int AddDataSource( key_t shmKey, int shmSize, DataSource& source );
269         int AddDataSource( void* pBuffer, int size, DataSource& source );
270         void FreeDataSources();
271         int FreeShmDataSource( DataSource& source );
272         int FreeTCPDataSource( DataSource& source );
273         int ReadNextEvent( bool useTimeout, unsigned long timeout );
274         void ReleaseCurrentEvent();
275         int TriggerTCPSource( DataSource& source, bool useTimeout, unsigned long timeout );
276         int TriggerShmSource( DataSource& source, bool useTimeout, unsigned long timeout ) const;
277         int ReadDataFromTCPSources( unsigned sourceCnt, DataSource* sources, bool useTimeout, unsigned long timeout );
278         int ReadDataFromShmSources( unsigned sourceCnt, DataSource* sources, bool useTimeout, unsigned long timeout );
279         int ParseSourceData( DataSource& source );
280         int ReAllocBlocks( unsigned long newCnt );
281         homer_uint64 GetSourceEventID( DataSource& source );
282         homer_uint64 GetSourceEventType( DataSource& source );
283         homer_uint64 Swap( homer_uint8 destFormat, homer_uint8 sourceFormat, homer_uint64 source ) const;
284
285         homer_uint32 Swap( homer_uint8 destFormat, homer_uint8 sourceFormat, homer_uint32 source ) const;
286
287
288         struct DataBlock
289             {
290                 unsigned int fSource; // Index of originating data source
291                 void* fData; // pointer to data
292                 unsigned long fLength; // buffer length
293                 homer_uint64* fMetaData; // Pointer to meta data describing data itself.
294                 const char* fOriginatingNodeID; // node id from which the data originates
295             };
296
297         /** type of the current event */
298         homer_uint64 fCurrentEventType;                             //!transient
299         /** ID of the current event */
300         homer_uint64 fCurrentEventID;                               //!transient
301         /** no of blocks currently used */
302         unsigned long fBlockCnt;                                    //!transient
303         /** available space in the block array */
304         unsigned long fMaxBlockCnt;                                 //!transient
305         /** block array */
306         DataBlock* fBlocks;                                         //!transient
307                 
308         /** total no of data sources */
309         unsigned int fDataSourceCnt;                                //!transient
310         /** no of TCP data sources */
311         unsigned int fTCPDataSourceCnt;                             //!transient
312         /** no of Shm data sources */
313         unsigned int fShmDataSourceCnt;                             //!transient
314         /** available space in the sources array */
315         unsigned int fDataSourceMaxCnt;                             //!transient
316         /** array of data source descriptions */
317         DataSource* fDataSources;                                   //!transient
318         
319         /** status of the connection */
320         int fConnectionStatus;                                      //!transient
321         /** flag an error for */
322         unsigned fErrorConnection;                                  //!transient
323         
324         /** */
325         unsigned long fEventRequestAdvanceTime;                     //!transient
326     private:
327         /** copy constructor prohibited */
328         AliHLTHOMERReader(const AliHLTHOMERReader&);
329         /** assignment operator prohibited */
330         AliHLTHOMERReader& operator=(const AliHLTHOMERReader&);
331         
332 #ifdef USE_ROOT
333         ClassDef(AliHLTHOMERReader,2);
334 #endif
335     };
336
337 /** defined for backward compatibility */
338 typedef AliHLTMonitoringReader MonitoringReader;
339 /** defined for backward compatibility */
340 typedef AliHLTHOMERReader HOMERReader;
341
342 // external interface of the HOMER reader
343 #define ALIHLTHOMERREADER_CREATE_FROM_TCPPORT  "AliHLTHOMERReaderCreateFromTCPPort"
344 #define ALIHLTHOMERREADER_CREATE_FROM_TCPPORTS "AliHLTHOMERReaderCreateFromTCPPorts"
345 #define ALIHLTHOMERREADER_CREATE_FROM_BUFFER   "AliHLTHOMERReaderCreateFromBuffer"
346 #define ALIHLTHOMERREADER_DELETE               "AliHLTHOMERReaderDelete"
347
348 #ifdef __cplusplus
349 extern "C" {
350 #endif
351
352   typedef AliHLTHOMERReader* (*AliHLTHOMERReaderCreateFromTCPPort_t)(const char* hostname, unsigned short port );
353   typedef AliHLTHOMERReader* (*AliHLTHOMERReaderCreateFromTCPPorts_t)(unsigned int tcpCnt, const char** hostnames, unsigned short* ports);
354   typedef AliHLTHOMERReader* (*AliHLTHOMERReaderCreateFromBuffer_t)(const void* pBuffer, int size);
355   typedef void (*AliHLTHOMERReaderDelete_t)(AliHLTHOMERReader* pInstance);
356
357   /**
358    * Create instance of HOMER reader working on a TCP port.
359    */
360   AliHLTHOMERReader* AliHLTHOMERReaderCreateFromTCPPort(const char* hostname, unsigned short port );
361   
362   /**
363    * Create instance of HOMER reader working on multiple TCP ports.
364    */
365   AliHLTHOMERReader* AliHLTHOMERReaderCreateFromTCPPorts(unsigned int tcpCnt, const char** hostnames, unsigned short* ports);
366
367   /**
368    * Create instance of HOMER reader working on buffer.
369    */
370   AliHLTHOMERReader* AliHLTHOMERReaderCreateFromBuffer(const void* pBuffer, int size);
371
372   /**
373    * Delete instance of HOMER reader.
374    */
375   void AliHLTHOMERReaderDelete(AliHLTHOMERReader* pInstance);
376 #ifdef __cplusplus
377 }
378 #endif
379
380 #endif /* AliHLTHOMERREADER_H */