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

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