Line data Source code
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 : #include <TObject.h>
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 0 : class AliHLTMonitoringReader
38 : {
39 : public:
40 :
41 0 : AliHLTMonitoringReader() {};
42 0 : 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 126 : ClassDef(AliHLTMonitoringReader,1);
121 : #endif
122 : };
123 :
124 :
125 :
126 : class AliHLTHOMERReader: public AliHLTMonitoringReader, public TObject
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, const 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, const key_t* shmKey, const int* shmSize );
142 : /* For reading from multiple TCP ports and multiple System V shared memory segments */
143 : AliHLTHOMERReader( unsigned int tcpCnt, const char** hostnames, const unsigned short* ports,
144 : unsigned int shmCnt, const key_t* shmKey, const 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 0 : 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 0 : return fErrorConnection;
161 : }
162 :
163 : void SetEventRequestAdvanceTime( unsigned long time )
164 : {
165 : // advance time in us
166 0 : fEventRequestAdvanceTime = time;
167 0 : }
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 0 : return fCurrentEventType;
179 : }
180 :
181 : /** Return the ID of the current event */
182 : virtual homer_uint64 GetEventID() const
183 : {
184 0 : return fCurrentEventID;
185 : }
186 :
187 : /** Return the number of data blocks in the current event */
188 : virtual unsigned long GetBlockCnt() const
189 : {
190 0 : return fBlockCnt;
191 : }
192 :
193 : /** Return a pointer to the start 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 the size (in bytes) 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 : /** Return the time stamp of when the data block was created.
239 : This is a UNIX time stamp in seconds.
240 : \param ndx The index of the block (starting at 0). */
241 : homer_uint64 GetBlockBirthSeconds( unsigned long ndx ) const;
242 :
243 : /** Return the micro seconds part of the time stamp of when the data
244 : block was created.
245 : \param ndx The index of the block (starting at 0). */
246 : homer_uint64 GetBlockBirthMicroSeconds( unsigned long ndx ) const;
247 :
248 : /** Find the next data block in the current event with the given
249 : data type, origin, and specification. Returns the block's
250 : index. */
251 : unsigned long FindBlockNdx( homer_uint64 type, homer_uint32 origin,
252 : homer_uint32 spec, unsigned long startNdx=0 ) const;
253 :
254 : /** Find the next data block in the current event with the given
255 : data type, origin, and specification. Returns the block's
256 : index. */
257 : unsigned long FindBlockNdx( char type[8], char origin[4],
258 : homer_uint32 spec, unsigned long startNdx=0 ) const;
259 :
260 : /** Return the ID of the node that actually produced this data block.
261 : This may be different from the node which sent the data to this
262 : monitoring object as returned by GetBlockSendNodeID. */
263 : const char* GetBlockCreateNodeID( unsigned long ndx ) const;
264 :
265 : protected:
266 :
267 : enum DataSourceType { kUndef=0, kTCP, kShm, kBuf};
268 : struct DataSource
269 : {
270 : DataSourceType fType; // source type (TCP or Shm)
271 : unsigned fNdx; // This source's index
272 : const char* fHostname; // Filled for both Shm and TCP
273 : unsigned short fTCPPort; // port if type TCP
274 : key_t fShmKey; // shm key if type Shm
275 : int fShmSize; // shm size if type Shm
276 : int fTCPConnection; // File descriptor for the TCP connection
277 : int fShmID; // ID of the shared memory area
278 : void* fShmPtr; // Pointer to shared memory area
279 : void* fData; // Pointer to data read in for current event from this source
280 : unsigned long fDataSize; // Size of data (to be) read in for current event from this source
281 : unsigned long fDataRead; // Data actually read for current event
282 : };
283 :
284 : void Init();
285 :
286 : bool AllocDataSources( unsigned int sourceCnt );
287 : int AddDataSource( const char* hostname, unsigned short port, DataSource& source );
288 : int AddDataSource( key_t shmKey, int shmSize, DataSource& source );
289 : int AddDataSource( void* pBuffer, int size, DataSource& source );
290 : void FreeDataSources();
291 : int FreeShmDataSource( DataSource& source );
292 : int FreeTCPDataSource( DataSource& source );
293 : int ReadNextEvent( bool useTimeout, unsigned long timeout );
294 : void ReleaseCurrentEvent();
295 : int TriggerTCPSource( DataSource& source, bool useTimeout, unsigned long timeout );
296 : int TriggerShmSource( DataSource& source, bool useTimeout, unsigned long timeout ) const;
297 : int ReadDataFromTCPSources( unsigned sourceCnt, DataSource* sources, bool useTimeout, unsigned long timeout );
298 : int ReadDataFromShmSources( unsigned sourceCnt, DataSource* sources, bool useTimeout, unsigned long timeout );
299 : int ParseSourceData( const DataSource& source );
300 : int ReAllocBlocks( unsigned long newCnt );
301 : homer_uint64 GetSourceEventID( const DataSource& source );
302 : homer_uint64 GetSourceEventType( const DataSource& source );
303 : homer_uint64 Swap( homer_uint8 destFormat, homer_uint8 sourceFormat, homer_uint64 source ) const;
304 :
305 : homer_uint32 Swap( homer_uint8 destFormat, homer_uint8 sourceFormat, homer_uint32 source ) const;
306 :
307 :
308 : struct DataBlock
309 : {
310 : unsigned int fSource; // Index of originating data source
311 : void* fData; // pointer to data
312 : unsigned long fLength; // buffer length
313 : homer_uint64* fMetaData; // Pointer to meta data describing data itself.
314 : const char* fOriginatingNodeID; // node id from which the data originates
315 : };
316 :
317 : /** type of the current event */
318 : homer_uint64 fCurrentEventType; //!transient
319 : /** ID of the current event */
320 : homer_uint64 fCurrentEventID; //!transient
321 : /** no of blocks currently used */
322 : unsigned long fBlockCnt; //!transient
323 : /** available space in the block array */
324 : unsigned long fMaxBlockCnt; //!transient
325 : /** block array */
326 : DataBlock* fBlocks; //!transient
327 :
328 : /** total no of data sources */
329 : unsigned int fDataSourceCnt; //!transient
330 : /** no of TCP data sources */
331 : unsigned int fTCPDataSourceCnt; //!transient
332 : /** no of Shm data sources */
333 : unsigned int fShmDataSourceCnt; //!transient
334 : /** available space in the sources array */
335 : unsigned int fDataSourceMaxCnt; //!transient
336 : /** array of data source descriptions */
337 : DataSource* fDataSources; //!transient
338 :
339 : /** status of the connection */
340 : int fConnectionStatus; //!transient
341 : /** flag an error for */
342 : unsigned fErrorConnection; //!transient
343 :
344 : /** */
345 : unsigned long fEventRequestAdvanceTime; //!transient
346 : private:
347 : /** copy constructor prohibited */
348 : AliHLTHOMERReader(const AliHLTHOMERReader&);
349 : /** assignment operator prohibited */
350 : AliHLTHOMERReader& operator=(const AliHLTHOMERReader&);
351 :
352 : #ifdef USE_ROOT
353 126 : ClassDef(AliHLTHOMERReader,2);
354 : #endif
355 : };
356 :
357 : /** defined for backward compatibility */
358 : typedef AliHLTMonitoringReader MonitoringReader;
359 : /** defined for backward compatibility */
360 : typedef AliHLTHOMERReader HOMERReader;
361 :
362 : // external interface of the HOMER reader
363 : #define ALIHLTHOMERREADER_CREATE_FROM_TCPPORT "AliHLTHOMERReaderCreateFromTCPPort"
364 : #define ALIHLTHOMERREADER_CREATE_FROM_TCPPORTS "AliHLTHOMERReaderCreateFromTCPPorts"
365 : #define ALIHLTHOMERREADER_CREATE_FROM_BUFFER "AliHLTHOMERReaderCreateFromBuffer"
366 : #define ALIHLTHOMERREADER_DELETE "AliHLTHOMERReaderDelete"
367 :
368 : #ifdef __cplusplus
369 : extern "C" {
370 : #endif
371 :
372 : typedef AliHLTHOMERReader* (*AliHLTHOMERReaderCreateFromTCPPort_t)(const char* hostname, unsigned short port );
373 : typedef AliHLTHOMERReader* (*AliHLTHOMERReaderCreateFromTCPPorts_t)(unsigned int tcpCnt, const char** hostnames, unsigned short* ports);
374 : typedef AliHLTHOMERReader* (*AliHLTHOMERReaderCreateFromBuffer_t)(const void* pBuffer, int size);
375 : typedef void (*AliHLTHOMERReaderDelete_t)(AliHLTHOMERReader* pInstance);
376 :
377 : /**
378 : * Create instance of HOMER reader working on a TCP port.
379 : */
380 : AliHLTHOMERReader* AliHLTHOMERReaderCreateFromTCPPort(const char* hostname, unsigned short port );
381 :
382 : /**
383 : * Create instance of HOMER reader working on multiple TCP ports.
384 : */
385 : AliHLTHOMERReader* AliHLTHOMERReaderCreateFromTCPPorts(unsigned int tcpCnt, const char** hostnames, unsigned short* ports);
386 :
387 : /**
388 : * Create instance of HOMER reader working on buffer.
389 : */
390 : AliHLTHOMERReader* AliHLTHOMERReaderCreateFromBuffer(const void* pBuffer, int size);
391 :
392 : /**
393 : * Delete instance of HOMER reader.
394 : */
395 : void AliHLTHOMERReaderDelete(AliHLTHOMERReader* pInstance);
396 : #ifdef __cplusplus
397 : }
398 : #endif
399 :
400 : #endif /* AliHLTHOMERREADER_H */
|