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

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

-some further fixed in the protocols and cleaning the interfaces for stuff we have no functionality yet

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