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

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