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

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