| 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 | 
 | 
|---|
| 52 | namespace ariba {
 | 
|---|
| 53 | namespace overlay {
 | 
|---|
| 54 | 
 | 
|---|
| 55 | using ariba::utility::LinkID;
 | 
|---|
| 56 | using ariba::utility::NodeID;
 | 
|---|
| 57 | using ariba::utility::ServiceID;
 | 
|---|
| 58 | using ariba::utility::Message;
 | 
|---|
| 59 | using ariba::communication::EndpointDescriptor;
 | 
|---|
| 60 | using_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 |  */
 | 
|---|
| 68 | class OverlayMsg: public Message { VSERIALIZEABLE;
 | 
|---|
| 69 | public:
 | 
|---|
| 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 |                 // topology signaling
 | 
|---|
| 92 |                 typeSignalingStart = 0x80, ///< start of the signaling types
 | 
|---|
| 93 |                 typeSignalingEnd = 0xFF    ///< end of the signaling types
 | 
|---|
| 94 |         };
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         /// default constructor
 | 
|---|
| 97 |         OverlayMsg(
 | 
|---|
| 98 |                 uint8_t type = typeInvalid,
 | 
|---|
| 99 |                 const ServiceID& _service      = ServiceID::UNSPECIFIED,
 | 
|---|
| 100 |                 const NodeID& _sourceNode      = NodeID::UNSPECIFIED,
 | 
|---|
| 101 |                 const NodeID& _destinationNode = NodeID::UNSPECIFIED,
 | 
|---|
| 102 |                 const LinkID& _sourceLink      = LinkID::UNSPECIFIED,
 | 
|---|
| 103 |                 const LinkID& _destinationLink = LinkID::UNSPECIFIED )
 | 
|---|
| 104 |         :       type(type), flags(0), hops(0), ttl(10),
 | 
|---|
| 105 |                 service(_service),
 | 
|---|
| 106 |                 sourceNode(_sourceNode), destinationNode(_destinationNode),
 | 
|---|
| 107 |                 sourceLink(_sourceLink), destinationLink(_destinationLink),
 | 
|---|
| 108 |                 routeRecord() {
 | 
|---|
| 109 |                 if (!_sourceLink.isUnspecified() || !_destinationLink.isUnspecified())
 | 
|---|
| 110 |                         setLinkMessage(true);
 | 
|---|
| 111 |         }
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         // copy constructor
 | 
|---|
| 114 |         OverlayMsg(const OverlayMsg& rhs)
 | 
|---|
| 115 |         :       type(rhs.type), flags(rhs.flags), hops(rhs.hops), ttl(rhs.ttl),
 | 
|---|
| 116 |                 service(rhs.service),
 | 
|---|
| 117 |                 sourceNode(rhs.sourceNode), destinationNode(rhs.destinationNode),
 | 
|---|
| 118 |                 sourceLink(rhs.sourceLink), destinationLink(rhs.destinationLink),
 | 
|---|
| 119 |                 routeRecord(rhs.routeRecord) {
 | 
|---|
| 120 |         }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |         /// destructor
 | 
|---|
| 123 |         ~OverlayMsg();
 | 
|---|
| 124 | 
 | 
|---|
| 125 |         /// type -------------------------------------------------------------------
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         type_ getType() const {
 | 
|---|
| 128 |                 return (type_) type;
 | 
|---|
| 129 |         }
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         void setType( type_ type ) {
 | 
|---|
| 132 |                 this->type = type;
 | 
|---|
| 133 |         }
 | 
|---|
| 134 | 
 | 
|---|
| 135 |         bool hasTypeMask( type_ mask ) const {
 | 
|---|
| 136 |                 return (type & (uint8_t)mask) == (uint8_t)mask;
 | 
|---|
| 137 |         }
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         /// flags ------------------------------------------------------------------
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         bool isRelayed() const {
 | 
|---|
| 142 |                 return (flags & 0x01)!=0;
 | 
|---|
| 143 |         }
 | 
|---|
| 144 | 
 | 
|---|
| 145 |         void setRelayed( bool relayed = true ) {
 | 
|---|
| 146 |                 if (relayed) flags |= 1; else flags &= ~1;
 | 
|---|
| 147 |         }
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         bool isRouteRecord() const {
 | 
|---|
| 150 |                 return (flags & 0x02)!=0;
 | 
|---|
| 151 |         }
 | 
|---|
| 152 | 
 | 
|---|
| 153 |         void setRouteRecord( bool route_record = true ) {
 | 
|---|
| 154 |                 if (route_record) flags |= 0x02; else flags &= ~0x02;
 | 
|---|
| 155 |         }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |         bool isAutoLink() const {
 | 
|---|
| 158 |                 return (flags & 0x80) == 0x80;
 | 
|---|
| 159 |         }
 | 
|---|
| 160 | 
 | 
|---|
| 161 |         void setAutoLink(bool auto_link = true ) {
 | 
|---|
| 162 |                 if (auto_link) flags |= 0x80; else flags &= ~0x80;
 | 
|---|
| 163 |         }
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         bool isLinkMessage() const {
 | 
|---|
| 166 |                 return (flags & 0x40)!=0;
 | 
|---|
| 167 |         }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |         void setLinkMessage(bool link_info = true ) {
 | 
|---|
| 170 |                 if (link_info) flags |= 0x40; else flags &= ~0x40;
 | 
|---|
| 171 |         }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | 
 | 
|---|
| 174 |         bool containsSourceEndpoint() const {
 | 
|---|
| 175 |                 return (flags & 0x20)!=0;
 | 
|---|
| 176 |         }
 | 
|---|
| 177 | 
 | 
|---|
| 178 |         void setContainsSourceEndpoint(bool contains_endpoint) {
 | 
|---|
| 179 |                 if (contains_endpoint) flags |= 0x20; else flags &= ~0x20;
 | 
|---|
| 180 |         }
 | 
|---|
| 181 | 
 | 
|---|
| 182 |         /// number of hops and time to live ----------------------------------------
 | 
|---|
| 183 | 
 | 
|---|
| 184 |         uint8_t getNumHops() const {
 | 
|---|
| 185 |                 return hops;
 | 
|---|
| 186 |         }
 | 
|---|
| 187 | 
 | 
|---|
| 188 |         void setNumHops( uint8_t hops ) {
 | 
|---|
| 189 |                 this->hops = hops;
 | 
|---|
| 190 |         }
 | 
|---|
| 191 | 
 | 
|---|
| 192 |         uint8_t increaseNumHops() {
 | 
|---|
| 193 |                 hops++;
 | 
|---|
| 194 |         }
 | 
|---|
| 195 | 
 | 
|---|
| 196 |         uint8_t getTimeToLive() const {
 | 
|---|
| 197 |                 return ttl;
 | 
|---|
| 198 |         }
 | 
|---|
| 199 | 
 | 
|---|
| 200 |         void setTimeToLive( uint8_t ttl ) {
 | 
|---|
| 201 |                 this->ttl = ttl;
 | 
|---|
| 202 |         }
 | 
|---|
| 203 | 
 | 
|---|
| 204 |         /// addresses and links ----------------------------------------------------
 | 
|---|
| 205 | 
 | 
|---|
| 206 |         const ServiceID& getService() const {
 | 
|---|
| 207 |                 return service;
 | 
|---|
| 208 |         }
 | 
|---|
| 209 | 
 | 
|---|
| 210 |         void setService( const ServiceID& service ) {
 | 
|---|
| 211 |                 this->service = service;
 | 
|---|
| 212 |         }
 | 
|---|
| 213 | 
 | 
|---|
| 214 |         const NodeID& getSourceNode() const {
 | 
|---|
| 215 |                 return sourceNode;
 | 
|---|
| 216 |         }
 | 
|---|
| 217 | 
 | 
|---|
| 218 |         void setSourceNode( const NodeID& node ) {
 | 
|---|
| 219 |                 this->sourceNode = node;
 | 
|---|
| 220 |         }
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         const NodeID& getDestinationNode() const {
 | 
|---|
| 223 |                 return destinationNode;
 | 
|---|
| 224 |         }
 | 
|---|
| 225 | 
 | 
|---|
| 226 |         void setDestinationNode( const NodeID& node ) {
 | 
|---|
| 227 |                 this->destinationNode = node;
 | 
|---|
| 228 |         }
 | 
|---|
| 229 | 
 | 
|---|
| 230 |         const LinkID& getSourceLink() const {
 | 
|---|
| 231 |                 return sourceLink;
 | 
|---|
| 232 |         }
 | 
|---|
| 233 | 
 | 
|---|
| 234 |         void setSourceLink( const LinkID& link ) {
 | 
|---|
| 235 |                 this->sourceLink = link;
 | 
|---|
| 236 |                 setLinkMessage();
 | 
|---|
| 237 |         }
 | 
|---|
| 238 | 
 | 
|---|
| 239 |         const LinkID& getDestinationLink() const {
 | 
|---|
| 240 |                 return destinationLink;
 | 
|---|
| 241 |         }
 | 
|---|
| 242 | 
 | 
|---|
| 243 |         void setDestinationLink( const LinkID& link ) {
 | 
|---|
| 244 |                 this->destinationLink = link;
 | 
|---|
| 245 |                 setLinkMessage();
 | 
|---|
| 246 |         }
 | 
|---|
| 247 | 
 | 
|---|
| 248 |         void setSourceEndpoint( const EndpointDescriptor& endpoint ) {
 | 
|---|
| 249 |                 sourceEndpoint = endpoint;
 | 
|---|
| 250 |                 setContainsSourceEndpoint(true);
 | 
|---|
| 251 |         }
 | 
|---|
| 252 | 
 | 
|---|
| 253 |         const EndpointDescriptor& getSourceEndpoint() const {
 | 
|---|
| 254 |                 return sourceEndpoint;
 | 
|---|
| 255 |         }
 | 
|---|
| 256 | 
 | 
|---|
| 257 |         /// swaps source and destination
 | 
|---|
| 258 |         void swapRoles() {
 | 
|---|
| 259 |                 NodeID dummyNode = sourceNode;
 | 
|---|
| 260 |                 sourceNode = destinationNode;
 | 
|---|
| 261 |                 destinationNode = dummyNode;
 | 
|---|
| 262 |                 LinkID dummyLink = sourceLink;
 | 
|---|
| 263 |                 sourceLink = destinationLink;
 | 
|---|
| 264 |                 destinationLink = dummyLink;
 | 
|---|
| 265 |                 hops = 0;
 | 
|---|
| 266 |         }
 | 
|---|
| 267 | 
 | 
|---|
| 268 |         const vector<NodeID> getRouteRecord() const {
 | 
|---|
| 269 |                 return routeRecord;
 | 
|---|
| 270 |         }
 | 
|---|
| 271 | 
 | 
|---|
| 272 |         void addRouteRecord( const NodeID& node ) {
 | 
|---|
| 273 |                 if (isRouteRecord())
 | 
|---|
| 274 |                         routeRecord.push_back(node);
 | 
|---|
| 275 |         }
 | 
|---|
| 276 | 
 | 
|---|
| 277 | private:
 | 
|---|
| 278 |         uint8_t type, flags, hops, ttl;
 | 
|---|
| 279 |         ServiceID service;
 | 
|---|
| 280 |         NodeID sourceNode;
 | 
|---|
| 281 |         NodeID destinationNode;
 | 
|---|
| 282 |         LinkID sourceLink;
 | 
|---|
| 283 |         LinkID destinationLink;
 | 
|---|
| 284 |         EndpointDescriptor sourceEndpoint;
 | 
|---|
| 285 |         vector<NodeID> routeRecord;
 | 
|---|
| 286 | };
 | 
|---|
| 287 | 
 | 
|---|
| 288 | }} // ariba::overlay
 | 
