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

Last change on this file since 5871 was 5871, checked in by Christoph Mayer, 15 years ago

merge noch nicht fertig

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