| 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 "BaseOverlay.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include <sstream> | 
|---|
| 42 | #include <iostream> | 
|---|
| 43 | #include <string> | 
|---|
| 44 | #include <boost/foreach.hpp> | 
|---|
| 45 |  | 
|---|
| 46 | #include "ariba/NodeListener.h" | 
|---|
| 47 | #include "ariba/CommunicationListener.h" | 
|---|
| 48 | #include "ariba/SideportListener.h" | 
|---|
| 49 |  | 
|---|
| 50 | #include "ariba/overlay/LinkDescriptor.h" | 
|---|
| 51 |  | 
|---|
| 52 | #include "ariba/overlay/messages/OverlayMsg.h" | 
|---|
| 53 | #include "ariba/overlay/messages/JoinRequest.h" | 
|---|
| 54 | #include "ariba/overlay/messages/JoinReply.h" | 
|---|
| 55 |  | 
|---|
| 56 | #include "ariba/utility/misc/OvlVis.h" | 
|---|
| 57 |  | 
|---|
| 58 | namespace ariba { | 
|---|
| 59 | namespace overlay { | 
|---|
| 60 |  | 
|---|
| 61 | /* ***************************************************************************** | 
|---|
| 62 | * PREREQUESITES | 
|---|
| 63 | * ****************************************************************************/ | 
|---|
| 64 |  | 
|---|
| 65 | CommunicationListener* BaseOverlay::getListener( const ServiceID& service ) { | 
|---|
| 66 | if( !communicationListeners.contains( service ) ) { | 
|---|
| 67 | logging_error( "No listener found for service " << service.toString() ); | 
|---|
| 68 | return NULL; | 
|---|
| 69 | } | 
|---|
| 70 | CommunicationListener* listener = communicationListeners.get( service ); | 
|---|
| 71 | assert( listener != NULL ); | 
|---|
| 72 | return listener; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | // link descriptor handling ---------------------------------------------------- | 
|---|
| 76 |  | 
|---|
| 77 | LinkDescriptor* BaseOverlay::getDescriptor( const LinkID& link, bool communication ) { | 
|---|
| 78 | BOOST_FOREACH( LinkDescriptor* lp, links ) | 
|---|
| 79 | if ((communication ? lp->communicationId : lp->overlayId) == link) | 
|---|
| 80 | return lp; | 
|---|
| 81 | return NULL; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | const LinkDescriptor* BaseOverlay::getDescriptor( const LinkID& link, bool communication ) const { | 
|---|
| 85 | BOOST_FOREACH( const LinkDescriptor* lp, links ) | 
|---|
| 86 | if ((communication ? lp->communicationId : lp->overlayId) == link) | 
|---|
| 87 | return lp; | 
|---|
| 88 | return NULL; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /// erases a link descriptor | 
|---|
| 92 | void BaseOverlay::eraseDescriptor( const LinkID& link, bool communication ) { | 
|---|
| 93 | for ( vector<LinkDescriptor*>::iterator i = links.begin(); i!= links.end(); i++) { | 
|---|
| 94 | LinkDescriptor* ld = *i; | 
|---|
| 95 | if ((communication ? ld->communicationId : ld->overlayId) == link) { | 
|---|
| 96 | delete ld; | 
|---|
| 97 | links.erase(i); | 
|---|
| 98 | break; | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | /// adds a link descriptor | 
|---|
| 104 | LinkDescriptor* BaseOverlay::addDescriptor( const LinkID& link ) { | 
|---|
| 105 | LinkDescriptor* desc = getDescriptor( link ); | 
|---|
| 106 | if ( desc == NULL ) { | 
|---|
| 107 | desc = new LinkDescriptor(); | 
|---|
| 108 | if (!link.isUnspecified()) desc->overlayId = link; | 
|---|
| 109 | links.push_back(desc); | 
|---|
| 110 | } | 
|---|
| 111 | return desc; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | /// returns a auto-link descriptor | 
|---|
| 115 | LinkDescriptor* BaseOverlay::getAutoDescriptor( const NodeID& node, const ServiceID& service ) { | 
|---|
| 116 | // search for a descriptor that is already up | 
|---|
| 117 | BOOST_FOREACH( LinkDescriptor* lp, links ) | 
|---|
| 118 | if (lp->autolink && lp->remoteNode == node && lp->service == service && lp->up && lp->keepAliveMissed == 0) | 
|---|
| 119 | return lp; | 
|---|
| 120 | // if not found, search for one that is about to come up... | 
|---|
| 121 | BOOST_FOREACH( LinkDescriptor* lp, links ) | 
|---|
| 122 | if (lp->autolink && lp->remoteNode == node && lp->service == service && lp->keepAliveMissed == 0 ) | 
|---|
| 123 | return lp; | 
|---|
| 124 | return NULL; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /// stabilizes link information | 
|---|
| 128 | void BaseOverlay::stabilizeLinks() { | 
|---|
| 129 | // send keep-alive messages over established links | 
|---|
| 130 | BOOST_FOREACH( LinkDescriptor* ld, links ) { | 
|---|
| 131 | if (!ld->up) continue; | 
|---|
| 132 | OverlayMsg msg( OverlayMsg::typeLinkAlive, | 
|---|
| 133 | OverlayInterface::OVERLAY_SERVICE_ID, nodeId, ld->remoteNode ); | 
|---|
| 134 | msg.setRelayed(true); | 
|---|
| 135 | if (ld->relayed) msg.setRouteRecord(true); | 
|---|
| 136 | send_link( &msg, ld->overlayId ); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | // iterate over all links and check for time boundaries | 
|---|
| 140 | vector<LinkDescriptor*> oldlinks; | 
|---|
| 141 | time_t now = time(NULL); | 
|---|
| 142 | BOOST_FOREACH( LinkDescriptor* ld, links ) { | 
|---|
| 143 |  | 
|---|
| 144 | // keep alives and not up? yes-> link connection request is stale! | 
|---|
| 145 | if ( !ld->up && difftime( now, ld->keepAliveTime ) >= 2 ) { | 
|---|
| 146 |  | 
|---|
| 147 | // increase counter | 
|---|
| 148 | ld->keepAliveMissed++; | 
|---|
| 149 |  | 
|---|
| 150 | // missed more than four keep-alive messages (10 sec)? -> drop link | 
|---|
| 151 | if (ld->keepAliveMissed > 4) { | 
|---|
| 152 | logging_info( "Link connection request is stale, closing: " << ld ); | 
|---|
| 153 | oldlinks.push_back( ld ); | 
|---|
| 154 | continue; | 
|---|
| 155 | } | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | if (!ld->up) continue; | 
|---|
| 159 |  | 
|---|
| 160 | // remote used as relay flag | 
|---|
| 161 | if ( ld->relaying && difftime( now, ld->timeRelaying ) > 10) | 
|---|
| 162 | ld->relaying = false; | 
|---|
| 163 |  | 
|---|
| 164 | // drop links that are dropped and not used as relay | 
|---|
| 165 | if (ld->dropAfterRelaying && !ld->relaying && !ld->autolink) { | 
|---|
| 166 | oldlinks.push_back( ld ); | 
|---|
| 167 | continue; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | // auto-link time exceeded? | 
|---|
| 171 | if ( ld->autolink && difftime( now, ld->lastuse ) > 30 ) { | 
|---|
| 172 | oldlinks.push_back( ld ); | 
|---|
| 173 | continue; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | // keep alives missed? yes-> | 
|---|
| 177 | if ( difftime( now, ld->keepAliveTime ) > 2 ) { | 
|---|
| 178 |  | 
|---|
| 179 | // increase counter | 
|---|
| 180 | ld->keepAliveMissed++; | 
|---|
| 181 |  | 
|---|
| 182 | // missed more than four keep-alive messages (4 sec)? -> drop link | 
|---|
| 183 | if (ld->keepAliveMissed >= 4) { | 
|---|
| 184 | logging_info( "Link is stale, closing: " << ld ); | 
|---|
| 185 | oldlinks.push_back( ld ); | 
|---|
| 186 | continue; | 
|---|
| 187 | } | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | // drop links | 
|---|
| 192 | BOOST_FOREACH( LinkDescriptor* ld, oldlinks ) { | 
|---|
| 193 | logging_info( "Link timed out. Dropping " << ld ); | 
|---|
| 194 | ld->relaying = false; | 
|---|
| 195 | dropLink( ld->overlayId ); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | // show link state | 
|---|
| 199 | counter++; | 
|---|
| 200 | if (counter>=4) showLinks(); | 
|---|
| 201 | if (counter>=4 || counter<0) counter = 0; | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | /// shows the current link state | 
|---|
| 205 | void BaseOverlay::showLinks() { | 
|---|
| 206 | int i=0; | 
|---|
| 207 | logging_info("--- link state -------------------------------"); | 
|---|
| 208 | BOOST_FOREACH( LinkDescriptor* ld, links ) { | 
|---|
| 209 | logging_info("link " << i << ": " << ld); | 
|---|
| 210 | i++; | 
|---|
| 211 | } | 
|---|
| 212 | logging_info("----------------------------------------------"); | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | /// compares two arbitrary links to the same node | 
|---|
| 216 | int BaseOverlay::compare( const LinkID& lhs, const LinkID& rhs ) { | 
|---|
| 217 | LinkDescriptor* lhsld = getDescriptor(lhs); | 
|---|
| 218 | LinkDescriptor* rhsld = getDescriptor(rhs); | 
|---|
| 219 | if (lhsld==NULL || rhsld==NULL | 
|---|
| 220 | || !lhsld->up || !rhsld->up | 
|---|
| 221 | || lhsld->remoteNode != rhsld->remoteNode) return -1; | 
|---|
| 222 |  | 
|---|
| 223 | if ((lhsld->remoteLink^lhsld->overlayId)<(rhsld->remoteLink^lhsld->overlayId)  ) | 
|---|
| 224 | return -1; | 
|---|
| 225 |  | 
|---|
| 226 | return 1; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 |  | 
|---|
| 230 | // internal message delivery --------------------------------------------------- | 
|---|
| 231 |  | 
|---|
| 232 | /// routes a message to its destination node | 
|---|
| 233 | void BaseOverlay::route( OverlayMsg* message ) { | 
|---|
| 234 |  | 
|---|
| 235 | // exceeded time-to-live? yes-> drop message | 
|---|
| 236 | if (message->getNumHops() > message->getTimeToLive()) { | 
|---|
| 237 | logging_warn("Message exceeded TTL -> drop message!"); | 
|---|
| 238 | return; | 
|---|
| 239 |  | 
|---|
| 240 | // no-> forward message | 
|---|
| 241 | } else { | 
|---|
| 242 | // destinastion myself? yes-> handle message | 
|---|
| 243 | if (message->getDestinationNode() == nodeId) { | 
|---|
| 244 | logging_warn("Usually I should not route messages to myself!"); | 
|---|
| 245 | Message msg; | 
|---|
| 246 | msg.encapsulate(message); | 
|---|
| 247 | handleMessage( &msg, NULL ); | 
|---|
| 248 | } else { | 
|---|
| 249 | // no->send message to next hop | 
|---|
| 250 | send( message, message->getDestinationNode() ); | 
|---|
| 251 | } | 
|---|
| 252 | } | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | /// sends a message to another node, delivers it to the base overlay class | 
|---|
| 256 | seqnum_t BaseOverlay::send( OverlayMsg* message, const NodeID& destination ) { | 
|---|
| 257 | LinkDescriptor* next_link = NULL; | 
|---|
| 258 |  | 
|---|
| 259 | // drop messages to unspecified destinations | 
|---|
| 260 | if (destination.isUnspecified()) return -1; | 
|---|
| 261 |  | 
|---|
| 262 | // send messages to myself -> handle message and drop warning! | 
|---|
| 263 | if (destination == nodeId) { | 
|---|
| 264 | logging_warn("Sent message to myself. Handling message.") | 
|---|
| 265 | Message msg; | 
|---|
| 266 | msg.encapsulate(message); | 
|---|
| 267 | handleMessage( &msg, NULL ); | 
|---|
| 268 | return -1; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | // relay path known? yes-> send over relay link | 
|---|
| 272 | next_link = getRelayLinkTo( destination ); | 
|---|
| 273 | if (next_link != NULL) { | 
|---|
| 274 | if (message->isRelayed()) next_link->setRelaying(); | 
|---|
| 275 | return send(message, next_link); | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | // no-> relay path! route over overlay path | 
|---|
| 279 | LinkID next_id = overlayInterface->getNextLinkId( destination ); | 
|---|
| 280 | if (next_id.isUnspecified()) { | 
|---|
| 281 | logging_error("Could not send message. No next hop found to " << destination ); | 
|---|
| 282 | return -1; | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | // get link descriptor, up and running? yes-> send message | 
|---|
| 286 | next_link = getDescriptor(next_id); | 
|---|
| 287 | if (next_link != NULL && next_link->up) | 
|---|
| 288 | return send(message, next_link); | 
|---|
| 289 |  | 
|---|
| 290 | // no-> error, dropping message | 
|---|
| 291 | else { | 
|---|
| 292 | logging_error("Could not send message. Link not known or up"); | 
|---|
| 293 | return -1; | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | // not reached-> fail | 
|---|
| 297 | return -1; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | /// send a message using a link descriptor, delivers it to the base overlay class | 
|---|
| 301 | seqnum_t BaseOverlay::send( OverlayMsg* message, LinkDescriptor* ldr, bool ignore_down ) { | 
|---|
| 302 | // check if null | 
|---|
| 303 | if (ldr == NULL) { | 
|---|
| 304 | logging_error("Can not send message to " << message->getDestinationAddress()); | 
|---|
| 305 | return -1; | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | // check if up | 
|---|
| 309 | if (!ldr->up && !ignore_down) { | 
|---|
| 310 | logging_error("Can not send message. Link not up:" << ldr ); | 
|---|
| 311 | return -1; | 
|---|
| 312 | } | 
|---|
| 313 | LinkDescriptor* ld = NULL; | 
|---|
| 314 |  | 
|---|
| 315 | // handle relayed link | 
|---|
| 316 | if (ldr->relayed) { | 
|---|
| 317 | logging_debug("Resolving direct link for relayed link to " | 
|---|
| 318 | << ldr->remoteNode); | 
|---|
| 319 | message->setRelayed(); | 
|---|
| 320 | return send(message, ldr->remoteNode); | 
|---|
| 321 | } else | 
|---|
| 322 | ld = ldr; | 
|---|
| 323 |  | 
|---|
| 324 | // handle direct link | 
|---|
| 325 | if (ld->communicationUp) { | 
|---|
| 326 | logging_debug("send(): Sending message over direct link."); | 
|---|
| 327 | return bc->sendMessage( ld->communicationId, message ); | 
|---|
| 328 | } else { | 
|---|
| 329 | logging_error("send(): Could not send message. " | 
|---|
| 330 | "Not a relayed link and direct link is not up."); | 
|---|
| 331 | return -1; | 
|---|
| 332 | } | 
|---|
| 333 | return -1; | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | seqnum_t BaseOverlay::send_node( OverlayMsg* message, const NodeID& remote, | 
|---|
| 337 | const ServiceID& service) { | 
|---|
| 338 | message->setSourceNode(nodeId); | 
|---|
| 339 | message->setDestinationNode(remote); | 
|---|
| 340 | message->setService(service); | 
|---|
| 341 | send( message, remote ); | 
|---|
| 342 | } | 
|---|
| 343 |  | 
|---|
| 344 | seqnum_t BaseOverlay::send_link( OverlayMsg* message, const LinkID& link,bool ignore_down ) { | 
|---|
| 345 | LinkDescriptor* ld = getDescriptor(link); | 
|---|
| 346 | if (ld==NULL) { | 
|---|
| 347 | logging_error("Cannot find descriptor to link id=" << link.toString()); | 
|---|
| 348 | return -1; | 
|---|
| 349 | } | 
|---|
| 350 | message->setSourceNode(nodeId); | 
|---|
| 351 | message->setDestinationNode(ld->remoteNode); | 
|---|
| 352 |  | 
|---|
| 353 | message->setSourceLink(ld->overlayId); | 
|---|
| 354 | message->setDestinationLink(ld->remoteLink); | 
|---|
| 355 |  | 
|---|
| 356 | message->setService(ld->service); | 
|---|
| 357 | return send( message, ld, ignore_down ); | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | // relay route management ------------------------------------------------------ | 
|---|
| 361 |  | 
|---|
| 362 | /// stabilize relay information | 
|---|
| 363 | void BaseOverlay::stabilizeRelays() { | 
|---|
| 364 | vector<relay_route>::iterator i = relay_routes.begin(); | 
|---|
| 365 | while (i!=relay_routes.end() ) { | 
|---|
| 366 | relay_route& route = *i; | 
|---|
| 367 | LinkDescriptor* ld = getDescriptor(route.link); | 
|---|
| 368 | if (ld==NULL | 
|---|
| 369 | || !ld->up | 
|---|
| 370 | || ld->keepAliveMissed != 0 | 
|---|
| 371 | || !ld->communicationUp | 
|---|
| 372 | || difftime(route.used, time(NULL)) > 4) { | 
|---|
| 373 | logging_info("Forgetting relay information to node " | 
|---|
| 374 | << route.node.toString() ); | 
|---|
| 375 | i = relay_routes.erase(i); | 
|---|
| 376 | } else | 
|---|
| 377 | i++; | 
|---|
| 378 | } | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | void BaseOverlay::removeRelayLink( const LinkID& link ) { | 
|---|
| 382 | vector<relay_route>::iterator i = relay_routes.begin(); | 
|---|
| 383 | while (i!=relay_routes.end() ) { | 
|---|
| 384 | relay_route& route = *i; | 
|---|
| 385 | if (route.link == link ) i = relay_routes.erase(i); else i++; | 
|---|
| 386 | } | 
|---|
| 387 | } | 
|---|
| 388 |  | 
|---|
| 389 | void BaseOverlay::removeRelayNode( const NodeID& remote ) { | 
|---|
| 390 | vector<relay_route>::iterator i = relay_routes.begin(); | 
|---|
| 391 | while (i!=relay_routes.end() ) { | 
|---|
| 392 | relay_route& route = *i; | 
|---|
| 393 | if (route.node == remote ) i = relay_routes.erase(i); else i++; | 
|---|
| 394 | } | 
|---|
| 395 | } | 
|---|
| 396 |  | 
|---|
| 397 | /// refreshes relay information | 
|---|
| 398 | void BaseOverlay::refreshRelayInformation( const OverlayMsg* message, LinkDescriptor* ld ) { | 
|---|
| 399 |  | 
|---|
| 400 | // handle relayed messages from real links only | 
|---|
| 401 | if (ld == NULL | 
|---|
| 402 | || ld->relayed | 
|---|
| 403 | || !message->isRelayed() | 
|---|
| 404 | //              || !(message->getService() == OverlayInterface::OVERLAY_SERVICE_ID) | 
|---|
| 405 | || message->getSourceNode()==nodeId ) return; | 
|---|
| 406 |  | 
|---|
| 407 | // check wheter this node is already part of the routing table | 
|---|
| 408 | LinkID next_link = overlayInterface->getNextLinkId(message->getSourceNode()); | 
|---|
| 409 | if (next_link == ld->overlayId) return; | 
|---|
| 410 | ld->setRelaying(); | 
|---|
| 411 |  | 
|---|
| 412 | // try to find source node | 
|---|
| 413 | BOOST_FOREACH( relay_route& route, relay_routes ) { | 
|---|
| 414 |  | 
|---|
| 415 | // relay route found? yes-> | 
|---|
| 416 | if ( route.node == message->getSourceNode() ) { | 
|---|
| 417 |  | 
|---|
| 418 | // refresh timer | 
|---|
| 419 | route.used = time(NULL); | 
|---|
| 420 |  | 
|---|
| 421 | // route has a shorter hop count? yes-> replace | 
|---|
| 422 | if (route.hops > message->getNumHops() ) { | 
|---|
| 423 | logging_info("Updating relay information to node " | 
|---|
| 424 | << route.node.toString() | 
|---|
| 425 | << " reducing to " << message->getNumHops() << " hops."); | 
|---|
| 426 | route.hops = message->getNumHops(); | 
|---|
| 427 | route.link = ld->overlayId; | 
|---|
| 428 | } | 
|---|
| 429 | return; | 
|---|
| 430 | } | 
|---|
| 431 | } | 
|---|
| 432 |  | 
|---|
| 433 | // not found-> add new entry | 
|---|
| 434 | relay_route route; | 
|---|
| 435 | route.hops = message->getNumHops(); | 
|---|
| 436 | route.link = ld->overlayId; | 
|---|
| 437 | route.node = message->getSourceNode(); | 
|---|
| 438 | route.used = time(NULL); | 
|---|
| 439 | logging_info("Remembering relay information to node " << route.node.toString()); | 
|---|
| 440 | relay_routes.push_back(route); | 
|---|
| 441 | } | 
|---|
| 442 |  | 
|---|
| 443 | /// returns a known "vital" relay link which is up and running | 
|---|
| 444 | LinkDescriptor* BaseOverlay::getRelayLinkTo( const NodeID& remote ) { | 
|---|
| 445 | // try to find source node | 
|---|
| 446 | BOOST_FOREACH( relay_route& route, relay_routes ) { | 
|---|
| 447 | if (route.node == remote ) { | 
|---|
| 448 | LinkDescriptor* ld = getDescriptor( route.link ); | 
|---|
| 449 | if (ld==NULL || !ld->up) return NULL; else { | 
|---|
| 450 | route.used = time(NULL); | 
|---|
| 451 | return ld; | 
|---|
| 452 | } | 
|---|
| 453 | } | 
|---|
| 454 | } | 
|---|
| 455 | return NULL; | 
|---|
| 456 | } | 
|---|
| 457 |  | 
|---|
| 458 | /* ***************************************************************************** | 
|---|
| 459 | * PUBLIC MEMBERS | 
|---|
| 460 | * ****************************************************************************/ | 
|---|
| 461 |  | 
|---|
| 462 | use_logging_cpp(BaseOverlay); | 
|---|
| 463 |  | 
|---|
| 464 | // ---------------------------------------------------------------------------- | 
|---|
| 465 |  | 
|---|
| 466 | BaseOverlay::BaseOverlay() : | 
|---|
| 467 | bc(NULL), overlayInterface(NULL), nodeId(NodeID::UNSPECIFIED), | 
|---|
| 468 | spovnetId(SpoVNetID::UNSPECIFIED), state(BaseOverlayStateInvalid), | 
|---|
| 469 | sideport(&SideportListener::DEFAULT), started(false), counter(0) { | 
|---|
| 470 | } | 
|---|
| 471 |  | 
|---|
| 472 | BaseOverlay::~BaseOverlay() { | 
|---|
| 473 | } | 
|---|
| 474 |  | 
|---|
| 475 | // ---------------------------------------------------------------------------- | 
|---|
| 476 |  | 
|---|
| 477 | void BaseOverlay::start( BaseCommunication& _basecomm, const NodeID& _nodeid ) { | 
|---|
| 478 | logging_info("Starting..."); | 
|---|
| 479 |  | 
|---|
| 480 | // set parameters | 
|---|
| 481 | bc = &_basecomm; | 
|---|
| 482 | nodeId = _nodeid; | 
|---|
| 483 |  | 
|---|
| 484 | // register at base communication | 
|---|
| 485 | bc->registerMessageReceiver( this ); | 
|---|
| 486 | bc->registerEventListener( this ); | 
|---|
| 487 |  | 
|---|
| 488 | // timer for auto link management | 
|---|
| 489 | Timer::setInterval( 1000 ); | 
|---|
| 490 | Timer::start(); | 
|---|
| 491 |  | 
|---|
| 492 | started = true; | 
|---|
| 493 | state = BaseOverlayStateInvalid; | 
|---|
| 494 | } | 
|---|
| 495 |  | 
|---|
| 496 | void BaseOverlay::stop() { | 
|---|
| 497 | logging_info("Stopping..."); | 
|---|
| 498 |  | 
|---|
| 499 | // stop timer | 
|---|
| 500 | Timer::stop(); | 
|---|
| 501 |  | 
|---|
| 502 | // delete oberlay interface | 
|---|
| 503 | if(overlayInterface != NULL) { | 
|---|
| 504 | delete overlayInterface; | 
|---|
| 505 | overlayInterface = NULL; | 
|---|
| 506 | } | 
|---|
| 507 |  | 
|---|
| 508 | // unregister at base communication | 
|---|
| 509 | bc->unregisterMessageReceiver( this ); | 
|---|
| 510 | bc->unregisterEventListener( this ); | 
|---|
| 511 |  | 
|---|
| 512 | started = false; | 
|---|
| 513 | state = BaseOverlayStateInvalid; | 
|---|
| 514 | } | 
|---|
| 515 |  | 
|---|
| 516 | bool BaseOverlay::isStarted(){ | 
|---|
| 517 | return started; | 
|---|
| 518 | } | 
|---|
| 519 |  | 
|---|
| 520 | // ---------------------------------------------------------------------------- | 
|---|
| 521 |  | 
|---|
| 522 | void BaseOverlay::joinSpoVNet(const SpoVNetID& id, | 
|---|
| 523 | const EndpointDescriptor& bootstrapEp) { | 
|---|
| 524 |  | 
|---|
| 525 | if(id != spovnetId){ | 
|---|
| 526 | logging_error("attempt to join against invalid spovnet, call initiate first"); | 
|---|
| 527 | return; | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 |  | 
|---|
| 531 | //ovl.visShowNodeBubble ( ovlId, nodeId, "joining..." ); | 
|---|
| 532 | logging_info( "Starting to join spovnet " << id.toString() << | 
|---|
| 533 | " with nodeid " << nodeId.toString()); | 
|---|
| 534 |  | 
|---|
| 535 | if(bootstrapEp.isUnspecified() && state == BaseOverlayStateInvalid){ | 
|---|
| 536 |  | 
|---|
| 537 | // bootstrap against ourselfs | 
|---|
| 538 | logging_debug("joining spovnet locally"); | 
|---|
| 539 |  | 
|---|
| 540 | overlayInterface->joinOverlay(); | 
|---|
| 541 | state = BaseOverlayStateCompleted; | 
|---|
| 542 | BOOST_FOREACH( NodeListener* i, nodeListeners ) | 
|---|
| 543 | i->onJoinCompleted( spovnetId ); | 
|---|
| 544 |  | 
|---|
| 545 | //ovl.visChangeNodeIcon ( ovlId, nodeId, OvlVis::ICON_ID_CAMERA ); | 
|---|
| 546 | //ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN ); | 
|---|
| 547 |  | 
|---|
| 548 | logging_debug("starting overlay bootstrap module"); | 
|---|
| 549 | overlayBootstrap.start(this, spovnetId, nodeId); | 
|---|
| 550 | overlayBootstrap.publish(bc->getEndpointDescriptor()); | 
|---|
| 551 |  | 
|---|
| 552 | } else { | 
|---|
| 553 |  | 
|---|
| 554 | // bootstrap against another node | 
|---|
| 555 | logging_debug("joining spovnet remotely against " << bootstrapEp.toString()); | 
|---|
| 556 |  | 
|---|
| 557 | const LinkID& lnk = bc->establishLink( bootstrapEp ); | 
|---|
| 558 | bootstrapLinks.push_back(lnk); | 
|---|
| 559 | logging_info("join process initiated for " << id.toString() << "..."); | 
|---|
| 560 | } | 
|---|
| 561 | } | 
|---|
| 562 |  | 
|---|
| 563 | void BaseOverlay::leaveSpoVNet() { | 
|---|
| 564 |  | 
|---|
| 565 | logging_info( "Leaving spovnet " << spovnetId ); | 
|---|
| 566 | bool ret = ( state != this->BaseOverlayStateInvalid ); | 
|---|
| 567 |  | 
|---|
| 568 | logging_debug("stopping overlay bootstrap module"); | 
|---|
| 569 | overlayBootstrap.stop(); | 
|---|
| 570 | overlayBootstrap.revoke(); | 
|---|
| 571 |  | 
|---|
| 572 | logging_debug( "Dropping all auto-links" ); | 
|---|
| 573 |  | 
|---|
| 574 | // gather all service links | 
|---|
| 575 | vector<LinkID> servicelinks; | 
|---|
| 576 | BOOST_FOREACH( LinkDescriptor* ld, links ) { | 
|---|
| 577 | if( ld->service != OverlayInterface::OVERLAY_SERVICE_ID ) | 
|---|
| 578 | servicelinks.push_back( ld->overlayId ); | 
|---|
| 579 | } | 
|---|
| 580 |  | 
|---|
| 581 | // drop all service links | 
|---|
| 582 | BOOST_FOREACH( LinkID lnk, servicelinks ) | 
|---|
| 583 | dropLink( lnk ); | 
|---|
| 584 |  | 
|---|
| 585 | // let the node leave the spovnet overlay interface | 
|---|
| 586 | logging_debug( "Leaving overlay" ); | 
|---|
| 587 | if( overlayInterface != NULL ) | 
|---|
| 588 | overlayInterface->leaveOverlay(); | 
|---|
| 589 |  | 
|---|
| 590 | // drop still open bootstrap links | 
|---|
| 591 | BOOST_FOREACH( LinkID lnk, bootstrapLinks ) | 
|---|
| 592 | bc->dropLink( lnk ); | 
|---|
| 593 |  | 
|---|
| 594 | // change to inalid state | 
|---|
| 595 | state = BaseOverlayStateInvalid; | 
|---|
| 596 | //ovl.visShutdown( ovlId, nodeId, string("") ); | 
|---|
| 597 |  | 
|---|
| 598 | // inform all registered services of the event | 
|---|
| 599 | BOOST_FOREACH( NodeListener* i, nodeListeners ) { | 
|---|
| 600 | if( ret ) i->onLeaveCompleted( spovnetId ); | 
|---|
| 601 | else i->onLeaveFailed( spovnetId ); | 
|---|
| 602 | } | 
|---|
| 603 | } | 
|---|
| 604 |  | 
|---|
| 605 | void BaseOverlay::createSpoVNet(const SpoVNetID& id, | 
|---|
| 606 | const OverlayParameterSet& param, | 
|---|
| 607 | const SecurityParameterSet& sec, | 
|---|
| 608 | const QoSParameterSet& qos) { | 
|---|
| 609 |  | 
|---|
| 610 | // set the state that we are an initiator, this way incoming messages are | 
|---|
| 611 | // handled correctly | 
|---|
| 612 | logging_info( "creating spovnet " + id.toString() << | 
|---|
| 613 | " with nodeid " << nodeId.toString() ); | 
|---|
| 614 |  | 
|---|
| 615 | spovnetId = id; | 
|---|
| 616 |  | 
|---|
| 617 | overlayInterface = OverlayFactory::create( *this, param, nodeId, this ); | 
|---|
| 618 | if( overlayInterface == NULL ) { | 
|---|
| 619 | logging_fatal( "overlay structure not supported" ); | 
|---|
| 620 | state = BaseOverlayStateInvalid; | 
|---|
| 621 |  | 
|---|
| 622 | BOOST_FOREACH( NodeListener* i, nodeListeners ) | 
|---|
| 623 | i->onJoinFailed( spovnetId ); | 
|---|
| 624 |  | 
|---|
| 625 | return; | 
|---|
| 626 | } | 
|---|
| 627 | } | 
|---|
| 628 |  | 
|---|
| 629 | // ---------------------------------------------------------------------------- | 
|---|
| 630 |  | 
|---|
| 631 | const LinkID BaseOverlay::establishLink( const EndpointDescriptor& remoteEp, | 
|---|
| 632 | const NodeID& remoteId, const ServiceID& service ) { | 
|---|
| 633 |  | 
|---|
| 634 | // establish link via overlay | 
|---|
| 635 | if (!remoteId.isUnspecified()) | 
|---|
| 636 | return establishLink( remoteId, service ); | 
|---|
| 637 | else | 
|---|
| 638 |  | 
|---|
| 639 | // establish link directly if only ep is known | 
|---|
| 640 | if (remoteId.isUnspecified()) | 
|---|
| 641 | return establishDirectLink(remoteEp, service ); | 
|---|
| 642 |  | 
|---|
| 643 | } | 
|---|
| 644 |  | 
|---|
| 645 | /// call base communication's establish link and add link mapping | 
|---|
| 646 | const LinkID BaseOverlay::establishDirectLink( const EndpointDescriptor& ep, | 
|---|
| 647 | const ServiceID& service ) { | 
|---|
| 648 |  | 
|---|
| 649 | /// find a service listener | 
|---|
| 650 | if( !communicationListeners.contains( service ) ) { | 
|---|
| 651 | logging_error( "No listener registered for service id=" << service.toString() ); | 
|---|
| 652 | return LinkID::UNSPECIFIED; | 
|---|
| 653 | } | 
|---|
| 654 | CommunicationListener* listener = communicationListeners.get( service ); | 
|---|
| 655 | assert( listener != NULL ); | 
|---|
| 656 |  | 
|---|
| 657 | // create descriptor | 
|---|
| 658 | LinkDescriptor* ld = addDescriptor(); | 
|---|
| 659 | ld->relayed = false; | 
|---|
| 660 | ld->listener = listener; | 
|---|
| 661 | ld->service = service; | 
|---|
| 662 | ld->communicationId = bc->establishLink( ep ); | 
|---|
| 663 |  | 
|---|
| 664 | /// establish link and add mapping | 
|---|
| 665 | logging_info("Establishing direct link " << ld->communicationId.toString() | 
|---|
| 666 | << " using " << ep.toString()); | 
|---|
| 667 |  | 
|---|
| 668 | return ld->communicationId; | 
|---|
| 669 | } | 
|---|
| 670 |  | 
|---|
| 671 | /// establishes a link between two arbitrary nodes | 
|---|
| 672 | const LinkID BaseOverlay::establishLink( const NodeID& remote, | 
|---|
| 673 | const ServiceID& service ) { | 
|---|
| 674 |  | 
|---|
| 675 | // do not establish a link to myself! | 
|---|
| 676 | if (remote == nodeId) return LinkID::UNSPECIFIED; | 
|---|
| 677 |  | 
|---|
| 678 | // create a link descriptor | 
|---|
| 679 | LinkDescriptor* ld = addDescriptor(); | 
|---|
| 680 | ld->relayed = true; | 
|---|
| 681 | ld->remoteNode = remote; | 
|---|
| 682 | ld->service = service; | 
|---|
| 683 | ld->listener = getListener(ld->service); | 
|---|
| 684 |  | 
|---|
| 685 | // create link request message | 
|---|
| 686 | OverlayMsg msg(OverlayMsg::typeLinkRequest, service, nodeId, remote ); | 
|---|
| 687 | msg.setSourceLink(ld->overlayId); | 
|---|
| 688 | msg.setRelayed(true); | 
|---|
| 689 |  | 
|---|
| 690 | // debug message | 
|---|
| 691 | logging_info( | 
|---|
| 692 | "Sending link request with" | 
|---|
| 693 | << " link=" << ld->overlayId.toString() | 
|---|
| 694 | << " node=" << ld->remoteNode.toString() | 
|---|
| 695 | << " serv=" << ld->service.toString() | 
|---|
| 696 | ); | 
|---|
| 697 |  | 
|---|
| 698 | // sending message to node | 
|---|
| 699 | send_node( &msg, ld->remoteNode, ld->service ); | 
|---|
| 700 |  | 
|---|
| 701 | return ld->overlayId; | 
|---|
| 702 | } | 
|---|
| 703 |  | 
|---|
| 704 | /// drops an established link | 
|---|
| 705 | void BaseOverlay::dropLink(const LinkID& link) { | 
|---|
| 706 | logging_info( "Dropping link (initiated locally):" << link.toString() ); | 
|---|
| 707 |  | 
|---|
| 708 | // find the link item to drop | 
|---|
| 709 | LinkDescriptor* ld = getDescriptor(link); | 
|---|
| 710 | if( ld == NULL ) { | 
|---|
| 711 | logging_warn( "Can't drop link, link is unknown!"); | 
|---|
| 712 | return; | 
|---|
| 713 | } | 
|---|
| 714 |  | 
|---|
| 715 | // delete all queued messages | 
|---|
| 716 | if( ld->messageQueue.size() > 0 ) { | 
|---|
| 717 | logging_warn( "Dropping link " << ld->overlayId.toString() << " that has " | 
|---|
| 718 | << ld->messageQueue.size() << " waiting messages" ); | 
|---|
| 719 | ld->flushQueue(); | 
|---|
| 720 | } | 
|---|
| 721 |  | 
|---|
| 722 | // inform sideport and listener | 
|---|
| 723 | ld->listener->onLinkDown( ld->overlayId, ld->remoteNode ); | 
|---|
| 724 | sideport->onLinkDown(ld->overlayId, this->nodeId, ld->remoteNode, this->spovnetId ); | 
|---|
| 725 |  | 
|---|
| 726 | // do not drop relay links | 
|---|
| 727 | if (!ld->relaying) { | 
|---|
| 728 | // drop the link in base communication | 
|---|
| 729 | if (ld->communicationUp) bc->dropLink( ld->communicationId ); | 
|---|
| 730 |  | 
|---|
| 731 | // erase descriptor | 
|---|
| 732 | eraseDescriptor( ld->overlayId ); | 
|---|
| 733 | } else { | 
|---|
| 734 | ld->dropAfterRelaying = true; | 
|---|
| 735 | } | 
|---|
| 736 | } | 
|---|
| 737 |  | 
|---|
| 738 | // ---------------------------------------------------------------------------- | 
|---|
| 739 |  | 
|---|
| 740 | /// internal send message, always use this functions to send messages over links | 
|---|
| 741 | seqnum_t BaseOverlay::sendMessage( const Message* message, const LinkID& link ) { | 
|---|
| 742 | logging_debug( "Sending data message on link " << link.toString() ); | 
|---|
| 743 |  | 
|---|
| 744 | // get the mapping for this link | 
|---|
| 745 | LinkDescriptor* ld = getDescriptor(link); | 
|---|
| 746 | if( ld == NULL ) { | 
|---|
| 747 | logging_error("Could not send message. " | 
|---|
| 748 | << "Link not found id=" << link.toString()); | 
|---|
| 749 | return -1; | 
|---|
| 750 | } | 
|---|
| 751 |  | 
|---|
| 752 | // check if the link is up yet, if its an auto link queue message | 
|---|
| 753 | if( !ld->up ) { | 
|---|
| 754 | ld->setAutoUsed(); | 
|---|
| 755 | if( ld->autolink ) { | 
|---|
| 756 | logging_info("Auto-link " << link.toString() << " not up, queue message"); | 
|---|
| 757 | Data data = data_serialize( message ); | 
|---|
| 758 | const_cast<Message*>(message)->dropPayload(); | 
|---|
| 759 | ld->messageQueue.push_back( new Message(data) ); | 
|---|
| 760 | } else { | 
|---|
| 761 | logging_error("Link " << link.toString() << " not up, drop message"); | 
|---|
| 762 | } | 
|---|
| 763 | return -1; | 
|---|
| 764 | } | 
|---|
| 765 |  | 
|---|
| 766 | // compile overlay message (has service and node id) | 
|---|
| 767 | OverlayMsg overmsg( OverlayMsg::typeData ); | 
|---|
| 768 | overmsg.encapsulate( const_cast<Message*>(message) ); | 
|---|
| 769 |  | 
|---|
| 770 | // send message over relay/direct/overlay | 
|---|
| 771 | return send_link( &overmsg, ld->overlayId ); | 
|---|
| 772 | } | 
|---|
| 773 |  | 
|---|
| 774 | seqnum_t BaseOverlay::sendMessage(const Message* message, | 
|---|
| 775 | const NodeID& node, const ServiceID& service) { | 
|---|
| 776 |  | 
|---|
| 777 | // find link for node and service | 
|---|
| 778 | LinkDescriptor* ld = getAutoDescriptor( node, service ); | 
|---|
| 779 |  | 
|---|
| 780 | // if we found no link, create an auto link | 
|---|
| 781 | if( ld == NULL ) { | 
|---|
| 782 |  | 
|---|
| 783 | // debug output | 
|---|
| 784 | logging_info( "No link to send message to node " | 
|---|
| 785 | << node.toString() << " found for service " | 
|---|
| 786 | << service.toString() << ". Creating auto link ..." | 
|---|
| 787 | ); | 
|---|
| 788 |  | 
|---|
| 789 | // call base overlay to create a link | 
|---|
| 790 | LinkID link = establishLink( node, service ); | 
|---|
| 791 | ld = getDescriptor( link ); | 
|---|
| 792 | if( ld == NULL ) { | 
|---|
| 793 | logging_error( "Failed to establish auto-link."); | 
|---|
| 794 | return -1; | 
|---|
| 795 | } | 
|---|
| 796 | ld->autolink = true; | 
|---|
| 797 |  | 
|---|
| 798 | logging_debug( "Auto-link establishment in progress to node " | 
|---|
| 799 | << node.toString() << " with link id=" << link.toString() ); | 
|---|
| 800 | } | 
|---|
| 801 | assert(ld != NULL); | 
|---|
| 802 |  | 
|---|
| 803 | // mark the link as used, as we now send a message through it | 
|---|
| 804 | ld->setAutoUsed(); | 
|---|
| 805 |  | 
|---|
| 806 | // send / queue message | 
|---|
| 807 | return sendMessage( message, ld->overlayId ); | 
|---|
| 808 | } | 
|---|
| 809 |  | 
|---|
| 810 | // ---------------------------------------------------------------------------- | 
|---|
| 811 |  | 
|---|
| 812 | const EndpointDescriptor& BaseOverlay::getEndpointDescriptor( | 
|---|
| 813 | const LinkID& link) const { | 
|---|
| 814 |  | 
|---|
| 815 | // return own end-point descriptor | 
|---|
| 816 | if( link == LinkID::UNSPECIFIED ) | 
|---|
| 817 | return bc->getEndpointDescriptor(); | 
|---|
| 818 |  | 
|---|
| 819 | // find link descriptor. not found -> return unspecified | 
|---|
| 820 | const LinkDescriptor* ld = getDescriptor(link); | 
|---|
| 821 | if (ld==NULL) return EndpointDescriptor::UNSPECIFIED(); | 
|---|
| 822 |  | 
|---|
| 823 | // return endpoint-descriptor from base communication | 
|---|
| 824 | return bc->getEndpointDescriptor( ld->communicationId ); | 
|---|
| 825 | } | 
|---|
| 826 |  | 
|---|
| 827 | const EndpointDescriptor& BaseOverlay::getEndpointDescriptor( | 
|---|
| 828 | const NodeID& node) const { | 
|---|
| 829 |  | 
|---|
| 830 | // return own end-point descriptor | 
|---|
| 831 | if( node == nodeId || node == NodeID::UNSPECIFIED ) | 
|---|
| 832 | return bc->getEndpointDescriptor(); | 
|---|
| 833 |  | 
|---|
| 834 | // no joined and request remote descriptor? -> fail! | 
|---|
| 835 | if( overlayInterface == NULL ) { | 
|---|
| 836 | logging_error( "overlay interface not set, cannot resolve endpoint" ); | 
|---|
| 837 | return EndpointDescriptor::UNSPECIFIED(); | 
|---|
| 838 | } | 
|---|
| 839 |  | 
|---|
| 840 | // resolve end-point descriptor from the base-overlay routing table | 
|---|
| 841 | return overlayInterface->resolveNode( node ); | 
|---|
| 842 | } | 
|---|
| 843 |  | 
|---|
| 844 | // ---------------------------------------------------------------------------- | 
|---|
| 845 |  | 
|---|
| 846 | bool BaseOverlay::registerSidePort(SideportListener* _sideport) { | 
|---|
| 847 | sideport = _sideport; | 
|---|
| 848 | _sideport->configure( this ); | 
|---|
| 849 | } | 
|---|
| 850 |  | 
|---|
| 851 | bool BaseOverlay::unregisterSidePort(SideportListener* _sideport) { | 
|---|
| 852 | sideport = &SideportListener::DEFAULT; | 
|---|
| 853 | } | 
|---|
| 854 |  | 
|---|
| 855 | // ---------------------------------------------------------------------------- | 
|---|
| 856 |  | 
|---|
| 857 | bool BaseOverlay::bind(CommunicationListener* listener, const ServiceID& sid) { | 
|---|
| 858 | logging_debug( "binding communication listener " << listener | 
|---|
| 859 | << " on serviceid " << sid.toString() ); | 
|---|
| 860 |  | 
|---|
| 861 | if( communicationListeners.contains( sid ) ) { | 
|---|
| 862 | logging_error( "some listener already registered for service id " | 
|---|
| 863 | << sid.toString() ); | 
|---|
| 864 | return false; | 
|---|
| 865 | } | 
|---|
| 866 |  | 
|---|
| 867 | communicationListeners.registerItem( listener, sid ); | 
|---|
| 868 | return true; | 
|---|
| 869 | } | 
|---|
| 870 |  | 
|---|
| 871 |  | 
|---|
| 872 | bool BaseOverlay::unbind(CommunicationListener* listener, const ServiceID& sid) { | 
|---|
| 873 | logging_debug( "unbinding listener " << listener << " from serviceid " << sid.toString() ); | 
|---|
| 874 |  | 
|---|
| 875 | if( !communicationListeners.contains( sid ) ) { | 
|---|
| 876 | logging_warn( "cannot unbind listener. no listener registered on service id " << sid.toString() ); | 
|---|
| 877 | return false; | 
|---|
| 878 | } | 
|---|
| 879 |  | 
|---|
| 880 | if( communicationListeners.get(sid) != listener ) { | 
|---|
| 881 | logging_warn( "listener bound to service id " << sid.toString() | 
|---|
| 882 | << " is different than listener trying to unbind" ); | 
|---|
| 883 | return false; | 
|---|
| 884 | } | 
|---|
| 885 |  | 
|---|
| 886 | communicationListeners.unregisterItem( sid ); | 
|---|
| 887 | return true; | 
|---|
| 888 | } | 
|---|
| 889 |  | 
|---|
| 890 | // ---------------------------------------------------------------------------- | 
|---|
| 891 |  | 
|---|
| 892 | bool BaseOverlay::bind(NodeListener* listener) { | 
|---|
| 893 | logging_debug( "Binding node listener " << listener ); | 
|---|
| 894 |  | 
|---|
| 895 | // already bound? yes-> warning | 
|---|
| 896 | NodeListenerVector::iterator i = | 
|---|
| 897 | find( nodeListeners.begin(), nodeListeners.end(), listener ); | 
|---|
| 898 | if( i != nodeListeners.end() ) { | 
|---|
| 899 | logging_warn("Node listener " << listener << " is already bound!" ); | 
|---|
| 900 | return false; | 
|---|
| 901 | } | 
|---|
| 902 |  | 
|---|
| 903 | // no-> add | 
|---|
| 904 | nodeListeners.push_back( listener ); | 
|---|
| 905 | return true; | 
|---|
| 906 | } | 
|---|
| 907 |  | 
|---|
| 908 | bool BaseOverlay::unbind(NodeListener* listener) { | 
|---|
| 909 | logging_debug( "Unbinding node listener " << listener ); | 
|---|
| 910 |  | 
|---|
| 911 | // already unbound? yes-> warning | 
|---|
| 912 | NodeListenerVector::iterator i = find( nodeListeners.begin(), nodeListeners.end(), listener ); | 
|---|
| 913 | if( i == nodeListeners.end() ) { | 
|---|
| 914 | logging_warn( "Node listener " << listener << " is not bound!" ); | 
|---|
| 915 | return false; | 
|---|
| 916 | } | 
|---|
| 917 |  | 
|---|
| 918 | // no-> remove | 
|---|
| 919 | nodeListeners.erase( i ); | 
|---|
| 920 | return true; | 
|---|
| 921 | } | 
|---|
| 922 |  | 
|---|
| 923 | // ---------------------------------------------------------------------------- | 
|---|
| 924 |  | 
|---|
| 925 | void BaseOverlay::onLinkUp(const LinkID& id, | 
|---|
| 926 | const address_v* local, const address_v* remote) { | 
|---|
| 927 | logging_debug( "Link up with base communication link id=" << id ); | 
|---|
| 928 |  | 
|---|
| 929 | // get descriptor for link | 
|---|
| 930 | LinkDescriptor* ld = getDescriptor(id, true); | 
|---|
| 931 |  | 
|---|
| 932 | // handle bootstrap link we initiated | 
|---|
| 933 | if( std::find(bootstrapLinks.begin(), bootstrapLinks.end(), id) != bootstrapLinks.end() ){ | 
|---|
| 934 | logging_info( | 
|---|
| 935 | "Join has been initiated by me and the link is now up. " << | 
|---|
| 936 | "Sending out join request for SpoVNet " << spovnetId.toString() | 
|---|
| 937 | ); | 
|---|
| 938 |  | 
|---|
| 939 | // send join request message | 
|---|
| 940 | OverlayMsg overlayMsg( OverlayMsg::typeJoinRequest, | 
|---|
| 941 | OverlayInterface::OVERLAY_SERVICE_ID, nodeId ); | 
|---|
| 942 | JoinRequest joinRequest( spovnetId, nodeId ); | 
|---|
| 943 | overlayMsg.encapsulate( &joinRequest ); | 
|---|
| 944 | bc->sendMessage( id, &overlayMsg ); | 
|---|
| 945 | return; | 
|---|
| 946 | } | 
|---|
| 947 |  | 
|---|
| 948 | // no link found? -> link establishment from remote, add one! | 
|---|
| 949 | if (ld == NULL) { | 
|---|
| 950 | ld = addDescriptor( id ); | 
|---|
| 951 | logging_info( "onLinkUp (remote request) descriptor: " << ld ); | 
|---|
| 952 |  | 
|---|
| 953 | // update descriptor | 
|---|
| 954 | ld->fromRemote = true; | 
|---|
| 955 | ld->communicationId = id; | 
|---|
| 956 | ld->communicationUp = true; | 
|---|
| 957 | ld->setAutoUsed(); | 
|---|
| 958 | ld->setAlive(); | 
|---|
| 959 |  | 
|---|
| 960 | // in this case, do not inform listener, since service it unknown | 
|---|
| 961 | // -> wait for update message! | 
|---|
| 962 |  | 
|---|
| 963 | // link mapping found? -> send update message with node-id and service id | 
|---|
| 964 | } else { | 
|---|
| 965 | logging_info( "onLinkUp descriptor (initiated locally):" << ld ); | 
|---|
| 966 |  | 
|---|
| 967 | // update descriptor | 
|---|
| 968 | ld->setAutoUsed(); | 
|---|
| 969 | ld->setAlive(); | 
|---|
| 970 | ld->communicationUp = true; | 
|---|
| 971 | ld->fromRemote = false; | 
|---|
| 972 |  | 
|---|
| 973 | // if link is a relayed link->convert to direct link | 
|---|
| 974 | if (ld->relayed) { | 
|---|
| 975 | logging_info( "Converting to direct link: " << ld ); | 
|---|
| 976 | ld->up = true; | 
|---|
| 977 | ld->relayed = false; | 
|---|
| 978 | OverlayMsg overMsg( OverlayMsg::typeLinkDirect ); | 
|---|
| 979 | overMsg.setSourceLink( ld->overlayId ); | 
|---|
| 980 | overMsg.setDestinationLink( ld->remoteLink ); | 
|---|
| 981 | send_link( &overMsg, ld->overlayId ); | 
|---|
| 982 | } else { | 
|---|
| 983 | // note: necessary to validate the link on the remote side! | 
|---|
| 984 | logging_info( "Sending out update" << | 
|---|
| 985 | " for service " << ld->service.toString() << | 
|---|
| 986 | " with local node id " << nodeId.toString() << | 
|---|
| 987 | " on link " << ld->overlayId.toString() ); | 
|---|
| 988 |  | 
|---|
| 989 | // compile and send update message | 
|---|
| 990 | OverlayMsg overlayMsg( OverlayMsg::typeLinkUpdate ); | 
|---|
| 991 | overlayMsg.setSourceLink(ld->overlayId); | 
|---|
| 992 | overlayMsg.setAutoLink( ld->autolink ); | 
|---|
| 993 | send_link( &overlayMsg, ld->overlayId, true ); | 
|---|
| 994 | } | 
|---|
| 995 | } | 
|---|
| 996 | } | 
|---|
| 997 |  | 
|---|
| 998 | void BaseOverlay::onLinkDown(const LinkID& id, | 
|---|
| 999 | const address_v* local, const address_v* remote) { | 
|---|
| 1000 |  | 
|---|
| 1001 | // erase bootstrap links | 
|---|
| 1002 | vector<LinkID>::iterator it = std::find( bootstrapLinks.begin(), bootstrapLinks.end(), id ); | 
|---|
| 1003 | if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it ); | 
|---|
| 1004 |  | 
|---|
| 1005 | // get descriptor for link | 
|---|
| 1006 | LinkDescriptor* ld = getDescriptor(id, true); | 
|---|
| 1007 | if ( ld == NULL ) return; // not found? ->ignore! | 
|---|
| 1008 | logging_info( "onLinkDown descriptor: " << ld ); | 
|---|
| 1009 |  | 
|---|
| 1010 | // removing relay link information | 
|---|
| 1011 | removeRelayLink(ld->overlayId); | 
|---|
| 1012 |  | 
|---|
| 1013 | // inform listeners about link down | 
|---|
| 1014 | ld->communicationUp = false; | 
|---|
| 1015 | if (!ld->service.isUnspecified()) { | 
|---|
| 1016 | getListener(ld->service)->onLinkDown( ld->overlayId, ld->remoteNode ); | 
|---|
| 1017 | sideport->onLinkDown( id, this->nodeId, ld->remoteNode, this->spovnetId ); | 
|---|
| 1018 | } | 
|---|
| 1019 |  | 
|---|
| 1020 | // delete all queued messages (auto links) | 
|---|
| 1021 | if( ld->messageQueue.size() > 0 ) { | 
|---|
| 1022 | logging_warn( "Dropping link " << id.toString() << " that has " | 
|---|
| 1023 | << ld->messageQueue.size() << " waiting messages" ); | 
|---|
| 1024 | ld->flushQueue(); | 
|---|
| 1025 | } | 
|---|
| 1026 |  | 
|---|
| 1027 | // erase mapping | 
|---|
| 1028 | eraseDescriptor(ld->overlayId); | 
|---|
| 1029 | } | 
|---|
| 1030 |  | 
|---|
| 1031 | void BaseOverlay::onLinkChanged(const LinkID& id, | 
|---|
| 1032 | const address_v* oldlocal, const address_v* newlocal, | 
|---|
| 1033 | const address_v* oldremote, const address_v* newremote) { | 
|---|
| 1034 |  | 
|---|
| 1035 | // get descriptor for link | 
|---|
| 1036 | LinkDescriptor* ld = getDescriptor(id, true); | 
|---|
| 1037 | if ( ld == NULL ) return; // not found? ->ignore! | 
|---|
| 1038 | logging_debug( "onLinkChanged descriptor: " << ld ); | 
|---|
| 1039 |  | 
|---|
| 1040 | // inform listeners | 
|---|
| 1041 | ld->listener->onLinkChanged( ld->overlayId, ld->remoteNode ); | 
|---|
| 1042 | sideport->onLinkChanged( id, this->nodeId, ld->remoteNode, this->spovnetId ); | 
|---|
| 1043 |  | 
|---|
| 1044 | // autolinks: refresh timestamp | 
|---|
| 1045 | ld->setAutoUsed(); | 
|---|
| 1046 | } | 
|---|
| 1047 |  | 
|---|
| 1048 | void BaseOverlay::onLinkFail(const LinkID& id, | 
|---|
| 1049 | const address_v* local, const address_v* remote) { | 
|---|
| 1050 | logging_debug( "Link fail with base communication link id=" << id ); | 
|---|
| 1051 |  | 
|---|
| 1052 | // erase bootstrap links | 
|---|
| 1053 | vector<LinkID>::iterator it = std::find( bootstrapLinks.begin(), bootstrapLinks.end(), id ); | 
|---|
| 1054 | if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it ); | 
|---|
| 1055 |  | 
|---|
| 1056 | // get descriptor for link | 
|---|
| 1057 | LinkDescriptor* ld = getDescriptor(id, true); | 
|---|
| 1058 | if ( ld == NULL ) return; // not found? ->ignore! | 
|---|
| 1059 | logging_debug( "Link failed id=" << ld->overlayId.toString() ); | 
|---|
| 1060 |  | 
|---|
| 1061 | // inform listeners | 
|---|
| 1062 | ld->listener->onLinkFail( ld->overlayId, ld->remoteNode ); | 
|---|
| 1063 | sideport->onLinkFail( id, this->nodeId, ld->remoteNode, this->spovnetId ); | 
|---|
| 1064 | } | 
|---|
| 1065 |  | 
|---|
| 1066 | void BaseOverlay::onLinkQoSChanged(const LinkID& id, const address_v* local, | 
|---|
| 1067 | const address_v* remote, const QoSParameterSet& qos) { | 
|---|
| 1068 | logging_debug( "Link quality changed with base communication link id=" << id ); | 
|---|
| 1069 |  | 
|---|
| 1070 | // get descriptor for link | 
|---|
| 1071 | LinkDescriptor* ld = getDescriptor(id, true); | 
|---|
| 1072 | if ( ld == NULL ) return; // not found? ->ignore! | 
|---|
| 1073 | logging_debug( "Link quality changed id=" << ld->overlayId.toString() ); | 
|---|
| 1074 | } | 
|---|
| 1075 |  | 
|---|
| 1076 | bool BaseOverlay::onLinkRequest( const LinkID& id, const address_v* local, | 
|---|
| 1077 | const address_v* remote ) { | 
|---|
| 1078 | logging_debug("Accepting link request from " << remote->to_string() ); | 
|---|
| 1079 | return true; | 
|---|
| 1080 | } | 
|---|
| 1081 |  | 
|---|
| 1082 | /// handles a message from base communication | 
|---|
| 1083 | bool BaseOverlay::receiveMessage(const Message* message, | 
|---|
| 1084 | const LinkID& link, const NodeID& ) { | 
|---|
| 1085 | // get descriptor for link | 
|---|
| 1086 | LinkDescriptor* ld = getDescriptor( link, true ); | 
|---|
| 1087 | return handleMessage( message, ld, link ); | 
|---|
| 1088 | } | 
|---|
| 1089 |  | 
|---|
| 1090 | // ---------------------------------------------------------------------------- | 
|---|
| 1091 |  | 
|---|
| 1092 | /// Handle spovnet instance join requests | 
|---|
| 1093 | bool BaseOverlay::handleJoinRequest( OverlayMsg* overlayMsg, const LinkID& bcLink ) { | 
|---|
| 1094 |  | 
|---|
| 1095 | // decapsulate message | 
|---|
| 1096 | JoinRequest* joinReq = overlayMsg->decapsulate<JoinRequest>(); | 
|---|
| 1097 | logging_info( "Received join request for spovnet " << | 
|---|
| 1098 | joinReq->getSpoVNetID().toString() ); | 
|---|
| 1099 |  | 
|---|
| 1100 | // check spovnet id | 
|---|
| 1101 | if( joinReq->getSpoVNetID() != spovnetId ) { | 
|---|
| 1102 | logging_error( | 
|---|
| 1103 | "Received join request for spovnet we don't handle " << | 
|---|
| 1104 | joinReq->getSpoVNetID().toString() ); | 
|---|
| 1105 | return false; | 
|---|
| 1106 | } | 
|---|
| 1107 |  | 
|---|
| 1108 | // TODO: here you can implement mechanisms to deny joining of a node | 
|---|
| 1109 | bool allow = true; | 
|---|
| 1110 | logging_info( "Sending join reply for spovnet " << | 
|---|
| 1111 | spovnetId.toString() << " to node " << | 
|---|
| 1112 | overlayMsg->getSourceNode().toString() << | 
|---|
| 1113 | ". Result: " << (allow ? "allowed" : "denied") ); | 
|---|
| 1114 | joiningNodes.push_back( overlayMsg->getSourceNode() ); | 
|---|
| 1115 |  | 
|---|
| 1116 | // return overlay parameters | 
|---|
| 1117 | assert( overlayInterface != NULL ); | 
|---|
| 1118 | logging_debug( "Using bootstrap end-point " | 
|---|
| 1119 | << getEndpointDescriptor().toString() ) | 
|---|
| 1120 | OverlayParameterSet parameters = overlayInterface->getParameters(); | 
|---|
| 1121 | OverlayMsg retmsg( OverlayMsg::typeJoinReply, | 
|---|
| 1122 | OverlayInterface::OVERLAY_SERVICE_ID, nodeId ); | 
|---|
| 1123 | JoinReply replyMsg( spovnetId, parameters, | 
|---|
| 1124 | allow, getEndpointDescriptor() ); | 
|---|
| 1125 | retmsg.encapsulate(&replyMsg); | 
|---|
| 1126 | bc->sendMessage( bcLink, &retmsg ); | 
|---|
| 1127 |  | 
|---|
| 1128 | return true; | 
|---|
| 1129 | } | 
|---|
| 1130 |  | 
|---|
| 1131 | /// Handle replies to spovnet instance join requests | 
|---|
| 1132 | bool BaseOverlay::handleJoinReply( OverlayMsg* overlayMsg, const LinkID& bcLink ) { | 
|---|
| 1133 | // decapsulate message | 
|---|
| 1134 | logging_debug("received join reply message"); | 
|---|
| 1135 | JoinReply* replyMsg = overlayMsg->decapsulate<JoinReply>(); | 
|---|
| 1136 |  | 
|---|
| 1137 | // correct spovnet? | 
|---|
| 1138 | if( replyMsg->getSpoVNetID() != spovnetId ) { // no-> fail | 
|---|
| 1139 | logging_error( "Received SpoVNet join reply for " << | 
|---|
| 1140 | replyMsg->getSpoVNetID().toString() << | 
|---|
| 1141 | " != " << spovnetId.toString() ); | 
|---|
| 1142 | delete replyMsg; | 
|---|
| 1143 | return false; | 
|---|
| 1144 | } | 
|---|
| 1145 |  | 
|---|
| 1146 | // access granted? no -> fail | 
|---|
| 1147 | if( !replyMsg->getJoinAllowed() ) { | 
|---|
| 1148 | logging_error( "Our join request has been denied" ); | 
|---|
| 1149 |  | 
|---|
| 1150 | // drop initiator link | 
|---|
| 1151 | if( !bcLink.isUnspecified() ){ | 
|---|
| 1152 | bc->dropLink( bcLink ); | 
|---|
| 1153 |  | 
|---|
| 1154 | vector<LinkID>::iterator it = std::find( | 
|---|
| 1155 | bootstrapLinks.begin(), bootstrapLinks.end(), bcLink); | 
|---|
| 1156 | if( it != bootstrapLinks.end() ) | 
|---|
| 1157 | bootstrapLinks.erase(it); | 
|---|
| 1158 | } | 
|---|
| 1159 |  | 
|---|
| 1160 | // inform all registered services of the event | 
|---|
| 1161 | BOOST_FOREACH( NodeListener* i, nodeListeners ) | 
|---|
| 1162 | i->onJoinFailed( spovnetId ); | 
|---|
| 1163 |  | 
|---|
| 1164 | delete replyMsg; | 
|---|
| 1165 | return true; | 
|---|
| 1166 | } | 
|---|
| 1167 |  | 
|---|
| 1168 | // access has been granted -> continue! | 
|---|
| 1169 | logging_info("Join request has been accepted for spovnet " << | 
|---|
| 1170 | spovnetId.toString() ); | 
|---|
| 1171 |  | 
|---|
| 1172 | logging_debug( "Using bootstrap end-point " | 
|---|
| 1173 | << replyMsg->getBootstrapEndpoint().toString() ); | 
|---|
| 1174 |  | 
|---|
| 1175 | // create overlay structure from spovnet parameter set | 
|---|
| 1176 | // if we have not boostrapped yet against some other node | 
|---|
| 1177 | if( overlayInterface == NULL ){ | 
|---|
| 1178 |  | 
|---|
| 1179 | logging_debug("first-time bootstrapping"); | 
|---|
| 1180 |  | 
|---|
| 1181 | overlayInterface = OverlayFactory::create( | 
|---|
| 1182 | *this, replyMsg->getParam(), nodeId, this ); | 
|---|
| 1183 |  | 
|---|
| 1184 | // overlay structure supported? no-> fail! | 
|---|
| 1185 | if( overlayInterface == NULL ) { | 
|---|
| 1186 | logging_error( "overlay structure not supported" ); | 
|---|
| 1187 |  | 
|---|
| 1188 | if( !bcLink.isUnspecified() ){ | 
|---|
| 1189 | bc->dropLink( bcLink ); | 
|---|
| 1190 |  | 
|---|
| 1191 | vector<LinkID>::iterator it = std::find( | 
|---|
| 1192 | bootstrapLinks.begin(), bootstrapLinks.end(), bcLink); | 
|---|
| 1193 | if( it != bootstrapLinks.end() ) | 
|---|
| 1194 | bootstrapLinks.erase(it); | 
|---|
| 1195 | } | 
|---|
| 1196 |  | 
|---|
| 1197 | // inform all registered services of the event | 
|---|
| 1198 | BOOST_FOREACH( NodeListener* i, nodeListeners ) | 
|---|
| 1199 | i->onJoinFailed( spovnetId ); | 
|---|
| 1200 |  | 
|---|
| 1201 | delete replyMsg; | 
|---|
| 1202 | return true; | 
|---|
| 1203 | } | 
|---|
| 1204 |  | 
|---|
| 1205 | // everything ok-> join the overlay! | 
|---|
| 1206 | state = BaseOverlayStateCompleted; | 
|---|
| 1207 | overlayInterface->createOverlay(); | 
|---|
| 1208 |  | 
|---|
| 1209 | overlayInterface->joinOverlay( replyMsg->getBootstrapEndpoint() ); | 
|---|
| 1210 | overlayBootstrap.recordJoin( replyMsg->getBootstrapEndpoint() ); | 
|---|
| 1211 |  | 
|---|
| 1212 | // update ovlvis | 
|---|
| 1213 | //ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN); | 
|---|
| 1214 |  | 
|---|
| 1215 | // inform all registered services of the event | 
|---|
| 1216 | BOOST_FOREACH( NodeListener* i, nodeListeners ) | 
|---|
| 1217 | i->onJoinCompleted( spovnetId ); | 
|---|
| 1218 |  | 
|---|
| 1219 | delete replyMsg; | 
|---|
| 1220 |  | 
|---|
| 1221 | } else { | 
|---|
| 1222 |  | 
|---|
| 1223 | // this is not the first bootstrap, just join the additional node | 
|---|
| 1224 | logging_debug("not first-time bootstrapping"); | 
|---|
| 1225 | overlayInterface->joinOverlay( replyMsg->getBootstrapEndpoint() ); | 
|---|
| 1226 | overlayBootstrap.recordJoin( replyMsg->getBootstrapEndpoint() ); | 
|---|
| 1227 |  | 
|---|
| 1228 | delete replyMsg; | 
|---|
| 1229 |  | 
|---|
| 1230 | } // if( overlayInterface == NULL ) | 
|---|
| 1231 |  | 
|---|
| 1232 | return true; | 
|---|
| 1233 | } | 
|---|
| 1234 |  | 
|---|
| 1235 |  | 
|---|
| 1236 | bool BaseOverlay::handleData( OverlayMsg* overlayMsg, LinkDescriptor* ld ) { | 
|---|
| 1237 | // get service | 
|---|
| 1238 | const ServiceID& service = overlayMsg->getService(); | 
|---|
| 1239 | logging_debug( "Received data for service " << service.toString() | 
|---|
| 1240 | << " on link " << overlayMsg->getDestinationLink().toString() ); | 
|---|
| 1241 |  | 
|---|
| 1242 | // delegate data message | 
|---|
| 1243 | getListener(service)->onMessage( | 
|---|
| 1244 | overlayMsg, | 
|---|
| 1245 | overlayMsg->getSourceNode(), | 
|---|
| 1246 | overlayMsg->getDestinationLink() | 
|---|
| 1247 | ); | 
|---|
| 1248 |  | 
|---|
| 1249 | return true; | 
|---|
| 1250 | } | 
|---|
| 1251 |  | 
|---|
| 1252 |  | 
|---|
| 1253 | bool BaseOverlay::handleLinkUpdate( OverlayMsg* overlayMsg, LinkDescriptor* ld ) { | 
|---|
| 1254 |  | 
|---|
| 1255 | if( ld == NULL ) { | 
|---|
| 1256 | logging_warn( "received overlay update message for link for " | 
|---|
| 1257 | << "which we have no mapping" ); | 
|---|
| 1258 | return false; | 
|---|
| 1259 | } | 
|---|
| 1260 | logging_info("Received type update message on link " << ld ); | 
|---|
| 1261 |  | 
|---|
| 1262 | // update our link mapping information for this link | 
|---|
| 1263 | bool changed = | 
|---|
| 1264 | ( ld->remoteNode != overlayMsg->getSourceNode() ) | 
|---|
| 1265 | || ( ld->service != overlayMsg->getService() ); | 
|---|
| 1266 |  | 
|---|
| 1267 | // set parameters | 
|---|
| 1268 | ld->up         = true; | 
|---|
| 1269 | ld->remoteNode = overlayMsg->getSourceNode(); | 
|---|
| 1270 | ld->remoteLink = overlayMsg->getSourceLink(); | 
|---|
| 1271 | ld->service    = overlayMsg->getService(); | 
|---|
| 1272 | ld->autolink   = overlayMsg->isAutoLink(); | 
|---|
| 1273 |  | 
|---|
| 1274 | // if our link information changed, we send out an update, too | 
|---|
| 1275 | if( changed ) { | 
|---|
| 1276 | overlayMsg->swapRoles(); | 
|---|
| 1277 | overlayMsg->setSourceNode(nodeId); | 
|---|
| 1278 | overlayMsg->setSourceLink(ld->overlayId); | 
|---|
| 1279 | overlayMsg->setService(ld->service); | 
|---|
| 1280 | send( overlayMsg, ld ); | 
|---|
| 1281 | } | 
|---|
| 1282 |  | 
|---|
| 1283 | // service registered? no-> error! | 
|---|
| 1284 | if( !communicationListeners.contains( ld->service ) ) { | 
|---|
| 1285 | logging_warn( "Link up: event listener has not been registered" ); | 
|---|
| 1286 | return false; | 
|---|
| 1287 | } | 
|---|
| 1288 |  | 
|---|
| 1289 | // default or no service registered? | 
|---|
| 1290 | CommunicationListener* listener = communicationListeners.get( ld->service ); | 
|---|
| 1291 | if( listener == NULL || listener == &CommunicationListener::DEFAULT ) { | 
|---|
| 1292 | logging_warn("Link up: event listener is default or null!" ); | 
|---|
| 1293 | return true; | 
|---|
| 1294 | } | 
|---|
| 1295 |  | 
|---|
| 1296 | // update descriptor | 
|---|
| 1297 | ld->listener = listener; | 
|---|
| 1298 | ld->setAutoUsed(); | 
|---|
| 1299 | ld->setAlive(); | 
|---|
| 1300 |  | 
|---|
| 1301 | // ask the service whether it wants to accept this link | 
|---|
| 1302 | if( !listener->onLinkRequest(ld->remoteNode) ) { | 
|---|
| 1303 |  | 
|---|
| 1304 | logging_debug("Link id=" << ld->overlayId.toString() << | 
|---|
| 1305 | " has been denied by service " << ld->service.toString() << ", dropping link"); | 
|---|
| 1306 |  | 
|---|
| 1307 | // prevent onLinkDown calls to the service | 
|---|
| 1308 | ld->listener = &CommunicationListener::DEFAULT; | 
|---|
| 1309 |  | 
|---|
| 1310 | // drop the link | 
|---|
| 1311 | dropLink( ld->overlayId ); | 
|---|
| 1312 | return true; | 
|---|
| 1313 | } | 
|---|
| 1314 |  | 
|---|
| 1315 | // set link up | 
|---|
| 1316 | ld->up = true; | 
|---|
| 1317 | logging_info( "Link has been accepted by service and is up: " << ld ); | 
|---|
| 1318 |  | 
|---|
| 1319 | // auto links: link has been accepted -> send queued messages | 
|---|
| 1320 | if( ld->messageQueue.size() > 0 ) { | 
|---|
| 1321 | logging_info( "Sending out queued messages on link " << ld ); | 
|---|
| 1322 | BOOST_FOREACH( Message* msg, ld->messageQueue ) { | 
|---|
| 1323 | sendMessage( msg, ld->overlayId ); | 
|---|
| 1324 | delete msg; | 
|---|
| 1325 | } | 
|---|
| 1326 | ld->messageQueue.clear(); | 
|---|
| 1327 | } | 
|---|
| 1328 |  | 
|---|
| 1329 | // call the notification functions | 
|---|
| 1330 | listener->onLinkUp( ld->overlayId, ld->remoteNode ); | 
|---|
| 1331 | sideport->onLinkUp( ld->overlayId, nodeId, ld->remoteNode, this->spovnetId ); | 
|---|
| 1332 |  | 
|---|
| 1333 | return true; | 
|---|
| 1334 | } | 
|---|
| 1335 |  | 
|---|
| 1336 | /// handle a link request and reply | 
|---|
| 1337 | bool BaseOverlay::handleLinkRequest( OverlayMsg* overlayMsg, LinkDescriptor* ld ) { | 
|---|
| 1338 | logging_info( "Link request received from node id=" << overlayMsg->getSourceNode() ); | 
|---|
| 1339 |  | 
|---|
| 1340 | //TODO: Check if a request has already been sent using getSourceLink() ... | 
|---|
| 1341 |  | 
|---|
| 1342 | // create link descriptor | 
|---|
| 1343 | LinkDescriptor* ldn = addDescriptor(); | 
|---|
| 1344 |  | 
|---|
| 1345 | // flags | 
|---|
| 1346 | ldn->up = true; | 
|---|
| 1347 | ldn->fromRemote = true; | 
|---|
| 1348 | ldn->relayed = true; | 
|---|
| 1349 |  | 
|---|
| 1350 | // parameters | 
|---|
| 1351 | ldn->service = overlayMsg->getService(); | 
|---|
| 1352 | ldn->listener = getListener(ldn->service); | 
|---|
| 1353 | ldn->remoteNode = overlayMsg->getSourceNode(); | 
|---|
| 1354 | ldn->remoteLink = overlayMsg->getSourceLink(); | 
|---|
| 1355 |  | 
|---|
| 1356 | // update time-stamps | 
|---|
| 1357 | ldn->setAlive(); | 
|---|
| 1358 | ldn->setAutoUsed(); | 
|---|
| 1359 |  | 
|---|
| 1360 | // create reply message and send back! | 
|---|
| 1361 | overlayMsg->swapRoles(); // swap source/destination | 
|---|
| 1362 | overlayMsg->setType(OverlayMsg::typeLinkReply); | 
|---|
| 1363 | overlayMsg->setSourceLink(ldn->overlayId); | 
|---|
| 1364 | overlayMsg->setSourceEndpoint( bc->getEndpointDescriptor() ); | 
|---|
| 1365 | overlayMsg->setRelayed(true); | 
|---|
| 1366 | send( overlayMsg, ld ); // send back to link | 
|---|
| 1367 |  | 
|---|
| 1368 | // inform listener | 
|---|
| 1369 | ldn->listener->onLinkUp( ldn->overlayId, ldn->remoteNode ); | 
|---|
| 1370 |  | 
|---|
| 1371 | return true; | 
|---|
| 1372 | } | 
|---|
| 1373 |  | 
|---|
| 1374 | bool BaseOverlay::handleLinkReply( OverlayMsg* overlayMsg, LinkDescriptor* ld ) { | 
|---|
| 1375 |  | 
|---|
| 1376 | // find link request | 
|---|
| 1377 | LinkDescriptor* ldn = getDescriptor(overlayMsg->getDestinationLink()); | 
|---|
| 1378 |  | 
|---|
| 1379 | // not found? yes-> drop with error! | 
|---|
| 1380 | if (ldn == NULL) { | 
|---|
| 1381 | logging_error( "No link request pending for " | 
|---|
| 1382 | << overlayMsg->getDestinationLink().toString() ); | 
|---|
| 1383 | return false; | 
|---|
| 1384 | } | 
|---|
| 1385 | logging_debug("Handling link reply for " << ldn ) | 
|---|
| 1386 |  | 
|---|
| 1387 | // check if already up | 
|---|
| 1388 | if (ldn->up) { | 
|---|
| 1389 | logging_warn( "Link already up: " << ldn ); | 
|---|
| 1390 | return true; | 
|---|
| 1391 | } | 
|---|
| 1392 |  | 
|---|
| 1393 | // debug message | 
|---|
| 1394 | logging_debug( "Link request reply received. Establishing link" | 
|---|
| 1395 | << " for service " << overlayMsg->getService().toString() | 
|---|
| 1396 | << " with local id=" << overlayMsg->getDestinationLink() | 
|---|
| 1397 | << " and remote link id=" << overlayMsg->getSourceLink() | 
|---|
| 1398 | << " to " << overlayMsg->getSourceEndpoint().toString() | 
|---|
| 1399 | ); | 
|---|
| 1400 |  | 
|---|
| 1401 | // set local link descriptor data | 
|---|
| 1402 | ldn->up = true; | 
|---|
| 1403 | ldn->relayed = true; | 
|---|
| 1404 | ldn->service = overlayMsg->getService(); | 
|---|
| 1405 | ldn->listener = getListener(ldn->service); | 
|---|
| 1406 | ldn->remoteLink = overlayMsg->getSourceLink(); | 
|---|
| 1407 | ldn->remoteNode = overlayMsg->getSourceNode(); | 
|---|
| 1408 |  | 
|---|
| 1409 | // update timestamps | 
|---|
| 1410 | ldn->setAlive(); | 
|---|
| 1411 | ldn->setAutoUsed(); | 
|---|
| 1412 |  | 
|---|
| 1413 | // auto links: link has been accepted -> send queued messages | 
|---|
| 1414 | if( ldn->messageQueue.size() > 0 ) { | 
|---|
| 1415 | logging_info( "Sending out queued messages on link " << | 
|---|
| 1416 | ldn->overlayId.toString() ); | 
|---|
| 1417 | BOOST_FOREACH( Message* msg, ldn->messageQueue ) { | 
|---|
| 1418 | sendMessage( msg, ldn->overlayId ); | 
|---|
| 1419 | delete msg; | 
|---|
| 1420 | } | 
|---|
| 1421 | ldn->messageQueue.clear(); | 
|---|
| 1422 | } | 
|---|
| 1423 |  | 
|---|
| 1424 | // inform listeners about new link | 
|---|
| 1425 | ldn->listener->onLinkUp( ldn->overlayId, ldn->remoteNode ); | 
|---|
| 1426 |  | 
|---|
| 1427 | // try to replace relay link with direct link | 
|---|
| 1428 | ldn->communicationId = | 
|---|
| 1429 | bc->establishLink( overlayMsg->getSourceEndpoint() ); | 
|---|
| 1430 |  | 
|---|
| 1431 | return true; | 
|---|
| 1432 | } | 
|---|
| 1433 |  | 
|---|
| 1434 | /// handle a keep-alive message for a link | 
|---|
| 1435 | bool BaseOverlay::handleLinkAlive( OverlayMsg* overlayMsg, LinkDescriptor* ld ) { | 
|---|
| 1436 | LinkDescriptor* rld = getDescriptor(overlayMsg->getDestinationLink()); | 
|---|
| 1437 | if ( rld != NULL ) { | 
|---|
| 1438 | logging_debug("Keep-Alive for " << | 
|---|
| 1439 | overlayMsg->getDestinationLink() ); | 
|---|
| 1440 | if (overlayMsg->isRouteRecord()) | 
|---|
| 1441 | rld->routeRecord = overlayMsg->getRouteRecord(); | 
|---|
| 1442 | rld->setAlive(); | 
|---|
| 1443 | return true; | 
|---|
| 1444 | } else { | 
|---|
| 1445 | logging_error("Keep-Alive for " | 
|---|
| 1446 | << overlayMsg->getDestinationLink() << ": link unknown." ); | 
|---|
| 1447 | return false; | 
|---|
| 1448 | } | 
|---|
| 1449 | } | 
|---|
| 1450 |  | 
|---|
| 1451 | /// handle a direct link message | 
|---|
| 1452 | bool BaseOverlay::handleLinkDirect( OverlayMsg* overlayMsg, LinkDescriptor* ld ) { | 
|---|
| 1453 | logging_debug( "Received direct link replacement request" ); | 
|---|
| 1454 |  | 
|---|
| 1455 | /// get destination overlay link | 
|---|
| 1456 | LinkDescriptor* rld = getDescriptor( overlayMsg->getDestinationLink() ); | 
|---|
| 1457 | if (rld == NULL || ld == NULL) { | 
|---|
| 1458 | logging_error("Direct link replacement: Link " | 
|---|
| 1459 | << overlayMsg->getDestinationLink() << "not found error." ); | 
|---|
| 1460 | return false; | 
|---|
| 1461 | } | 
|---|
| 1462 | logging_info( "Received direct link convert notification for " << rld ); | 
|---|
| 1463 |  | 
|---|
| 1464 | // update information | 
|---|
| 1465 | rld->communicationId = ld->communicationId; | 
|---|
| 1466 | rld->communicationUp = true; | 
|---|
| 1467 | rld->relayed = false; | 
|---|
| 1468 |  | 
|---|
| 1469 | // mark used and alive! | 
|---|
| 1470 | rld->setAlive(); | 
|---|
| 1471 | rld->setAutoUsed(); | 
|---|
| 1472 |  | 
|---|
| 1473 | // erase the original descriptor | 
|---|
| 1474 | eraseDescriptor(ld->overlayId); | 
|---|
| 1475 | } | 
|---|
| 1476 |  | 
|---|
| 1477 | /// handles an incoming message | 
|---|
| 1478 | bool BaseOverlay::handleMessage( const Message* message, LinkDescriptor* ld, | 
|---|
| 1479 | const LinkID bcLink ) { | 
|---|
| 1480 | logging_debug( "Handling message: " << message->toString()); | 
|---|
| 1481 |  | 
|---|
| 1482 | // decapsulate overlay message | 
|---|
| 1483 | OverlayMsg* overlayMsg = | 
|---|
| 1484 | const_cast<Message*>(message)->decapsulate<OverlayMsg>(); | 
|---|
| 1485 | if( overlayMsg == NULL ) return false; | 
|---|
| 1486 |  | 
|---|
| 1487 | // increase number of hops | 
|---|
| 1488 | overlayMsg->increaseNumHops(); | 
|---|
| 1489 |  | 
|---|
| 1490 | // refresh relay information | 
|---|
| 1491 | refreshRelayInformation( overlayMsg, ld ); | 
|---|
| 1492 |  | 
|---|
| 1493 | // update route record | 
|---|
| 1494 | overlayMsg->addRouteRecord(nodeId); | 
|---|
| 1495 |  | 
|---|
| 1496 | // handle signaling messages (do not route!) | 
|---|
| 1497 | if (overlayMsg->getType()>=OverlayMsg::typeSignalingStart && | 
|---|
| 1498 | overlayMsg->getType()<=OverlayMsg::typeSignalingEnd ) { | 
|---|
| 1499 | overlayInterface->onMessage(overlayMsg, NodeID::UNSPECIFIED, LinkID::UNSPECIFIED); | 
|---|
| 1500 | delete overlayMsg; | 
|---|
| 1501 | return true; | 
|---|
| 1502 | } | 
|---|
| 1503 |  | 
|---|
| 1504 | // message for reached destination? no-> route message | 
|---|
| 1505 | if (!overlayMsg->getDestinationNode().isUnspecified() && | 
|---|
| 1506 | overlayMsg->getDestinationNode() != nodeId ) { | 
|---|
| 1507 | logging_debug("Routing message " | 
|---|
| 1508 | << " from " << overlayMsg->getSourceNode() | 
|---|
| 1509 | << " to " << overlayMsg->getDestinationNode() | 
|---|
| 1510 | ); | 
|---|
| 1511 | route( overlayMsg ); | 
|---|
| 1512 | delete overlayMsg; | 
|---|
| 1513 | return true; | 
|---|
| 1514 | } | 
|---|
| 1515 |  | 
|---|
| 1516 | // handle base overlay message | 
|---|
| 1517 | bool ret = false; // return value | 
|---|
| 1518 | switch ( overlayMsg->getType() ) { | 
|---|
| 1519 |  | 
|---|
| 1520 | // data transport messages | 
|---|
| 1521 | case OverlayMsg::typeData: | 
|---|
| 1522 | ret = handleData(overlayMsg, ld);                       break; | 
|---|
| 1523 |  | 
|---|
| 1524 | // overlay setup messages | 
|---|
| 1525 | case OverlayMsg::typeJoinRequest: | 
|---|
| 1526 | ret = handleJoinRequest(overlayMsg, bcLink );   break; | 
|---|
| 1527 | case OverlayMsg::typeJoinReply: | 
|---|
| 1528 | ret = handleJoinReply(overlayMsg, bcLink );     break; | 
|---|
| 1529 |  | 
|---|
| 1530 | // link specific messages | 
|---|
| 1531 | case OverlayMsg::typeLinkRequest: | 
|---|
| 1532 | ret = handleLinkRequest(overlayMsg, ld );       break; | 
|---|
| 1533 | case OverlayMsg::typeLinkReply: | 
|---|
| 1534 | ret = handleLinkReply(overlayMsg, ld );         break; | 
|---|
| 1535 | case OverlayMsg::typeLinkUpdate: | 
|---|
| 1536 | ret = handleLinkUpdate(overlayMsg, ld );        break; | 
|---|
| 1537 | case OverlayMsg::typeLinkAlive: | 
|---|
| 1538 | ret = handleLinkAlive(overlayMsg, ld );         break; | 
|---|
| 1539 | case OverlayMsg::typeLinkDirect: | 
|---|
| 1540 | ret = handleLinkDirect(overlayMsg, ld );        break; | 
|---|
| 1541 |  | 
|---|
| 1542 | // handle unknown message type | 
|---|
| 1543 | default: { | 
|---|
| 1544 | logging_error( "received message in invalid state! don't know " << | 
|---|
| 1545 | "what to do with this message of type " << overlayMsg->getType() ); | 
|---|
| 1546 | ret = false; | 
|---|
| 1547 | break; | 
|---|
| 1548 | } | 
|---|
| 1549 | } | 
|---|
| 1550 |  | 
|---|
| 1551 | // free overlay message and return value | 
|---|
| 1552 | delete overlayMsg; | 
|---|
| 1553 | return ret; | 
|---|
| 1554 | } | 
|---|
| 1555 |  | 
|---|
| 1556 | // ---------------------------------------------------------------------------- | 
|---|
| 1557 |  | 
|---|
| 1558 | void BaseOverlay::broadcastMessage(Message* message, const ServiceID& service) { | 
|---|
| 1559 |  | 
|---|
| 1560 | logging_debug( "broadcasting message to all known nodes " << | 
|---|
| 1561 | "in the overlay from service " + service.toString() ); | 
|---|
| 1562 |  | 
|---|
| 1563 | OverlayInterface::NodeList nodes = overlayInterface->getKnownNodes(true); | 
|---|
| 1564 | OverlayInterface::NodeList::iterator i = nodes.begin(); | 
|---|
| 1565 | for(; i != nodes.end(); i++ ) { | 
|---|
| 1566 | if( *i == nodeId) continue; // don't send to ourselfs | 
|---|
| 1567 | sendMessage( message, *i, service ); | 
|---|
| 1568 | } | 
|---|
| 1569 | } | 
|---|
| 1570 |  | 
|---|
| 1571 | /// return the overlay neighbors | 
|---|
| 1572 | vector<NodeID> BaseOverlay::getOverlayNeighbors(bool deep) const { | 
|---|
| 1573 | // the known nodes _can_ also include our node, so we remove ourself | 
|---|
| 1574 | vector<NodeID> nodes = overlayInterface->getKnownNodes(deep); | 
|---|
| 1575 | vector<NodeID>::iterator i = find( nodes.begin(), nodes.end(), this->nodeId ); | 
|---|
| 1576 | if( i != nodes.end() ) nodes.erase( i ); | 
|---|
| 1577 | return nodes; | 
|---|
| 1578 | } | 
|---|
| 1579 |  | 
|---|
| 1580 | const NodeID& BaseOverlay::getNodeID(const LinkID& lid) const { | 
|---|
| 1581 | if( lid == LinkID::UNSPECIFIED ) return nodeId; | 
|---|
| 1582 | const LinkDescriptor* ld = getDescriptor(lid); | 
|---|
| 1583 | if( ld == NULL ) return NodeID::UNSPECIFIED; | 
|---|
| 1584 | else return ld->remoteNode; | 
|---|
| 1585 | } | 
|---|
| 1586 |  | 
|---|
| 1587 | vector<LinkID> BaseOverlay::getLinkIDs( const NodeID& nid ) const { | 
|---|
| 1588 | vector<LinkID> linkvector; | 
|---|
| 1589 | BOOST_FOREACH( LinkDescriptor* ld, links ) { | 
|---|
| 1590 | if( ld->remoteNode == nid || nid == NodeID::UNSPECIFIED ) { | 
|---|
| 1591 | linkvector.push_back( ld->overlayId ); | 
|---|
| 1592 | } | 
|---|
| 1593 | } | 
|---|
| 1594 | return linkvector; | 
|---|
| 1595 | } | 
|---|
| 1596 |  | 
|---|
| 1597 |  | 
|---|
| 1598 | void BaseOverlay::onNodeJoin(const NodeID& node) { | 
|---|
| 1599 | JoiningNodes::iterator i = std::find( joiningNodes.begin(), joiningNodes.end(), node ); | 
|---|
| 1600 | if( i == joiningNodes.end() ) return; | 
|---|
| 1601 |  | 
|---|
| 1602 | logging_info( "node has successfully joined baseoverlay and overlay structure " | 
|---|
| 1603 | << node.toString() ); | 
|---|
| 1604 |  | 
|---|
| 1605 | joiningNodes.erase( i ); | 
|---|
| 1606 | } | 
|---|
| 1607 |  | 
|---|
| 1608 | void BaseOverlay::eventFunction() { | 
|---|
| 1609 | stabilizeRelays(); | 
|---|
| 1610 | stabilizeLinks(); | 
|---|
| 1611 | } | 
|---|
| 1612 |  | 
|---|
| 1613 | }} // namespace ariba, overlay | 
|---|