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