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

Last change on this file since 3695 was 3693, checked in by mies, 16 years ago

fixed <unspec> errors with chord

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