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