source: source/ariba/overlay/BaseOverlay.cpp@ 5487

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