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