|---|
| 289 | 
 | 
|---|
| 290 | /// serialization
 | 
|---|
| 291 | sznBeginDefault( ariba::overlay::OverlayMsg, X ){
 | 
|---|
| 292 |         // header
 | 
|---|
| 293 |         X && type && flags && hops && ttl;
 | 
|---|
| 294 | 
 | 
|---|
| 295 |         // addresses
 | 
|---|
| 296 |         X && &service && &sourceNode && &destinationNode;
 | 
|---|
| 297 | 
 | 
|---|
| 298 |         // message is associated with a end-to-end link
 | 
|---|
| 299 |         if (isLinkMessage())
 | 
|---|
| 300 |                 X && &sourceLink && &destinationLink;
 | 
|---|
| 301 | 
 | 
|---|
| 302 |         // message is associated with a source end-point
 | 
|---|
| 303 |         if (containsSourceEndpoint())
 | 
|---|
| 304 |                 X && sourceEndpoint;
 | 
|---|
| 305 | 
 | 
|---|
| 306 |         // message should record its route
 | 
|---|
| 307 |         if (isRouteRecord()) {
 | 
|---|
| 308 |                 uint8_t size = routeRecord.size();
 | 
|---|
| 309 |                 X && size;
 | 
|---|
| 310 |                 if (X.isDeserializer()) routeRecord.resize(size);
 | 
|---|
| 311 |                 for (uint8_t i=0;i<size; i++) X && &routeRecord[i];
 | 
|---|
| 312 |         }
 | 
|---|
| 313 | 
 | 
|---|
| 314 |         // payload
 | 
|---|
| 315 |         X && Payload();
 | 
|---|
| 316 | } sznEnd();
 | 
|---|
| 317 | 
 | 
|---|
| 318 | #endif // OVERLAY_MSG_H__
 | 
|---|