| 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 | #include "OneHop.h"
|
---|
| 40 | #include "ariba/overlay/BaseOverlay.h"
|
---|
| 41 |
|
---|
| 42 | #include "ariba/overlay/modules/onehop/messages/OneHopMessage.h"
|
---|
| 43 | #include "ariba/overlay/modules/onehop/messages/NodeListingRequest.h"
|
---|
| 44 | #include "ariba/overlay/modules/onehop/messages/NodeListingReply.h"
|
---|
| 45 |
|
---|
| 46 | namespace ariba {
|
---|
| 47 | namespace overlay {
|
---|
| 48 |
|
---|
| 49 | use_logging_cpp( OneHop );
|
---|
| 50 |
|
---|
| 51 | OneHop::OneHop(BaseOverlay& _baseoverlay, const NodeID& _nodeid,
|
---|
| 52 | OverlayStructureEvents* _eventsReceiver, const OverlayParameterSet& param)
|
---|
| 53 | : OverlayInterface( _baseoverlay, _nodeid, _eventsReceiver, param ),
|
---|
| 54 | state ( OneHopStateInvalid ),
|
---|
| 55 | bootstrapLink ( LinkID::UNSPECIFIED ),
|
---|
| 56 | pendingLinks ( 0 ) {
|
---|
| 57 |
|
---|
| 58 | //
|
---|
| 59 | // insert us as the first node in the overlay
|
---|
| 60 | //
|
---|
| 61 | overlayNodes.insert( make_pair(_nodeid, LinkID::UNSPECIFIED) );
|
---|
| 62 |
|
---|
| 63 | Timer::setInterval(5000);
|
---|
| 64 | Timer::start();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | OneHop::~OneHop(){
|
---|
| 68 | Timer::stop();
|
---|
| 69 | deleteOverlay();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | const EndpointDescriptor& OneHop::resolveNode(const NodeID& node){
|
---|
| 73 |
|
---|
| 74 | OverlayNodeMapping::const_iterator i = overlayNodes.find( node );
|
---|
| 75 | if (i == overlayNodes.end()) return EndpointDescriptor::UNSPECIFIED;
|
---|
| 76 |
|
---|
| 77 | const EndpointDescriptor& ep = baseoverlay.getEndpointDescriptor( i->second );
|
---|
| 78 |
|
---|
| 79 | logging_debug( "resolved node " << node.toString() << " to endpoint " << ep.toString() );
|
---|
| 80 | return ep;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void OneHop::routeMessage(const NodeID& destnode, Message* msg){
|
---|
| 84 |
|
---|
| 85 | // in the fullmesh overlay we know every other node
|
---|
| 86 | // so we also have a link to each other node
|
---|
| 87 |
|
---|
| 88 | logging_debug( "routing message to node " << destnode.toString() );
|
---|
| 89 |
|
---|
| 90 | OverlayNodeMapping::const_iterator i = overlayNodes.find( destnode );
|
---|
| 91 | if (i == overlayNodes.end()) {
|
---|
| 92 | logging_error( "not able to route message to node " << destnode.toString() );
|
---|
| 93 | return;
|
---|
| 94 | }
|
---|
| 95 | OneHopMessage onehopRoute( OneHopMessage::OneHopMessageTypeRoute );
|
---|
| 96 | onehopRoute.encapsulate(msg);
|
---|
| 97 |
|
---|
| 98 | baseoverlay.sendMessage( &onehopRoute, i->second );
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | void OneHop::createOverlay() {
|
---|
| 102 | // don't need to bootstrap against ourselfs.
|
---|
| 103 | // the create and join process is completed now.
|
---|
| 104 | logging_info( "creating onehop overlay structure" );
|
---|
| 105 | state = OneHopStateCompleted;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void OneHop::deleteOverlay(){
|
---|
| 109 |
|
---|
| 110 | logging_info( "deleting onehop overlay structure" );
|
---|
| 111 | state = OneHopStateInvalid;
|
---|
| 112 | pendingLinks = 0;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | OverlayInterface::NodeList OneHop::getKnownNodes() const {
|
---|
| 116 |
|
---|
| 117 | OverlayInterface::NodeList retlist;
|
---|
| 118 |
|
---|
| 119 | OverlayNodeMapping::const_iterator i = overlayNodes.begin();
|
---|
| 120 | OverlayNodeMapping::const_iterator iend = overlayNodes.end();
|
---|
| 121 |
|
---|
| 122 | for( ; i != iend; i++ )
|
---|
| 123 | retlist.push_back( i->first );
|
---|
| 124 |
|
---|
| 125 | return retlist;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void OneHop::joinOverlay(const EndpointDescriptor& bootstrapEp){
|
---|
| 129 |
|
---|
| 130 | logging_info( "joining onehop overlay structure through end-point " <<
|
---|
| 131 | (bootstrapEp == EndpointDescriptor::UNSPECIFIED ?
|
---|
| 132 | "local" : bootstrapEp.toString()) );
|
---|
| 133 |
|
---|
| 134 | state = OneHopStateJoinInitiated;
|
---|
| 135 | pendingLinks = 0;
|
---|
| 136 |
|
---|
| 137 | if( bootstrapEp == EndpointDescriptor::UNSPECIFIED ){
|
---|
| 138 |
|
---|
| 139 | // we are the initiator and we are to bootstrap against
|
---|
| 140 | // ourselfs. in the onehop overlay this is not an issue
|
---|
| 141 | // and we can just ignore this call.
|
---|
| 142 |
|
---|
| 143 | state = OneHopStateCompleted;
|
---|
| 144 | } else {
|
---|
| 145 | bootstrapLink = baseoverlay.establishLink( bootstrapEp,
|
---|
| 146 | OverlayInterface::OVERLAY_SERVICE_ID );
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void OneHop::leaveOverlay(){
|
---|
| 151 |
|
---|
| 152 | logging_info( "leaving onehop overlay structure" );
|
---|
| 153 |
|
---|
| 154 | // set the state to invalid, this will prevent from
|
---|
| 155 | // handling onLinkDown events, as we are traversing the
|
---|
| 156 | // overlayNodes map and the onLinkDown function is called
|
---|
| 157 | // from the BaseOverlay and OneHop::onLinkDown will also
|
---|
| 158 | // try to access the overlayNodes structure.
|
---|
| 159 | state = OneHopStateInvalid;
|
---|
| 160 |
|
---|
| 161 | //
|
---|
| 162 | // send leave messages to all nodes. the nodes
|
---|
| 163 | // will then drop the links
|
---|
| 164 | //
|
---|
| 165 |
|
---|
| 166 | OverlayNodeMapping::iterator i = overlayNodes.begin();
|
---|
| 167 | OverlayNodeMapping::iterator iend = overlayNodes.end();
|
---|
| 168 |
|
---|
| 169 | for( ; i != iend; i++){
|
---|
| 170 | if( i->first != nodeid && i->second != LinkID::UNSPECIFIED ){
|
---|
| 171 |
|
---|
| 172 | OneHopMessage msg (OneHopMessage::OneHopMessageTypeLeave);
|
---|
| 173 | baseoverlay.sendMessage( &msg, i->second );
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | pendingLinks = 0;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | void OneHop::onLinkDown(const LinkID& lnk, const NodeID& remote){
|
---|
| 182 |
|
---|
| 183 | // don't handle when we are in state-invalid,
|
---|
| 184 | // see comment in OneHop::leaveOverlay
|
---|
| 185 | if( state == OneHopStateInvalid ) return;
|
---|
| 186 |
|
---|
| 187 | // node went down, remove from overlay mapping
|
---|
| 188 | logging_debug( "link " << lnk.toString() << " to node " << remote.toString() << " went down, removing node" );
|
---|
| 189 |
|
---|
| 190 | OverlayNodeMapping::iterator i = overlayNodes.begin();
|
---|
| 191 | OverlayNodeMapping::iterator iend = overlayNodes.end();
|
---|
| 192 |
|
---|
| 193 | for( ; i != iend; i++ ){
|
---|
| 194 | if( i->second == lnk ){
|
---|
| 195 | overlayNodes.erase( i );
|
---|
| 196 | break;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | void OneHop::onLinkUp(const LinkID& lnk, const NodeID& remote){
|
---|
| 202 |
|
---|
| 203 | //
|
---|
| 204 | // as soon as a link goes up, we always request the node listing.
|
---|
| 205 | // and try to get connections to as much nodes as possible in a greedy way.
|
---|
| 206 | //
|
---|
| 207 |
|
---|
| 208 | if( lnk != bootstrapLink ){
|
---|
| 209 | if( pendingLinks > 0 ) pendingLinks--;
|
---|
| 210 | if( pendingLinks == 0 ) state = OneHopStateCompleted;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | logging_debug( "link is up, sending out node listing request" );
|
---|
| 214 |
|
---|
| 215 | NodeListingRequest requestmsg;
|
---|
| 216 | OneHopMessage onemsg( OneHopMessage::OneHopMessageTypeListingRequest );
|
---|
| 217 | onemsg.encapsulate( &requestmsg );
|
---|
| 218 |
|
---|
| 219 | state = OneHopStateJoinListingRequested;
|
---|
| 220 | baseoverlay.sendMessage( &onemsg, lnk );
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | void OneHop::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk){
|
---|
| 224 |
|
---|
| 225 | OneHopMessage* onemsg = msg.getMessage()->convert<OneHopMessage>();
|
---|
| 226 | if( onemsg == NULL ) return;
|
---|
| 227 |
|
---|
| 228 | //
|
---|
| 229 | // handle node listing request
|
---|
| 230 | //
|
---|
| 231 |
|
---|
| 232 | if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingRequest ) ){
|
---|
| 233 |
|
---|
| 234 | NodeListingRequest* request = onemsg->decapsulate<NodeListingRequest>();
|
---|
| 235 |
|
---|
| 236 | logging_info( "onehop received node listing request from node " << remote.toString() );
|
---|
| 237 |
|
---|
| 238 | //
|
---|
| 239 | // first, insert the nodes and the link into our mapping
|
---|
| 240 | //
|
---|
| 241 |
|
---|
| 242 | overlayNodes.insert( make_pair(remote, lnk) );
|
---|
| 243 |
|
---|
| 244 | //
|
---|
| 245 | // send back a message with all nodes
|
---|
| 246 | // and their current EndpointDescriptor
|
---|
| 247 | //
|
---|
| 248 |
|
---|
| 249 | OneHopMessage onehopReply( OneHopMessage::OneHopMessageTypeListingReply );
|
---|
| 250 | NodeListingReply listingReply;
|
---|
| 251 |
|
---|
| 252 | OverlayNodeMapping::iterator i = overlayNodes.begin();
|
---|
| 253 | OverlayNodeMapping::iterator iend = overlayNodes.end();
|
---|
| 254 |
|
---|
| 255 | logging_debug( "sending out node listing reply with the following items" );
|
---|
| 256 |
|
---|
| 257 | for( ; i != iend; i++ ){
|
---|
| 258 |
|
---|
| 259 | const NodeID node = i->first;
|
---|
| 260 | const LinkID link = i->second;
|
---|
| 261 | const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
|
---|
| 262 |
|
---|
| 263 | logging_debug( "node: " + node.toString() + ", endp: " + endpoint.toString());
|
---|
| 264 | listingReply.add( node, const_cast<EndpointDescriptor*>(new EndpointDescriptor(endpoint)) );
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | onehopReply.encapsulate( &listingReply );
|
---|
| 268 | baseoverlay.sendMessage( &onehopReply, lnk );
|
---|
| 269 |
|
---|
| 270 | //
|
---|
| 271 | // now that we know the node, we can tell the baseoverlay
|
---|
| 272 | // that the node has joined our overlay structure
|
---|
| 273 | //
|
---|
| 274 |
|
---|
| 275 | eventsReceiver->onNodeJoin( remote );
|
---|
| 276 |
|
---|
| 277 | } // OneHopMessageTypeListingRequest
|
---|
| 278 |
|
---|
| 279 | //
|
---|
| 280 | // handle node listing reply
|
---|
| 281 | //
|
---|
| 282 |
|
---|
| 283 | if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingReply) ){
|
---|
| 284 |
|
---|
| 285 | NodeListingReply* reply = onemsg->decapsulate<NodeListingReply>();
|
---|
| 286 |
|
---|
| 287 | logging_debug( "received node listing reply from node " << remote.toString()
|
---|
| 288 | << " with all overlay nodes. connecting to all of them" );
|
---|
| 289 |
|
---|
| 290 | //
|
---|
| 291 | // get out all the EndpointDescriptors from the
|
---|
| 292 | // overlay nodes and connect to all nodes where
|
---|
| 293 | // we don't have a link yet
|
---|
| 294 | //
|
---|
| 295 |
|
---|
| 296 | const NodeListingReply::NodeEndpointList& endpoints = reply->getList();
|
---|
| 297 | logging_debug( "received " << endpoints.size() << " nodes in listing" );
|
---|
| 298 | pendingLinks = 0;
|
---|
| 299 |
|
---|
| 300 | NodeListingReply::NodeEndpointList::const_iterator i = endpoints.begin();
|
---|
| 301 | NodeListingReply::NodeEndpointList::const_iterator iend = endpoints.end();
|
---|
| 302 |
|
---|
| 303 | for( ; i != iend; i++ ){
|
---|
| 304 |
|
---|
| 305 | //
|
---|
| 306 | // don't connect to nodes that we already have
|
---|
| 307 | // a link to and don't connect to ourself
|
---|
| 308 | //
|
---|
| 309 |
|
---|
| 310 | const NodeID& node = (*i).first;
|
---|
| 311 | if( overlayNodes.find(node) != overlayNodes.end() ) continue;
|
---|
| 312 | if( node == nodeid ) continue;
|
---|
| 313 |
|
---|
| 314 | logging_debug( "building up link to node in overlay " << node.toString() );
|
---|
| 315 | const LinkID link = baseoverlay.establishLink( *((*i).second),
|
---|
| 316 | OverlayInterface::OVERLAY_SERVICE_ID );
|
---|
| 317 |
|
---|
| 318 | overlayNodes.insert( make_pair(node, link) );
|
---|
| 319 | pendingLinks++;
|
---|
| 320 |
|
---|
| 321 | } // for( ; i != iend; i++ )
|
---|
| 322 |
|
---|
| 323 | } // OneHopMessageTypeListingReply
|
---|
| 324 |
|
---|
| 325 | //
|
---|
| 326 | // handle node leaves
|
---|
| 327 | //
|
---|
| 328 |
|
---|
| 329 | if( onemsg->isType(OneHopMessage::OneHopMessageTypeLeave) ){
|
---|
| 330 |
|
---|
| 331 | logging_debug("received leave message from " <<
|
---|
| 332 | remote.toString() << " on link " << lnk.toString());
|
---|
| 333 |
|
---|
| 334 | // drop the link to the node
|
---|
| 335 | baseoverlay.dropLink( lnk );
|
---|
| 336 |
|
---|
| 337 | } // OneHopMessageTypeLeave
|
---|
| 338 |
|
---|
| 339 | //
|
---|
| 340 | // handle kbr route messages
|
---|
| 341 | //
|
---|
| 342 |
|
---|
| 343 | if( onemsg->isType( OneHopMessage::OneHopMessageTypeRoute) ){
|
---|
| 344 | logging_debug( "Route message arrived at destination node -> delegate to BaseOverlay" );
|
---|
| 345 | baseoverlay.incomingRouteMessage( onemsg );
|
---|
| 346 | } // OneHopMessageTypeRoute
|
---|
| 347 |
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | void OneHop::eventFunction(){
|
---|
| 351 |
|
---|
| 352 | logging_debug("<<<<<<<<<<<<<<<<onehop-table<<<<<<<<<<<<<<<<<<<");
|
---|
| 353 |
|
---|
| 354 | OverlayNodeMapping::iterator i = overlayNodes.begin();
|
---|
| 355 | OverlayNodeMapping::iterator iend = overlayNodes.end();
|
---|
| 356 |
|
---|
| 357 | for( ; i != iend; i++ ){
|
---|
| 358 |
|
---|
| 359 | const NodeID node = i->first;
|
---|
| 360 | const LinkID link = i->second;
|
---|
| 361 | const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
|
---|
| 362 |
|
---|
| 363 | logging_debug( "node: " << node.toString() <<
|
---|
| 364 | ", link_: " << link.toString() << ", endp: " << endpoint.toString());
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | logging_debug(">>>>>>>>>>>>>>>>>onehop-table>>>>>>>>>>>>>>>>>>>>>");
|
---|
| 368 |
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | }} // namespace ariba, overlay
|
---|