| 1 | #ifndef DATAMESSAGE_H_ | 
|---|
| 2 | #define DATAMESSAGE_H_ | 
|---|
| 3 |  | 
|---|
| 4 | #define USE_MESSAGE_UTILITY | 
|---|
| 5 |  | 
|---|
| 6 | #include <memory> | 
|---|
| 7 | #include <inttypes.h> | 
|---|
| 8 |  | 
|---|
| 9 | // use message utility | 
|---|
| 10 | #ifdef USE_MESSAGE_UTILITY | 
|---|
| 11 | //  #include "ariba/utility/messages.h" | 
|---|
| 12 | #include "ariba/utility/messages/Message.h" | 
|---|
| 13 | namespace ariba { | 
|---|
| 14 | typedef utility::Message Message; | 
|---|
| 15 | } | 
|---|
| 16 | #endif | 
|---|
| 17 |  | 
|---|
| 18 | namespace ariba { | 
|---|
| 19 |  | 
|---|
| 20 | // define sequence number type | 
|---|
| 21 | typedef uint16_t seqnum_t; | 
|---|
| 22 |  | 
|---|
| 23 | /** | 
|---|
| 24 | * \addtogroup public | 
|---|
| 25 | * @{ | 
|---|
| 26 | * This class wraps different representations of a message. In its current | 
|---|
| 27 | * version is allows to specify binary data (as void*) with a size specifying | 
|---|
| 28 | * the number of bytes of data or an message object that can be | 
|---|
| 29 | * serialized if necessary. The main idea is, that simulation environments | 
|---|
| 30 | * do not necessarily need to serialize messages. | 
|---|
| 31 | * | 
|---|
| 32 | * For performance reasons methods of this class are inlined where possible! | 
|---|
| 33 | * | 
|---|
| 34 | * @author Sebastian Mies <mies@tm.uka.de> | 
|---|
| 35 | */ | 
|---|
| 36 | class DataMessage { | 
|---|
| 37 | private: | 
|---|
| 38 | void* data; //< internal buffer pointer | 
|---|
| 39 | size_t size; //< internal buffer pointer size | 
|---|
| 40 | public: | 
|---|
| 41 | static const DataMessage UNSPECIFIED; //< default implementation of a data message | 
|---|
| 42 |  | 
|---|
| 43 | /** | 
|---|
| 44 | * Default constructor for a data message | 
|---|
| 45 | */ | 
|---|
| 46 | inline DataMessage() { | 
|---|
| 47 | this->data = NULL; | 
|---|
| 48 | this->size = 0; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 | * Constructor for a data message | 
|---|
| 53 | * @param data Data buffer to carry in the message | 
|---|
| 54 | * @param size Size of the buffer pointed to | 
|---|
| 55 | */ | 
|---|
| 56 | inline DataMessage( const void* data, const size_t size ) { | 
|---|
| 57 | this->data = const_cast<void*>(data); | 
|---|
| 58 | this->size = size; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | /** | 
|---|
| 62 | * Copy constructor for a data message | 
|---|
| 63 | * @param message The other message to copy from | 
|---|
| 64 | */ | 
|---|
| 65 | inline DataMessage(const DataMessage& message){ | 
|---|
| 66 | this->data = message.data; | 
|---|
| 67 | this->size = message.size; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | #ifdef USE_MESSAGE_UTILITY | 
|---|
| 71 | /** | 
|---|
| 72 | * Construct a data message from a normal message | 
|---|
| 73 | * @param message The normal message to store | 
|---|
| 74 | */ | 
|---|
| 75 | inline DataMessage( const Message* message ) { | 
|---|
| 76 | this->data = (void*)const_cast<Message*>(message); | 
|---|
| 77 | this->size = ~0; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | * Construct a data message from a normal message | 
|---|
| 82 | * @param message The normal message to store | 
|---|
| 83 | */ | 
|---|
| 84 | inline DataMessage( const Message& message ) { | 
|---|
| 85 | this->data = (void*)const_cast<Message*>(&message); | 
|---|
| 86 | this->size = ~0; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | * Get the internal message when constructued through one | 
|---|
| 91 | * @return pointer to the message | 
|---|
| 92 | */ | 
|---|
| 93 | inline Message* getMessage() const { | 
|---|
| 94 | if (isData()) { | 
|---|
| 95 | return new Message( Data((uint8_t*)data,size*8) ); | 
|---|
| 96 | } | 
|---|
| 97 | return (Message*)data; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * Conversion function to convert to Message* | 
|---|
| 102 | * @return internal message | 
|---|
| 103 | */ | 
|---|
| 104 | inline operator Message* () const { | 
|---|
| 105 | return getMessage(); | 
|---|
| 106 | } | 
|---|
| 107 | #endif | 
|---|
| 108 |  | 
|---|
| 109 | /** | 
|---|
| 110 | * Is the data message a normal message? | 
|---|
| 111 | * @return true, if the data message is a normal message | 
|---|
| 112 | */ | 
|---|
| 113 | inline bool isMessage() const { | 
|---|
| 114 | return size == ~(size_t)0; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | /** | 
|---|
| 118 | * Is the data message a data message | 
|---|
| 119 | * @return true, if the data message is not a normal message | 
|---|
| 120 | */ | 
|---|
| 121 | inline bool isData() const { | 
|---|
| 122 | return !isMessage(); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | /** | 
|---|
| 126 | * Directly access the internal data pointer | 
|---|
| 127 | * @return internal data pointer | 
|---|
| 128 | */ | 
|---|
| 129 | inline void* getData() const { | 
|---|
| 130 | return data; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | /** | 
|---|
| 134 | * Get the size of the internal buffer | 
|---|
| 135 | * @return internal buffer size | 
|---|
| 136 | */ | 
|---|
| 137 | inline size_t getSize() const { | 
|---|
| 138 | return size; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 | * Is the data message invalid? | 
|---|
| 143 | * @return true, if data message is invalid | 
|---|
| 144 | */ | 
|---|
| 145 | inline bool isUnspecified() const { | 
|---|
| 146 | return data == NULL; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | }; | 
|---|
| 150 |  | 
|---|
| 151 | } // namespace ariba | 
|---|
| 152 |  | 
|---|
| 153 | /** @} */ | 
|---|
| 154 |  | 
|---|
| 155 | #endif /* DATAMESSAGE_H_ */ | 
|---|