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

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