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

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