Ignore:
Timestamp:
May 26, 2009, 1:40:23 AM (15 years ago)
Author:
mies
Message:

Merged 20090512-mies-connectors changes r3472:r3689 into trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/overlay/BaseOverlay.cpp

    r3374 r3690  
    1 // [Licence]
     1// [License]
    22// The Ariba-Underlay Copyright
    33//
     
    3535// official policies, either expressed or implied, of the Institute of
    3636// Telematics.
    37 // [Licence]
     37// [License]
    3838
    3939#include "BaseOverlay.h"
     
    4343#include "ariba/CommunicationListener.h"
    4444#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"
    4550
    4651namespace ariba {
     
    175180void BaseOverlay::createSpoVNet(const SpoVNetID& id, const OverlayParameterSet& param, const SecurityParameterSet& sec, const QoSParameterSet& qos){
    176181
    177         //
    178         // set the state that we are an initiator,
    179         // this way incoming messages are handled correctly
    180         //
    181 
    182         logging_info( "creating spovnet " + id.toString() << " with nodeid " << nodeId.toString() );
     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() );
    183186
    184187        spovnetId = id;
     
    192195        }
    193196
    194         //
    195197        // bootstrap against ourselfs
    196         //
    197 
    198198        overlayInterface->joinOverlay();
    199         BOOST_FOREACH( NodeListener* i, nodeListeners ){
     199        BOOST_FOREACH( NodeListener* i, nodeListeners )
    200200                i->onJoinCompleted( spovnetId );
    201         }
    202201
    203202        ovl.visChangeNodeIcon ( ovlId, nodeId, OvlVis::ICON_ID_CAMERA );
     
    205204}
    206205
    207 const LinkID BaseOverlay::establishLink(const NodeID& node, const ServiceID& service){
    208 
    209         // TODO: if this is not a onehop overlay the operation will go asynchronously
    210         const EndpointDescriptor& endpoint = overlayInterface->resolveNode( node );
    211         if( endpoint == EndpointDescriptor::UNSPECIFIED ){
    212                 logging_error( "could not resolve node to endpoint. unable to establish link" );
    213                 return LinkID::UNSPECIFIED;
    214         }
    215 
    216         logging_debug( "baseoverlay called to establish link between node " <<
    217                         node.toString() << " on endpoint " << endpoint.toString() <<
    218                         " for service " << service.toString() );
    219 
    220         return establishLink( endpoint, service );
    221 }
    222 
    223 const LinkID BaseOverlay::establishLink(const EndpointDescriptor& ep, const ServiceID& service){
     206
     207/// establishes a link between two arbitrary nodes
     208const LinkID BaseOverlay::establishLink( const NodeID& node,
     209                const ServiceID& service, const LinkID& link_id ) {
    224210
    225211        if( !communicationListeners.contains( service ) ){
     
    228214        }
    229215
    230         const LinkID link = bc->establishLink( ep );
     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 );
    231257
    232258        CommunicationListener* receiver = communicationListeners.get( service );
     
    260286seqnum_t BaseOverlay::sendMessage(const Message* message, const LinkID& link ){
    261287
    262         logging_debug( "baseoverlay is sending message on link " << link.toString() );
     288        logging_debug( "baseoverlay is sending data message on link " << link.toString() );
    263289
    264290        LinkMapping::iterator i = linkMapping.find( link );
     
    268294        }
    269295
    270         OverlayMsg overmsg( OverlayMsg::OverlayMessageTypeData, i->second.service, nodeId );
     296        OverlayMsg overmsg(
     297                OverlayMsg::OverlayMessageTypeData,     i->second.service, nodeId );
    271298        overmsg.encapsulate( const_cast<Message*>(message) );
    272299
     
    342369        return overlayInterface->resolveNode( node );
    343370}
     371
    344372
    345373bool BaseOverlay::bind(CommunicationListener* listener, const ServiceID& sid){
     
    567595}
    568596
     597
    569598bool BaseOverlay::receiveMessage(const Message* message,
    570         const LinkID& link, const NodeID& /*the nodeid is invalid in this case! removed var to prevent errors*/ ){
    571 
    572         OverlayMsg* overlayMsg = ((Message*)message)->decapsulate<OverlayMsg>();
     599        const LinkID& link, const NodeID&
     600        /*the nodeid is invalid in this case! removed var to prevent errors*/ ){
     601
     602        // decapsulate overlay message
     603        logging_debug( "go msg " << message->toString());
     604        OverlayMsg* overlayMsg = const_cast<Message*>(message)->decapsulate<OverlayMsg>();
    573605        if( overlayMsg == NULL ) return false;
    574606
     
    577609        if( item != linkMapping.end() ) item->second.markused();
    578610
    579         //
    580         // handle user date that we forward to the
    581         // appropriate service using the service id
    582         // in the message. as we don't know the class
    583         // of message that the service handles, we
    584         // forward it as a pure Message*
    585         //
    586 
     611        /* ************************************************************************
     612        /* handle user date that we forward to the appropriate service using the
     613         * service id in the message. as we don't know the class of message that
     614         * the service handles, we forward it as a pure Message
     615         */
    587616        if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeData) ) {
    588617
     
    601630        } // if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeData) )
    602631
    603         //
    604         // handle spovnet instance join requests
    605         //
    606 
     632        /* ************************************************************************
     633        /* Handle spovnet instance join requests
     634         */
    607635        else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinRequest) &&
    608636                                                state == BaseOverlayStateInitiator){
     
    614642                                         joinReq->getSpoVNetID().toString() );
    615643
    616                 //
    617                 // make sure that the node actually wants to join
    618                 // the correct spovnet id that we administrate
    619                 //
    620 
     644                /* make sure that the node actually wants to join
     645                 * the correct spovnet id that we administrate */
    621646                if( joinReq->getSpoVNetID() != spovnetId ){
    622647                        logging_error( "received join request for spovnet we don't handle " <<
     
    641666                joiningNodes.push_back( overlayMsg->getSourceNode() );
    642667
    643                 //
    644                 // send back our spovnetid, default overlay parameters,
    645                 // join allow result, and ourself as the endpoint
    646                 // to bootstrap the overlay against
    647                 //
    648 
     668                // send back our spovnetid, default overlay parameters, join allow
     669                // result, and ourself as the end-point to bootstrap the overlay against
    649670                OverlayMsg retmsg( OverlayMsg::OverlayMessageTypeJoinReply, nodeId );
    650671                JoinReply replyMsg( spovnetId, OverlayParameterSet::DEFAULT,
     
    656677                return true;
    657678
    658         } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinRequest) && state == BaseOverlayStateInitiator)
    659 
    660         //
    661         // handle replies to spovnet instance join requests
    662         //
    663 
     679        } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinRequest)
     680          //          && state == BaseOverlayStateInitiator)
     681
     682        /* ************************************************************************
     683         * handle replies to spovnet instance join requests
     684         */
    664685        else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeJoinReply) &&
    665686                                                state == BaseOverlayStateJoinInitiated){
    666687
    667                 logging_debug( "baseoverlay received message of type OverlayMessageTypeJoinReply" );
     688                logging_debug(
     689                        "baseoverlay received message of type OverlayMessageTypeJoinReply");
    668690
    669691                JoinReply* replyMsg = overlayMsg->decapsulate<JoinReply>();
    670692                logging_info( "received spovnet join reply" );
    671693
    672                 //
    673                 // make sure that we actually wanted to get
    674                 // into the spovnet whose id is in the message
    675                 //
    676 
     694                // ensure that we actually wanted to get into the spovnet whose id is
     695                // in the message
    677696                if( replyMsg->getSpoVNetID() != spovnetId ){
    678697                        logging_error( "received spovnet join reply for spovnet " <<
     
    681700                                        spovnetId.toString() );
    682701
    683                         // state does not change here, maybe
    684                         // the reply does come in later
     702                        // state does not change here, maybe the reply does come in later
    685703                        return false;
    686704                }
    687705
    688                 //
    689                 // if we did not get access to the spovnet
    690                 // notify of the failure and
     706                // if we did not get access to the spovnet notify of the failure and
    691707                // close the link to the initiator
    692                 //
    693 
    694708                if( ! replyMsg->getJoinAllowed() ){
    695709
     
    708722                }
    709723
    710                 logging_info( "join request has been accepted for spovnet " << spovnetId.toString() );
    711 
    712                 //
    713                 // if we did get access to the spovnet
    714                 // we try to create the overlay structure
    715                 // as given in the reply message
    716                 //
    717 
    718                 overlayInterface = OverlayFactory::create( *this, replyMsg->getParam(), nodeId, this );
     724                logging_info( "join request has been accepted for spovnet " <<
     725                                spovnetId.toString() );
     726
     727                // if we did get access to the spovnet we try to create the overlay
     728                // structure as given in the reply message
     729                overlayInterface = OverlayFactory::create( *this,
     730                                replyMsg->getParam(), nodeId, this );
    719731
    720732                if( overlayInterface == NULL ){
     
    726738
    727739                        // inform all registered services of the event
    728                         BOOST_FOREACH( NodeListener* i, nodeListeners ){
     740                        BOOST_FOREACH( NodeListener* i, nodeListeners )
    729741                                i->onJoinFailed( spovnetId );
    730                         }
    731742
    732743                        return true;
    733744                }
    734745
    735                 //
    736                 // now start the join process for the overlay.
    737                 // the join process for the spovnet baseoverlay
    738                 // is now complete. we use the endpoint for
    739                 // overlay structure bootstrapping that the
    740                 // initiator provided in his reply message
    741                 //
    742 
     746                /* now start the join process for the overlay. the join process for the
     747                 * spovnet baseoverlay is now complete. we use the endpoint for overlay
     748                 * structure bootstrapping that the initiator provided in his reply
     749                 * message */
    743750                state = BaseOverlayStateCompleted;
    744751                ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN);
     
    757764
    758765
    759         //
    760         // handle update messages for link establishment
    761         //
    762 
     766        /* ************************************************************************
     767     * handle update messages for link establishment
     768     */
    763769        else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeUpdate) ){
    764770
    765                 logging_debug( "baseoverlay received message of type OverlayMessageTypeUpdate" );
     771                logging_debug(
     772                        "baseoverlay received message of type OverlayMessageTypeUpdate"
     773                );
    766774
    767775                const NodeID& sourcenode = overlayMsg->getSourceNode();
    768776                const ServiceID& service = overlayMsg->getService();
    769777
    770                 //
    771                 // we should have a linkmapping for the link, otherwise
    772                 // we ignore update messages
    773                 //
    774 
     778                // linkmapping for the link available? no-> ignore
    775779                LinkMapping::iterator i = linkMapping.find( link );
    776                 if( i == linkMapping.end() ){
     780                if( i == linkMapping.end() ) {
    777781                        logging_warn( "received overlay update message for link " <<
    778782                                        link.toString() << " for which we have no mapping" );
     
    780784                }
    781785
    782                 //
    783786                // update our link mapping information for this link
    784                 //
    785 
    786                 bool changed = ( i->second.node != sourcenode ) || ( i->second.service != service );
    787 
     787                bool changed =
     788                        ( i->second.node != sourcenode ) ||
     789                        ( i->second.service != service );
    788790                i->second.node = sourcenode;
    789791                i->second.service = service;
    790792
    791                 //
    792793                // if our link information changed, we send out an update, too
    793                 //
    794 
    795794                if( changed ){
    796795                        OverlayMsg overMsg( OverlayMsg::OverlayMessageTypeUpdate, i->second.service, nodeId );
     
    798797                }
    799798
    800                 //
    801799                // set the correct listener service for the linkitem
    802800                // now we can tell the registered service of the linkup event
    803                 //
    804 
    805801                if( !communicationListeners.contains( service ) ){
    806802                        logging_warn( "linkup event for service that has not been registered" );
     
    810806                CommunicationListener* iface = communicationListeners.get( service );
    811807                if( iface == NULL || iface == &CommunicationListener::DEFAULT ){
    812                         logging_warn( "linkup event for service that has been registered with a NULL interface" );
     808                        logging_warn( "linkup event for service that has been registered "
     809                                "with a NULL interface" );
    813810                        return true;
    814811                }
     
    817814                i->second.markused();
    818815
    819                 //
    820816                // ask the service whether it wants to accept this link
    821                 //
    822 
    823817                if( iface->onLinkRequest(sourcenode) ){
    824818
     
    838832        } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeUpdate) )
    839833
    840         //
    841         // bye messages to say goodbye
    842         //
    843 
    844         else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeBye)){
    845 
    846                 logging_debug( "baseoverlay received message of type OverlayMessageTypeBye" );
    847 
    848                 logging_debug( "received bye message from " <<
     834        /* ************************************************************************
     835         * handle bye messages
     836         */
     837        else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeBye) ) {
     838
     839                logging_debug( "BaseOverlay received message of type OverlayMessageTypeBye" );
     840                logging_debug( "Received bye message from " <<
    849841                                overlayMsg->getSourceNode().toString() );
    850842
    851                 //
    852                 // if we are the initiator and receive a bye from a node
    853                 // the node just left. if we are a node and receive a bye
    854                 // from the initiator, we have to close, too.
    855                 //
    856 
     843                /* if we are the initiator and receive a bye from a node
     844                 * the node just left. if we are a node and receive a bye
     845                 * from the initiator, we have to close, too.
     846                 */
    857847                if( overlayMsg->getSourceNode() == spovnetInitiator ){
    858848
     
    869859
    870860                } else {
    871 
    872                         // a node that said goodbye and we are the initiator
    873                         // don't have to do much here, as the node also
    874                         // will go out of the overlay structure
     861                        // a node that said goodbye and we are the initiator don't have to
     862                        // do much here, as the node also will go out of the overlay
     863                        // structure
    875864                        logging_info( "node left " << overlayMsg->getSourceNode() );
    876 
    877865                }
    878866
     
    881869        } // else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeBye))
    882870
    883         //
    884         // something wrong ...
    885         //
    886 
     871        /* ************************************************************************
     872         * handle link request forwarded through the overlay
     873         */
     874        else if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeLinkRequest)) {
     875                LinkRequest* linkReq = overlayMsg->decapsulate<LinkRequest>();
     876                const ServiceID& service = overlayMsg->getService();
     877                if (linkReq->isReply()) {
     878
     879                        // find link
     880                        PendingLinkMap::iterator i = pendingLinks.find( linkReq->getNonce() );
     881                        if ( i == pendingLinks.end() ) {
     882                                logging_error( "Nonce not found in link request" );
     883                                return true;
     884                        }
     885
     886                        // debug message
     887                        logging_debug( "LinkRequest reply received. Establishing link "
     888                                << i->second << " to " << (linkReq->getEndpoint()->toString())
     889                                << " for service " << service.toString()
     890                                << " with nonce " << linkReq->getNonce()
     891                        );
     892
     893                        // establishing link
     894                        bc->establishLink( *linkReq->getEndpoint(), i->second );
     895                } else {
     896                        OverlayMsg overlay_msg( OverlayMsg::OverlayMessageTypeLinkRequest, service, nodeId );
     897                        LinkRequest link_request_msg(
     898                                        linkReq->getNonce(), &bc->getEndpointDescriptor(), true );
     899                        overlay_msg.encapsulate( &link_request_msg );
     900
     901                        // debug message
     902                        logging_debug( "Sending LinkRequest reply for link with nonce " <<
     903                                        linkReq->getNonce()     );
     904
     905                        // route message back over overlay
     906                        overlayInterface->routeMessage(
     907                                overlayMsg->getSourceNode(), &overlay_msg
     908                        );
     909                }
     910        } // if( overlayMsg->isType(OverlayMsg::OverlayMessageTypeLinkRequest))
     911
     912        /* ************************************************************************
     913         * unknown message type ... error!
     914         */
    887915        else {
    888916
     
    10081036        }
    10091037
    1010         BOOST_FOREACH( const LinkID lnk, oldlinks ){
     1038        BOOST_FOREACH( const LinkID lnk, oldlinks ) {
    10111039                logging_debug( "auto-link " << lnk.toString() << " timed out and is getting dropped" );
    10121040                dropLink( lnk );
Note: See TracChangeset for help on using the changeset viewer.