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

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