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

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