source: trunk/ETSI-Testsuites/ETSI_auto_IOT/adapter/src/Messages/TrafficCaptureMessage.h @ 35

Last change on this file since 35 was 22, checked in by rings, 14 years ago
  • Property svn:executable set to *
File size: 12.7 KB
Line 
1/**
2 * @file TrafficCaptureMessage.h
3 * This header file contains basic definition for TCP/IP based communication with the
4 * TrafficCapture application.
5 * @author Tomas Urban
6 * @version 0.1
7 * @date 16/07/2009
8 */
9#ifndef TRAFFIC_CAPTURE_MESSAGE_H
10#define TRAFFIC_CAPTURE_MESSAGE_H
11#include <string>
12
13#ifdef WIN32
14#include <Winsock2.h>
15#else
16#include <sys/time.h>
17#endif
18
19/**
20 * The enumeration contains means which can be used for traffic capture.
21 */
22enum ECaptureType
23{
24        ECaptureType_PCAP /**< PCAP library (for capturing network traffic on link-layer lever) */
25};
26/**
27 * The enumeration defines the mode in which traffic capture occurs.
28 */
29enum ECaptureMode
30{
31        ECaptureMode_Live, /**< Traffic capture occurs in real time */
32        ECaptureMode_Offline /**< Traffic capture is based on saved data from previous sessions */
33};
34
35/**
36 * The enumeration defines results of traffic capture initialization.
37 */
38enum ECaptureInitResult
39{
40        ECaptureInit_Successful, /**< Init succeeded */
41        ECaptureInit_Failed, /**< Init completely failed, nothing can be captured */
42        ECaptureInit_PartiallySuccessful /**< Init wasn't completely successful, but there' at least one device available for capture */
43};
44
45#pragma pack(1)
46/**
47 * Common message header of data message used in communication with the TrafficCapture
48 * application. It is usually used for reading the header data from the socket input stream.
49 */
50struct TMessageHeader
51{       
52        unsigned short nMsgType; /**< Contains message type */
53        unsigned int nMsgLen; /**< Contains length of the payload */
54
55};
56#pragma pack()
57
58/**
59 * Abstract superclass for all data messages used in communication with the TrafficCapture
60 * application. It defines methods for handling the content of message header and common
61 * encoding and decoding methods.\n
62 * The implementation solution for message classes uses strategy and factory patterns.
63 * Child classes containing definitions of real messages should implement the following:\n
64 * 1. #GetId() method returning the message ID\n
65 * 2. Member variables and methods for setting and getting message properties\n
66 * 3. Methods for encoding and decoding message properties\n\n
67 * There are two basic use cases how instances of can be used.\n
68 * 1. When sending a message:\n
69 * a. Create an instance of the class using its constructor\n
70 * b. Initialize message values\n
71 * c. Write the message to a socket stream using the #GetEncodedMessage() and
72 * #GetEncodedDataLength() methods\n\n
73 * 2. When receiving a message:\n
74 * a. Read the message header from a socket stream\n
75 * b. Using the length information contained in the header, read the payload from the
76 * socket stream\n
77 * c. Instantiate the message using TrafficCaptureMessageFactory. The message instantiated
78 * this way will be automatically decoded.\n
79 * d. Read value data from the message
80 */
81class TrafficCaptureMessage
82{
83private:
84        char * m_pData;
85        unsigned int m_nDataLen;
86        void Encode();
87protected:
88        TrafficCaptureMessage(void);
89        /**
90         * This constant contains the size of encoded timestamp. It is usually used
91         * in the #CalculateDataLength() method.
92         */
93        static const size_t TIME_SIZE = 12;
94        /**
95         * Disposes the encoded data. This method is called by set operations of child
96         * classes.
97         */
98        void DisposeEncodedData();
99        /**
100         * The method calculates the size of the buffer needed for storing encoded data.
101         * If a child class introduces new data fields which increase the encoding size,
102         * the number returned by this method has to be increased accordingly.
103         * @return Number of bytes needed for storing the encoded message
104         */
105        virtual unsigned int CalculateDataLength();
106        /**
107         * Returns number of bytes needed for storing the string. Encoded string is
108         * composed of a 2-byte length field followed by a byte buffer of variable size
109         * containg string data (without terminating zero). This method is usually used
110         * inside the #CalculateDataLength() method
111         * @param sValue String whose length should be calculated
112         * @return Number of bytes needed for storing the encoded string
113         */
114        unsigned int CalculateStringLength(const std::string & sValue);
115        /**
116         * The method encodes message payload. This method shall be overriden if a child
117         * class introduces new data fields. These fields shall be encoded using the
118         * encoding methods provided by this class.
119         * @param nOffset Contains the writing position in the underlying data buffer.
120         * The parameter is required for encoding methods provided by this class. It's value
121         * should never be changed manually.
122         */
123        virtual void EncodePayload(unsigned int & nOffset);
124        /**
125         * Writes raw binary data to a buffer containing encoded data.
126         * @param pData Binary data to write
127         * @param nSize Size of the binary data
128         * @param nOffset Writing position
129         */
130        void EncodeData(const void * pData, size_t nSize, unsigned int & nOffset);
131        /**
132         * Writes a single unsigned byte to a buffer containing encoded data.
133         * @param nValue Value to write
134         * @param nOffset Writing position
135         */
136        void EncodeUChar(unsigned char nValue, unsigned int & nOffset);
137        /**
138         * Writes an unsigned short number (2 bytes) to a buffer containing encoded data.
139         * @param nValue Value to write
140         * @param nOffset Writing position
141         */
142        void EncodeUShort(unsigned short nValue, unsigned int & nOffset);
143        /**
144         * Writes a signed int number (4 bytes) to a buffer containing encoded data.
145         * @param nValue Value to write
146         * @param nOffset Writing position
147         */
148        void EncodeInt(int nValue, unsigned int & nOffset);
149        /**
150         * Writes an unsigned int number (4 bytes) to a buffer containing encoded data.
151         * @param nValue Value to write
152         * @param nOffset Writing position
153         */
154        void EncodeUInt(unsigned int nValue, unsigned int & nOffset);
155        /**
156         * Writes a time_t value to a buffer containing encoded data.
157         * @param timestamp Timestamp to write
158         * @param nOffset Writing position
159         */
160        void EncodeTime(struct timeval & timestamp, unsigned int & nOffset);
161        /**
162         * Writes a bool value to a buffer containing encoded data. The encoding size
163         * is 1 byte.
164         * @param bValue Value to write
165         * @param nOffset Writing position
166         */
167        void EncodeBool(bool bValue, unsigned int & nOffset);
168        /**
169         * Writes a string value to a buffer containing encoded data. Encoded string is
170         * composed of a 2-byte length field followed by a byte buffer of variable size
171         * containg string data (without terminating zero).
172         * @param sValue String to write
173         * @param nOffset Writing position
174         */
175        void EncodeString(const std::string & sValue, unsigned int & nOffset);
176        /**
177         * The method decodes message payload. This method shall be overriden if a child
178         * class introduces new data fields. These fields can be decoded using the
179         * decoding methods provided by this class.
180         * @param pPayload Pointer to the byte buffer containing encoded payload data
181         * @param nPayloadLength Length of the payload data
182         * @param nOffset Contains the reading position in the payload data buffer.
183         * The parameter is required for decoding methods provided by this class.
184         * @return \c true if decoding was successful, \c false otherwise
185         */
186        virtual bool DecodePayload(const char * pPayload, unsigned int nPayloadLength, unsigned int & nOffset);
187        /**
188         * Reads raw binary data from a data buffer and copies it to a target buffer.
189         * @param pValue Target buffer
190         * @param nValueSize Size of the target buffer
191         * @param pData Buffer containing encoded payload data
192         * @param nDataLen Length of the payload data
193         * @param nOffset Reading position
194         * @return \c true if decoding was successful, \c false otherwise
195         */
196        bool DecodeData(void * pValue, size_t nValueSize, const char * pData, unsigned int nDataLen,
197                unsigned int & nOffset);
198        /**
199         * Reads a single unsigned byte from a data buffer.
200         * @param nValue Target value
201         * @param pData Buffer containing encoded payload data
202         * @param nDataLen Length of the payload data
203         * @param nOffset Reading position
204         * @return \c true if decoding was successful, \c false otherwise
205         */
206        bool DecodeUChar(unsigned char & nValue, const char * pData, unsigned int nDataLen, unsigned int & nOffset);
207        /**
208         * Reads an unsigned short number (2 bytes) from a data buffer.
209         * @param nValue Target value
210         * @param pData Buffer containing encoded payload data
211         * @param nDataLen Length of the payload data
212         * @param nOffset Reading position
213         * @return \c true if decoding was successful, \c false otherwise
214         */
215        bool DecodeUShort(unsigned short & nValue, const char * pData, unsigned int nDataLen, unsigned int & nOffset);
216        /**
217         * Reads a signed int number (4 bytes) from a data buffer.
218         * @param nValue Target value
219         * @param pData Buffer containing encoded payload data
220         * @param nDataLen Length of the payload data
221         * @param nOffset Reading position
222         * @return \c true if decoding was successful, \c false otherwise
223         */
224        bool DecodeInt(int & nValue, const char * pData, unsigned int nDataLen, unsigned int & nOffset);
225        /**
226         * Reads an unsigned int number (4 bytes) from a data buffer.
227         * @param nValue Target value
228         * @param pData Buffer containing encoded payload data
229         * @param nDataLen Length of the payload data
230         * @param nOffset Reading position
231         * @return \c true if decoding was successful, \c false otherwise
232         */
233        bool DecodeUInt(unsigned int & nValue, const char * pData, unsigned int nDataLen, unsigned int & nOffset);
234        /**
235         * Reads a timestamp from a data buffer.
236         * @param timestamp Target timestamp
237         * @param pData Buffer containing encoded payload data
238         * @param nDataLen Length of the payload data
239         * @param nOffset Reading position
240         * @return \c true if decoding was successful, \c false otherwise
241         */
242        bool DecodeTime(struct timeval & timestamp, const char * pData, unsigned int nDataLen, unsigned int & nOffset);
243        /**
244         * Reads a bool value from a data buffer.
245         * @param bValue Target value
246         * @param pData Buffer containing encoded payload data
247         * @param nDataLen Length of the payload data
248         * @param nOffset Reading position
249         * @return \c true if decoding was successful, \c false otherwise
250         */
251        bool DecodeBool(bool & bValue, const char * pData, unsigned int nDataLen, unsigned int & nOffset);
252        /**
253         * Reads a string value from a data buffer. The encoded string value is
254         * composed of a 2-byte length field followed by a byte buffer of variable size
255         * containg string data (without terminating zero).
256         * @param sValue Target string
257         * @param pData Buffer containing encoded payload data
258         * @param nDataLen Length of the payload data
259         * @param nOffset Reading position
260         * @return \c true if decoding was successful, \c false otherwise
261         */
262        bool DecodeString(std::string & sValue, const char * pData, unsigned int nDataLen, unsigned int & nOffset);
263public:
264        virtual ~TrafficCaptureMessage(void);
265        /**
266         * Returns message identification number. It is not defined by parent abstract classes level;
267         * thus it has to be overridden in child classes. The identification numbers returned by
268         * individual child classes shall be distinct.
269         * @return Message identification number
270         */
271        virtual unsigned short GetId() const = 0;
272        /**
273         * Encodes the message to a byte buffer and return a pointer to it. The length of the buffer
274         * can be obtained calling the #GetEncodedDataLength() method. The pointer to the encoded
275         * data is valid as long as the instance that created it stays alive and unchanged.
276         * @return Pointer to the byte buffer containing encoded message
277         */
278        const char * GetEncodedMessage();
279        /**
280         * This method returns the length of the encoded message data. It is usually used with the
281         * #GetEncodedMessage() method when writing the message to a socket stream.
282         * @return Length of encoded data
283         */
284        unsigned int GetEncodedDataLength();
285        /**
286         * Initializes message data from a byte buffer containing encoded data for this message.
287         * This method is used by the TrafficCaptureMessageFactory when creating a message received
288         * from a remote host.
289         * @param pPayload Pointer to the byte buffer with encoded payload
290         * @param nPayloadLength Length of the encoded payload
291         * @return \c true if decoding was successful, \c false otherwise
292         */
293        bool DecodePayload(const char * pPayload, unsigned int nPayloadLength);
294};
295
296/**
297 * Common abstract class for reply messages. It defines a \c bool data field containing
298 * operation success.
299 */
300class CommonReplyMessage : public TrafficCaptureMessage
301{
302private:
303        bool m_bResult;
304protected:
305        CommonReplyMessage();
306        virtual unsigned int CalculateDataLength();
307        virtual void EncodePayload(unsigned int & nOffset);
308        virtual bool DecodePayload(const char * pPayload, unsigned int nPayloadLength, unsigned int & nOffset);
309public:
310        virtual ~CommonReplyMessage() {}
311        /**
312         * The method returns the result of the operation.
313         * @return Operation result
314         */
315        bool GetResult() const { return m_bResult; }
316        /**
317         * The method sets the result of the operation. The default value is false.
318         * @param bResult Operation result
319         */
320        void SetResult(bool bResult);
321};
322
323#endif
Note: See TracBrowser for help on using the repository browser.