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