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

Last change on this file since 3712 was 3712, checked in by Christoph Mayer, 16 years ago

-autolink message buffer von basecomm nach baseoverlay und komplette logik hochgezogen damit mit neuem overlay routing zurecht kommt, damit ist letzer fehler raus

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