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

Last change on this file since 3683 was 3374, checked in by Christoph Mayer, 16 years ago

-Integration of Branch 20090424-mayer-sideport

File size: 30.1 KB
Line 
1// [Licence]
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// [Licence]
38
39#include "BaseOverlay.h"
40
41#include "ariba/utility/misc/OvlVis.h"
42#include "ariba/NodeListener.h"
43#include "ariba/CommunicationListener.h"
44#include "ariba/SideportListener.h"
45
46namespace ariba {
47namespace overlay {
48
49use_logging_cpp(BaseOverlay);
50
51BaseOverlay::BaseOverlay()
52 : bc(NULL), overlayInterface(NULL), nodeId(NodeID::UNSPECIFIED),
53 spovnetId(SpoVNetID::UNSPECIFIED), initiatorLink(LinkID::UNSPECIFIED),
54 state(BaseOverlayStateInvalid), sideport(&SideportListener::DEFAULT){
55}
56
57BaseOverlay::~BaseOverlay(){
58}
59
60void BaseOverlay::start( BaseCommunication& _basecomm, const NodeID& _nodeid ){
61
62 bc = &_basecomm;
63 nodeId = _nodeid;
64
65 logging_info("creating base overlay");
66
67 bc->registerMessageReceiver( this );
68 bc->registerEventListener( this );
69
70 ovl.visCreate( ovlId, nodeId, string(""), string("") );
71 ovl.visChangeNodeColor(ovlId, nodeId, OvlVis::NODE_COLORS_GREY);
72
73// if (Identifier(Configuration::instance().read<unsigned long>("BASE_nodeid")) ==
74// Identifier(Configuration::instance().read<unsigned long>("SOURCE"))) {
75// ovl.visChangeNodeIcon(ovlId, nodeId, OvlVis::ICON_ID_CAMERA);
76// } else if (Identifier(Configuration::instance().read<unsigned long>("BASE_nodeid")) ==
77// Identifier(Configuration::instance().read<unsigned long>("MR_A"))) {
78// ovl.visChangeNodeIcon(ovlId, nodeId, OvlVis::ICON_ID_CHARACTER_A);
79// } else if (Identifier(Configuration::instance().read<unsigned long>("BASE_nodeid")) ==
80// Identifier(Configuration::instance().read<unsigned long>("MR_W"))) {
81// ovl.visChangeNodeIcon(ovlId, nodeId, OvlVis::ICON_ID_CHARACTER_W);
82// }
83
84 // timer for auto link management
85 Timer::setInterval( 5000 );
86 Timer::start();
87}
88
89void BaseOverlay::stop() {
90
91 logging_info("deleting base overlay");
92
93 Timer::stop();
94 bc->unregisterMessageReceiver( this );
95 bc->unregisterEventListener( this );
96}
97
98void BaseOverlay::joinSpoVNet(const SpoVNetID& id, const EndpointDescriptor& bootstrapEp){
99
100 ovl.visShowNodeBubble ( ovlId, nodeId, "joining..." );
101 logging_info( "starting to join spovnet " << id.toString() <<
102 " with nodeid " << nodeId.toString());
103
104 //
105 // contact the spovnet initiator and request
106 // to join. if the join is granted we will
107 // receive further information on the structure
108 // of the overlay that is used in the spovnet
109 //
110 // but first, we have to establish a link to the initiator...
111 //
112
113 spovnetId = id;
114 state = BaseOverlayStateJoinInitiated;
115
116 initiatorLink = bc->establishLink( bootstrapEp );
117 logging_info("join process initiated for " << id.toString() << "...");
118}
119
120void BaseOverlay::leaveSpoVNet(){
121
122 logging_info( "leaving spovnet " << spovnetId );
123 bool ret = ( state != this->BaseOverlayStateInvalid );
124
125 logging_debug( "dropping all auto-links ..." );
126
127 // now we start leaving the spovnet: fist delete all links
128 // that we still have in the baseoverlay initiated by
129 // some services, the leave the actual overlay structure,
130 // then leave the spovnet
131
132 // --> drop all service links
133
134 vector<LinkID> servicelinks;
135 BOOST_FOREACH( LinkPair item, linkMapping ){
136 if( item.second.service != OverlayInterface::OVERLAY_SERVICE_ID )
137 servicelinks.push_back( item.first );
138 }
139 BOOST_FOREACH( LinkID lnk, servicelinks ){
140 // the dropLink function will remove
141 // the item from the linkMapping
142 dropLink( lnk );
143 }
144
145 // --> leave overlay structure
146
147 logging_debug( "leaving overlay" );
148 // first, leave the overlay interface
149 if( overlayInterface != NULL )
150 overlayInterface->leaveOverlay();
151
152 // --> leave spovnet
153
154 if( state != BaseOverlayStateInitiator ){
155
156 // then, leave the spovnet baseoverlay
157 OverlayMsg overMsg( OverlayMsg::OverlayMessageTypeBye, nodeId );
158 bc->sendMessage( initiatorLink, &overMsg );
159
160 // drop the link and set to correct state
161 bc->dropLink( initiatorLink );
162 initiatorLink = LinkID::UNSPECIFIED;
163 }
164
165 state = BaseOverlayStateInvalid;
166 ovl.visShutdown( ovlId, nodeId, string("") );
167
168 // inform all registered services of the event
169 BOOST_FOREACH( NodeListener* i, nodeListeners ){
170 if( ret ) i->onLeaveCompleted( spovnetId );
171 else i->onLeaveFailed( spovnetId );
172 }
173}
174
175void BaseOverlay::createSpoVNet(const SpoVNetID& id, const OverlayParameterSet& param, const SecurityParameterSet& sec, const QoSParameterSet& qos){
176
177 //
178 // set the state that we are an initiator,
179 // this way incoming messages are handled correctly
180 //
181
182 logging_info( "creating spovnet " + id.toString() << " with nodeid " << nodeId.toString() );
183
184 spovnetId = id;
185 state = BaseOverlayStateInitiator;
186
187 overlayInterface = OverlayFactory::create( *this, param, nodeId, this );
188 if( overlayInterface == NULL ){
189 logging_fatal( "overlay structure not supported" );
190 state = BaseOverlayStateInvalid;
191 return;
192 }
193
194 //
195 // bootstrap against ourselfs
196 //
197
198 overlayInterface->joinOverlay();
199 BOOST_FOREACH( NodeListener* i, nodeListeners ){
200 i->onJoinCompleted( spovnetId );
201 }
202
203 ovl.visChangeNodeIcon ( ovlId, nodeId, OvlVis::ICON_ID_CAMERA );
204 ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN);
205}
206
207const LinkID BaseOverlay::establishLink(const NodeID& node, const ServiceID& service){
208
209 // TODO: if this is not a onehop overlay the operation will go asynchronously
210 const EndpointDescriptor& endpoint = overlayInterface->resolveNode( node );
211 if( endpoint == EndpointDescriptor::UNSPECIFIED ){
212 logging_error( "could not resolve node to endpoint. unable to establish link" );
213 return LinkID::UNSPECIFIED;
214 }
215
216 logging_debug( "baseoverlay called to establish link between node " <<
217 node.toString() << " on endpoint " << endpoint.toString() <<
218 " for service " << service.toString() );
219
220 return establishLink( endpoint, service );
221}
222
223const LinkID BaseOverlay::establishLink(const EndpointDescriptor& ep, const ServiceID& service){
224
225 if( !communicationListeners.contains( service ) ){
226 logging_error( "no registered listener on serviceid " << service.toString() );
227 return LinkID::UNSPECIFIED;
228 }
229
230 const LinkID link = bc->establishLink( ep );
231
232 CommunicationListener* receiver = communicationListeners.get( service );
233 assert( receiver != NULL );
234
235 LinkItem item (link, NodeID::UNSPECIFIED, service, receiver);
236 linkMapping.insert( make_pair(link, item) );
237
238 return link;
239}
240
241void BaseOverlay::dropLink(const LinkID& link){
242
243 logging_debug( "baseoverlay dropping link " << link.toString() );
244 LinkMapping::iterator i = linkMapping.find( link );
245
246 if( i == linkMapping.end() ){
247 logging_warn( "can't drop link, mapping unknown " << link.toString() );
248 return;
249 }
250
251 linkMapping.erase( i );
252
253 LinkItem item = i->second;
254 bc->dropLink( link );
255
256 item.interface->onLinkDown( link, item.node );
257 sideport->onLinkDown(link, this->nodeId, item.node, this->spovnetId );
258}
259
260seqnum_t BaseOverlay::sendMessage(const Message* message, const LinkID& link ){
261
262 logging_debug( "baseoverlay is sending message on link " << link.toString() );
263
264 LinkMapping::iterator i = linkMapping.find( link );
265 if( i == linkMapping.end() ){
266 logging_error( "could not send message. link not found " << link.toString() );
267 return -1;
268 }
269
270 OverlayMsg overmsg( OverlayMsg::OverlayMessageTypeData, i->second.service, nodeId );
271 overmsg.encapsulate( const_cast<Message*>(message) );
272
273 i->second.markused();
274 return bc->sendMessage( link, &overmsg );
275}
276
277seqnum_t BaseOverlay::sendMessage(const Message* message, const NodeID& node, const ServiceID& service){
278
279 LinkID link = LinkID::UNSPECIFIED;
280
281 LinkMapping::iterator i = linkMapping.begin();
282 LinkMapping::iterator iend = linkMapping.end();
283
284 for( ; i != iend; i++ ){
285 if( i->second.node == node && i->second.service == service ){
286 link = i->second.link;
287 break;
288 }
289 }
290
291 if( link == LinkID::UNSPECIFIED ){
292
293 logging_info( "no link could be found to send message to node " <<
294 node.toString() << " for service " << service.toString() <<
295 ". creating auto link ...");
296
297 link = establishLink( node, service );
298 LinkMapping::iterator i = linkMapping.find( link );
299
300 if( i == linkMapping.end() || link == LinkID::UNSPECIFIED ){
301 logging_error( "failed to establish auto link to node " << node.toString() <<
302 " for service " << service.toString() );
303 return -1;
304 }
305
306 i->second.autolink = true;
307
308 logging_debug( "establishing autolink in progress to node "
309 << node.toString() << " with new link-id " << link.toString() );
310
311 } // if( link != LinkID::UNSPECIFIED )
312
313 assert( link != LinkID::UNSPECIFIED );
314
315 // mark the link as used, as we
316 // now send a message through it
317 i->second.markused();
318
319 // send the message through the new link. the link may not be functional,
320 // but for us there is a link-id so we can send messages through it. if
321 // the link is not yet up and the message needs to be cached, this is the
322 // task of the BaseCommunication, it will cache and send it later.
323 return sendMessage( message, link );
324}
325
326const EndpointDescriptor& BaseOverlay::getEndpointDescriptor(const LinkID& link) const {
327
328 return bc->getEndpointDescriptor( link );
329}
330
331const EndpointDescriptor& BaseOverlay::getEndpointDescriptor(const NodeID& node) const {
332
333 if( node == nodeId || node == NodeID::UNSPECIFIED )
334 return bc->getEndpointDescriptor();
335
336 if( overlayInterface == NULL ){
337 logging_error( "overlay interface not set, cannot resolve endpoint" );
338 return EndpointDescriptor::UNSPECIFIED;
339 }
340
341 // TODO: if this is not a onehop overlay the operation will go asynchronously
342 return overlayInterface->resolveNode( node );
343}
344
345bool BaseOverlay::bind(CommunicationListener* listener, const ServiceID& sid){
346 logging_debug( "binding communication listener " << listener
347 << " on serviceid " << sid.toString() );
348
349 if( communicationListeners.contains( sid ) ){
350 logging_error( "some listener already registered for service id "
351 << sid.toString() );
352 return false;
353 }
354
355 communicationListeners.registerItem( listener, sid );
356 return true;
357}
358
359bool BaseOverlay::registerSidePort(SideportListener* _sideport){
360 sideport = _sideport;
361 _sideport->configure( this );
362}
363
364bool BaseOverlay::unregisterSidePort(SideportListener* _sideport){
365 sideport = &SideportListener::DEFAULT;
366}
367
368bool BaseOverlay::unbind(CommunicationListener* listener, const ServiceID& sid){
369 logging_debug( "unbinding listener " << listener
370 << " from serviceid " << sid.toString() );
371
372 if( !communicationListeners.contains( sid ) ){
373 logging_warn( "cannot unbind listener. no listener registered on service id " << sid.toString() );
374 return false;
375 }
376
377 if( communicationListeners.get(sid) != listener ){
378 logging_warn( "listener bound to service id " << sid.toString()
379 << " is different than listener trying to unbind" );
380 return false;
381 }
382
383 communicationListeners.unregisterItem( sid );
384 return true;
385}
386
387bool BaseOverlay::bind(NodeListener* listener){
388 logging_debug( "binding node listener " << listener );
389
390 NodeListenerVector::iterator i = find( nodeListeners.begin(), nodeListeners.end(), listener );
391 if( i != nodeListeners.end() ){
392 logging_warn( "node listener " << listener << " is already bound, cannot bind" );
393 return false;
394 }
395
396 nodeListeners.push_back( listener );
397 return true;
398}
399
400bool BaseOverlay::unbind(NodeListener* listener){
401 logging_debug( "unbinding node listener " << listener );
402
403 NodeListenerVector::iterator i = find( nodeListeners.begin(), nodeListeners.end(), listener );
404 if( i == nodeListeners.end() ){
405 logging_warn( "node listener " << listener << " is not bound, cannot unbind" );
406 return false;
407 }
408
409 nodeListeners.erase( i );
410 return true;
411}
412
413void BaseOverlay::onLinkUp(const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote){
414
415 logging_debug( "base overlay received linkup event " + id.toString() );
416 // TODO: updateOvlVis( getNodeID(id) );
417
418 //
419 // if we get up a link while we are in the
420 // join phase and this is the link that
421 // we have initiated towards the spovnet owner
422 // continue the join process by sending
423 // a join request message through the link
424 //
425
426 if( state == BaseOverlayStateJoinInitiated && id == initiatorLink){
427
428 logging_info( "join has been initiated by me and the link is now up. " <<
429 "sending out join request for spovnet " <<
430 spovnetId.toString() );
431
432 OverlayMsg overMsg( OverlayMsg::OverlayMessageTypeJoinRequest, nodeId );
433 JoinRequest joinmsg( spovnetId, nodeId );
434 overMsg.encapsulate( &joinmsg );
435
436 state = BaseOverlayStateJoinInitiated; // state remains in JoinInitiated
437 bc->sendMessage( id, &overMsg );
438
439 return;
440
441 } // if( state == BaseOverlayStateJoinInitiated && id == initiatorLink)
442
443 //
444 // otherwise this is a link initiated by a service
445 // then we exchange update messages to exchange the
446 // service id and node id for the link. in this case
447 // we should have a link mapping for this link. if
448 // we have no link mapping this link was initiated by
449 // the remote side.
450 //
451
452 LinkMapping::iterator i = linkMapping.find( id );
453
454 if( i == linkMapping.end() ){
455
456 LinkItem item (id, NodeID::UNSPECIFIED, ServiceID::UNSPECIFIED, &CommunicationListener::DEFAULT );
457 linkMapping.insert( make_pair(id, item) );
458
459 } else {
460
461 logging_debug( "sending out OverlayMessageTypeUpdate" <<
462 " for service " << i->second.service.toString() <<
463 " with local node id " << nodeId.toString() <<
464 " on link " << id.toString() );
465
466 OverlayMsg overMsg(
467 OverlayMsg::OverlayMessageTypeUpdate,
468 i->second.service,
469 nodeId
470 );
471
472 bc->sendMessage( id, &overMsg );
473 i->second.markused();
474
475 } // if( i == linkMapping.end() )
476
477 // the link is only valid for the service when we receive
478 // the OverlayMessageTypeUpdate from the remote node and
479 // have the nodeid and serviceid for the link!
480}
481
482void BaseOverlay::onLinkDown(const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote){
483
484 logging_debug( "link went down " << id.toString() );
485
486 //
487 // tell the service that the link went
488 // down and remove the mapping
489 //
490
491 LinkMapping::iterator i = linkMapping.find( id );
492 if( i == linkMapping.end() ) {
493 // this can also be one of the baseoverlay links that
494 // no mapping is stored for. therefore we issue no warning.
495 // it can also be a link that has been dropped and the
496 // mapping is already deleted in the dropLink function.
497 // also, the service notification is issued then in dropLink
498 return;
499 }
500
501 i->second.interface->onLinkDown( id, i->second.node );
502 sideport->onLinkDown( id, this->nodeId, i->second.node, this->spovnetId );
503
504 linkMapping.erase( i );
505}
506
507void BaseOverlay::onLinkChanged(const LinkID& id, const NetworkLocator* oldlocal, const NetworkLocator* newlocal, const NetworkLocator* oldremote, const NetworkLocator* newremote){
508
509 logging_debug( "link changed " << id.toString() );
510
511 //
512 // tell the service that the link changed
513 //
514
515 LinkMapping::iterator i = linkMapping.find( id );
516 if( i == linkMapping.end() ) return;
517
518 i->second.interface->onLinkChanged( id, i->second.node );
519 sideport->onLinkChanged( id, this->nodeId, i->second.node, this->spovnetId );
520
521 // TODO call onLinkQoSChanged?
522
523 i->second.markused();
524}
525
526void BaseOverlay::onLinkFail(const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote){
527
528 logging_debug( "link failed " << id.toString() );
529
530 //
531 // tell the service that the link failed
532 //
533
534 LinkMapping::iterator i = linkMapping.find( id );
535 if( i == linkMapping.end() ) return;
536
537 i->second.interface->onLinkFail( id, i->second.node );
538 sideport->onLinkFail( id, this->nodeId, i->second.node, this->spovnetId );
539
540 i->second.markused();
541}
542
543void BaseOverlay::onLinkQoSChanged(const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote, const QoSParameterSet& qos) {
544
545 logging_debug( "link qos changed " << id.toString() );
546
547 //
548 // tell the service that the link qos has changed
549 //
550
551 LinkMapping::iterator i = linkMapping.find( id );
552 if( i == linkMapping.end() ) return;
553
554 // TODO: convert QoSParameterSet to the LinkProperties properties
555 // TODO: currently not in the interface: i->second.interface->onLinkQoSChanged( id, i->second.node, LinkProperties::DEFAULT );
556
557 i->second.markused();
558}
559
560bool BaseOverlay::onLinkRequest( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ){
561
562 // also see in the receiveMessage function. there the higher layer service
563 // is asked whether to accept link requests, but there a basic link association is
564 // already built up, so we know the node id
565 logging_debug("received link request from " << remote->toString() << ", accepting");
566 return true;
567}
568
569bool BaseOverlay::receiveMessage(const Message* message,
570 const LinkID& link, const NodeID& /*the nodeid is invalid in this case! removed var to prevent errors*/ ){
571
572 OverlayMsg* overlayMsg = ((Message*)message)->decapsulate<OverlayMsg>();
573 if( overlayMsg == NULL ) return false;
574
575 // mark the link as in action
576 LinkMapping::iterator item = linkMapping.find( link );
577 if( item != linkMapping.end() ) item->second.markused();
578
579 //
580 // handle user date that we forward to the
581 // appropriate service using the service id
582 // in the message. as we don't know the class
583 // of message that the service handles, we
584 // forward it as a pure Message*
585 //
586
587 if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeData) ) {
588
589 logging_debug( "baseoverlay received message of type OverlayMessageTypeData" );
590
591 const ServiceID& service = overlayMsg->getService();
592 CommunicationListener* serviceListener = communicationListeners.get( service );
593
594 logging_debug( "received data for service " << service.toString() );
595
596 if( serviceListener != NULL )
597 serviceListener->onMessage( overlayMsg, overlayMsg->getSourceNode(), link );
598
599 return true;
600
601 } // if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeData) )
602
603 //
604 // handle spovnet instance join requests
605 //
606
607 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinRequest) &&
608 state == BaseOverlayStateInitiator){
609
610 logging_debug( "baseoverlay received message of type OverlayMessageTypeJoinRequest" );
611
612 JoinRequest* joinReq = overlayMsg->decapsulate<JoinRequest>();
613 logging_info( "received join request for spovnet " <<
614 joinReq->getSpoVNetID().toString() );
615
616 //
617 // make sure that the node actually wants to join
618 // the correct spovnet id that we administrate
619 //
620
621 if( joinReq->getSpoVNetID() != spovnetId ){
622 logging_error( "received join request for spovnet we don't handle " <<
623 joinReq->getSpoVNetID().toString() );
624 return false;
625 }
626
627 //
628 // only if all services allow the node to join it is allowed
629 // using the isJoinAllowed interface security policies can be
630 // implemented by higher layer services
631 //
632
633 // TODO: here you can implement mechanisms to deny joining of a node
634 bool allow = true;
635
636 logging_info( "sending back join reply for spovnet " <<
637 spovnetId.toString() << " to node " <<
638 overlayMsg->getSourceNode().toString() <<
639 ". result: " << (allow ? "allowed" : "denied") );
640
641 joiningNodes.push_back( overlayMsg->getSourceNode() );
642
643 //
644 // send back our spovnetid, default overlay parameters,
645 // join allow result, and ourself as the endpoint
646 // to bootstrap the overlay against
647 //
648
649 OverlayMsg retmsg( OverlayMsg::OverlayMessageTypeJoinReply, nodeId );
650 JoinReply replyMsg( spovnetId, OverlayParameterSet::DEFAULT,
651 allow, getEndpointDescriptor() );
652
653 retmsg.encapsulate(&replyMsg);
654 bc->sendMessage( link, &retmsg );
655
656 return true;
657
658 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinRequest) && state == BaseOverlayStateInitiator)
659
660 //
661 // handle replies to spovnet instance join requests
662 //
663
664 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinReply) &&
665 state == BaseOverlayStateJoinInitiated){
666
667 logging_debug( "baseoverlay received message of type OverlayMessageTypeJoinReply" );
668
669 JoinReply* replyMsg = overlayMsg->decapsulate<JoinReply>();
670 logging_info( "received spovnet join reply" );
671
672 //
673 // make sure that we actually wanted to get
674 // into the spovnet whose id is in the message
675 //
676
677 if( replyMsg->getSpoVNetID() != spovnetId ){
678 logging_error( "received spovnet join reply for spovnet " <<
679 replyMsg->getSpoVNetID().toString() <<
680 " but we wanted to join spovnet " <<
681 spovnetId.toString() );
682
683 // state does not change here, maybe
684 // the reply does come in later
685 return false;
686 }
687
688 //
689 // if we did not get access to the spovnet
690 // notify of the failure and
691 // close the link to the initiator
692 //
693
694 if( ! replyMsg->getJoinAllowed() ){
695
696 logging_error( "our join request has been denied" );
697
698 bc->dropLink( initiatorLink );
699 initiatorLink = LinkID::UNSPECIFIED;
700 state = BaseOverlayStateInvalid;
701
702 // inform all registered services of the event
703 BOOST_FOREACH( NodeListener* i, nodeListeners ){
704 i->onJoinFailed( spovnetId );
705 }
706
707 return true;
708 }
709
710 logging_info( "join request has been accepted for spovnet " << spovnetId.toString() );
711
712 //
713 // if we did get access to the spovnet
714 // we try to create the overlay structure
715 // as given in the reply message
716 //
717
718 overlayInterface = OverlayFactory::create( *this, replyMsg->getParam(), nodeId, this );
719
720 if( overlayInterface == NULL ){
721 logging_error( "overlay structure not supported" );
722
723 bc->dropLink( initiatorLink );
724 initiatorLink = LinkID::UNSPECIFIED;
725 state = BaseOverlayStateInvalid;
726
727 // inform all registered services of the event
728 BOOST_FOREACH( NodeListener* i, nodeListeners ){
729 i->onJoinFailed( spovnetId );
730 }
731
732 return true;
733 }
734
735 //
736 // now start the join process for the overlay.
737 // the join process for the spovnet baseoverlay
738 // is now complete. we use the endpoint for
739 // overlay structure bootstrapping that the
740 // initiator provided in his reply message
741 //
742
743 state = BaseOverlayStateCompleted;
744 ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN);
745
746 overlayInterface->createOverlay();
747 overlayInterface->joinOverlay( replyMsg->getBootstrapEndpoint() );
748
749 // inform all registered services of the event
750 BOOST_FOREACH( NodeListener* i, nodeListeners ){
751 i->onJoinCompleted( spovnetId );
752 }
753
754 return true;
755
756 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinReply) && state == BaseOverlayStateJoinInitiated)
757
758
759 //
760 // handle update messages for link establishment
761 //
762
763 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeUpdate) ){
764
765 logging_debug( "baseoverlay received message of type OverlayMessageTypeUpdate" );
766
767 const NodeID& sourcenode = overlayMsg->getSourceNode();
768 const ServiceID& service = overlayMsg->getService();
769
770 //
771 // we should have a linkmapping for the link, otherwise
772 // we ignore update messages
773 //
774
775 LinkMapping::iterator i = linkMapping.find( link );
776 if( i == linkMapping.end() ){
777 logging_warn( "received overlay update message for link " <<
778 link.toString() << " for which we have no mapping" );
779 return false;
780 }
781
782 //
783 // update our link mapping information for this link
784 //
785
786 bool changed = ( i->second.node != sourcenode ) || ( i->second.service != service );
787
788 i->second.node = sourcenode;
789 i->second.service = service;
790
791 //
792 // if our link information changed, we send out an update, too
793 //
794
795 if( changed ){
796 OverlayMsg overMsg( OverlayMsg::OverlayMessageTypeUpdate, i->second.service, nodeId );
797 bc->sendMessage( link, &overMsg );
798 }
799
800 //
801 // set the correct listener service for the linkitem
802 // now we can tell the registered service of the linkup event
803 //
804
805 if( !communicationListeners.contains( service ) ){
806 logging_warn( "linkup event for service that has not been registered" );
807 return false;
808 }
809
810 CommunicationListener* iface = communicationListeners.get( service );
811 if( iface == NULL || iface == &CommunicationListener::DEFAULT ){
812 logging_warn( "linkup event for service that has been registered with a NULL interface" );
813 return true;
814 }
815
816 i->second.interface = iface;
817 i->second.markused();
818
819 //
820 // ask the service whether it wants to accept this link
821 //
822
823 if( iface->onLinkRequest(sourcenode) ){
824
825 // call the notification functions
826 iface->onLinkUp( link, sourcenode );
827 sideport->onLinkUp( link, nodeId, sourcenode, this->spovnetId );
828
829 } else {
830 // prevent onLinkDown calls to the service
831 i->second.interface = &CommunicationListener::DEFAULT;
832 // drop the link
833 dropLink( link );
834 }
835
836 return true;
837
838 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeUpdate) )
839
840 //
841 // bye messages to say goodbye
842 //
843
844 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeBye)){
845
846 logging_debug( "baseoverlay received message of type OverlayMessageTypeBye" );
847
848 logging_debug( "received bye message from " <<
849 overlayMsg->getSourceNode().toString() );
850
851 //
852 // if we are the initiator and receive a bye from a node
853 // the node just left. if we are a node and receive a bye
854 // from the initiator, we have to close, too.
855 //
856
857 if( overlayMsg->getSourceNode() == spovnetInitiator ){
858
859 bc->dropLink( initiatorLink );
860 initiatorLink = LinkID::UNSPECIFIED;
861 state = BaseOverlayStateInvalid;
862
863 logging_fatal( "initiator ended spovnet" );
864
865 // inform all registered services of the event
866 BOOST_FOREACH( NodeListener* i, nodeListeners ){
867 i->onLeaveFailed( spovnetId );
868 }
869
870 } else {
871
872 // a node that said goodbye and we are the initiator
873 // don't have to do much here, as the node also
874 // will go out of the overlay structure
875 logging_info( "node left " << overlayMsg->getSourceNode() );
876
877 }
878
879 return true;
880
881 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeBye))
882
883 //
884 // something wrong ...
885 //
886
887 else {
888
889 logging_error( "received message in invalid state! don't know " <<
890 "what to do with this message of type " <<
891 overlayMsg->getType() );
892 return false;
893
894 } // else
895
896 return false;
897}
898
899void BaseOverlay::broadcastMessage(Message* message, const ServiceID& service){
900
901 logging_debug( "broadcasting message to all known nodes " <<
902 "in the overlay from service " + service.toString() );
903
904 OverlayInterface::NodeList nodes = overlayInterface->getKnownNodes();
905
906 OverlayInterface::NodeList::iterator i = nodes.begin();
907 OverlayInterface::NodeList::iterator iend = nodes.end();
908
909 for( ; i != iend; i++ ){
910 if( *i == nodeId) continue; // don't send to ourselfs
911 sendMessage( message, *i, service );
912 }
913}
914
915vector<NodeID> BaseOverlay::getOverlayNeighbors() const {
916 // the known nodes _can_ also include our
917 // node, so we remove ourselfs
918
919 vector<NodeID> nodes = overlayInterface->getKnownNodes();
920 vector<NodeID>::iterator i = find( nodes.begin(), nodes.end(), this->nodeId );
921 if( i != nodes.end() ) nodes.erase( i );
922
923 return nodes;
924}
925
926void BaseOverlay::updateOvlVis( const NodeID& n ) {
927 NodeID node = n;
928/* void visShowNodeBubble (
929 NETWORK_ID network,
930 NodeID& node,
931 string label
932 );
933*/
934 using namespace std;
935
936 if (node == nodeId || node.isUnspecified()) return;
937
938 // min/max
939 if ( node < min || min.isUnspecified() ) min = node;
940 if ( node > max || max.isUnspecified() ) max = node;
941
942 // successor
943 if ( succ.isUnspecified() || (node > nodeId && (succ < nodeId || (node-nodeId) < (succ-nodeId))) ) {
944 if (!succ.isUnspecified() && node != succ)
945 ovl.visDisconnect(ovlId, nodeId, succ, string(""));
946 succ = node;
947 ovl.visConnect(ovlId, nodeId, succ, string(""));
948 }
949
950 // set successor (circle-wrap)
951 if (succ.isUnspecified() && !min.isUnspecified()) {
952 succ = min;
953 ovl.visConnect(ovlId, nodeId, succ, string(""));
954 }
955}
956
957const NodeID& BaseOverlay::getNodeID(const LinkID& lid) const {
958
959 if( lid == LinkID::UNSPECIFIED ) return nodeId;
960
961 LinkMapping::const_iterator i = linkMapping.find( lid );
962 if( i == linkMapping.end() ) return NodeID::UNSPECIFIED;
963 else return i->second.node;
964}
965
966vector<LinkID> BaseOverlay::getLinkIDs( const NodeID& nid ) const {
967
968 vector<LinkID> linkvector;
969
970 BOOST_FOREACH( LinkPair item, linkMapping ){
971 if( item.second.node == nid || nid == NodeID::UNSPECIFIED ){
972 linkvector.push_back( item.second.link );
973 }
974 }
975
976 return linkvector;
977}
978
979void BaseOverlay::incomingRouteMessage(Message* msg){
980 // gets handled as normal data message
981 receiveMessage( msg, LinkID::UNSPECIFIED, NodeID::UNSPECIFIED );
982}
983
984void BaseOverlay::onNodeJoin(const NodeID& node){
985
986 JoiningNodes::iterator i = std::find( joiningNodes.begin(), joiningNodes.end(), node );
987 if( i == joiningNodes.end() ) return;
988
989 logging_info( "node has successfully joined baseoverlay and overlay structure "
990 << node.toString() );
991
992 joiningNodes.erase( i );
993}
994
995void BaseOverlay::eventFunction(){
996
997 list<LinkID> oldlinks;
998 time_t now = time(NULL);
999
1000 // first gather all the links from linkMapping that need droppin
1001 // don't directly drop, as the dropLink function affects the
1002 // linkMapping structure that we are traversing here.
1003 // drop links after a timeout of 30s
1004
1005 BOOST_FOREACH( LinkPair item, linkMapping ){
1006 if( item.second.autolink && difftime(now, item.second.lastuse) > 30)
1007 oldlinks.push_back( item.first );
1008 }
1009
1010 BOOST_FOREACH( const LinkID lnk, oldlinks ){
1011 logging_debug( "auto-link " << lnk.toString() << " timed out and is getting dropped" );
1012 dropLink( lnk );
1013 }
1014}
1015
1016}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.