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

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

-fixed all compile errors, but now the overlay functionality is messed up somewhere

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