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