| 1 | // [Licence]
|
|---|
| 2 | // The Ariba-Underlay Copyright
|
|---|
| 3 | //
|
|---|
| 4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
|---|
| 5 | //
|
|---|
| 6 | // Institute of Telematics
|
|---|
| 7 | // UniversitÀt Karlsruhe (TH)
|
|---|
| 8 | // Zirkel 2, 76128 Karlsruhe
|
|---|
| 9 | // Germany
|
|---|
| 10 | //
|
|---|
| 11 | // Redistribution and use in source and binary forms, with or without
|
|---|
| 12 | // modification, are permitted provided that the following conditions are
|
|---|
| 13 | // met:
|
|---|
| 14 | //
|
|---|
| 15 | // 1. Redistributions of source code must retain the above copyright
|
|---|
| 16 | // notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | // 2. Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | // notice, this list of conditions and the following disclaimer in the
|
|---|
| 19 | // documentation and/or other materials provided with the distribution.
|
|---|
| 20 | //
|
|---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
|---|
| 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
|---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|---|
| 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|---|
| 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|---|
| 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|---|
| 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 32 | //
|
|---|
| 33 | // The views and conclusions contained in the software and documentation
|
|---|
| 34 | // are those of the authors and should not be interpreted as representing
|
|---|
| 35 | // official policies, either expressed or implied, of the Institute of
|
|---|
| 36 | // Telematics.
|
|---|
| 37 | // [Licence]
|
|---|
| 38 |
|
|---|
| 39 | #ifndef MESSAGE_H_
|
|---|
| 40 | #define MESSAGE_H_
|
|---|
| 41 |
|
|---|
| 42 | // library includes
|
|---|
| 43 | #include<new>
|
|---|
| 44 | #include<string>
|
|---|
| 45 | #include<iostream>
|
|---|
| 46 | #include<boost/shared_array.hpp>
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | // local includes
|
|---|
| 50 | #include "MessageControlInfo.h"
|
|---|
| 51 |
|
|---|
| 52 | // forward declaration
|
|---|
| 53 | #include "_namespace.h"
|
|---|
| 54 | NAMESPACE_BEGIN
|
|---|
| 55 | class Message;
|
|---|
| 56 | typedef size_t seqnum_t;
|
|---|
| 57 | NAMESPACE_END
|
|---|
| 58 |
|
|---|
| 59 | // library includes
|
|---|
| 60 | #include <cassert>
|
|---|
| 61 | #include<boost/shared_array.hpp>
|
|---|
| 62 | #include<boost/cstdint.hpp>
|
|---|
| 63 |
|
|---|
| 64 | // common includes
|
|---|
| 65 | #include "ariba/utility/types/Address.h"
|
|---|
| 66 | #include "ariba/utility/serialization.h"
|
|---|
| 67 |
|
|---|
| 68 | std::ostream& operator<<(std::ostream& stream, const ariba::utility::Message& msg );
|
|---|
| 69 |
|
|---|
| 70 | #include "_namespace.h"
|
|---|
| 71 | NAMESPACE_BEGIN
|
|---|
| 72 |
|
|---|
| 73 | using_serialization;
|
|---|
| 74 | using ariba::utility::Address;
|
|---|
| 75 | using boost::shared_array;
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * This class implements an abstract message format.
|
|---|
| 79 | *
|
|---|
| 80 | * @author Sebastian Mies
|
|---|
| 81 | */
|
|---|
| 82 | class Message: public VSerializeable {
|
|---|
| 83 | VSERIALIZEABLE;
|
|---|
| 84 |
|
|---|
| 85 | protected:
|
|---|
| 86 | friend std::ostream& ::operator<<(std::ostream& stream, const ariba::utility::Message& msg );
|
|---|
| 87 |
|
|---|
| 88 | // root binary data
|
|---|
| 89 | shared_array<uint8_t> root;
|
|---|
| 90 |
|
|---|
| 91 | // payload
|
|---|
| 92 | bool releasePayload;
|
|---|
| 93 | Data payload; //< messages binary data
|
|---|
| 94 |
|
|---|
| 95 | // addresses and control info
|
|---|
| 96 | const Address* srcAddr;
|
|---|
| 97 | const Address* destAddr;
|
|---|
| 98 | const MessageControlInfo* ctrlInfo;
|
|---|
| 99 |
|
|---|
| 100 | public:
|
|---|
| 101 | /**
|
|---|
| 102 | * Constructor initializing name of message to the given one
|
|---|
| 103 | */
|
|---|
| 104 | inline Message() :
|
|---|
| 105 | root(), releasePayload(true), payload(), srcAddr(NULL),destAddr(NULL),ctrlInfo(NULL) {
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Constructs a new "root" message by copying the data described by
|
|---|
| 110 | * data.
|
|---|
| 111 | */
|
|---|
| 112 | inline Message( const Data& data ) :
|
|---|
| 113 | srcAddr(NULL),destAddr(NULL),ctrlInfo(NULL), releasePayload(true) {
|
|---|
| 114 | this->payload = data.clone();
|
|---|
| 115 | // this->root = shared_array<uint8_t>((uint8_t*)data.getBuffer());
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | inline void dropPayload() {
|
|---|
| 119 | if (this->releasePayload) payload.release();
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | inline void setReleasePayload( bool release ) {
|
|---|
| 123 | this->releasePayload = release;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | inline Data getPayload() const {
|
|---|
| 127 | return payload;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | inline void setPayload( const Data& payload ) {
|
|---|
| 131 | this->payload = payload;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Default destructor.
|
|---|
| 136 | */
|
|---|
| 137 | virtual ~Message();
|
|---|
| 138 |
|
|---|
| 139 | std::string toString() const;
|
|---|
| 140 |
|
|---|
| 141 | /**
|
|---|
| 142 | * Sets the destination address
|
|---|
| 143 | *
|
|---|
| 144 | * @param An abstract address representation
|
|---|
| 145 | */
|
|---|
| 146 | inline void setDestinationAddress(const Address* addr) {
|
|---|
| 147 | destAddr = addr;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Returns the optional abstract destination address or NULL
|
|---|
| 152 | *
|
|---|
| 153 | * @return the abstract destination address
|
|---|
| 154 | */
|
|---|
| 155 | inline const Address* getDestinationAddress() const {
|
|---|
| 156 | return destAddr;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Set the source address of the message
|
|---|
| 161 | *
|
|---|
| 162 | * @param addr The abstract source address
|
|---|
| 163 | */
|
|---|
| 164 | inline void setSourceAddress(const Address* addr) {
|
|---|
| 165 | srcAddr = addr;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Returns the optional abstract source address or NULL
|
|---|
| 170 | *
|
|---|
| 171 | * @return The abstract source address
|
|---|
| 172 | */
|
|---|
| 173 | inline const Address* getSourceAddress() const {
|
|---|
| 174 | return srcAddr;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Set optional control information
|
|---|
| 179 | *
|
|---|
| 180 | * @parm ctrlInfo The optional control information
|
|---|
| 181 | */
|
|---|
| 182 | inline void setControlInfo(const MessageControlInfo* ctrlInfo) {
|
|---|
| 183 | this->ctrlInfo = ctrlInfo;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * Returns the optional control information or NULL
|
|---|
| 188 | *
|
|---|
| 189 | * @return The optional control information or NULL
|
|---|
| 190 | */
|
|---|
| 191 | inline const MessageControlInfo* getControlInfo() const {
|
|---|
| 192 | return ctrlInfo;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Returns a short human-readable description of this message
|
|---|
| 197 | *
|
|---|
| 198 | * @return A short human-readable description of this message
|
|---|
| 199 | */
|
|---|
| 200 | virtual const char* getDescription() const;
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * Returns a return message, that can be used to send a message
|
|---|
| 204 | * back to the recipient or NULL if no message can be returned.
|
|---|
| 205 | * The default implementation returns NULL.
|
|---|
| 206 | *
|
|---|
| 207 | * @return Return message.
|
|---|
| 208 | */
|
|---|
| 209 | virtual Message* createReturnMessage() const;
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * Encapsulate a message into the payload.
|
|---|
| 213 | *
|
|---|
| 214 | * @param message The message to be encapsulated.
|
|---|
| 215 | */
|
|---|
| 216 | inline void encapsulate( Message* message, int variant = DEFAULT_V ) {
|
|---|
| 217 | if ( !payload.isUnspecified() ) throw "Error: Message already encapsulated";
|
|---|
| 218 | payload = data_serialize( message, variant );
|
|---|
| 219 | message->dropPayload();
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | /**
|
|---|
| 223 | * Decapsulates message. In case the message
|
|---|
| 224 | * has not been deserialized, this method class
|
|---|
| 225 | * serialization to get an object.
|
|---|
| 226 | *
|
|---|
| 227 | * @return The message object or NULL if a deserialization
|
|---|
| 228 | */
|
|---|
| 229 | template<class T>
|
|---|
| 230 | inline T* decapsulate() {
|
|---|
| 231 | if (!payload.isUnspecified()) {
|
|---|
| 232 | T* payloadMsg = new T();
|
|---|
| 233 | data_deserialize( payloadMsg, payload );
|
|---|
| 234 | return payloadMsg;
|
|---|
| 235 | }
|
|---|
| 236 | return NULL;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * The same as decapsulate, but this function
|
|---|
| 241 | * is used in the samples to make the semantics easier
|
|---|
| 242 | * to understand. The semantics is shown to be: you get
|
|---|
| 243 | * a message and convert it to your type. Not as: you
|
|---|
| 244 | * get a message and have to extract your message from it.
|
|---|
| 245 | */
|
|---|
| 246 | template<class T>
|
|---|
| 247 | inline T* convert() {
|
|---|
| 248 | return decapsulate<T>();
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | protected:
|
|---|
| 252 | /**
|
|---|
| 253 | * This class implements an explicit serializer for
|
|---|
| 254 | * the message's payload.
|
|---|
| 255 | */
|
|---|
| 256 | class PayloadSerializer: public ExplicitSerializer {
|
|---|
| 257 | private:
|
|---|
| 258 | Message* msg;
|
|---|
| 259 | size_t len;
|
|---|
| 260 | public:
|
|---|
| 261 | finline PayloadSerializer(Message* msg, size_t length = ~0) {
|
|---|
| 262 | this->msg = msg;
|
|---|
| 263 | this->len = length;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | sznMethodBegin(X) {
|
|---|
| 267 | if (X.isSerializer()) {
|
|---|
| 268 | if (!msg->payload.isUnspecified()) X && msg->payload;
|
|---|
| 269 | } else {
|
|---|
| 270 | if (msg->payload.isUnspecified()) {
|
|---|
| 271 | size_t l = ((len == ~0) ? X.getRemainingLength() : len);
|
|---|
| 272 | msg->payload = X.getRemainingData(l);
|
|---|
| 273 | msg->releasePayload = false;
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|
| 277 | sznMethodEnd();
|
|---|
| 278 | };
|
|---|
| 279 |
|
|---|
| 280 | /**
|
|---|
| 281 | * Returns a serializer of the messages payload/encapsulated
|
|---|
| 282 | * message.
|
|---|
| 283 | *
|
|---|
| 284 | * @param length The length of the payload
|
|---|
| 285 | * @return A explicit payload serializer
|
|---|
| 286 | */
|
|---|
| 287 | finline PayloadSerializer Payload( size_t length = ~0 ) {
|
|---|
| 288 | return PayloadSerializer( this, length );
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | };
|
|---|
| 292 |
|
|---|
| 293 | NAMESPACE_END
|
|---|
| 294 |
|
|---|
| 295 | sznBeginDefault(ariba::utility::Message, X) {
|
|---|
| 296 | X && Payload();
|
|---|
| 297 | } sznEnd();
|
|---|
| 298 |
|
|---|
| 299 | #endif /* MESSAGE_H_ */
|
|---|