]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/HOMER/AliHLTHOMERReader.h
added HOMER library from PubSub package HLT-stable-20070905.141318 (rev. 2375)
[u/mrichter/AliRoot.git] / HLT / BASE / HOMER / AliHLTHOMERReader.h
CommitLineData
2be16a33 1// XEMacs -*-C++-*-
2#ifndef _HOMER_H_
3#define _HOMER_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
28class MonitoringReader
29 {
30 public:
31
32 MonitoringReader() {};
33 virtual ~MonitoringReader() {};
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
82 ClassDef(MonitoringReader,1);
83#endif
84 };
85
86
87
88class HOMERReader: public MonitoringReader
89 {
90 public:
91#ifdef USE_ROOT
92 HOMERReader();
93#endif
94
95 /* Constructors & destructors, HOMER specific */
96 /* For reading from a TCP port */
97 HOMERReader( const char* hostname, unsigned short port );
98 /* For reading from multiple TCP ports */
99 HOMERReader( unsigned int tcpCnt, const char** hostnames, unsigned short* ports );
100 /* For reading from a System V shared memory segment */
101 HOMERReader( key_t shmKey, int shmSize );
102 /* For reading from multiple System V shared memory segments */
103 HOMERReader( unsigned int shmCnt, key_t* shmKey, int* shmSize );
104 /* For reading from multiple TCP ports and multiple System V shared memory segments */
105 HOMERReader( unsigned int tcpCnt, const char** hostnames, unsigned short* ports,
106 unsigned int shmCnt, key_t* shmKey, int* shmSize );
107 virtual ~HOMERReader();
108
109 /* Return the status of the connection as established by one of the constructors.
110 0 means connection is ok, non-zero specifies the type of error that occured. */
111 int GetConnectionStatus() const
112 {
113 return fConnectionStatus;
114 }
115
116 /* Return the index of the connection for which an error given by the above
117 function occured. */
118 unsigned int GetErrorConnectionNdx() const
119 {
120 return fErrorConnection;
121 }
122
123 void SetEventRequestAdvanceTime( unsigned long time_us )
124 {
125 fEventRequestAdvanceTime_us = time_us;
126 }
127
128 /* Defined in MonitoringReader */
129 /* Read in the next available event */
130 virtual int ReadNextEvent();
131 /* Read in the next available event */
132 virtual int ReadNextEvent( unsigned long timeout );
133
134 /* Return the type of the current event */
135 virtual homer_uint64 GetEventType() const
136 {
137 return fCurrentEventType;
138 }
139
140 /* Return the ID of the current event */
141 virtual homer_uint64 GetEventID() const
142 {
143 return fCurrentEventID;
144 }
145
146 /* Return the number of data blocks in the current event */
147 virtual unsigned long GetBlockCnt() const
148 {
149 return fBlockCnt;
150 }
151
152 /* Return the size (in bytes) of the current event's data
153 block with the given block index (starting at 0). */
154 virtual const void* GetBlockData( unsigned long ndx ) const;
155 /* Return a pointer to the start of the current event's data
156 block with the given block index (starting at 0). */
157 virtual unsigned long GetBlockDataLength( unsigned long ndx ) const;
158 /* Return IP address or hostname of node which sent the
159 current event's data block with the given block index
160 (starting at 0).
161 For HOMER this is the ID of the node on which the subscriber
162 that provided this data runs/ran. */
163 virtual const char* GetBlockSendNodeID( unsigned long ndx ) const;
164 /* Return byte order of the data stored in the
165 current event's data block with the given block
166 index (starting at 0).
167 0 is unknown alignment,
168 1 ist little endian,
169 2 is big endian. */
170 virtual homer_uint8 GetBlockByteOrder( unsigned long ndx ) const;
171 /* Return the alignment (in bytes) of the given datatype
172 in the data stored in the current event's data block
173 with the given block index (starting at 0).
174 Possible values for the data type are
175 0: homer_uint64
176 1: homer_uint32
177 2: uin16
178 3: homer_uint8
179 4: double
180 5: float
181 */
182 virtual homer_uint8 GetBlockTypeAlignment( unsigned long ndx, homer_uint8 dataType ) const;
183
184 virtual homer_uint64 GetBlockStatusFlags( unsigned long ndx ) const;
185
186 /* HOMER specific */
187 /* Return the type of the data in the current event's data
188 block with the given block index (starting at 0). */
189 homer_uint64 GetBlockDataType( unsigned long ndx ) const;
190 /* Return the origin of the data in the current event's data
191 block with the given block index (starting at 0). */
192 homer_uint32 GetBlockDataOrigin( unsigned long ndx ) const;
193 /* Return a specification of the data in the current event's data
194 block with the given block index (starting at 0). */
195 homer_uint32 GetBlockDataSpec( unsigned long ndx ) const;
196
197 /* Find the next data block in the current event with the given
198 data type, origin, and specification. Returns the block's
199 index. */
200 unsigned long FindBlockNdx( homer_uint64 type, homer_uint32 origin,
201 homer_uint32 spec, unsigned long startNdx=0 ) const;
202
203 /* Find the next data block in the current event with the given
204 data type, origin, and specification. Returns the block's
205 index. */
206 unsigned long FindBlockNdx( char type[8], char origin[4],
207 homer_uint32 spec, unsigned long startNdx=0 ) const;
208
209 /* Return the ID of the node that actually produced this data block.
210 This may be different from the node which sent the data to this
211 monitoring object as returned by GetBlockSendNodeID. */
212 const char* GetBlockCreateNodeID( unsigned long ndx ) const;
213
214 protected:
215
216 enum DataSourceType { kUndef=0, kTCP, kShm };
217 struct DataSource
218 {
219 DataSource() { fType = kUndef; };
220 DataSourceType fType;
221 unsigned fNdx; // This source's index
222 const char* fHostname; // Filled for both Shm and TCP
223 unsigned short fTCPPort;
224 key_t fShmKey;
225 int fShmSize;
226 int fTCPConnection; // File descriptor for the TCP connection
227 int fShmID; // ID of the shared memory area
228 void* fShmPtr; // Pointer to shared memory area
229 void* fData; // Pointer to data read in for current event from this source
230 unsigned long fDataSize; // Size of data (to be) read in for current event from this source
231 unsigned long fDataRead; // Data actually read for current event
232 };
233
234 void Init();
235
236 bool AllocDataSources( unsigned int sourceCnt );
237 int AddDataSource( const char* hostname, unsigned short port, DataSource& source );
238 int AddDataSource( key_t shmKey, int shmSize, DataSource& source );
239 void FreeDataSources();
240 int FreeShmDataSource( DataSource& source );
241 int FreeTCPDataSource( DataSource& source );
242 int ReadNextEvent( bool useTimeout, unsigned long timeout );
243 void ReleaseCurrentEvent();
244 int TriggerTCPSource( DataSource& source, bool useTimeout, unsigned long timeout );
245 int TriggerShmSource( DataSource& source, bool useTimeout, unsigned long timeout );
246 int ReadDataFromTCPSources( unsigned sourceCnt, DataSource* sources, bool useTimeout, unsigned long timeout );
247 int ReadDataFromShmSources( unsigned sourceCnt, DataSource* sources, bool useTimeout, unsigned long timeout );
248 int ParseSourceData( DataSource& source );
249 int ReAllocBlocks( unsigned long newCnt );
250 homer_uint64 GetSourceEventID( DataSource& source );
251 homer_uint64 GetSourceEventType( DataSource& source );
252 homer_uint64 Swap( homer_uint8 destFormat, homer_uint8 sourceFormat, homer_uint64 source )
253 {
254 if ( destFormat == sourceFormat )
255 return source;
256 else
257 return ((source & 0xFFULL) << 56) |
258 ((source & 0xFF00ULL) << 40) |
259 ((source & 0xFF0000ULL) << 24) |
260 ((source & 0xFF000000ULL) << 8) |
261 ((source & 0xFF00000000ULL) >> 8) |
262 ((source & 0xFF0000000000ULL) >> 24) |
263 ((source & 0xFF000000000000ULL) >> 40) |
264 ((source & 0xFF00000000000000ULL) >> 56);
265 }
266 homer_uint32 Swap( homer_uint8 destFormat, homer_uint8 sourceFormat, homer_uint32 source )
267 {
268 if ( destFormat == sourceFormat )
269 return source;
270 else
271 return ((source & 0xFFUL) << 24) |
272 ((source & 0xFF00UL) << 8) |
273 ((source & 0xFF0000UL) >> 8) |
274 ((source & 0xFF000000UL) >> 24);
275 }
276
277
278 struct DataBlock
279 {
280 unsigned int fSource; // Index of originating data source
281 void* fData;
282 unsigned long fLength;
283 homer_uint64* fMetaData; // Pointer to meta data describing data itself.
284 const char* fOriginatingNodeID;
285 };
286
287 homer_uint64 fCurrentEventType;
288 homer_uint64 fCurrentEventID;
289 unsigned long fBlockCnt;
290 unsigned long fMaxBlockCnt;
291 DataBlock* fBlocks;
292
293 unsigned int fDataSourceCnt;
294 unsigned int fTCPDataSourceCnt;
295 unsigned int fShmDataSourceCnt;
296 unsigned int fDataSourceMaxCnt;
297 DataSource* fDataSources;
298
299
300 int fConnectionStatus;
301 unsigned fErrorConnection;
302
303 unsigned long fEventRequestAdvanceTime_us;
304
305#ifdef USE_ROOT
306 ClassDef(HOMERReader,2);
307#endif
308 };
309
310#endif /* _HOMER_H_ */