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

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