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