source: source/ariba/utility/messages/Message.h@ 2392

Last change on this file since 2392 was 2392, checked in by mies, 16 years ago
File size: 7.1 KB
Line 
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"
54NAMESPACE_BEGIN
55class Message;
56typedef size_t seqnum_t;
57NAMESPACE_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
68std::ostream& operator<<(std::ostream& stream, const ariba::utility::Message& msg );
69
70#include "_namespace.h"
71NAMESPACE_BEGIN
72
73using_serialization;
74using ariba::utility::Address;
75using boost::shared_array;
76
77/**
78 * This class implements an abstract message format.
79 *
80 * @author Sebastian Mies
81 */
82class Message: public VSerializeable {
83 VSERIALIZEABLE;
84
85protected:
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
100public:
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) {
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
239protected:
240 /**
241 * This class implements an explicit serializer for
242 * the message's payload.
243 */
244 class PayloadSerializer: public ExplicitSerializer {
245 private:
246 Message* msg;
247 size_t len;
248 public:
249 finline PayloadSerializer(Message* msg, size_t length = ~0) {
250 this->msg = msg;
251 this->len = length;
252 }
253
254 sznMethodBegin(X) {
255 if (X.isSerializer()) {
256 if (!msg->payload.isUnspecified()) X && msg->payload;
257 } else {
258 if (msg->payload.isUnspecified()) {
259 size_t l = ((len == ~0) ? X.getRemainingLength() : len);
260 msg->payload = X.getRemainingData(l);
261 msg->releasePayload = false;
262 }
263 }
264 }
265 sznMethodEnd();
266 };
267
268 /**
269 * Returns a serializer of the messages payload/encapsulated
270 * message.
271 *
272 * @param length The length of the payload
273 * @return A explicit payload serializer
274 */
275 finline PayloadSerializer Payload( size_t length = ~0 ) {
276 return PayloadSerializer( this, length );
277 }
278
279};
280
281NAMESPACE_END
282
283sznBeginDefault(ariba::utility::Message, X) {
284 X && Payload();
285} sznEnd();
286
287#endif /* MESSAGE_H_ */
Note: See TracBrowser for help on using the repository browser.