close Warning: Can't use blame annotator:
No changeset 2259 in the repository

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

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

-dynamische wahl des overlays durch den initiator, nodes kriegen bei join mitgeteilt welche overlay struktur zu verwenden ist

File size: 35.0 KB
RevLine 
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 //
729 // send back our spovnetid, default overlay parameters, join allow
730 // result, and ourself as the end-point to bootstrap the overlay against
731 //
732
733 assert( overlayInterface != NULL );
734 OverlayParameterSet parameters = overlayInterface->getParameters();
735
736 OverlayMsg retmsg( OverlayMsg::OverlayMessageTypeJoinReply, nodeId );
737 JoinReply replyMsg( spovnetId, parameters,
738 allow, getEndpointDescriptor() );
739
740 retmsg.encapsulate(&replyMsg);
741 bc->sendMessage( link, &retmsg );
742
743 return true;
744
745 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinRequest)
746 // && state == BaseOverlayStateInitiator)
747
748 /* ************************************************************************
749 * handle replies to spovnet instance join requests
750 */
751 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinReply) &&
752 state == BaseOverlayStateJoinInitiated){
753
754 logging_debug(
755 "baseoverlay received message of type OverlayMessageTypeJoinReply");
756
757 JoinReply* replyMsg = overlayMsg->decapsulate<JoinReply>();
758 logging_info( "received spovnet join reply" );
759
760 // ensure that we actually wanted to get into the spovnet whose id is
761 // in the message
762 if( replyMsg->getSpoVNetID() != spovnetId ){
763 logging_error( "received spovnet join reply for spovnet " <<
764 replyMsg->getSpoVNetID().toString() <<
765 " but we wanted to join spovnet " <<
766 spovnetId.toString() );
767
768 // state does not change here, maybe the reply does come in later
769 return false;
770 }
771
772 // if we did not get access to the spovnet notify of the failure and
773 // close the link to the initiator
774 if( ! replyMsg->getJoinAllowed() ){
775
776 logging_error( "our join request has been denied" );
777
778 bc->dropLink( initiatorLink );
779 initiatorLink = LinkID::UNSPECIFIED;
780 state = BaseOverlayStateInvalid;
781
782 // inform all registered services of the event
783 BOOST_FOREACH( NodeListener* i, nodeListeners ){
784 i->onJoinFailed( spovnetId );
785 }
786
787 return true;
788 }
789
790 logging_info( "join request has been accepted for spovnet " <<
791 spovnetId.toString() );
792
793 // if we did get access to the spovnet we try to create the overlay
794 // structure as given in the reply message
795 overlayInterface = OverlayFactory::create( *this,
796 replyMsg->getParam(), nodeId, this );
797
798 if( overlayInterface == NULL ){
799 logging_error( "overlay structure not supported" );
800
801 bc->dropLink( initiatorLink );
802 initiatorLink = LinkID::UNSPECIFIED;
803 state = BaseOverlayStateInvalid;
804
805 // inform all registered services of the event
806 BOOST_FOREACH( NodeListener* i, nodeListeners )
807 i->onJoinFailed( spovnetId );
808
809 return true;
810 }
811
812 /* now start the join process for the overlay. the join process for the
813 * spovnet baseoverlay is now complete. we use the endpoint for overlay
814 * structure bootstrapping that the initiator provided in his reply
815 * message */
816 state = BaseOverlayStateCompleted;
817 ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN);
818
819 overlayInterface->createOverlay();
820 overlayInterface->joinOverlay( replyMsg->getBootstrapEndpoint() );
821
822 // inform all registered services of the event
823 BOOST_FOREACH( NodeListener* i, nodeListeners ){
824 i->onJoinCompleted( spovnetId );
825 }
826
827 return true;
828
829 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinReply) && state == BaseOverlayStateJoinInitiated)
830
831
832 /* ************************************************************************
833 * handle update messages for link establishment
834 */
835 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeUpdate) ){
836
837 logging_debug(
838 "baseoverlay received message of type OverlayMessageTypeUpdate"
839 );
840
841 const NodeID& sourcenode = overlayMsg->getSourceNode();
842 const ServiceID& service = overlayMsg->getService();
843
844 // linkmapping for the link available? no-> ignore
845 LinkMapping::iterator i = linkMapping.find( link );
846 if( i == linkMapping.end() ) {
847 logging_warn( "received overlay update message for link " <<
848 link.toString() << " for which we have no mapping" );
849 return false;
850 }
851
852 // update our link mapping information for this link
853 bool changed = ( i->second.node != sourcenode ) || ( i->second.service != service );
854 i->second.node = sourcenode;
855 i->second.service = service;
856
857 // if our link information changed, we send out an update, too
858 if( changed ){
859 OverlayMsg overMsg( OverlayMsg::OverlayMessageTypeUpdate, i->second.service, nodeId );
860 bc->sendMessage( link, &overMsg );
861 }
862
863 // set the correct listener service for the linkitem
864 // now we can tell the registered service of the linkup event
865 if( !communicationListeners.contains( service ) ){
866 logging_warn( "linkup event for service that has not been registered" );
867 return false;
868 }
869
870 CommunicationListener* iface = communicationListeners.get( service );
871 if( iface == NULL || iface == &CommunicationListener::DEFAULT ){
872 logging_warn( "linkup event for service that has been registered "
873 "with a NULL interface" );
874 return true;
875 }
876
877 i->second.interface = iface;
878 i->second.markused();
879
880 // ask the service whether it wants to accept this link
881 if( !iface->onLinkRequest(sourcenode) ){
882
883 logging_debug("link " << link.toString() <<
884 " has been denied by service " << service.toString() << ", dropping link");
885
886 // prevent onLinkDown calls to the service
887 i->second.interface = &CommunicationListener::DEFAULT;
888 // drop the link
889 dropLink( link );
890
891 return true;
892 }
893
894 //
895 // link has been accepted, link is now up, send messages out first
896 //
897
898 i->second.linkup = true;
899 logging_debug("link " << link.toString() <<
900 " has been accepted by service " << service.toString() << " and is now up");
901
902 if( i->second.waitingmsg.size() > 0 ){
903 logging_info( "sending out queued messages on link " << link.toString() );
904
905 BOOST_FOREACH( Message* msg, i->second.waitingmsg ){
906 sendMessage( msg, link );
907 delete msg;
908 }
909
910 i->second.waitingmsg.clear();
911 }
912
913 // call the notification functions
914 iface->onLinkUp( link, sourcenode );
915 sideport->onLinkUp( link, nodeId, sourcenode, this->spovnetId );
916
917 return true;
918
919 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeUpdate) )
920
921 /* ************************************************************************
922 * handle bye messages
923 */
924 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeBye) ) {
925
926 logging_debug( "BaseOverlay received message of type OverlayMessageTypeBye" );
927 logging_debug( "Received bye message from " <<
928 overlayMsg->getSourceNode().toString() );
929
930 /* if we are the initiator and receive a bye from a node
931 * the node just left. if we are a node and receive a bye
932 * from the initiator, we have to close, too.
933 */
934 if( overlayMsg->getSourceNode() == spovnetInitiator ){
935
936 bc->dropLink( initiatorLink );
937 initiatorLink = LinkID::UNSPECIFIED;
938 state = BaseOverlayStateInvalid;
939
940 logging_fatal( "initiator ended spovnet" );
941
942 // inform all registered services of the event
943 BOOST_FOREACH( NodeListener* i, nodeListeners ){
944 i->onLeaveFailed( spovnetId );
945 }
946
947 } else {
948 // a node that said goodbye and we are the initiator don't have to
949 // do much here, as the node also will go out of the overlay
950 // structure
951 logging_info( "node left " << overlayMsg->getSourceNode() );
952 }
953
954 return true;
955
956 } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeBye))
957
958 /* ************************************************************************
959 * handle link request forwarded through the overlay
960 */
961 else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeLinkRequest)) {
962 LinkRequest* linkReq = overlayMsg->decapsulate<LinkRequest>();
963 const ServiceID& service = overlayMsg->getService();
964 if (linkReq->isReply()) {
965
966 // find link
967 PendingLinkMap::iterator i = pendingLinks.find( linkReq->getNonce() );
968 if ( i == pendingLinks.end() ) {
969 logging_error( "Nonce not found in link request" );
970 return true;
971 }
972
973 // debug message
974 logging_debug( "LinkRequest reply received. Establishing link "
975 << i->second << " to " << (linkReq->getEndpoint()->toString())
976 << " for service " << service.toString()
977 << " with nonce " << linkReq->getNonce()
978 );
979
980 // establishing link
981 bc->establishLink( *linkReq->getEndpoint(), i->second );
982 } else {
983 OverlayMsg overlay_msg( OverlayMsg::OverlayMessageTypeLinkRequest, service, nodeId );
984 LinkRequest link_request_msg(
985 linkReq->getNonce(), &bc->getEndpointDescriptor(), true );
986 overlay_msg.encapsulate( &link_request_msg );
987
988 // debug message
989 logging_debug( "Sending LinkRequest reply for link with nonce " <<
990 linkReq->getNonce() );
991
992 // route message back over overlay
993 overlayInterface->routeMessage(
994 overlayMsg->getSourceNode(), &overlay_msg
995 );
996 }
997 } // if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeLinkRequest))
998
999 /* ************************************************************************
1000 * unknown message type ... error!
1001 */
1002 else {
1003
1004 logging_error( "received message in invalid state! don't know " <<
1005 "what to do with this message of type " <<
1006 overlayMsg->getType() );
1007 return false;
1008
1009 } // else
1010
1011 return false;
1012}
1013
1014void BaseOverlay::broadcastMessage(Message* message, const ServiceID& service){
1015
1016 logging_debug( "broadcasting message to all known nodes " <<
1017 "in the overlay from service " + service.toString() );
1018
1019 OverlayInterface::NodeList nodes = overlayInterface->getKnownNodes();
1020
1021 OverlayInterface::NodeList::iterator i = nodes.begin();
1022 OverlayInterface::NodeList::iterator iend = nodes.end();
1023
1024 for( ; i != iend; i++ ){
1025 if( *i == nodeId) continue; // don't send to ourselfs
1026 sendMessage( message, *i, service );
1027 }
1028}
1029
1030vector<NodeID> BaseOverlay::getOverlayNeighbors() const {
1031 // the known nodes _can_ also include our
1032 // node, so we remove ourselfs
1033
1034 vector<NodeID> nodes = overlayInterface->getKnownNodes();
1035 vector<NodeID>::iterator i = find( nodes.begin(), nodes.end(), this->nodeId );
1036 if( i != nodes.end() ) nodes.erase( i );
1037
1038 return nodes;
1039}
1040
1041void BaseOverlay::updateOvlVis( const NodeID& n ) {
1042 NodeID node = n;
1043/* void visShowNodeBubble (
1044 NETWORK_ID network,
1045 NodeID& node,
1046 string label
1047 );
1048*/
1049 using namespace std;
1050
1051 if (node == nodeId || node.isUnspecified()) return;
1052
1053 // min/max
1054 if ( node < min || min.isUnspecified() ) min = node;
1055 if ( node > max || max.isUnspecified() ) max = node;
1056
1057 // successor
1058 if ( succ.isUnspecified() || (node > nodeId && (succ < nodeId || (node-nodeId) < (succ-nodeId))) ) {
1059 if (!succ.isUnspecified() && node != succ)
1060 ovl.visDisconnect(ovlId, nodeId, succ, string(""));
1061 succ = node;
1062 ovl.visConnect(ovlId, nodeId, succ, string(""));
1063 }
1064
1065 // set successor (circle-wrap)
1066 if (succ.isUnspecified() && !min.isUnspecified()) {
1067 succ = min;
1068 ovl.visConnect(ovlId, nodeId, succ, string(""));
1069 }
1070}
1071
1072const NodeID& BaseOverlay::getNodeID(const LinkID& lid) const {
1073
1074 if( lid == LinkID::UNSPECIFIED ) return nodeId;
1075
1076 LinkMapping::const_iterator i = linkMapping.find( lid );
1077 if( i == linkMapping.end() ) return NodeID::UNSPECIFIED;
1078 else return i->second.node;
1079}
1080
1081vector<LinkID> BaseOverlay::getLinkIDs( const NodeID& nid ) const {
1082
1083 vector<LinkID> linkvector;
1084
1085 BOOST_FOREACH( LinkPair item, linkMapping ){
1086 if( item.second.node == nid || nid == NodeID::UNSPECIFIED ){
1087 linkvector.push_back( item.second.link );
1088 }
1089 }
1090
1091 return linkvector;
1092}
1093
1094void BaseOverlay::incomingRouteMessage(Message* msg){
1095 // gets handled as normal data message
1096 receiveMessage( msg, LinkID::UNSPECIFIED, NodeID::UNSPECIFIED );
1097}
1098
1099void BaseOverlay::onNodeJoin(const NodeID& node){
1100
1101 JoiningNodes::iterator i = std::find( joiningNodes.begin(), joiningNodes.end(), node );
1102 if( i == joiningNodes.end() ) return;
1103
1104 logging_info( "node has successfully joined baseoverlay and overlay structure "
1105 << node.toString() );
1106
1107 joiningNodes.erase( i );
1108}
1109
1110void BaseOverlay::eventFunction(){
1111
1112 list<LinkID> oldlinks;
1113 time_t now = time(NULL);
1114
1115 // first gather all the links from linkMapping that need droppin
1116 // don't directly drop, as the dropLink function affects the
1117 // linkMapping structure that we are traversing here.
1118 // drop links after a timeout of 30s
1119
1120 BOOST_FOREACH( LinkPair item, linkMapping ){
1121 if( item.second.autolink && difftime(now, item.second.lastuse) > 30)
1122 oldlinks.push_back( item.first );
1123 }
1124
1125 BOOST_FOREACH( const LinkID lnk, oldlinks ) {
1126 logging_debug( "auto-link " << lnk.toString() << " timed out and is getting dropped" );
1127 dropLink( lnk );
1128 }
1129}
1130
1131}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.