| 1 | // [Licence]
|
---|
| 2 | // The Ariba-Underlay Copyright
|
---|
| 3 | //
|
---|
| 4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
| 5 | //
|
---|
| 6 | // Institute of Telematics
|
---|
| 7 | // UniversitÀt Karlsruhe (TH)
|
---|
| 8 | // Zirkel 2, 76128 Karlsruhe
|
---|
| 9 | // Germany
|
---|
| 10 | //
|
---|
| 11 | // Redistribution and use in source and binary forms, with or without
|
---|
| 12 | // modification, are permitted provided that the following conditions are
|
---|
| 13 | // met:
|
---|
| 14 | //
|
---|
| 15 | // 1. Redistributions of source code must retain the above copyright
|
---|
| 16 | // notice, this list of conditions and the following disclaimer.
|
---|
| 17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | // notice, this list of conditions and the following disclaimer in the
|
---|
| 19 | // documentation and/or other materials provided with the distribution.
|
---|
| 20 | //
|
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
| 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | //
|
---|
| 33 | // The views and conclusions contained in the software and documentation
|
---|
| 34 | // are those of the authors and should not be interpreted as representing
|
---|
| 35 | // official policies, either expressed or implied, of the Institute of
|
---|
| 36 | // Telematics.
|
---|
| 37 | // [Licence]
|
---|
| 38 |
|
---|
| 39 | #include "BaseCommunication.h"
|
---|
| 40 |
|
---|
| 41 | #ifdef UNDERLAY_OMNET
|
---|
| 42 | #include "ariba/communication/modules/transport/omnet/AribaOmnetModule.h"
|
---|
| 43 | #include "ariba/communication/modules/network/omnet/OmnetNetworkProtocol.h"
|
---|
| 44 | #include "ariba/utility/system/StartupWrapper.h"
|
---|
| 45 |
|
---|
| 46 | using ariba::communication::AribaOmnetModule;
|
---|
| 47 | using ariba::communication::OmnetNetworkProtocol;
|
---|
| 48 | using ariba::utility::StartupWrapper;
|
---|
| 49 | #endif
|
---|
| 50 |
|
---|
| 51 | namespace ariba {
|
---|
| 52 | namespace communication {
|
---|
| 53 |
|
---|
| 54 | use_logging_cpp(BaseCommunication);
|
---|
| 55 | const BaseCommunication::LinkDescriptor BaseCommunication::LinkDescriptor::UNSPECIFIED;
|
---|
| 56 |
|
---|
| 57 | BaseCommunication::BaseCommunication()
|
---|
| 58 | : messageReceiver(NULL), network(NULL), transport(NULL){
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | BaseCommunication::~BaseCommunication(){
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void BaseCommunication::start(const NetworkLocator* _locallocator, const uint16_t _listenport){
|
---|
| 65 |
|
---|
| 66 | currentSeqnum = 0;
|
---|
| 67 | listenport = _listenport;
|
---|
| 68 |
|
---|
| 69 | logging_info( "starting up base communication and creating transports ..." );
|
---|
| 70 | logging_info( "using port " << listenport );
|
---|
| 71 |
|
---|
| 72 | #ifdef UNDERLAY_OMNET
|
---|
| 73 | AribaOmnetModule* module = StartupWrapper::getCurrentModule();
|
---|
| 74 | module->setServerPort( listenport );
|
---|
| 75 |
|
---|
| 76 | transport = module;
|
---|
| 77 | network = new OmnetNetworkProtocol( module );
|
---|
| 78 | #else
|
---|
| 79 | transport = new TCPTransport( listenport );
|
---|
| 80 | network = new IPv4NetworkProtocol();
|
---|
| 81 | #endif
|
---|
| 82 |
|
---|
| 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 );
|
---|
| 133 | transport->start();
|
---|
| 134 |
|
---|
| 135 | #ifndef UNDERLAY_OMNET
|
---|
| 136 | //
|
---|
| 137 | // bind to the network change detection
|
---|
| 138 | //
|
---|
| 139 |
|
---|
| 140 | networkMonitor.registerNotification( this );
|
---|
| 141 | #endif
|
---|
| 142 |
|
---|
| 143 | //
|
---|
| 144 | // base comm startup done
|
---|
| 145 | //
|
---|
| 146 |
|
---|
| 147 | logging_info( "base communication started up" );
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void BaseCommunication::stop() {
|
---|
| 151 |
|
---|
| 152 | logging_info( "stopping base communication and transport ..." );
|
---|
| 153 |
|
---|
| 154 | transport->stop();
|
---|
| 155 | delete transport;
|
---|
| 156 | delete network;
|
---|
| 157 |
|
---|
| 158 | logging_info( "base communication stopped" );
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | const LinkID BaseCommunication::establishLink(
|
---|
| 162 | const EndpointDescriptor& descriptor,
|
---|
| 163 | const QoSParameterSet& qos,
|
---|
| 164 | const SecurityParameterSet& sec){
|
---|
| 165 |
|
---|
| 166 | logging_debug( "request to establish link" );
|
---|
| 167 |
|
---|
| 168 | //
|
---|
| 169 | // just use the first locator in the endp descriptors
|
---|
| 170 | //
|
---|
| 171 |
|
---|
| 172 | if( descriptor.locator == NULL ){
|
---|
| 173 | logging_error( "invalid destination endpoint" );
|
---|
| 174 | return LinkID::UNSPECIFIED;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | if( localDescriptor.locator == NULL ){
|
---|
| 178 | logging_error( "invalid local endpoint" );
|
---|
| 179 | return LinkID::UNSPECIFIED;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | const NetworkLocator* remote = descriptor.locator;
|
---|
| 183 | const NetworkLocator* local = localDescriptor.locator;
|
---|
| 184 |
|
---|
| 185 | //
|
---|
| 186 | // create link and link descriptor
|
---|
| 187 | //
|
---|
| 188 |
|
---|
| 189 | LinkID linkid = LinkID::create();
|
---|
| 190 |
|
---|
| 191 | logging_debug( "creating new local descriptor entry with local link id " << linkid.toString() );
|
---|
| 192 | LinkDescriptor linkDescriptor( linkid, local, LinkID::UNSPECIFIED, remote, descriptor, false );
|
---|
| 193 | addLink( linkDescriptor );
|
---|
| 194 |
|
---|
| 195 | //
|
---|
| 196 | // create a base msg with our link id and
|
---|
| 197 | // a request to open a link on the other side
|
---|
| 198 | //
|
---|
| 199 |
|
---|
| 200 | logging_debug( "sending out base messages with request to open link to " << remote->toString() );
|
---|
| 201 | AribaBaseMsg baseMsg( remote, AribaBaseMsg::LINK_STATE_OPEN_REQUEST, linkid,
|
---|
| 202 | LinkID::UNSPECIFIED );
|
---|
| 203 | transport->sendMessage(&baseMsg);
|
---|
| 204 |
|
---|
| 205 | return linkid;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | void BaseCommunication::dropLink(const LinkID link) {
|
---|
| 209 |
|
---|
| 210 | logging_debug( "starting to drop link " + link.toString() );
|
---|
| 211 |
|
---|
| 212 | // see if we have the link
|
---|
| 213 | LinkDescriptor& descriptor = queryLocalLink( link );
|
---|
| 214 | if( descriptor.isUnspecified() ){
|
---|
| 215 | logging_error( "don't know the link you want to drop" );
|
---|
| 216 | return;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | // warn if this link has some queued messages attached
|
---|
| 220 | if( descriptor.waitingmsg.size() > 0 ){
|
---|
| 221 | logging_warn( "dropping link " << link.toString() <<
|
---|
| 222 | " that has " << descriptor.waitingmsg.size() << " waiting messages" );
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | // create message to drop the link
|
---|
| 226 | logging_debug( "sending out link close request. for us, the link is closed now" );
|
---|
| 227 | AribaBaseMsg msg(
|
---|
| 228 | descriptor.remoteLocator,
|
---|
| 229 | AribaBaseMsg::LINK_STATE_CLOSE_REQUEST,
|
---|
| 230 | descriptor.localLink,
|
---|
| 231 | descriptor.remoteLink
|
---|
| 232 | );
|
---|
| 233 |
|
---|
| 234 | // send message to drop the link
|
---|
| 235 | transport->sendMessage( &msg );
|
---|
| 236 |
|
---|
| 237 | // tell the registered listeners
|
---|
| 238 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 239 | i->onLinkDown( link, descriptor.localLocator, descriptor.remoteLocator );
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | // remove from map
|
---|
| 243 | removeLink(link);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | seqnum_t BaseCommunication::sendMessage( const LinkID lid, const Message* message) {
|
---|
| 247 |
|
---|
| 248 | logging_debug( "sending out message to link " << lid.toString() );
|
---|
| 249 |
|
---|
| 250 | // query local link info
|
---|
| 251 | LinkDescriptor& linkDesc = queryLocalLink(lid);
|
---|
| 252 | if( linkDesc.isUnspecified() ){
|
---|
| 253 | logging_error( "don't know the link with id " << lid.toString() );
|
---|
| 254 | return -1;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | // create message
|
---|
| 258 | AribaBaseMsg msg(
|
---|
| 259 | linkDesc.remoteLocator,
|
---|
| 260 | AribaBaseMsg::LINK_STATE_DATA,
|
---|
| 261 | linkDesc.localLink,
|
---|
| 262 | linkDesc.remoteLink
|
---|
| 263 | );
|
---|
| 264 |
|
---|
| 265 | // encapsulate the payload message
|
---|
| 266 | msg.encapsulate( const_cast<Message*>(message) );
|
---|
| 267 |
|
---|
| 268 | if( linkDesc.linkup ){
|
---|
| 269 |
|
---|
| 270 | // send message
|
---|
| 271 | transport->sendMessage( &msg );
|
---|
| 272 | return ++currentSeqnum;
|
---|
| 273 |
|
---|
| 274 | } else {
|
---|
| 275 |
|
---|
| 276 | // queue message
|
---|
| 277 | logging_info( "link " << lid.toString() << " is not up yet, queueing message" );
|
---|
| 278 | linkDesc.waitingmsg.push_back( new Message(msg) ); // TODO ooooo
|
---|
| 279 |
|
---|
| 280 | return 0;
|
---|
| 281 |
|
---|
| 282 | } // if( linkDesc.linkup )
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | const EndpointDescriptor& BaseCommunication::getEndpointDescriptor(const LinkID link) const {
|
---|
| 286 |
|
---|
| 287 | if( link == LinkID::UNSPECIFIED){
|
---|
| 288 | return localDescriptor;
|
---|
| 289 | } else {
|
---|
| 290 | LinkDescriptor& linkDesc = queryLocalLink(link);
|
---|
| 291 | if (linkDesc.isUnspecified()) return EndpointDescriptor::UNSPECIFIED;
|
---|
| 292 | return linkDesc.remoteEndpoint;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | void BaseCommunication::registerMessageReceiver(MessageReceiver* _receiver) {
|
---|
| 297 | messageReceiver = _receiver;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | void BaseCommunication::unregisterMessageReceiver(MessageReceiver* _receiver) {
|
---|
| 301 | messageReceiver = NULL;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | void BaseCommunication::registerEventListener(CommunicationEvents* _events){
|
---|
| 305 |
|
---|
| 306 | if( eventListener.find( _events ) == eventListener.end() )
|
---|
| 307 | eventListener.insert( _events );
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | void BaseCommunication::unregisterEventListener(CommunicationEvents* _events){
|
---|
| 311 |
|
---|
| 312 | EventListenerSet::iterator i = eventListener.find( _events );
|
---|
| 313 | if( i != eventListener.end() )
|
---|
| 314 | eventListener.erase( i );
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 |
|
---|
| 318 | bool BaseCommunication::receiveMessage(const Message* message, const LinkID& link, const NodeID& node){
|
---|
| 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
|
---|
| 349 | );
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 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 |
|
---|
| 376 | const NetworkLocator* localLocator = dynamic_cast<const NetworkLocator*>(localDescriptor.locator);
|
---|
| 377 | const NetworkLocator* remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
|
---|
| 378 |
|
---|
| 379 | logging_debug( "localLocator=" << localLocator->toString()
|
---|
| 380 | << " remoteLocator=" << remoteLocator->toString());
|
---|
| 381 |
|
---|
| 382 | // ask the registered listeners if this link
|
---|
| 383 | // creation is fine. we will only allow the
|
---|
| 384 | // link if all of them agree
|
---|
| 385 |
|
---|
| 386 | bool allowlink = true;
|
---|
| 387 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 388 | allowlink &= i->onLinkRequest( localLink, localLocator, remoteLocator );
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | if( !allowlink ){
|
---|
| 392 | logging_warn( "overlay denied creation of link" );
|
---|
| 393 | return true;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | //
|
---|
| 397 | // create and save the descriptor for the link
|
---|
| 398 | //
|
---|
| 399 |
|
---|
| 400 | LinkDescriptor linkDescriptor(localLink, localLocator, remoteLink,
|
---|
| 401 | remoteLocator, EndpointDescriptor(remoteLocator), true);
|
---|
| 402 |
|
---|
| 403 | logging_debug( "saving new link descriptor with " <<
|
---|
| 404 | "[local link " << localLink.toString() << "] " <<
|
---|
| 405 | "[local locator " << localLocator->toString() << "] " <<
|
---|
| 406 | "[remote link " << remoteLink.toString() << "] " <<
|
---|
| 407 | "[remote locator " << remoteLocator->toString() << "]" <<
|
---|
| 408 | "[link up true]" );
|
---|
| 409 |
|
---|
| 410 | addLink( linkDescriptor );
|
---|
| 411 |
|
---|
| 412 | //
|
---|
| 413 | // send out a link reply
|
---|
| 414 | //
|
---|
| 415 |
|
---|
| 416 | logging_debug( "sending back link open reply for " <<
|
---|
| 417 | "[local link " << localLink.toString() << "] " <<
|
---|
| 418 | "[remote link " << remoteLink.toString() << "]" );
|
---|
| 419 |
|
---|
| 420 | AribaBaseMsg reply(remoteLocator,
|
---|
| 421 | AribaBaseMsg::LINK_STATE_OPEN_REPLY,
|
---|
| 422 | localLink,
|
---|
| 423 | remoteLink);
|
---|
| 424 |
|
---|
| 425 | transport->sendMessage( &reply );
|
---|
| 426 |
|
---|
| 427 | //
|
---|
| 428 | // the link is now open
|
---|
| 429 | //
|
---|
| 430 |
|
---|
| 431 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 432 | i->onLinkUp( localLink, localLocator, remoteLocator );
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | } // LINK_STATE_OPEN_REQUEST
|
---|
| 436 |
|
---|
| 437 | //
|
---|
| 438 | // handle link open replies
|
---|
| 439 | //
|
---|
| 440 |
|
---|
| 441 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REPLY ){
|
---|
| 442 |
|
---|
| 443 | logging_debug( "received link open reply for a link we initiated" );
|
---|
| 444 |
|
---|
| 445 | // this is a reply to a link open request, so we have already
|
---|
| 446 | // a link mapping and can now set the remote link to valid
|
---|
| 447 | LinkDescriptor& linkDesc = queryLocalLink( spovmsg->getRemoteLink() );
|
---|
| 448 |
|
---|
| 449 | if (linkDesc.isUnspecified()) {
|
---|
| 450 | logging_warn("Failed to find local link " << spovmsg->getRemoteLink().toString());
|
---|
| 451 | return false;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | linkDesc.remoteLink = spovmsg->getLocalLink();
|
---|
| 455 | linkDesc.linkup = true;
|
---|
| 456 |
|
---|
| 457 | logging_debug( "the link is now up with local link id " << spovmsg->getRemoteLink().toString() );
|
---|
| 458 |
|
---|
| 459 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 460 | i->onLinkUp( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | if( linkDesc.waitingmsg.size() > 0 ){
|
---|
| 464 | logging_info( "sending out queued messages on link " << linkDesc.localLink.toString() );
|
---|
| 465 |
|
---|
| 466 | BOOST_FOREACH( Message* msg, linkDesc.waitingmsg ){
|
---|
| 467 | sendMessage( linkDesc.localLink, msg );
|
---|
| 468 | delete msg;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | linkDesc.waitingmsg.clear();
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | } // LINK_STATE_OPEN_REPLY
|
---|
| 475 |
|
---|
| 476 | //
|
---|
| 477 | // handle link close requests
|
---|
| 478 | //
|
---|
| 479 |
|
---|
| 480 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_CLOSE_REQUEST ){
|
---|
| 481 |
|
---|
| 482 | const LinkID& localLink = spovmsg->getRemoteLink();
|
---|
| 483 | logging_debug( "received link close request for link " << localLink.toString() );
|
---|
| 484 |
|
---|
| 485 | //
|
---|
| 486 | // the link is closed immediately, we
|
---|
| 487 | // don't need to send out a reply, so we
|
---|
| 488 | // delete the mapping and inform
|
---|
| 489 | //
|
---|
| 490 |
|
---|
| 491 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
| 492 | if (linkDesc.isUnspecified()) {
|
---|
| 493 | logging_warn("Failed to find local link " << localLink.toString());
|
---|
| 494 | return false;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 498 | i->onLinkDown( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | //
|
---|
| 502 | // delete all pending messages for the link that has been closed
|
---|
| 503 | //
|
---|
| 504 |
|
---|
| 505 | BOOST_FOREACH( Message* msg, linkDesc.waitingmsg ){
|
---|
| 506 | delete msg;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | linkDesc.waitingmsg.clear();
|
---|
| 510 |
|
---|
| 511 | //
|
---|
| 512 | // remove the link descriptor
|
---|
| 513 | //
|
---|
| 514 |
|
---|
| 515 | removeLink( localLink );
|
---|
| 516 |
|
---|
| 517 | } // LINK_STATE_CLOSE_REQUEST
|
---|
| 518 |
|
---|
| 519 | //
|
---|
| 520 | // handle locator updates
|
---|
| 521 | //
|
---|
| 522 |
|
---|
| 523 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_UPDATE ){
|
---|
| 524 |
|
---|
| 525 | const LinkID& localLink = spovmsg->getRemoteLink();
|
---|
| 526 | logging_debug( "received link update for link " << localLink.toString() );
|
---|
| 527 |
|
---|
| 528 | //
|
---|
| 529 | // find the link description
|
---|
| 530 | //
|
---|
| 531 |
|
---|
| 532 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
| 533 | if (linkDesc.isUnspecified()) {
|
---|
| 534 | logging_warn("Failed to update local link " << localLink.toString());
|
---|
| 535 | return false;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | //
|
---|
| 539 | // update the remote locator
|
---|
| 540 | //
|
---|
| 541 |
|
---|
| 542 | const NetworkLocator* oldremote = linkDesc.remoteLocator;
|
---|
| 543 | linkDesc.remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
|
---|
| 544 |
|
---|
| 545 | //
|
---|
| 546 | // inform the listeners (local link has _not_ changed!)
|
---|
| 547 | //
|
---|
| 548 |
|
---|
| 549 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 550 | i->onLinkChanged(
|
---|
| 551 | linkDesc.localLink, // linkid
|
---|
| 552 | linkDesc.localLocator, // old local
|
---|
| 553 | linkDesc.localLocator, // new local
|
---|
| 554 | oldremote, // old remote
|
---|
| 555 | linkDesc.remoteLocator // new remote
|
---|
| 556 | );
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | } // LINK_STATE_UPDATE
|
---|
| 560 |
|
---|
| 561 | return true;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | void BaseCommunication::addLink( const LinkDescriptor& link ) {
|
---|
| 565 | linkSet.push_back( link );
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | void BaseCommunication::removeLink( const LinkID& localLink ) {
|
---|
| 569 |
|
---|
| 570 | LinkSet::iterator i = linkSet.begin();
|
---|
| 571 | LinkSet::iterator iend = linkSet.end();
|
---|
| 572 |
|
---|
| 573 | for( ; i != iend; i++){
|
---|
| 574 | if( (*i).localLink != localLink) continue;
|
---|
| 575 |
|
---|
| 576 | BOOST_FOREACH( Message* msg, i->waitingmsg ){
|
---|
| 577 | delete msg;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | i->waitingmsg.clear();
|
---|
| 581 | linkSet.erase( i );
|
---|
| 582 |
|
---|
| 583 | break;
|
---|
| 584 | }
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | BaseCommunication::LinkDescriptor& BaseCommunication::queryLocalLink( const LinkID& link ) const {
|
---|
| 588 | for (int i=0; i<linkSet.size();i++)
|
---|
| 589 | if (linkSet[i].localLink == link) return (LinkDescriptor&)linkSet[i];
|
---|
| 590 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | BaseCommunication::LinkDescriptor& BaseCommunication::queryRemoteLink( const LinkID& link ) const {
|
---|
| 594 | for (int i=0; i<linkSet.size();i++)
|
---|
| 595 | if (linkSet[i].remoteLink == link) return (LinkDescriptor&)linkSet[i];
|
---|
| 596 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | LinkIDs BaseCommunication::getLocalLinks( const EndpointDescriptor& ep ) const {
|
---|
| 600 | LinkIDs ids;
|
---|
| 601 |
|
---|
| 602 | for (int i=0; i<linkSet.size(); i++){
|
---|
| 603 | if( ep == EndpointDescriptor::UNSPECIFIED ){
|
---|
| 604 | ids.push_back( linkSet[i].localLink );
|
---|
| 605 | } else {
|
---|
| 606 | if ( linkSet[i].remoteLocator == ep.locator )
|
---|
| 607 | ids.push_back( linkSet[i].localLink );
|
---|
| 608 | }
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | return ids;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | void BaseCommunication::onNetworkChange(const NetworkChangeInterface::NetworkChangeInfo& info){
|
---|
| 615 |
|
---|
| 616 | #ifdef UNDERLAY_OMNET
|
---|
| 617 |
|
---|
| 618 | // we have no mobility support for simulations
|
---|
| 619 | return
|
---|
| 620 |
|
---|
| 621 | #endif // UNDERLAY_OMNET
|
---|
| 622 |
|
---|
| 623 | //
|
---|
| 624 | // we only care about address changes, not about interface changes
|
---|
| 625 | // as address changes are triggered by interface changes, we are safe here
|
---|
| 626 | //
|
---|
| 627 |
|
---|
| 628 | if( info.type != NetworkChangeInterface::EventTypeAddressNew &&
|
---|
| 629 | info.type != NetworkChangeInterface::EventTypeAddressDelete ) return;
|
---|
| 630 |
|
---|
| 631 | logging_info( "base communication is handling network address changes" );
|
---|
| 632 |
|
---|
| 633 | //
|
---|
| 634 | // get all now available addresses
|
---|
| 635 | //
|
---|
| 636 |
|
---|
| 637 | NetworkInformation networkInformation;
|
---|
| 638 | AddressInformation addressInformation;
|
---|
| 639 |
|
---|
| 640 | NetworkInterfaceList interfaces = networkInformation.getInterfaces();
|
---|
| 641 | AddressList addresses;
|
---|
| 642 |
|
---|
| 643 | for( NetworkInterfaceList::iterator i = interfaces.begin(); i != interfaces.end(); i++ ){
|
---|
| 644 | AddressList newaddr = addressInformation.getAddresses(*i);
|
---|
| 645 | addresses.insert( addresses.end(), newaddr.begin(), newaddr.end() );
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | //
|
---|
| 649 | // get current locators for the local endpoint
|
---|
| 650 | // TODO: this code is dublicate of the ctor code!!! cleanup!
|
---|
| 651 | //
|
---|
| 652 |
|
---|
| 653 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
|
---|
| 654 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
|
---|
| 655 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
|
---|
| 656 |
|
---|
| 657 | //
|
---|
| 658 | // remember the old local endpoint, in case it changes
|
---|
| 659 | //
|
---|
| 660 |
|
---|
| 661 | EndpointDescriptor oldLocalDescriptor( localDescriptor );
|
---|
| 662 |
|
---|
| 663 | //
|
---|
| 664 | // look for local locators that we can use in communication
|
---|
| 665 | //
|
---|
| 666 | // choose the first locator that is not localhost
|
---|
| 667 | //
|
---|
| 668 |
|
---|
| 669 | bool foundLocator = false;
|
---|
| 670 | bool changedLocator = false;
|
---|
| 671 |
|
---|
| 672 | for( ; i != iend; i++){
|
---|
| 673 | logging_debug( "local locator found " << (*i)->toString() );
|
---|
| 674 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
|
---|
| 675 |
|
---|
| 676 | if( *ipv4locator != IPv4Locator::LOCALHOST &&
|
---|
| 677 | *ipv4locator != IPv4Locator::ANY &&
|
---|
| 678 | *ipv4locator != IPv4Locator::BROADCAST ){
|
---|
| 679 |
|
---|
| 680 | ipv4locator->setPort( listenport );
|
---|
| 681 | changedLocator = *localDescriptor.locator != *ipv4locator;
|
---|
| 682 | localDescriptor.locator = ipv4locator;
|
---|
| 683 | logging_info( "binding to addr = " << ipv4locator->toString() );
|
---|
| 684 | foundLocator = true;
|
---|
| 685 | break;
|
---|
| 686 | }
|
---|
| 687 | } // for( ; i != iend; i++)
|
---|
| 688 |
|
---|
| 689 | //
|
---|
| 690 | // if we found no locator, bind to localhost
|
---|
| 691 | //
|
---|
| 692 |
|
---|
| 693 | if( !foundLocator ){
|
---|
| 694 | changedLocator = *localDescriptor.locator != IPv4Locator::LOCALHOST;
|
---|
| 695 | localDescriptor.locator = new IPv4Locator( IPv4Locator::LOCALHOST );
|
---|
| 696 | ((IPv4Locator*)(localDescriptor.locator))->setPort( listenport );
|
---|
| 697 | logging_info( "found no good local lcoator, binding to addr = " <<
|
---|
| 698 | localDescriptor.locator->toString() );
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | //
|
---|
| 702 | // if we have connections that have no more longer endpoints
|
---|
| 703 | // close these. they will be automatically built up again.
|
---|
| 704 | // also update the local locator in the linkset mapping
|
---|
| 705 | //
|
---|
| 706 |
|
---|
| 707 | if( changedLocator ){
|
---|
| 708 |
|
---|
| 709 | logging_debug( "local endp locator has changed to " << localDescriptor.toString() <<
|
---|
| 710 | ", resettings connections that end at old locator " <<
|
---|
| 711 | oldLocalDescriptor.toString());
|
---|
| 712 |
|
---|
| 713 | LinkSet::iterator i = linkSet.begin();
|
---|
| 714 | LinkSet::iterator iend = linkSet.end();
|
---|
| 715 |
|
---|
| 716 | for( ; i != iend; i++ ){
|
---|
| 717 |
|
---|
| 718 | logging_debug( "checking connection for locator change: " <<
|
---|
| 719 | " local " << (*i).localLocator->toString() <<
|
---|
| 720 | " old " << oldLocalDescriptor.locator->toString() );
|
---|
| 721 |
|
---|
| 722 | if( *((*i).localLocator) == *(oldLocalDescriptor.locator) ){
|
---|
| 723 |
|
---|
| 724 | logging_debug("terminating connection to " << (*i).remoteLocator->toString() );
|
---|
| 725 | transport->terminate( oldLocalDescriptor.locator, (*i).remoteLocator );
|
---|
| 726 |
|
---|
| 727 | (*i).localLocator = localDescriptor.locator;
|
---|
| 728 | }
|
---|
| 729 | } // for( ; i != iend; i++ )
|
---|
| 730 |
|
---|
| 731 | // wait 500ms to give the sockets time to shut down
|
---|
| 732 | usleep( 500000 );
|
---|
| 733 |
|
---|
| 734 | } else {
|
---|
| 735 |
|
---|
| 736 | logging_debug( "locator has not changed, not resetting connections" );
|
---|
| 737 |
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | //
|
---|
| 741 | // handle the connections that have no longer any
|
---|
| 742 | // valid locator. send update messages with the new
|
---|
| 743 | // locator, so the remote node updates its locator/link mapping
|
---|
| 744 | //
|
---|
| 745 |
|
---|
| 746 | LinkSet::iterator iAffected = linkSet.begin();
|
---|
| 747 | LinkSet::iterator endAffected = linkSet.end();
|
---|
| 748 |
|
---|
| 749 | for( ; iAffected != endAffected; iAffected++ ){
|
---|
| 750 | LinkDescriptor descr = *iAffected;
|
---|
| 751 | logging_debug( "sending out link locator update to " << descr.remoteLocator->toString() );
|
---|
| 752 |
|
---|
| 753 | AribaBaseMsg updateMsg( descr.remoteLocator,
|
---|
| 754 | AribaBaseMsg::LINK_STATE_UPDATE,
|
---|
| 755 | descr.localLink, descr.remoteLink );
|
---|
| 756 |
|
---|
| 757 | transport->sendMessage( &updateMsg );
|
---|
| 758 | }
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | }} // namespace ariba, communication
|
---|