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

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