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