| 1 | // [License]
|
---|
| 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 | // [License]
|
---|
| 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 | // forward declaration
|
---|
| 49 | #include "_namespace.h"
|
---|
| 50 | NAMESPACE_BEGIN
|
---|
| 51 | class Message;
|
---|
| 52 | typedef size_t seqnum_t;
|
---|
| 53 | NAMESPACE_END
|
---|
| 54 |
|
---|
| 55 | // library includes
|
---|
| 56 | #include <cassert>
|
---|
| 57 | #include<boost/shared_array.hpp>
|
---|
| 58 | #include<boost/cstdint.hpp>
|
---|
| 59 |
|
---|
| 60 | // common includes
|
---|
| 61 | #include "ariba/utility/types/Address.h"
|
---|
| 62 | #include "ariba/utility/serialization.h"
|
---|
| 63 |
|
---|
| 64 | // reboost messages
|
---|
| 65 | #include "ariba/utility/transport/messages/message.hpp"
|
---|
| 66 |
|
---|
| 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 | // payload
|
---|
| 89 | bool legacy_payload_disabled;
|
---|
| 90 | bool releasePayload;
|
---|
| 91 | Data payload; //< messages binary data
|
---|
| 92 |
|
---|
| 93 | // XXX testing...
|
---|
| 94 | reboost::message_t newstyle_payload;
|
---|
| 95 | bool wrapped_up;
|
---|
| 96 |
|
---|
| 97 | // addresses and control info
|
---|
| 98 | const Address* srcAddr;
|
---|
| 99 | const Address* destAddr;
|
---|
| 100 |
|
---|
| 101 | public:
|
---|
| 102 | /**
|
---|
| 103 | * Constructor initializing name of message to the given one
|
---|
| 104 | */
|
---|
| 105 | inline Message() :
|
---|
| 106 | legacy_payload_disabled(false), releasePayload(true), payload(),
|
---|
| 107 | newstyle_payload(), wrapped_up(false), srcAddr(NULL),destAddr(NULL) {
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Constructs a new "root" message by copying the data described by
|
---|
| 112 | * data.
|
---|
| 113 | */
|
---|
| 114 | explicit inline Message( const Data& data ) :
|
---|
| 115 | legacy_payload_disabled(false), releasePayload(true),
|
---|
| 116 | newstyle_payload(), wrapped_up(false), srcAddr(NULL),destAddr(NULL) { // FIXME newstyle_payload..?
|
---|
| 117 | this->payload = data.clone();
|
---|
| 118 | // this->root = shared_array<uint8_t>((uint8_t*)data.getBuffer());
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | inline void dropPayload() {
|
---|
| 122 | if (this->releasePayload) payload.release();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | inline void setReleasePayload( bool release ) {
|
---|
| 126 | this->releasePayload = release;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | inline Data getPayload() const {
|
---|
| 130 | return payload;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | inline void setPayload( const Data& payload ) {
|
---|
| 134 | this->payload = payload;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Default destructor.
|
---|
| 139 | */
|
---|
| 140 | virtual ~Message();
|
---|
| 141 |
|
---|
| 142 | std::string toString() const;
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * Sets the destination address
|
---|
| 146 | *
|
---|
| 147 | * @param An abstract address representation
|
---|
| 148 | */
|
---|
| 149 | inline void setDestinationAddress(const Address* addr) {
|
---|
| 150 | destAddr = addr;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * Returns the optional abstract destination address or NULL
|
---|
| 155 | *
|
---|
| 156 | * @return the abstract destination address
|
---|
| 157 | */
|
---|
| 158 | inline const Address* getDestinationAddress() const {
|
---|
| 159 | return destAddr;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * Set the source address of the message
|
---|
| 164 | *
|
---|
| 165 | * @param addr The abstract source address
|
---|
| 166 | */
|
---|
| 167 | inline void setSourceAddress(const Address* addr) {
|
---|
| 168 | srcAddr = addr;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * Returns the optional abstract source address or NULL
|
---|
| 173 | *
|
---|
| 174 | * @return The abstract source address
|
---|
| 175 | */
|
---|
| 176 | inline const Address* getSourceAddress() const {
|
---|
| 177 | return srcAddr;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /**
|
---|
| 181 | * Returns a short human-readable description of this message
|
---|
| 182 | *
|
---|
| 183 | * @return A short human-readable description of this message
|
---|
| 184 | */
|
---|
| 185 | virtual const char* getDescription() const;
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * Returns a return message, that can be used to send a message
|
---|
| 189 | * back to the recipient or NULL if no message can be returned.
|
---|
| 190 | * The default implementation returns NULL.
|
---|
| 191 | *
|
---|
| 192 | * @return Return message.
|
---|
| 193 | */
|
---|
| 194 | virtual Message* createReturnMessage() const;
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * Encapsulate a message into the payload.
|
---|
| 198 | *
|
---|
| 199 | * @param message The message to be encapsulated.
|
---|
| 200 | */
|
---|
| 201 | inline void encapsulate( Message* message, int variant = DEFAULT_V ) {
|
---|
| 202 | if ( !payload.isUnspecified() ) throw "Error: Message already encapsulated";
|
---|
| 203 | payload = data_serialize( message, variant );
|
---|
| 204 | message->dropPayload();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * Decapsulates message. In case the message
|
---|
| 209 | * has not been deserialized, this method class
|
---|
| 210 | * serialization to get an object.
|
---|
| 211 | *
|
---|
| 212 | * @return The message object or NULL if a deserialization
|
---|
| 213 | */
|
---|
| 214 | template<class T>
|
---|
| 215 | inline T* decapsulate() {
|
---|
| 216 | if (!payload.isUnspecified()) {
|
---|
| 217 | T* payloadMsg = new T();
|
---|
| 218 | data_deserialize( payloadMsg, payload );
|
---|
| 219 | return payloadMsg;
|
---|
| 220 | }
|
---|
| 221 | return NULL;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /**
|
---|
| 225 | * The same as decapsulate, but this function
|
---|
| 226 | * is used in the samples to make the semantics easier
|
---|
| 227 | * to understand. The semantics is shown to be: you get
|
---|
| 228 | * a message and convert it to your type. Not as: you
|
---|
| 229 | * get a message and have to extract your message from it.
|
---|
| 230 | */
|
---|
| 231 | template<class T>
|
---|
| 232 | inline T* convert() {
|
---|
| 233 | return decapsulate<T>();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | // XXX testing
|
---|
| 238 | void set_payload_message(reboost::message_t msg)
|
---|
| 239 | {
|
---|
| 240 | newstyle_payload = msg;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void append_buffer(reboost::shared_buffer_t buff)
|
---|
| 244 | {
|
---|
| 245 | newstyle_payload.push_back(buff);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | // XXX testing... packs this message into the payload message (do not use twice!!)
|
---|
| 250 | virtual reboost::message_t wrap_up_for_sending();
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | /**
|
---|
| 254 | * Uses the old serialization system to serialize itself into a (new style) shared buffer.
|
---|
| 255 | */
|
---|
| 256 | virtual reboost::shared_buffer_t serialize_into_shared_buffer();
|
---|
| 257 |
|
---|
| 258 | /*
|
---|
| 259 | * XXX experimental
|
---|
| 260 | *
|
---|
| 261 | * Uses the old serialization system to deserialize itself out of a (new style) shared buffer.
|
---|
| 262 | * @return remaining sub-buffer (the "payload")
|
---|
| 263 | *
|
---|
| 264 | * Note: This is some kind of a hack! handle with care.
|
---|
| 265 | */
|
---|
| 266 | virtual reboost::shared_buffer_t deserialize_from_shared_buffer(reboost::shared_buffer_t buff);
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 | protected:
|
---|
| 270 | /**
|
---|
| 271 | * This class implements an explicit serializer for
|
---|
| 272 | * the message's payload.
|
---|
| 273 | */
|
---|
| 274 | class PayloadSerializer: public ExplicitSerializer {
|
---|
| 275 | private:
|
---|
| 276 | Message* msg;
|
---|
| 277 | size_t len;
|
---|
| 278 | public:
|
---|
| 279 | finline PayloadSerializer(Message* msg, size_t length = ~0) {
|
---|
| 280 | this->msg = msg;
|
---|
| 281 | this->len = length;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | sznMethodBegin(X) {
|
---|
| 285 | if (X.isSerializer()) {
|
---|
| 286 | if (!msg->payload.isUnspecified()) X && msg->payload;
|
---|
| 287 | } else {
|
---|
| 288 | if (msg->payload.isUnspecified()) {
|
---|
| 289 | size_t l = ((len == ~(size_t)0) ? X.getRemainingLength() : len);
|
---|
| 290 | msg->payload = X.getRemainingData(l);
|
---|
| 291 | msg->releasePayload = false;
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 | sznMethodEnd();
|
---|
| 296 | };
|
---|
| 297 |
|
---|
| 298 | /**
|
---|
| 299 | * Returns a serializer of the messages payload/encapsulated
|
---|
| 300 | * message.
|
---|
| 301 | *
|
---|
| 302 | * @param length The length of the payload
|
---|
| 303 | * @return A explicit payload serializer
|
---|
| 304 | */
|
---|
| 305 | finline PayloadSerializer Payload( size_t length = ~0 )
|
---|
| 306 | {
|
---|
| 307 | // assert( ! legacy_payload_disabled ); // FIXME aktuell
|
---|
| 308 |
|
---|
| 309 | return PayloadSerializer( this, length );
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | };
|
---|
| 313 |
|
---|
| 314 | NAMESPACE_END
|
---|
| 315 |
|
---|
| 316 | sznBeginDefault(ariba::utility::Message, X) {
|
---|
| 317 | X && Payload();
|
---|
| 318 | } sznEnd();
|
---|
| 319 |
|
---|
| 320 | #endif /* MESSAGE_H_ */
|
---|