source: source/ariba/overlay/messages/OverlayMsg.h@ 10653

Last change on this file since 10653 was 10653, checked in by Michael Tänzer, 12 years ago

Merge the ASIO branch back into trunk

File size: 9.0 KB
Line 
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 OVERLAY_MSG_H__
40#define OVERLAY_MSG_H__
41
42#include <boost/cstdint.hpp>
43
44#include "ariba/utility/messages.h"
45#include "ariba/utility/serialization.h"
46#include "ariba/utility/types/ServiceID.h"
47#include "ariba/utility/types/NodeID.h"
48#include "ariba/utility/types/LinkID.h"
49#include "ariba/communication/EndpointDescriptor.h"
50
51
52namespace ariba {
53namespace overlay {
54
55using ariba::utility::LinkID;
56using ariba::utility::NodeID;
57using ariba::utility::ServiceID;
58using ariba::utility::Message;
59using ariba::communication::EndpointDescriptor;
60using_serialization;
61
62/**
63 * A general purpose overlay message that is used to exchange messages
64 * between nodes.
65 *
66 * @author Sebastian Mies <mies@tm.uka.de>
67 */
68class OverlayMsg: public Message { VSERIALIZEABLE;
69public:
70 /// message types, is: uint8_t
71 enum type_ { // is: uint8_t
72 typeInvalid = 0x00, ///< invalid, unspecified type
73
74 // data transfer
75 maskTransfer = 0x10, ///< bit mask for transfer messages
76 typeData = 0x11, ///< message contains data for higher layers
77
78 // join signaling
79 maskJoin = 0x20, ///< bit mask for join messages
80 typeJoinRequest = 0x21, ///< join request
81 typeJoinReply = 0x22, ///< join reply
82
83 // link messages
84 maskLink = 0x30, ///< bit mask for link messages
85 typeLinkRequest = 0x31, ///< request a new link
86 typeLinkReply = 0x32, ///< link request reply
87 typeLinkUpdate = 0x33, ///< update message for link association
88 typeLinkDirect = 0x34, ///< direct connection has been established
89 typeLinkAlive = 0x35, ///< keep-alive message
90
91 /// DHT routed messages
92 /// @deprecated because the DHT has been moved into a separate service
93 maskDHT = 0x40, ///< bit mask for dht messages
94 typeDHTPut = 0x41, ///< DHT put operation
95 typeDHTGet = 0x42, ///< DHT get operation
96 typeDHTRemove = 0x43, ///< DHT remove operation
97
98 /// DHT response messages
99 /// @deprecated because the DHT has been moved into a separate service
100 maskDHTResponse = 0x50, ///< bit mask for dht responses
101 typeDHTData = 0x51, ///< DHT get data
102
103 // topology signaling
104 typeSignalingStart = 0x80, ///< start of the signaling types
105 typeSignalingEnd = 0xFF ///< end of the signaling types
106 };
107
108 /// default constructor
109 OverlayMsg(
110 uint8_t type = typeInvalid,
111 const ServiceID& _service = ServiceID::UNSPECIFIED,
112 const NodeID& _sourceNode = NodeID::UNSPECIFIED,
113 const NodeID& _destinationNode = NodeID::UNSPECIFIED,
114 const LinkID& _sourceLink = LinkID::UNSPECIFIED,
115 const LinkID& _destinationLink = LinkID::UNSPECIFIED )
116 : type(type), flags(0), hops(0), ttl(10),
117 service(_service),
118 sourceNode(_sourceNode), destinationNode(_destinationNode),
119 sourceLink(_sourceLink), destinationLink(_destinationLink),
120 routeRecord() {
121 if (!_sourceLink.isUnspecified() || !_destinationLink.isUnspecified())
122 setLinkMessage(true);
123 }
124
125 // copy constructor
126 OverlayMsg(const OverlayMsg& rhs)
127 : type(rhs.type), flags(rhs.flags), hops(rhs.hops), ttl(rhs.ttl),
128 service(rhs.service),
129 sourceNode(rhs.sourceNode), destinationNode(rhs.destinationNode),
130 sourceLink(rhs.sourceLink), destinationLink(rhs.destinationLink),
131 routeRecord(rhs.routeRecord) {
132 }
133
134 /// destructor
135 ~OverlayMsg();
136
137 /// type -------------------------------------------------------------------
138
139 type_ getType() const {
140 return (type_) type;
141 }
142
143 void setType( type_ type ) {
144 this->type = type;
145 }
146
147 bool hasTypeMask( type_ mask ) const {
148 return (type & (uint8_t)mask) == (uint8_t)mask;
149 }
150
151 /// flags ------------------------------------------------------------------
152
153 bool isRelayed() const {
154 return (flags & 0x01)!=0;
155 }
156
157 void setRelayed( bool relayed = true ) {
158 if (relayed) flags |= 1; else flags &= ~1;
159 }
160
161 bool isRegisterRelay() const {
162 return (flags & 0x02)!=0;
163 }
164
165 void setRegisterRelay( bool relayed = true ) {
166 if (relayed) flags |= 0x02; else flags &= ~0x02;
167 }
168
169 bool isRouteRecord() const {
170 return (flags & 0x04)!=0;
171 }
172
173 void setRouteRecord( bool route_record = true ) {
174 if (route_record) flags |= 0x04; else flags &= ~0x04;
175 }
176
177 bool isAutoLink() const {
178 return (flags & 0x80) == 0x80;
179 }
180
181 void setAutoLink(bool auto_link = true ) {
182 if (auto_link) flags |= 0x80; else flags &= ~0x80;
183 }
184
185 bool isLinkMessage() const {
186 return (flags & 0x40)!=0;
187 }
188
189 void setLinkMessage(bool link_info = true ) {
190 if (link_info) flags |= 0x40; else flags &= ~0x40;
191 }
192
193 bool containsSourceEndpoint() const {
194 return (flags & 0x20)!=0;
195 }
196
197 void setContainsSourceEndpoint(bool contains_endpoint) {
198 if (contains_endpoint) flags |= 0x20; else flags &= ~0x20;
199 }
200
201 /// number of hops and time to live ----------------------------------------
202
203 uint8_t getNumHops() const {
204 return hops;
205 }
206
207 void setNumHops( uint8_t hops ) {
208 this->hops = hops;
209 }
210
211 uint8_t increaseNumHops() {
212 hops++;
213 return hops;
214 }
215
216 uint8_t getTimeToLive() const {
217 return ttl;
218 }
219
220 void setTimeToLive( uint8_t ttl ) {
221 this->ttl = ttl;
222 }
223
224 /// addresses and links ----------------------------------------------------
225
226 const ServiceID& getService() const {
227 return service;
228 }
229
230 void setService( const ServiceID& service ) {
231 this->service = service;
232 }
233
234 const NodeID& getSourceNode() const {
235 return sourceNode;
236 }
237
238 void setSourceNode( const NodeID& node ) {
239 this->sourceNode = node;
240 }
241
242 const NodeID& getDestinationNode() const {
243 return destinationNode;
244 }
245
246 void setDestinationNode( const NodeID& node ) {
247 this->destinationNode = node;
248 }
249
250 const LinkID& getSourceLink() const {
251 return sourceLink;
252 }
253
254 void setSourceLink( const LinkID& link ) {
255 this->sourceLink = link;
256 setLinkMessage();
257 }
258
259 const LinkID& getDestinationLink() const {
260 return destinationLink;
261 }
262
263 void setDestinationLink( const LinkID& link ) {
264 this->destinationLink = link;
265 setLinkMessage();
266 }
267
268 void setSourceEndpoint( const EndpointDescriptor& endpoint ) {
269 sourceEndpoint = endpoint;
270 setContainsSourceEndpoint(true);
271 }
272
273 const EndpointDescriptor& getSourceEndpoint() const {
274 return sourceEndpoint;
275 }
276
277 /// swaps source and destination
278 void swapRoles() {
279 NodeID dummyNode = sourceNode;
280 sourceNode = destinationNode;
281 destinationNode = dummyNode;
282 LinkID dummyLink = sourceLink;
283 sourceLink = destinationLink;
284 destinationLink = dummyLink;
285 hops = 0;
286 }
287
288 const vector<NodeID> getRouteRecord() const {
289 return routeRecord;
290 }
291
292 void addRouteRecord( const NodeID& node ) {
293 if (isRouteRecord())
294 routeRecord.push_back(node);
295 }
296
297private:
298 uint8_t type, flags, hops, ttl;
299 ServiceID service;
300 NodeID sourceNode;
301 NodeID destinationNode;
302 LinkID sourceLink;
303 LinkID destinationLink;
304 EndpointDescriptor sourceEndpoint;
305 vector<NodeID> routeRecord;
306};
307
308}} // ariba::overlay
309
310/// serialization
311sznBeginDefault( ariba::overlay::OverlayMsg, X ){
312 // header
313 X && type && flags && hops && ttl;
314
315 // addresses
316 X && &service && &sourceNode && &destinationNode;
317
318 // message is associated with a end-to-end link
319 if (isLinkMessage())
320 X && &sourceLink && &destinationLink;
321
322 // message is associated with a source end-point
323 if (containsSourceEndpoint())
324 X && sourceEndpoint;
325
326 // message should record its route
327 if (isRouteRecord()) {
328 uint8_t size = routeRecord.size();
329 X && size;
330 if (X.isDeserializer()) routeRecord.resize(size);
331 for (uint8_t i=0;i<size; i++) X && &routeRecord[i];
332 }
333
334 // payload
335 X && Payload();
336} sznEnd();
337
338#endif // OVERLAY_MSG_H__
Note: See TracBrowser for help on using the repository browser.