Ignore:
Timestamp:
Jul 24, 2009, 3:23:11 PM (15 years ago)
Author:
mies
Message:

+ added new transport modules and adapted ariba to them
+ exchange endpoint descriptors an link establishment
+ clean up of base communication
+ link establishment with in the presence of multiple endpoints
+ local discovery for ipv6, ipv4 and bluetooth mac addresses

Location:
source/ariba/communication
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/communication/BaseCommunication.cpp

    r5151 r5284  
    5252namespace communication {
    5353
     54#include "networkinfo/AddressDiscovery.hpp"
     55
    5456use_logging_cpp(BaseCommunication);
    55 const BaseCommunication::LinkDescriptor BaseCommunication::LinkDescriptor::UNSPECIFIED;
    56 
    57 BaseCommunication::BaseCommunication()
    58         : messageReceiver(NULL), network(NULL), transport(NULL), basecommStarted(false){
     57const BaseCommunication::LinkDescriptor
     58        BaseCommunication::LinkDescriptor::UNSPECIFIED;
     59
     60BaseCommunication::BaseCommunication() {
     61        this->transport = NULL;
     62        this->started = false;
    5963}
    6064
     
    6266}
    6367
    64 void BaseCommunication::start(const NetworkLocator* _locallocator, const uint16_t _listenport){
    65 
     68void BaseCommunication::start() {
     69        logging_info( "Starting up ..." );
    6670        currentSeqnum = 0;
    67         listenport = _listenport;
    68 
    69         logging_info( "starting up base communication and creating transports ..." );
    70         logging_info( "using port " << listenport );
     71
     72        // creating transports
     73        logging_info( "Creating transports ..." );
    7174
    7275#ifdef UNDERLAY_OMNET
     
    7780        network = new OmnetNetworkProtocol( module );
    7881#else
    79         transport = new TCPTransport( listenport );
    80         network = new IPv4NetworkProtocol();
     82        transport = new transport_peer( localDescriptor.getEndpoints() );
    8183#endif
    8284
    83         logging_debug( "searching for local locators ..." );
    84 
    85         NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
    86         NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
    87         NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
    88 
    89         //
    90         // choose the first locator that is not localhost
    91         //
    92 
    93         bool foundLocator = false;
    94 
    95         for( ; i != iend; i++){
    96                 logging_debug( "local locator found " << (*i)->toString() );
    97                 IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
    98 
    99                 // TODO: which locators are find to bind to?
    100                 // localhost is not too bad, works when testing locally
    101                 // with several instances. the manual override currently
    102                 // enables to use an arbitrary address, guess this is fine.
    103                 // so the manual override also can use ANY, LOCALHOST, BROADCAST
    104 
    105                 if( *ipv4locator != IPv4Locator::LOCALHOST &&
    106                     *ipv4locator != IPv4Locator::ANY       &&
    107                     *ipv4locator != IPv4Locator::BROADCAST  ){
    108 
    109                         ipv4locator->setPort(listenport);
    110                         localDescriptor.locator = ipv4locator;
    111                         localDescriptor.isUnspec = false;
    112                         logging_info( "binding to addr = " << ipv4locator->toString() );
    113                         foundLocator = true;
    114                         break;
    115                 }
    116         } // for( ; i != iend; i++)
    117 
    118 
    119         if( _locallocator != NULL ) {
    120                 if( localDescriptor.locator != NULL) delete localDescriptor.locator;
    121                 localDescriptor.locator = new IPv4Locator( IPv4Locator::fromString( _locallocator->toString()) );
    122                 localDescriptor.isUnspec = false;
    123                 logging_debug( "manual locator override, using locator=" <<
    124                                         localDescriptor.locator->toString() );
    125                 foundLocator = true;
    126         }
    127 
    128         // if we found no local locator, exit using logging fatal
    129         if( !foundLocator )
    130                 logging_fatal( "did not find a useable local locator!" );
    131 
    132         transport->addMessageReceiver( this );
     85        logging_info( "Searching for local locators ..." );
     86        discoverEndpoints(localDescriptor.getEndpoints());
     87        logging_info( "Done. Local endpoints = " << localDescriptor.toString() );
     88
     89        transport->register_listener( this );
    13390        transport->start();
    13491
    13592#ifndef UNDERLAY_OMNET
    136         //
    13793        // bind to the network change detection
    138         //
    139 
    14094        networkMonitor.registerNotification( this );
    14195#endif
    14296
    143         //
    14497        // base comm startup done
    145         //
    146 
    147         basecommStarted = true;
    148         logging_info( "base communication started up" );
     98        started = true;
     99        logging_info( "Started up." );
    149100}
    150101
    151102void BaseCommunication::stop() {
    152 
    153         logging_info( "stopping base communication and transport ..." );
     103        logging_info( "Stopping transports ..." );
    154104
    155105        transport->stop();
    156106        delete transport;
    157         delete network;
    158 
    159         basecommStarted = false;
    160         logging_info( "base communication stopped" );
     107        started = false;
     108
     109        logging_info( "Stopped." );
    161110}
    162111
    163112bool BaseCommunication::isStarted(){
    164         return basecommStarted;
     113        return started;
     114}
     115
     116/// Sets the endpoints
     117void BaseCommunication::setEndpoints( string& _endpoints ) {
     118        localDescriptor.getEndpoints().assign(_endpoints);
     119        logging_info("Setting local end-points: "
     120                << localDescriptor.getEndpoints().to_string());
    165121}
    166122
     
    175131
    176132        // debug
    177         logging_debug( "request to establish link" );
    178 
    179         //
    180         // just use the first locator in the endp descriptors
    181         //
    182         if( descriptor.locator == NULL ){
    183                 logging_error( "invalid destination endpoint" );
    184                 return LinkID::UNSPECIFIED;
    185         }
    186 
    187         if( localDescriptor.locator == NULL ){
    188                 logging_error( "invalid local endpoint" );
    189                 return LinkID::UNSPECIFIED;
    190         }
    191 
    192         const NetworkLocator* remote = descriptor.locator;
    193         const NetworkLocator* local =  localDescriptor.locator;
    194 
    195         // create link identifier and link descriptor
    196         if (linkid.isUnspecified()){
    197                 linkid = LinkID::create();
    198                 assert(!linkid.isUnspecified());
    199         }
    200 
    201         logging_debug( "creating new local descriptor entry with local link id " << linkid.toString() );
    202         LinkDescriptor linkDescriptor( linkid, local, LinkID::UNSPECIFIED, remote, descriptor, false );
    203         addLink( linkDescriptor );
    204 
    205         //
    206         // create a base msg with our link id and
    207         // a request to open a link on the other side
    208         //
    209 
    210         logging_debug( "sending out base messages with request to open link to " << remote->toString() );
    211         AribaBaseMsg baseMsg(
    212                         remote,
    213                         AribaBaseMsg::LINK_STATE_OPEN_REQUEST,
    214                         linkid,
    215                         LinkID::UNSPECIFIED );
    216 
    217         transport->sendMessage(&baseMsg);
     133        logging_debug( "Request to establish link" );
     134
     135        // create link identifier
     136        if (linkid.isUnspecified())     linkid = LinkID::create();
     137
     138        // create link descriptor
     139        logging_debug( "Creating new descriptor entry with local link id=" << linkid.toString() );
     140        LinkDescriptor* ld = new LinkDescriptor();
     141        ld->localLink = linkid;
     142        addLink( ld );
     143
     144        // send a message to request new link to remote
     145        logging_debug( "Send messages with request to open link to " << descriptor.toString() );
     146        AribaBaseMsg baseMsg( AribaBaseMsg::typeLinkRequest, linkid );
     147        baseMsg.getLocalDescriptor() = localDescriptor;
     148
     149        // serialize and send message
     150        send( &baseMsg, descriptor );
     151
    218152        return linkid;
    219153}
     
    221155void BaseCommunication::dropLink(const LinkID link) {
    222156
    223         logging_debug( "starting to drop link " + link.toString() );
     157        logging_debug( "Starting to drop link " + link.toString() );
    224158
    225159        // see if we have the link
    226         LinkDescriptor& descriptor = queryLocalLink( link );
    227         if( descriptor.isUnspecified() ){
    228                 logging_error( "don't know the link you want to drop "+ link.toString() );
     160        LinkDescriptor& ld = queryLocalLink( link );
     161        if( ld.isUnspecified() ) {
     162                logging_error( "Don't know the link you want to drop "+ link.toString() );
    229163                return;
    230164        }
    231165
     166        // tell the registered listeners
     167        BOOST_FOREACH( CommunicationEvents* i, eventListener ) {
     168                i->onLinkDown( link, ld.localLocator, ld.remoteLocator );
     169        }
     170
    232171        // create message to drop the link
    233         logging_debug( "sending out link close request. for us, the link is closed now" );
    234         AribaBaseMsg msg(
    235                 descriptor.remoteLocator,
    236                 AribaBaseMsg::LINK_STATE_CLOSE_REQUEST,
    237                 descriptor.localLink,
    238                 descriptor.remoteLink
    239         );
     172        logging_debug( "Sending out link close request. for us, the link is closed now" );
     173        AribaBaseMsg msg( AribaBaseMsg::typeLinkClose, ld.localLink, ld.remoteLink );
    240174
    241175        // send message to drop the link
    242         transport->sendMessage( &msg );
    243 
    244         // tell the registered listeners
    245         BOOST_FOREACH( CommunicationEvents* i, eventListener ){
    246                 i->onLinkDown( link, descriptor.localLocator, descriptor.remoteLocator );
    247         }
     176        send( &msg, ld );
    248177
    249178        // remove from map
     
    253182seqnum_t BaseCommunication::sendMessage( const LinkID lid, const Message* message) {
    254183
    255         logging_debug( "sending out message to link " << lid.toString() );
     184        logging_debug( "Sending out message to link " << lid.toString() );
    256185
    257186        // query local link info
    258         LinkDescriptor& linkDesc = queryLocalLink(lid);
    259         if( linkDesc.isUnspecified() ){
    260                 logging_error( "don't know the link with id " << lid.toString() );
     187        LinkDescriptor& ld = queryLocalLink(lid);
     188        if( ld.isUnspecified() ){
     189                logging_error( "Don't know the link with id " << lid.toString() );
    261190                return -1;
    262191        }
    263192
     193        // link not up-> error
     194        if( !ld.up ) {
     195                logging_error("Can not send on link " << lid.toString() << ": link not up");
     196                return -1;
     197        }
     198
    264199        // create message
    265         AribaBaseMsg msg(
    266                 linkDesc.remoteLocator,
    267                 AribaBaseMsg::LINK_STATE_DATA,
    268                 linkDesc.localLink,
    269                 linkDesc.remoteLink
    270         );
     200        AribaBaseMsg msg( AribaBaseMsg::typeData, ld.localLink, ld.remoteLink );
    271201
    272202        // encapsulate the payload message
    273203        msg.encapsulate( const_cast<Message*>(message) );
    274204
    275         if( !linkDesc.linkup ){
    276                 logging_error("cant send message on link " << lid.toString() << ", link not up");
    277                 return -1;
    278         }
    279 
    280205        // send message
    281         transport->sendMessage( &msg );
     206        send( &msg, ld );
     207
     208        // return sequence number
    282209        return ++currentSeqnum;
    283210}
    284211
    285212const EndpointDescriptor& BaseCommunication::getEndpointDescriptor(const LinkID link) const {
    286 
    287213        if( link == LinkID::UNSPECIFIED){
    288214                return localDescriptor;
     
    294220}
    295221
    296 void BaseCommunication::registerMessageReceiver(MessageReceiver* _receiver) {
    297         messageReceiver = _receiver;
    298 }
    299 
    300 void BaseCommunication::unregisterMessageReceiver(MessageReceiver* _receiver) {
    301         messageReceiver = NULL;
    302 }
    303 
    304222void BaseCommunication::registerEventListener(CommunicationEvents* _events){
    305 
    306223        if( eventListener.find( _events ) == eventListener.end() )
    307224                eventListener.insert( _events );
     
    309226
    310227void BaseCommunication::unregisterEventListener(CommunicationEvents* _events){
    311 
    312228        EventListenerSet::iterator i = eventListener.find( _events );
    313229        if( i != eventListener.end() )
     
    315231}
    316232
    317 
    318 bool BaseCommunication::receiveMessage(const Message* message, const LinkID& /*invalid*/, const NodeID& ){
    319 
    320         //
    321         // these messages arrive from the Transport module
    322         // and are incoming network messages. Unpack the
    323         // AribaBaseMsg and handle control packets,
    324         // deliver data packets to the overlay
    325         //
    326 
    327         AribaBaseMsg* spovmsg = ((Message*)message)->decapsulate<AribaBaseMsg>();
    328         logging_debug( "receiving base comm message of type " << spovmsg->getTypeString() );
    329 
    330         //
    331         // deliver data to the overlays. we just give the
    332         // inner packet to every registered overlay ...
    333         //
    334 
    335         if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_DATA ){
    336 
    337                 logging_debug( "received data message, forwarding to overlay" );
    338 
    339                 //
    340                 // put the linkid as address into the message
    341                 // and sent it to the receiver
    342                 //
    343 
    344                 if( messageReceiver != NULL ) {
    345                         messageReceiver->receiveMessage(
    346                                 spovmsg,
    347                                 spovmsg->getRemoteLink(),
    348                                 NodeID::UNSPECIFIED
     233SystemEventType TransportEvent("Transport");
     234SystemEventType MessageDispatchEvent("MessageDispatchEvent", TransportEvent );
     235
     236class DispatchMsg {
     237public:
     238        address_v* local;
     239        address_v* remote;
     240        Message* message;
     241};
     242
     243/// called when a system event is emitted by system queue
     244void BaseCommunication::handleSystemEvent(const SystemEvent& event) {
     245
     246        // dispatch received messages
     247        if ( event.getType() == MessageDispatchEvent ){
     248                logging_debug( "Forwarding message receiver" );
     249                DispatchMsg* dmsg = event.getData<DispatchMsg>();
     250                Message* msg = dmsg->message;
     251                receiveMessage(msg, dmsg->local, dmsg->remote);
     252                msg->dropPayload();
     253                delete dmsg;
     254                delete msg;
     255        }
     256}
     257
     258/// called when a message is received form transport_peer
     259void BaseCommunication::receive_message(transport_protocol* transport,
     260        const address_vf local, const address_vf remote, const uint8_t* data,
     261        size_t size) {
     262
     263//      logging_debug( "Dispatching message" );
     264
     265        // convert data
     266        Data data_( const_cast<uint8_t*>(data), size * 8 );
     267        DispatchMsg* dmsg = new DispatchMsg();
     268
     269        Message* msg = new Message(data_);
     270        dmsg->local = local->clone();
     271        dmsg->remote = remote->clone();
     272        dmsg->message = msg;
     273
     274        SystemQueue::instance().scheduleEvent(
     275                SystemEvent( this, MessageDispatchEvent, dmsg )
     276        );
     277}
     278
     279/// handles a message from the underlay transport
     280void BaseCommunication::receiveMessage(const Message* message,
     281        const address_v* local, const address_v* remote ){
     282
     283        /// decapsulate message
     284        AribaBaseMsg* msg = ((Message*)message)->decapsulate<AribaBaseMsg>();
     285        logging_debug( "Receiving message of type " << msg->getTypeString() );
     286
     287        // handle message
     288        switch (msg->getType()) {
     289
     290                // ---------------------------------------------------------------------
     291                // data message
     292                // ---------------------------------------------------------------------
     293                case AribaBaseMsg::typeData: {
     294                        logging_debug( "Received data message, forwarding to overlay" );
     295                        if( messageReceiver != NULL ) {
     296                                messageReceiver->receiveMessage(
     297                                        msg, msg->getRemoteLink(), NodeID::UNSPECIFIED
     298                                );
     299                        }
     300                        break;
     301                }
     302
     303                // ---------------------------------------------------------------------
     304                // handle link request from remote
     305                // ---------------------------------------------------------------------
     306                case AribaBaseMsg::typeLinkRequest: {
     307                        logging_debug( "Received link open request" );
     308
     309                        /// only answer the first request
     310                        if (!queryRemoteLink(msg->getLocalLink()).isUnspecified()) {
     311                                logging_debug("Link request already received. Ignore!");
     312                                break;
     313                        }
     314
     315                        /// create link ids
     316                        LinkID localLink  = LinkID::create();
     317                        LinkID remoteLink = msg->getLocalLink();
     318                        logging_debug( "local=" << local->to_string()
     319                                << " remote=" << remote->to_string()
    349320                        );
     321
     322                        // check if link creation is allowed by ALL listeners
     323                        bool allowlink = true;
     324                        BOOST_FOREACH( CommunicationEvents* i, eventListener ){
     325                                allowlink &= i->onLinkRequest( localLink, local, remote );
     326                        }
     327
     328                        // not allowed-> warn
     329                        if( !allowlink ){
     330                                logging_warn( "Overlay denied creation of link" );
     331                                return;
     332                        }
     333
     334                        // create descriptor
     335                        LinkDescriptor* ld = new LinkDescriptor();
     336                        ld->localLink = localLink;
     337                        ld->remoteLink = remoteLink;
     338                        ld->localLocator = local->clone();
     339                        ld->remoteLocator = remote->clone();
     340                        ld->remoteEndpoint = msg->getLocalDescriptor();
     341
     342                        // add layer 1-3 addresses
     343                        ld->remoteEndpoint.getEndpoints().add(
     344                                ld->remoteLocator, endpoint_set::Layer1_3);
     345                        localDescriptor.getEndpoints().add(
     346                                local, endpoint_set::Layer1_3
     347                        );
     348
     349                        // link is now up-> add it
     350                        ld->up = true;
     351                        addLink(ld);
     352
     353                        // link is up!
     354                        logging_debug( "Link (initiated from remote) is up with "
     355                                << "local(id=" << ld->localLink.toString() << ","
     356                                << "locator=" << ld->localLocator->to_string() << ") "
     357                                << "remote(id=" << ld->remoteLink.toString() << ", "
     358                                << "locator=" << ld->remoteLocator->to_string() << ")"
     359                        );
     360
     361                        // sending link request reply
     362                        logging_debug( "Sending link request reply with ids "
     363                                << "local=" << localLink.toString() << ", "
     364                                << "remote=" << remoteLink.toString() );
     365                        AribaBaseMsg reply( AribaBaseMsg::typeLinkReply, localLink, remoteLink );
     366                        reply.getLocalDescriptor() = localDescriptor;
     367                        reply.getRemoteDescriptor() = ld->remoteEndpoint;
     368
     369                        send( &reply, *ld );
     370
     371                        // inform listeners about new open link
     372                        BOOST_FOREACH( CommunicationEvents* i, eventListener ) {
     373                                i->onLinkUp( localLink, ld->localLocator, ld->remoteLocator);
     374                        }
     375
     376                        // done
     377                        break;
    350378                }
    351379
    352         } // LINK_STATE_DATA
    353 
    354         //
    355         // handle link open requests
    356         //
    357 
    358         else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REQUEST ){
    359 
    360                 logging_debug( "received link open request" );
    361 
    362                 //
    363                 // create a link context
    364                 //
    365 
    366                 //  in an incoming packet the localLink is from
    367                 // the sender perspective local and from our
    368                 // perspective remote
    369 
    370                 logging_debug( "creating local link" );
    371 
    372                 LinkID localLink  = LinkID::create();
    373                 LinkID remoteLink = spovmsg->getLocalLink();
    374 
    375                 if(localLink.isUnspecified()){
    376                         logging_error("local link is unspecified");
    377                         return false;
     380                // ---------------------------------------------------------------------
     381                // handle link request reply
     382                // ---------------------------------------------------------------------
     383                case AribaBaseMsg::typeLinkReply: {
     384                        logging_debug( "Received link open reply for a link we initiated" );
     385
     386                        // this is a reply to a link open request, so we have already
     387                        // a link mapping and can now set the remote link to valid
     388                        LinkDescriptor& ld = queryLocalLink( msg->getRemoteLink() );
     389
     390                        // no link found-> warn!
     391                        if (ld.isUnspecified()) {
     392                                logging_warn("Failed to find local link " << msg->getRemoteLink().toString());
     393                                return;
     394                        }
     395
     396                        // set remote locator and link id
     397                        ld.remoteLink = msg->getLocalLink();
     398                        ld.remoteLocator = remote->clone();
     399                        localDescriptor.getEndpoints().add(
     400                                msg->getRemoteDescriptor().getEndpoints(),
     401                                endpoint_set::Layer1_3
     402                        );
     403                        ld.up = true;
     404
     405                        logging_debug( "Link is now up with local id "
     406                                << ld.localLink.toString() << " and remote id "
     407                                << ld.remoteLink.toString() );
     408
     409
     410                        // inform lisneters about link up event
     411                        BOOST_FOREACH( CommunicationEvents* i, eventListener ){
     412                                i->onLinkUp( ld.localLink, ld.localLocator, ld.remoteLocator );
     413                        }
     414
     415                        // done
     416                        break;
    378417                }
    379418
    380                 if(remoteLink.isUnspecified()){
    381                         logging_error("remote link is unspecified");
    382                         return false;
     419                // ---------------------------------------------------------------------
     420                // handle link close requests
     421                // ---------------------------------------------------------------------
     422                case AribaBaseMsg::typeLinkClose: {
     423                        // get remote link
     424                        const LinkID& localLink = msg->getRemoteLink();
     425                        logging_debug( "Received link close request for link " << localLink.toString() );
     426
     427                        // searching for link, not found-> warn
     428                        LinkDescriptor& linkDesc = queryLocalLink( localLink );
     429                        if (linkDesc.isUnspecified()) {
     430                                logging_warn("Failed to find local link " << localLink.toString());
     431                                return;
     432                        }
     433
     434                        // inform listeners
     435                        BOOST_FOREACH( CommunicationEvents* i, eventListener ){
     436                                i->onLinkDown( linkDesc.localLink,
     437                                                linkDesc.localLocator, linkDesc.remoteLocator );
     438                        }
     439
     440                        // remove the link descriptor
     441                        removeLink( localLink );
     442
     443                        // done
     444                        break;
    383445                }
    384446
    385                 const NetworkLocator* localLocator  = dynamic_cast<const NetworkLocator*>(localDescriptor.locator);
    386                 const NetworkLocator* remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
    387 
    388                 logging_debug( "localLocator=" << localLocator->toString()
    389                                 << " remoteLocator=" << remoteLocator->toString());
    390 
    391                 // ask the registered listeners if this link
    392                 // creation is fine. we will only allow the
    393                 // link if all of them agree
    394 
    395                 bool allowlink = true;
    396                 BOOST_FOREACH( CommunicationEvents* i, eventListener ){
    397                         allowlink &= i->onLinkRequest( localLink, localLocator, remoteLocator );
     447                // ---------------------------------------------------------------------
     448                // handle link locator changes
     449                // ---------------------------------------------------------------------
     450                case AribaBaseMsg::typeLinkUpdate: {
     451                        const LinkID& localLink = msg->getRemoteLink();
     452                        logging_debug( "Received link update for link "
     453                                << localLink.toString() );
     454
     455                        // find the link description
     456                        LinkDescriptor& linkDesc = queryLocalLink( localLink );
     457                        if (linkDesc.isUnspecified()) {
     458                                logging_warn("Failed to update local link "
     459                                        << localLink.toString());
     460                                return;
     461                        }
     462
     463                        // update the remote locator
     464                        const address_v* oldremote = linkDesc.remoteLocator;
     465                        linkDesc.remoteLocator = remote->clone();
     466
     467                        // inform the listeners (local link has _not_ changed!)
     468                        BOOST_FOREACH( CommunicationEvents* i, eventListener ){
     469                                i->onLinkChanged(
     470                                        linkDesc.localLink,     // linkid
     471                                        linkDesc.localLocator,  // old local
     472                                        linkDesc.localLocator,  // new local
     473                                        oldremote,              // old remote
     474                                        linkDesc.remoteLocator  // new remote
     475                                );
     476                        }
     477
     478                        // done
     479                        break;
    398480                }
    399 
    400                 if( !allowlink ){
    401                         logging_warn( "overlay denied creation of link" );
    402                         return true;
    403                 }
    404 
    405                 //
    406                 // create and save the descriptor for the link
    407                 //
    408 
    409                 LinkDescriptor linkDescriptor(localLink, localLocator, remoteLink,
    410                                         remoteLocator, EndpointDescriptor(remoteLocator), true);
    411 
    412                 logging_debug( "saving new link descriptor with " <<
    413                                 "[local link " << localLink.toString() << "] " <<
    414                                 "[local locator " << localLocator->toString() << "] " <<
    415                                 "[remote link " << remoteLink.toString() << "] " <<
    416                                 "[remote locator " << remoteLocator->toString() << "]" <<
    417                                 "[link up true]" );
    418 
    419                 addLink( linkDescriptor );
    420 
    421                 //
    422                 // send out a link reply
    423                 //
    424 
    425                 logging_debug( "sending back link open reply for " <<
    426                                         "[local link " << localLink.toString() << "] " <<
    427                                         "[remote link " << remoteLink.toString() << "]" );
    428 
    429                 AribaBaseMsg reply(remoteLocator,
    430                                      AribaBaseMsg::LINK_STATE_OPEN_REPLY,
    431                                      localLink,
    432                                      remoteLink);
    433 
    434                 transport->sendMessage( &reply );
    435 
    436                 //
    437                 // the link is now open
    438                 //
    439 
    440                 BOOST_FOREACH( CommunicationEvents* i, eventListener ){
    441                         i->onLinkUp( localLink, localLocator, remoteLocator );
    442                 }
    443 
    444         } // LINK_STATE_OPEN_REQUEST
    445 
    446         //
    447         // handle link open replies
    448         //
    449 
    450         else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REPLY ){
    451 
    452                 logging_debug( "received link open reply for a link we initiated" );
    453 
    454                 // this is a reply to a link open request, so we have already
    455                 // a link mapping and can now set the remote link to valid
    456                 LinkDescriptor& linkDesc = queryLocalLink( spovmsg->getRemoteLink() );
    457 
    458                 if (linkDesc.isUnspecified()) {
    459                         logging_warn("failed to find local link " << spovmsg->getRemoteLink().toString());
    460                         return false;
    461                 }
    462 
    463                 linkDesc.remoteLink = spovmsg->getLocalLink();
    464                 linkDesc.linkup = true;
    465 
    466                 logging_debug( "the link is now up with local link id " << linkDesc.localLink.toString() <<
    467                                                                                         " and remote link id " << linkDesc.remoteLink.toString() );
    468 
    469                 // notify the baseoverlay that the link is up, so
    470                 // it can exchange nodeids over this link. then we
    471                 // can send the queued messages, as both nodes have
    472                 // to know their nodeids first
    473 
    474                 BOOST_FOREACH( CommunicationEvents* i, eventListener ){
    475                         i->onLinkUp( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
    476                 }
    477 
    478         } // LINK_STATE_OPEN_REPLY
    479 
    480         //
    481         // handle link close requests
    482         //
    483 
    484         else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_CLOSE_REQUEST ){
    485 
    486                 const LinkID& localLink = spovmsg->getRemoteLink();
    487                 logging_debug( "received link close request for link " << localLink.toString() );
    488 
    489                 //
    490                 // the link is closed immediately, we
    491                 // don't need to send out a reply, so we
    492                 // delete the mapping and inform
    493                 //
    494 
    495                 LinkDescriptor& linkDesc = queryLocalLink( localLink );
    496                 if (linkDesc.isUnspecified()) {
    497                         logging_warn("Failed to find local link " << localLink.toString());
    498                         return false;
    499                 }
    500 
    501                 BOOST_FOREACH( CommunicationEvents* i, eventListener ){
    502                         i->onLinkDown( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
    503                 }
    504 
    505                 //
    506                 // remove the link descriptor
    507                 //
    508 
    509                 removeLink( localLink );
    510 
    511         } // LINK_STATE_CLOSE_REQUEST
    512 
    513         //
    514         // handle locator updates
    515         //
    516 
    517         else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_UPDATE ){
    518 
    519                 const LinkID& localLink = spovmsg->getRemoteLink();
    520                 logging_debug( "received link update for link " << localLink.toString() );
    521 
    522                 //
    523                 // find the link description
    524                 //
    525 
    526                 LinkDescriptor& linkDesc = queryLocalLink( localLink );
    527                 if (linkDesc.isUnspecified()) {
    528                         logging_warn("Failed to update local link " << localLink.toString());
    529                         return false;
    530                 }
    531 
    532                 //
    533                 // update the remote locator
    534                 //
    535 
    536                 const NetworkLocator* oldremote = linkDesc.remoteLocator;
    537                 linkDesc.remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
    538 
    539                 //
    540                 // inform the listeners (local link has _not_ changed!)
    541                 //
    542 
    543                 BOOST_FOREACH( CommunicationEvents* i, eventListener ){
    544                         i->onLinkChanged(
    545                                 linkDesc.localLink,     // linkid
    546                                 linkDesc.localLocator,  // old local
    547                                 linkDesc.localLocator,  // new local
    548                                 oldremote,              // old remote
    549                                 linkDesc.remoteLocator  // new remote
    550                         );
    551                 }
    552 
    553         } // LINK_STATE_UPDATE
    554 
    555         return true;
    556 }
    557 
    558 void BaseCommunication::addLink( const LinkDescriptor& link ) {
     481        }
     482}
     483
     484/// add a newly allocated link to the set of links
     485void BaseCommunication::addLink( LinkDescriptor* link ) {
    559486        linkSet.push_back( link );
    560487}
    561488
     489/// remove a link from set
    562490void BaseCommunication::removeLink( const LinkID& localLink ) {
    563 
    564         LinkSet::iterator i = linkSet.begin();
    565         LinkSet::iterator iend = linkSet.end();
    566 
    567         for( ; i != iend; i++){
    568                 if( (*i).localLink != localLink) continue;
    569 
     491        for(LinkSet::iterator i=linkSet.begin(); i != linkSet.end(); i++){
     492                if( (*i)->localLink != localLink) continue;
     493                delete *i;
    570494                linkSet.erase( i );
    571495                break;
     
    573497}
    574498
     499/// query a descriptor by local link id
    575500BaseCommunication::LinkDescriptor& BaseCommunication::queryLocalLink( const LinkID& link ) const {
    576501        for (int i=0; i<linkSet.size();i++)
    577                 if (linkSet[i].localLink == link) return (LinkDescriptor&)linkSet[i];
     502                if (linkSet[i]->localLink == link) return (LinkDescriptor&)*linkSet[i];
    578503        return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
    579504}
    580505
     506/// query a descriptor by remote link id
    581507BaseCommunication::LinkDescriptor& BaseCommunication::queryRemoteLink( const LinkID& link ) const {
    582508        for (int i=0; i<linkSet.size();i++)
    583                 if (linkSet[i].remoteLink == link) return (LinkDescriptor&)linkSet[i];
     509                if (linkSet[i]->remoteLink == link) return (LinkDescriptor&)*linkSet[i];
    584510        return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
    585511}
    586512
    587 LinkIDs BaseCommunication::getLocalLinks( const EndpointDescriptor& ep ) const {
     513LinkIDs BaseCommunication::getLocalLinks( const address_v* addr ) const {
    588514        LinkIDs ids;
    589 
    590515        for (int i=0; i<linkSet.size(); i++){
    591                 if( ep == EndpointDescriptor::UNSPECIFIED ){
    592                         ids.push_back( linkSet[i].localLink );
     516                if( addr == NULL ){
     517                        ids.push_back( linkSet[i]->localLink );
    593518                } else {
    594                         if ( linkSet[i].remoteLocator == ep.locator )
    595                                 ids.push_back( linkSet[i].localLink );
     519                        if ( *linkSet[i]->remoteLocator == *addr )
     520                                ids.push_back( linkSet[i]->localLink );
    596521                }
    597522        }
    598 
    599523        return ids;
    600524}
     
    609533#endif // UNDERLAY_OMNET
    610534
    611         //
     535/*- disabled!
     536
    612537        // we only care about address changes, not about interface changes
    613538        // as address changes are triggered by interface changes, we are safe here
    614         //
    615 
    616539        if( info.type != NetworkChangeInterface::EventTypeAddressNew &&
    617540                info.type != NetworkChangeInterface::EventTypeAddressDelete ) return;
     
    619542        logging_info( "base communication is handling network address changes" );
    620543
    621         //
    622544        // get all now available addresses
    623         //
    624 
    625545        NetworkInformation networkInformation;
    626546        AddressInformation addressInformation;
     
    745665                transport->sendMessage( &updateMsg );
    746666        }
     667*/
     668}
     669
     670/// sends a message to all end-points in the end-point descriptor
     671void BaseCommunication::send(Message* message, const EndpointDescriptor& endpoint) {
     672        Data data = data_serialize( message, DEFAULT_V );
     673        transport->send( endpoint.getEndpoints(), data.getBuffer(), data.getLength() / 8);
     674}
     675
     676/// sends a message to the remote locator inside the link descriptor
     677void BaseCommunication::send(Message* message, const LinkDescriptor& desc) {
     678        Data data = data_serialize( message, DEFAULT_V );
     679        transport->send( desc.remoteLocator, data.getBuffer(), data.getLength() / 8);
    747680}
    748681
  • source/ariba/communication/BaseCommunication.h

    r4983 r5284  
    4040#define BASECOMMUNICATION_H_
    4141
     42// boost & std includes
    4243#include <ext/hash_map>
    4344#include <ext/hash_set>
     
    4950#include <boost/foreach.hpp>
    5051
     52// utilities
    5153#include "ariba/utility/types.h"
    5254#include "ariba/utility/messages.h"
    5355#include "ariba/utility/logging/Logging.h"
    5456#include "ariba/utility/misc/Demultiplexer.hpp"
    55 
     57#include "ariba/utility/system/SystemEventListener.h"
     58
     59// new transport and addressing
     60#include "ariba/utility/addressing/addressing.hpp"
     61#include "ariba/utility/transport/transport.hpp"
     62
     63// communication
    5664#include "ariba/communication/CommunicationEvents.h"
    5765#include "ariba/communication/EndpointDescriptor.h"
     66#include "ariba/communication/messages/AribaBaseMsg.h"
     67
     68// network changes
    5869#include "ariba/communication/networkinfo/NetworkChangeInterface.h"
    5970#include "ariba/communication/networkinfo/NetworkChangeDetection.h"
    6071#include "ariba/communication/networkinfo/NetworkInformation.h"
    6172#include "ariba/communication/networkinfo/AddressInformation.h"
    62 #include "ariba/communication/messages/AribaBaseMsg.h"
    63 #include "ariba/communication/modules/transport/TransportProtocol.h"
    64 #include "ariba/communication/modules/network/NetworkProtocol.h"
    65 #include "ariba/communication/modules/network/NetworkLocator.h"
    66 
    67 #ifndef UNDERLAY_OMNET
    68   #include "ariba/communication/modules/transport/tcp/TCPTransport.h"
    69   #include "ariba/communication/modules/network/ip/IPv4NetworkProtocol.h"
    70 #endif
    71 
    72 using __gnu_cxx::hash_set;
    73 using __gnu_cxx::hash_map;
    74 
    75 using std::cout;
    76 using std::set;
    77 using std::map;
    78 using std::vector;
    79 using std::pair;
    80 using std::make_pair;
    81 using std::find;
    82 
    83 using ariba::communication::NetworkChangeDetection;
    84 using ariba::communication::NetworkChangeInterface;
    85 using ariba::communication::NetworkInterfaceList;
    86 using ariba::communication::NetworkInformation;
    87 using ariba::communication::AddressInformation;
    88 using ariba::communication::AddressList;
    89 using ariba::communication::AribaBaseMsg;
    90 using ariba::communication::CommunicationEvents;
    91 
    92 using ariba::utility::Demultiplexer;
    93 using ariba::utility::QoSParameterSet;
    94 using ariba::utility::SecurityParameterSet;
    95 using ariba::utility::Address;
    96 using ariba::utility::LinkID;
    97 using ariba::utility::LinkIDs;
    98 using ariba::utility::Message;
    99 using ariba::utility::MessageReceiver;
    100 using ariba::utility::seqnum_t;
    101 
    102 using ariba::communication::TransportProtocol;
    103 using ariba::communication::NetworkProtocol;
    104 using ariba::communication::NetworkLocator;
    105 #ifndef UNDERLAY_OMNET
    106   using ariba::communication::IPv4NetworkProtocol;
    107   using ariba::communication::TCPTransport;
    108 #endif
     73
     74// deprecated
     75//#include "ariba/communication/modules/transport/TransportProtocol.h"
     76//#include "ariba/communication/modules/network/NetworkProtocol.h"
     77//#include "ariba/communication/modules/network/NetworkLocator.h"
     78
     79// disabled
     80//#ifndef UNDERLAY_OMNET
     81//  #include "ariba/communication/modules/transport/tcp/TCPTransport.h"
     82//  #include "ariba/communication/modules/network/ip/IPv4NetworkProtocol.h"
     83//#endif
     84
     85// deprecated
     86//using ariba::communication::TransportProtocol;
     87//using ariba::communication::NetworkProtocol;
     88//using ariba::communication::NetworkLocator;
     89
     90// disabled
     91//#ifndef UNDERLAY_OMNET
     92//  using ariba::communication::IPv4NetworkProtocol;
     93//  using ariba::communication::TCPTransport;
     94//#endif
    10995
    11096namespace ariba {
    11197namespace communication {
     98
     99using namespace std;
     100using namespace ariba::addressing;
     101using namespace ariba::transport;
     102using namespace ariba::utility;
     103
     104// use base ariba types (clarifies multiple definitions)
     105using ariba::utility::Message;
     106using ariba::utility::seqnum_t;
    112107
    113108/**
     
    119114 * @author Sebastian Mies, Christoph Mayer
    120115 */
    121 class BaseCommunication : public MessageReceiver, NetworkChangeInterface {
     116class BaseCommunication: public NetworkChangeInterface,
     117        public SystemEventListener, public transport_listener {
    122118        use_logging_h(BaseCommunication);
     119
    123120public:
    124 
    125         /**
    126          * Default ctor that just creates an empty
    127          * non functional base communication
    128          */
     121        /// Default ctor that just creates an non-functional base communication
    129122        BaseCommunication();
    130123
    131         /**
    132          * Default dtor that does nothing
    133          */
     124        /// Default dtor that does nothing
    134125        virtual ~BaseCommunication();
    135126
    136         /**
    137          * Startup the base communication, start modules etc.
    138          */
    139         void start(const NetworkLocator* _locallocator, const uint16_t _listenport);
    140 
    141         /**
    142          * stop the base communication, stop modules etc.
    143          */
     127        /// Startup the base communication, start modules etc.
     128        void start();
     129
     130        /// Stops the base communication, stop modules etc.
    144131        void stop();
    145132
    146         /*
    147          * Check whether the base communication has been started up
    148          */
     133        /// Sets the endpoints
     134        void setEndpoints( string& endpoints );
     135
     136        /// Check whether the base communication has been started up
    149137        bool isStarted();
    150138
    151 
    152         /**
    153          * Establishes a link to another end-point.
    154          */
    155         const LinkID establishLink(
    156                 const EndpointDescriptor& descriptor,
    157                 const LinkID& linkid = LinkID::UNSPECIFIED,
    158                 const QoSParameterSet& qos = QoSParameterSet::DEFAULT,
    159                 const SecurityParameterSet& sec = SecurityParameterSet::DEFAULT
    160         );
    161 
    162         /**
    163          * Drops a link.
    164          *
    165          * @param The link id of the link that should be dropped
    166          */
     139        /// Establishes a link to another end-point.
     140        const LinkID establishLink(const EndpointDescriptor& descriptor,
     141                const LinkID& linkid = LinkID::UNSPECIFIED, const QoSParameterSet& qos =
     142                                QoSParameterSet::DEFAULT, const SecurityParameterSet& sec =
     143                                SecurityParameterSet::DEFAULT);
     144
     145        /// Drops a link
    167146        void dropLink(const LinkID link);
    168147
     
    182161         * @return The end-point descriptor of the link's end-point
    183162         */
    184         const EndpointDescriptor& getEndpointDescriptor( const LinkID link = LinkID::UNSPECIFIED ) const;
     163        const EndpointDescriptor& getEndpointDescriptor(const LinkID link =
     164                        LinkID::UNSPECIFIED) const;
    185165
    186166        /**
     
    190170         * @return List of LinkID
    191171         */
    192         LinkIDs getLocalLinks( const EndpointDescriptor& ep = EndpointDescriptor::UNSPECIFIED ) const;
     172        LinkIDs getLocalLinks(const address_v* addr) const;
    193173
    194174        /**
     
    197177         * @param _receiver The receiving side
    198178         */
    199         void registerMessageReceiver( MessageReceiver* _receiver );
     179        void registerMessageReceiver(MessageReceiver* receiver) {
     180                messageReceiver = receiver;
     181        }
    200182
    201183        /**
     
    204186         * @param _receiver The receiving side
    205187         */
    206         void unregisterMessageReceiver( MessageReceiver* _receiver );
    207 
    208         void registerEventListener( CommunicationEvents* _events );
    209         void unregisterEventListener( CommunicationEvents* _events );
     188        void unregisterMessageReceiver(MessageReceiver* receiver) {
     189                messageReceiver = NULL;
     190        }
     191
     192        void registerEventListener(CommunicationEvents* _events);
     193
     194        void unregisterEventListener(CommunicationEvents* _events);
     195
     196public:
     197
     198        /// called when a system event is emitted by system queue
     199        virtual void handleSystemEvent(const SystemEvent& event);
     200
     201        /// called when a message is received form transport_peer
     202        virtual void receive_message(transport_protocol* transport,
     203                const address_vf local, const address_vf remote, const uint8_t* data,
     204                size_t size);
    210205
    211206protected:
    212207
    213         /**
    214          * Called from the Transport when async items
    215          * from the SystemQueue are delivered
    216          */
    217         virtual bool receiveMessage( const Message* message, const LinkID& link, const NodeID& node );
    218 
    219         /**
    220          * Called when a network interface change happens
    221          */
    222         virtual void onNetworkChange( const NetworkChangeInterface::NetworkChangeInfo& info );
     208        /// handle received message from a transport module
     209        void receiveMessage(const Message* message,
     210                const address_v* local, const address_v* remote );
     211
     212        /// called when a network interface change happens
     213        virtual void onNetworkChange(
     214                const NetworkChangeInterface::NetworkChangeInfo& info);
    223215
    224216private:
    225 
    226         /**
    227          * A link descriptor consisting of the
    228          * end-point descriptor and currently used locator and
    229          * message receiver
     217        /**
     218         * A link descriptor consisting of the end-point descriptor and currently
     219         * used underlay address.
    230220         */
    231221        class LinkDescriptor {
     
    233223                static const LinkDescriptor UNSPECIFIED;
    234224
     225                /// default constructor
    235226                LinkDescriptor() :
    236                         localLink(LinkID::UNSPECIFIED),
    237                         localLocator(NULL),
    238                         remoteLink(LinkID::UNSPECIFIED),
    239                         remoteLocator(NULL),
    240                         remoteEndpoint(EndpointDescriptor::UNSPECIFIED),
    241                         linkup(false) {
     227                        localLink(LinkID::UNSPECIFIED), localLocator(NULL),
     228                        remoteLink(LinkID::UNSPECIFIED), remoteLocator(NULL),
     229                        remoteEndpoint(EndpointDescriptor::UNSPECIFIED), up(false) {
    242230                }
    243231
    244                 LinkDescriptor(const LinkID& _localLink, const NetworkLocator*& _localLocator,
    245                                 const LinkID& _remoteLink, const NetworkLocator*& _remoteLocator,
    246                                 const EndpointDescriptor& _remoteEndpoint, bool _linkup ) :
    247                         localLink(_localLink),
    248                         localLocator(_localLocator),
    249                         remoteLink(_remoteLink),
    250                         remoteLocator(_remoteLocator),
    251                         remoteEndpoint(_remoteEndpoint),
    252                         linkup(_linkup) {
     232                ~LinkDescriptor() {
     233                        if (localLocator!=NULL)  delete localLocator;
     234                        if (remoteLocator!=NULL) delete remoteLocator;
    253235                }
    254236
    255                 LinkDescriptor( const LinkDescriptor& desc ) :
    256                         localLink(desc.localLink),
    257                         localLocator(desc.localLocator),
    258                         remoteLink(desc.remoteLink),
    259                         remoteLocator(desc.remoteLocator),
    260                         remoteEndpoint(desc.remoteEndpoint),
    261                         linkup(desc.linkup) {
     237                /// returns true if this is the UNSPECIFIED object
     238                bool isUnspecified() const {
     239                        return this == &UNSPECIFIED;
    262240                }
    263241
    264                 bool isUnspecified() const {
    265                         return (this == &UNSPECIFIED);
    266                 }
    267 
    268                 LinkID                                  localLink;
    269                 const NetworkLocator*   localLocator;
    270                 LinkID                                  remoteLink;
    271                 const NetworkLocator*   remoteLocator;
    272                 EndpointDescriptor              remoteEndpoint;
    273 
    274                 bool                                    linkup;
     242                /// link identifiers
     243                LinkID localLink;
     244                LinkID remoteLink;
     245
     246                /// used underlay addresses for the link
     247                const address_v* localLocator;
     248                const address_v* remoteLocator;
     249
     250                /// the remote end-point descriptor
     251                EndpointDescriptor remoteEndpoint;
     252
     253                /// flag, whether this link is up
     254                bool up;
    275255        };
    276256
    277         /**
    278          * Link management: add a link
    279          */
    280         void addLink( const LinkDescriptor& link );
    281 
    282         /**
    283          * Link management: remove alink
    284          */
    285         void removeLink( const LinkID& localLink );
    286 
    287         /**
    288          * Link management: get link information using the local link
    289          */
    290         LinkDescriptor& queryLocalLink( const LinkID& localLink ) const;
    291 
    292         /**
    293          * Link management: get link information using the remote link
    294          */
    295         LinkDescriptor& queryRemoteLink( const LinkID& remoteLink ) const;
    296 
    297         /**
    298          * Link management: list of links
    299          */
    300         typedef vector<LinkDescriptor> LinkSet;
    301 
    302         /**
    303          * Link management: the set of currently managed links
    304          */
     257        /// Link management: list of links
     258        typedef vector<LinkDescriptor*> LinkSet;
     259
     260        /// Link management: the set of currently managed links
    305261        LinkSet linkSet;
    306262
    307         /**
    308          * The message receiver
    309          */
    310         MessageReceiver* messageReceiver;
    311 
    312         /**
    313          * The local end-point descriptor
    314          */
     263        /// Link management: add a link
     264        void addLink( LinkDescriptor* link );
     265
     266        /// Link management: remove a link
     267        void removeLink(const LinkID& localLink);
     268
     269        /// Link management: get link information using the local link
     270        LinkDescriptor& queryLocalLink(const LinkID& localLink) const;
     271
     272        /// Link management: get link information using the remote link
     273        LinkDescriptor& queryRemoteLink(const LinkID& remoteLink) const;
     274
     275        /// The local end-point descriptor
    315276        EndpointDescriptor localDescriptor;
    316277
    317         /**
    318          * Network information and protocol
    319          */
    320         NetworkProtocol* network;
    321 
    322         /**
    323          * Transport information and protocol
    324          */
    325         TransportProtocol* transport;
    326 
    327278#ifndef UNDERLAY_OMNET
    328         /**
    329          * Detect changes in the routing/interface, etc.
    330          * stuff needed for mobility detection
    331          */
     279        /// network change detector
    332280        NetworkChangeDetection networkMonitor;
    333281#endif
    334 
    335         /**
    336          * The local listen port
    337          */
    338         uint16_t listenport;
    339 
     282        /// event listener
    340283        typedef set<CommunicationEvents*> EventListenerSet;
    341284        EventListenerSet eventListener;
    342285
     286        /// sequence numbers
    343287        seqnum_t currentSeqnum;
    344288
    345         /**
    346          * state of the base communication
    347          */
    348         bool basecommStarted;
     289        /// transport peer
     290        transport_peer* transport;
     291
     292        /// the base overlay message receiver
     293        MessageReceiver* messageReceiver;
     294
     295        /// convenience: send message to peer
     296        void send( Message* message, const EndpointDescriptor& endpoint );
     297        void send( Message* message, const LinkDescriptor& descriptor );
     298
     299        /// state of the base communication
     300        bool started;
    349301
    350302};
     
    352304}} // namespace ariba, communication
    353305
    354 #endif /*BASECOMMUNICATION_H_*/
     306#endif /* BASECOMMUNICATION_H_ */
  • source/ariba/communication/CommunicationEvents.cpp

    r3690 r5284  
    4848}
    4949
    50 bool CommunicationEvents::onLinkRequest( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ) {
     50bool CommunicationEvents::onLinkRequest(const LinkID& id,
     51        const address_v* local, const address_v* remote) {
    5152        return true;
    5253}
    5354
    54 void CommunicationEvents::onLinkUp( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ) {
     55void CommunicationEvents::onLinkUp(const LinkID& id, const address_v* local,
     56        const address_v* remote) {
    5557}
    5658
    57 void CommunicationEvents::onLinkDown( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ) {
     59void CommunicationEvents::onLinkDown(const LinkID& id, const address_v* local,
     60        const address_v* remote) {
    5861}
    5962
    60 
    61 void CommunicationEvents::onLinkChanged( const LinkID& id, const NetworkLocator* oldlocal, const NetworkLocator* newlocal, const NetworkLocator* oldremote, const NetworkLocator* newremote ) {
     63void CommunicationEvents::onLinkChanged(const LinkID& id,
     64        const address_v* oldlocal, const address_v* newlocal,
     65        const address_v* oldremote, const address_v* newremote) {
    6266}
    6367
    64 void CommunicationEvents::onLinkFail( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ){
     68void CommunicationEvents::onLinkFail(const LinkID& id, const address_v* local,
     69        const address_v* remote) {
    6570}
    6671
    67 void CommunicationEvents::onLinkQoSChanged( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote, const QoSParameterSet& qos ){
     72void CommunicationEvents::onLinkQoSChanged(const LinkID& id,
     73        const address_v* local, const address_v* remote, const QoSParameterSet& qos) {
    6874}
    6975
  • source/ariba/communication/CommunicationEvents.h

    r3690 r5284  
    4141
    4242#include "ariba/utility/types/LinkID.h"
    43 #include "ariba/communication/modules/network/NetworkLocator.h"
    4443#include "ariba/utility/types/QoSParameterSet.h"
    45 
    46 using ariba::utility::LinkID;
    47 using ariba::utility::QoSParameterSet;
    48 using ariba::communication::NetworkLocator;
     44#include "ariba/utility/addressing/addressing.hpp"
    4945
    5046namespace ariba {
    5147namespace communication {
    5248
     49using ariba::utility::LinkID;
     50using ariba::utility::QoSParameterSet;
     51using namespace ariba::addressing;
     52
    5353class CommunicationEvents {
    54 
    5554        friend class BaseCommunication;
    5655
     
    6968         * @return True, if the link should be established
    7069         */
    71         virtual bool onLinkRequest( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote );
     70        virtual bool onLinkRequest(const LinkID& id, const address_v* local,
     71                const address_v* remote);
    7272
    7373        /**
     
    7777         * @param id The link id of the established link
    7878         */
    79         virtual void onLinkUp( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote );
     79        virtual void onLinkUp(const LinkID& id, const address_v* local,
     80                const address_v* remote);
    8081
    8182        /**
     
    8485         * @param id The link identifier of the dropped link
    8586         */
    86         virtual void onLinkDown( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote );
     87        virtual void onLinkDown(const LinkID& id, const address_v* local,
     88                const address_v* remote);
    8789
    8890        /**
     
    9496         * @param id The link identifier of the changed link
    9597         */
    96         virtual void onLinkChanged( const LinkID& id, const NetworkLocator* oldlocal, const NetworkLocator* newlocal, const NetworkLocator* oldremote, const NetworkLocator* newremote );
     98        virtual void onLinkChanged(const LinkID& id,
     99                const address_v* oldlocal,  const address_v* newlocal,
     100                const address_v* oldremote, const address_v* newremote
     101        );
    97102
    98         virtual void onLinkFail( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote );
     103        virtual void onLinkFail(const LinkID& id, const address_v* local,
     104                const address_v* remote);
    99105
    100         virtual void onLinkQoSChanged( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote, const QoSParameterSet& qos );
     106        virtual void onLinkQoSChanged(const LinkID& id, const address_v* local,
     107                const address_v* remote, const QoSParameterSet& qos);
    101108};
    102109
  • source/ariba/communication/EndpointDescriptor.cpp

    r4986 r5284  
    4848const EndpointDescriptor EndpointDescriptor::UNSPECIFIED;
    4949
    50 EndpointDescriptor::EndpointDescriptor() : locator( NULL ), isUnspec( true ){
    51 }
    52 
    53 EndpointDescriptor::EndpointDescriptor(const EndpointDescriptor& rh){
    54         locator = (rh.locator != NULL) ? new IPv4Locator(*rh.locator) : NULL;
    55         isUnspec = rh.isUnspec;
    56 }
    57 
    58 EndpointDescriptor::EndpointDescriptor(const Locator* _locator){
    59         if( _locator == NULL ) return;
    60 
    61         locator = new IPv4Locator(*dynamic_cast<IPv4Locator*>((Locator*)_locator));
    62         isUnspec = false;
    63 }
    64 
    65 EndpointDescriptor::EndpointDescriptor(const string str) {
    66         using namespace boost::xpressive;
    67         using namespace ariba::utility::string_format;
    68         using namespace ariba::utility::Helper;
    69         using namespace std;
    70 
    71         locator = NULL;
    72         isUnspec = true;
    73 
    74         smatch match;
    75         if (regex_search(str, match, robjects)) {
    76                 regex_nav nav = match;
    77                 for (int i=0; i<nav.size(); i++) {
    78                         string type = nav[i][robject_id].str();
    79                         if (type=="ip") {
    80                                 string ip = nav[i][robject_data].str();
    81                                 ip = ip.substr(1,ip.size()-2);
    82                                 this->locator = new IPv4Locator();
    83                                 this->locator->setIP(ip);
    84                                 this->isUnspec = false;
    85                         } else
    86                         if (type=="tcp") {
    87                                 string port = nav[i][robject_data][rfields][1].str();
    88                                 port = port.substr(1,port.size()-2);
    89                                 this->locator->setPort(stoi(port));
    90                         }
    91                 }
    92         }
    93 }
    94 
     50/// destructor.
    9551EndpointDescriptor::~EndpointDescriptor() {
    96 }
    97 
    98 bool EndpointDescriptor::isUnspecified() const {
    99         return isUnspec;
    100 }
    101 
    102 string EndpointDescriptor::toString() const {
    103         if( locator == NULL ) return "<undefined locator>";
    104         std::ostringstream o;
    105         o << "ip{" << locator->getIP() << "}";
    106         o << ",";
    107         o << "tcp(ip,{" << locator->getPort() << "})";
    108         return o.str();
    109 }
    110 
    111 EndpointDescriptor* EndpointDescriptor::fromString( string str ) {
    112         return new EndpointDescriptor( str );
    113 }
    114 
    115 bool EndpointDescriptor::operator!=( const EndpointDescriptor& rh ) const {
    116         return !operator==(rh);
    117 }
    118 
    119 bool EndpointDescriptor::operator==(const EndpointDescriptor& rh) const {
    120 
    121         if( isUnspecified() && rh.isUnspecified() ) {
    122 
    123                 // both unspec bit set
    124                 return true;
    125 
    126         } else if( (!isUnspecified()) && (!rh.isUnspecified()) ) {
    127 
    128                 //
    129                 // both are valid, check locators
    130                 //
    131 
    132                 if( locator == NULL && rh.locator == NULL ){
    133 
    134                         // both locators are invalid, ok true
    135                         return true;
    136 
    137                 } else if( locator == NULL ^ rh.locator == NULL ) {
    138 
    139                         // one locator is invalid, the other not, false
    140                         return false;
    141 
    142                 } else {
    143 
    144                         // both locators are valid, compare
    145                         assert( locator != NULL && rh.locator != NULL );
    146                         return ( locator->operator==(*rh.locator) );
    147 
    148                 }
    149 
    150         } else {
    151 
    152                 // one is unspec, the other not
    153                 assert( isUnspecified() ^ rh.isUnspecified() );
    154                 return false;
    155 
    156         }
    15752
    15853}
    15954
     55
    16056}} // namespace ariba, communication
  • source/ariba/communication/EndpointDescriptor.h

    r4986 r5284  
    4040#define ENDPOINTDESCRIPTOR_H_
    4141
     42// deprecated
     43//#include "ariba/communication/modules/network/ip/IPv4Locator.h"
     44
     45// deprecated
     46//using ariba::utility::Locator;
     47//using ariba::communication::IPv4Locator;
     48
     49// typical types
    4250#include "ariba/utility/types.h"
    4351#include "ariba/utility/serialization.h"
    44 #include "ariba/communication/modules/network/ip/IPv4Locator.h"
     52
     53// new addressing and transport
     54#include "ariba/utility/addressing/addressing.hpp"
     55#include "ariba/utility/transport/transport.hpp"
     56
     57// stdlibc++
    4558#include <string>
    4659#include <set>
    47 
    48 using std::string;
    49 using std::set;
    50 using ariba::utility::Locator;
    51 using ariba::communication::IPv4Locator;
    5260
    5361namespace ariba {
     
    5563
    5664using_serialization;
     65using namespace std;
     66using namespace ariba::addressing;
    5767
    58 class EndpointDescriptor : public VSerializeable {
    59         VSERIALIZEABLE;
    60 
     68class EndpointDescriptor: public VSerializeable { VSERIALIZEABLE
    6169        friend class BaseCommunication;
    6270
    6371public:
    64         /**
    65          * The default constructor.
    66          */
    67         EndpointDescriptor();
     72        /// the unspecified endpoint descriptor
     73        static const EndpointDescriptor UNSPECIFIED;
    6874
    69         EndpointDescriptor(const EndpointDescriptor& rh);
    70         EndpointDescriptor(const Locator* _locator);
    71         EndpointDescriptor(const string str);
     75        /// creates an empty endpoint descriptor with zero endpoints
     76        EndpointDescriptor() : endpoints() {
     77        }
    7278
    73         /**
    74          * The destructor.
    75          */
     79        /// destructor.
    7680        virtual ~EndpointDescriptor();
    7781
    78         /**
    79          * An unspecified end-point
    80          */
    81         static const EndpointDescriptor UNSPECIFIED;
     82        /// copy constructor
     83        EndpointDescriptor(const EndpointDescriptor& rh) :
     84                endpoints(rh.endpoints) {
     85        }
    8286
    83         /**
    84          * Returns true, if the descriptor is unspecified.
    85          *
    86          * @return True, if the descriptor is unspecified.
    87          */
    88         bool isUnspecified() const;
     87        /// construct end-points from an endpoint set
     88        EndpointDescriptor(const endpoint_set& endpoints ) :
     89                endpoints(endpoints) {
     90        }
    8991
    90         virtual string toString() const;
    91         static EndpointDescriptor* fromString( string str );
     92        /// construct end-points from a string
     93        EndpointDescriptor(const string& str) : endpoints(str) {
     94        }
    9295
    93         bool operator==( const EndpointDescriptor& rh ) const;
    94         bool operator!=( const EndpointDescriptor& rh ) const;
     96        /// convert end-points to string
     97        string toString() const {
     98                return endpoints.to_string();
     99        }
     100
     101        /// returns true, if this object is the unspecified object
     102        bool isUnspecified() const {
     103                return this == &UNSPECIFIED;
     104        }
     105
     106        /// create endpoint
     107        static EndpointDescriptor* fromString(string str) {
     108                return new EndpointDescriptor(str);
     109        }
     110
     111        bool operator==(const EndpointDescriptor& rh) const {
     112                if (rh.isUnspecified() && isUnspecified()) return true;
     113                return false;
     114        }
     115
     116        bool operator!=(const EndpointDescriptor& rh) const {
     117                if (!rh.isUnspecified() && !isUnspecified()) return true;
     118                return false;
     119        }
     120
     121        EndpointDescriptor& operator=( const EndpointDescriptor& rhs) {
     122                endpoints = rhs.endpoints;
     123        }
     124
     125        /// returns the end-points of this descriptor
     126        endpoint_set& getEndpoints() {
     127                return endpoints;
     128        }
     129
     130        /// returns the end-points of this descriptor
     131        const endpoint_set& getEndpoints() const {
     132                return endpoints;
     133        }
    95134
    96135private:
    97         bool isUnspec;
    98         IPv4Locator* locator;
     136        endpoint_set endpoints;
    99137};
    100138
    101139}} // namespace ariba, communication
    102140
    103 sznBeginDefault( ariba::communication::EndpointDescriptor, X ) {
    104         uint8_t unspec = isUnspec;
    105 
    106         X && unspec && VO(locator);
    107 
    108         // when deserializing reset unspec flag
    109         if (X.isDeserializer()) isUnspec = unspec;
    110 } sznEnd();
     141sznBeginDefault( ariba::communication::EndpointDescriptor, X ){
     142        // serialize endpoints
     143        uint16_t len = endpoints.to_bytes_size();
     144        X && len;
     145        uint8_t* buffer = X.bytes( len );
     146        if (buffer!=NULL) {
     147                if (X.isDeserializer()) endpoints.assign(buffer,len);
     148                else endpoints.to_bytes(buffer);
     149        }
     150}sznEnd();
    111151
    112152#endif /*ENDPOINTDESCRIPTOR_H_*/
  • source/ariba/communication/messages/AribaBaseMsg.cpp

    r4582 r5284  
    4444vsznDefault(AribaBaseMsg);
    4545
    46 AribaBaseMsg::AribaBaseMsg(
    47         const Address* address,
    48         LINK_STATE _state,
    49         const LinkID& _localLink,
    50         const LinkID& _remoteLink )
    51                 : state( (uint8_t)_state ),
    52                   localLink( _localLink ),
    53                   remoteLink( _remoteLink ){
    54 
    55         Message::setDestinationAddress( address );
     46AribaBaseMsg::AribaBaseMsg( type_ _type,
     47        const LinkID& localLink, const LinkID& remoteLink ) :
     48        type((uint8_t)_type),
     49        localLink(localLink),remoteLink(remoteLink) {
    5650}
    5751
    58 AribaBaseMsg::~AribaBaseMsg(){
     52AribaBaseMsg::~AribaBaseMsg() {
    5953}
    6054
    61 const AribaBaseMsg::LINK_STATE AribaBaseMsg::getType(){
    62         return (LINK_STATE)state;
    63 }
    64 
    65 const LinkID& AribaBaseMsg::getLocalLink(){
    66         return localLink;
    67 }
    68 
    69 const LinkID& AribaBaseMsg::getRemoteLink(){
    70         return remoteLink;
    71 }
    72 
    73 const string AribaBaseMsg::getTypeString(){
    74 
    75         switch( getType() ){
    76                 case LINK_STATE_DATA:                   return "LINK_STATE_DATA";
    77                 case LINK_STATE_OPEN_REQUEST:   return "LINK_STATE_OPEN_REQUEST";
    78                 case LINK_STATE_OPEN_REPLY:     return "LINK_STATE_OPEN_REPLY";
    79                 case LINK_STATE_CLOSE_REQUEST:  return "LINK_STATE_CLOSE_REQUEST";
    80                 case LINK_STATE_UPDATE:                 return "LINK_STATE_UPDATE";
    81                 default:                                                "unknown";
     55const string AribaBaseMsg::getTypeString() const {
     56        switch (getType()) {
     57                case typeData:
     58                        return "typeData";
     59                case typeLinkRequest:
     60                        return "typeLinkRequest";
     61                case typeLinkReply:
     62                        return "typeLinkReply";
     63                case typeLinkClose:
     64                        return "typeLinkClose";
     65                case typeLinkUpdate:
     66                        return "typeLinkUpdate";
     67                default:
     68                        "unknown";
    8269        }
    83 
    8470        return "unknown";
    8571}
  • source/ariba/communication/messages/AribaBaseMsg.h

    r4625 r5284  
    4848#include "ariba/utility/types/ServiceID.h"
    4949
     50#include "../EndpointDescriptor.h"
     51
    5052using std::string;
    5153using ariba::utility::Address;
     
    6264        VSERIALIZEABLE;
    6365public:
     66        enum type_ {
     67                typeData = 0,
     68                typeLinkRequest = 1,
     69                typeLinkReply = 2,
     70                typeLinkClose = 3,
     71                typeLinkUpdate = 4
     72        };
    6473
    65         typedef enum _LINK_STATE {
    66                 LINK_STATE_DATA          = 0,
    67                 LINK_STATE_OPEN_REQUEST  = 1,
    68                 LINK_STATE_OPEN_REPLY    = 2,
    69                 LINK_STATE_CLOSE_REQUEST = 3, // there is no close reply. send request and local link is closed
    70                 LINK_STATE_UPDATE        = 4,
    71         } LINK_STATE;
    72 
    73         AribaBaseMsg(
    74                         const Address* address    = NULL,
    75                         LINK_STATE _state         = LINK_STATE_DATA,
    76                         const LinkID& _localLink  = LinkID::UNSPECIFIED,
    77                         const LinkID& _remoteLink = LinkID::UNSPECIFIED );
     74        AribaBaseMsg( type_ type = typeData,
     75                        const LinkID& localLink = LinkID::UNSPECIFIED,
     76                        const LinkID& remoteLink = LinkID::UNSPECIFIED );
    7877
    7978        virtual ~AribaBaseMsg();
    8079
    81         const LINK_STATE getType();
    82         const string getTypeString();
    83         const LinkID& getLocalLink();
    84         const LinkID& getRemoteLink();
     80        const string getTypeString() const;
     81
     82        const type_ getType() const {
     83                return (type_)type;
     84        }
     85
     86        const LinkID& getLocalLink() const {
     87                return localLink;
     88        }
     89
     90        const LinkID& getRemoteLink() const {
     91                return remoteLink;
     92        }
     93
     94        EndpointDescriptor& getLocalDescriptor() {
     95                return localDescriptor;
     96        }
     97
     98        EndpointDescriptor& getRemoteDescriptor() {
     99                return remoteDescriptor;
     100        }
    85101
    86102private:
    87         uint8_t state;          // the link message type
     103        uint8_t type;           // the link message type
     104
     105        // remote and local link ids
    88106        LinkID localLink;       // the local link id
    89107        LinkID remoteLink;      // the remote link id
    90108
     109        // remote and local endpoint descriptors
     110        EndpointDescriptor localDescriptor;
     111        EndpointDescriptor remoteDescriptor;
    91112};
    92113
     
    94115
    95116sznBeginDefault( ariba::communication::AribaBaseMsg, X ) {
    96         X && state && &localLink && &remoteLink && Payload();
     117        X && type && &localLink && &remoteLink;
     118        if (type == typeLinkReply || type == typeLinkRequest)
     119                X && localDescriptor && remoteDescriptor;
     120        X && Payload();
    97121} sznEnd();
    98122
  • source/ariba/communication/networkinfo/AddressInformation.cpp

    r3690 r5284  
    3939#include "AddressInformation.h"
    4040
     41#include <ifaddrs.h>
     42
     43#include <boost/asio/ip/address.hpp>
     44#include <boost/asio/ip/address_v4.hpp>
     45
    4146namespace ariba {
    4247namespace communication {
     48
     49using namespace ariba::addressing;
    4350
    4451AddressInformation::AddressInformation(){
     
    5259        AddressList retlist;
    5360
    54         //
    5561        // gather transport addresses
    56         //
    57 
    5862        struct ifaddrs* ifap;
    5963        getifaddrs( &ifap );
    6064
    6165        for( struct ifaddrs* p = ifap; p != NULL; p=p->ifa_next ){
     66
     67                // no name set? ->continue
    6268                if( interface.name.compare(string(p->ifa_name)) != 0 ) continue;
    6369
    64                 // TODO: currently only handle IPv4
     70                // handle IPv4 entry
    6571                struct sockaddr* addr = p->ifa_addr;
    66                 if( addr->sa_family != AF_INET ) continue;
     72                if( addr->sa_family == AF_INET ) {
     73                        // get address
     74                        const struct sockaddr_in& ipv4 = (const struct sockaddr_in&)*addr;
     75                        boost::asio::ip::address_v4::bytes_type bytes;
     76                        for( int i=0; i<4; i++ )
     77                                bytes[i] = (ipv4.sin_addr.s_addr >> (24-i*8)) & 0xff;
    6778
    68                 const struct sockaddr_in& ipv4 = (const struct sockaddr_in&)*addr;
    69                 boost::asio::ip::address_v4::bytes_type bytes;
    70                 for( int i=0; i<4; i++ )
    71                         bytes[i] = (ipv4.sin_addr.s_addr >> (24-i*8)) & 0xff;
    72 
    73                 boost::asio::ip::address boost_addr = boost::asio::ip::address_v4( bytes );
    74                 retlist.push_back( IPv4Locator::fromString(boost_addr.to_string()) );
     79                        // add ipv4 address
     80                        boost::asio::ip::address boost_addr = boost::asio::ip::address_v4( bytes );
     81                        retlist.push_back( vcapsule<address_v>( ip_address(boost_addr) ) );
     82                }
    7583        }
    7684
    7785        freeifaddrs( ifap );
    78 
    79         //
    80         // TODO: gather further addresses like MAC etc.
    81         //
    8286
    8387        return retlist;
  • source/ariba/communication/networkinfo/AddressInformation.h

    r3690 r5284  
    4040#define __ADDRESS_INFORMATION_H
    4141
    42 #include <ifaddrs.h>
    4342#include <vector>
    44 #include <boost/asio/ip/address.hpp>
    45 #include <boost/asio/ip/address_v4.hpp>
     43
    4644#include "ariba/communication/networkinfo/NetworkInterface.h"
    47 #include "ariba/communication/modules/network/ip/IPv4Locator.h"
    48 
    49 using std::vector;
    50 using ariba::communication::NetworkInterface;
    51 using ariba::communication::IPv4Locator;
     45#include "ariba/utility/addressing/addressing.hpp"
    5246
    5347namespace ariba {
    5448namespace communication {
    5549
    56 typedef vector<IPv4Locator> AddressList; // TODO: make more general, not only ipv4
     50using namespace std;
     51using namespace ariba::addressing;
     52
     53/// a list of addresses
     54typedef vector<address_v*> AddressList;
    5755
    5856class AddressInformation {
     
    6260
    6361        AddressList getAddresses(const NetworkInterface& interface);
    64 
    6562};
    6663
Note: See TracChangeset for help on using the changeset viewer.