| 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(const NetworkLocator* _locallocator, const uint16_t _listenport)
|
|---|
| 58 | : messageReceiver( NULL ), currentSeqnum( 0 ), listenport( _listenport ) {
|
|---|
| 59 |
|
|---|
| 60 | logging_info( "starting up base communication and creating transports ..." );
|
|---|
| 61 | logging_info( "using port " << listenport );
|
|---|
| 62 |
|
|---|
| 63 | #ifdef UNDERLAY_OMNET
|
|---|
| 64 | AribaOmnetModule* module = StartupWrapper::getCurrentModule();
|
|---|
| 65 | module->setServerPort( listenport );
|
|---|
| 66 |
|
|---|
| 67 | transport = module;
|
|---|
| 68 | network = new OmnetNetworkProtocol( module );
|
|---|
| 69 | #else
|
|---|
| 70 | transport = new TCPTransport( listenport );
|
|---|
| 71 | network = new IPv4NetworkProtocol();
|
|---|
| 72 | #endif
|
|---|
| 73 |
|
|---|
| 74 | logging_debug( "searching for local locators ..." );
|
|---|
| 75 |
|
|---|
| 76 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
|
|---|
| 77 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
|
|---|
| 78 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
|
|---|
| 79 |
|
|---|
| 80 | //
|
|---|
| 81 | // choose the first locator that is not localhost
|
|---|
| 82 | //
|
|---|
| 83 |
|
|---|
| 84 | bool foundLocator = false;
|
|---|
| 85 |
|
|---|
| 86 | for( ; i != iend; i++){
|
|---|
| 87 | logging_debug( "local locator found " << (*i)->toString() );
|
|---|
| 88 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
|
|---|
| 89 |
|
|---|
| 90 | // TODO: which locators are find to bind to?
|
|---|
| 91 | // localhost is not too bad, works when testing locally
|
|---|
| 92 | // with several instances. the manual override currently
|
|---|
| 93 | // enables to use an arbitrary address, guess this is fine.
|
|---|
| 94 | // so the manual override also can use ANY, LOCALHOST, BROADCAST
|
|---|
| 95 |
|
|---|
| 96 | if( *ipv4locator != IPv4Locator::LOCALHOST &&
|
|---|
| 97 | *ipv4locator != IPv4Locator::ANY &&
|
|---|
| 98 | *ipv4locator != IPv4Locator::BROADCAST ){
|
|---|
| 99 |
|
|---|
| 100 | ipv4locator->setPort(listenport);
|
|---|
| 101 | localDescriptor.locator = ipv4locator;
|
|---|
| 102 | localDescriptor.isUnspec = false;
|
|---|
| 103 | logging_info( "binding to addr = " << ipv4locator->toString() );
|
|---|
| 104 | foundLocator = true;
|
|---|
| 105 | break;
|
|---|
| 106 | }
|
|---|
| 107 | } // for( ; i != iend; i++)
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | if( _locallocator != NULL ) {
|
|---|
| 111 | if( localDescriptor.locator != NULL) delete localDescriptor.locator;
|
|---|
| 112 | localDescriptor.locator = new IPv4Locator( IPv4Locator::fromString( _locallocator->toString()) );
|
|---|
| 113 | localDescriptor.isUnspec = false;
|
|---|
| 114 | logging_debug( "manual locator override, using locator=" <<
|
|---|
| 115 | localDescriptor.locator->toString() );
|
|---|
| 116 | foundLocator = true;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // if we found no local locator, exit using logging fatal
|
|---|
| 120 | if( !foundLocator )
|
|---|
| 121 | logging_fatal( "did not find a useable local locator!" );
|
|---|
| 122 |
|
|---|
| 123 | transport->addMessageReceiver( this );
|
|---|
| 124 | transport->start();
|
|---|
| 125 |
|
|---|
| 126 | #ifndef UNDERLAY_OMNET
|
|---|
| 127 | //
|
|---|
| 128 | // bind to the network change detection
|
|---|
| 129 | //
|
|---|
| 130 |
|
|---|
| 131 | networkMonitor.registerNotification( this );
|
|---|
| 132 | #endif
|
|---|
| 133 |
|
|---|
| 134 | //
|
|---|
| 135 | // base comm startup done
|
|---|
| 136 | //
|
|---|
| 137 |
|
|---|
| 138 | logging_info( "base communication started up" );
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | BaseCommunication::~BaseCommunication() {
|
|---|
| 142 |
|
|---|
| 143 | logging_info( "stopping base communication and transport ..." );
|
|---|
| 144 |
|
|---|
| 145 | transport->stop();
|
|---|
| 146 | delete transport;
|
|---|
| 147 | delete network;
|
|---|
| 148 |
|
|---|
| 149 | logging_info( "base communication stopped" );
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | const LinkID BaseCommunication::establishLink(
|
|---|
| 153 | const EndpointDescriptor& descriptor,
|
|---|
| 154 | const QoSParameterSet& qos,
|
|---|
| 155 | const SecurityParameterSet& sec){
|
|---|
| 156 |
|
|---|
| 157 | logging_debug( "request to establish link" );
|
|---|
| 158 |
|
|---|
| 159 | //
|
|---|
| 160 | // just use the first locator in the endp descriptors
|
|---|
| 161 | //
|
|---|
| 162 |
|
|---|
| 163 | if( descriptor.locator == NULL ){
|
|---|
| 164 | logging_error( "invalid destination endpoint" );
|
|---|
| 165 | return LinkID::UNSPECIFIED;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | if( localDescriptor.locator == NULL ){
|
|---|
| 169 | logging_error( "invalid local endpoint" );
|
|---|
| 170 | return LinkID::UNSPECIFIED;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | const NetworkLocator* remote = descriptor.locator;
|
|---|
| 174 | const NetworkLocator* local = localDescriptor.locator;
|
|---|
| 175 |
|
|---|
| 176 | //
|
|---|
| 177 | // create link and link descriptor
|
|---|
| 178 | //
|
|---|
| 179 |
|
|---|
| 180 | LinkID linkid = LinkID::create();
|
|---|
| 181 |
|
|---|
| 182 | logging_debug( "creating new local descriptor entry with local link id " << linkid.toString() );
|
|---|
| 183 | LinkDescriptor linkDescriptor( linkid, local, LinkID::UNSPECIFIED, remote, descriptor, false );
|
|---|
| 184 | addLink( linkDescriptor );
|
|---|
| 185 |
|
|---|
| 186 | //
|
|---|
| 187 | // create a base msg with our link id and
|
|---|
| 188 | // a request to open a link on the other side
|
|---|
| 189 | //
|
|---|
| 190 |
|
|---|
| 191 | logging_debug( "sending out base messages with request to open link to " << remote->toString() );
|
|---|
| 192 | AribaBaseMsg baseMsg( remote, AribaBaseMsg::LINK_STATE_OPEN_REQUEST, linkid,
|
|---|
| 193 | LinkID::UNSPECIFIED );
|
|---|
| 194 | transport->sendMessage(&baseMsg);
|
|---|
| 195 |
|
|---|
| 196 | return linkid;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void BaseCommunication::dropLink(const LinkID link) {
|
|---|
| 200 |
|
|---|
| 201 | logging_debug( "starting to drop link " + link.toString() );
|
|---|
| 202 |
|
|---|
| 203 | // see if we have the link
|
|---|
| 204 | LinkDescriptor& descriptor = queryLocalLink( link );
|
|---|
| 205 | if( descriptor.isUnspecified() ){
|
|---|
| 206 | logging_error( "don't know the link you want to drop" );
|
|---|
| 207 | return;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | // warn if this link has some queued messages attached
|
|---|
| 211 | if( descriptor.waitingmsg.size() > 0 ){
|
|---|
| 212 | logging_warn( "dropping link " << link.toString() <<
|
|---|
| 213 | " that has " << descriptor.waitingmsg.size() << " waiting messages" );
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | // create message to drop the link
|
|---|
| 217 | logging_debug( "sending out link close request. for us, the link is closed now" );
|
|---|
| 218 | AribaBaseMsg msg(
|
|---|
| 219 | descriptor.remoteLocator,
|
|---|
| 220 | AribaBaseMsg::LINK_STATE_CLOSE_REQUEST,
|
|---|
| 221 | descriptor.localLink,
|
|---|
| 222 | descriptor.remoteLink
|
|---|
| 223 | );
|
|---|
| 224 |
|
|---|
| 225 | // send message to drop the link
|
|---|
| 226 | transport->sendMessage( &msg );
|
|---|
| 227 |
|
|---|
| 228 | // tell the registered listeners
|
|---|
| 229 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
|---|
| 230 | i->onLinkDown( link, descriptor.localLocator, descriptor.remoteLocator );
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | // remove from map
|
|---|
| 234 | removeLink(link);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | seqnum_t BaseCommunication::sendMessage( const LinkID lid, const Message* message) {
|
|---|
| 238 |
|
|---|
| 239 | logging_debug( "sending out message to link " << lid.toString() );
|
|---|
| 240 |
|
|---|
| 241 | // query local link info
|
|---|
| 242 | LinkDescriptor& linkDesc = queryLocalLink(lid);
|
|---|
| 243 | if( linkDesc.isUnspecified() ){
|
|---|
| 244 | logging_error( "don't know the link with id " << lid.toString() );
|
|---|
| 245 | return -1;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | // create message
|
|---|
| 249 | AribaBaseMsg msg(
|
|---|
| 250 | linkDesc.remoteLocator,
|
|---|
| 251 | AribaBaseMsg::LINK_STATE_DATA,
|
|---|
| 252 | linkDesc.localLink,
|
|---|
| 253 | linkDesc.remoteLink
|
|---|
| 254 | );
|
|---|
| 255 |
|
|---|
| 256 | // encapsulate the payload message
|
|---|
| 257 | msg.encapsulate( const_cast<Message*>(message) );
|
|---|
| 258 |
|
|---|
| 259 | if( linkDesc.linkup ){
|
|---|
| 260 |
|
|---|
| 261 | // send message
|
|---|
| 262 | transport->sendMessage( &msg );
|
|---|
| 263 | return ++currentSeqnum;
|
|---|
| 264 |
|
|---|
| 265 | } else {
|
|---|
| 266 |
|
|---|
| 267 | // queue message
|
|---|
| 268 | logging_info( "link " << lid.toString() << " is not up yet, queueing message" );
|
|---|
| 269 | linkDesc.waitingmsg.push_back( new Message(msg) ); // TODO ooooo
|
|---|
| 270 |
|
|---|
| 271 | return 0;
|
|---|
| 272 |
|
|---|
| 273 | } // if( linkDesc.linkup )
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | const EndpointDescriptor& BaseCommunication::getEndpointDescriptor(const LinkID link) const {
|
|---|
| 277 |
|
|---|
| 278 | if( link == LinkID::UNSPECIFIED){
|
|---|
| 279 | return localDescriptor;
|
|---|
| 280 | } else {
|
|---|
| 281 | LinkDescriptor& linkDesc = queryLocalLink(link);
|
|---|
| 282 | if (linkDesc.isUnspecified()) return EndpointDescriptor::UNSPECIFIED;
|
|---|
| 283 | return linkDesc.remoteEndpoint;
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | void BaseCommunication::registerMessageReceiver(MessageReceiver* _receiver) {
|
|---|
| 288 | messageReceiver = _receiver;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | void BaseCommunication::unregisterMessageReceiver(MessageReceiver* _receiver) {
|
|---|
| 292 | messageReceiver = NULL;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | void BaseCommunication::registerEventListener(CommunicationEvents* _events){
|
|---|
| 296 |
|
|---|
| 297 | if( eventListener.find( _events ) == eventListener.end() )
|
|---|
| 298 | eventListener.insert( _events );
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | void BaseCommunication::unregisterEventListener(CommunicationEvents* _events){
|
|---|
| 302 |
|
|---|
| 303 | EventListenerSet::iterator i = eventListener.find( _events );
|
|---|
| 304 | if( i != eventListener.end() )
|
|---|
| 305 | eventListener.erase( i );
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 | bool BaseCommunication::receiveMessage(const Message* message, const LinkID& link, const NodeID& node){
|
|---|
| 310 |
|
|---|
| 311 | //
|
|---|
| 312 | // these messages arrive from the Transport module
|
|---|
| 313 | // and are incoming network messages. Unpack the
|
|---|
| 314 | // AribaBaseMsg and handle control packets,
|
|---|
| 315 | // deliver data packets to the overlay
|
|---|
| 316 | //
|
|---|
| 317 |
|
|---|
| 318 | AribaBaseMsg* spovmsg = ((Message*)message)->decapsulate<AribaBaseMsg>();
|
|---|
| 319 | logging_debug( "receiving base comm message of type " << spovmsg->getTypeString() );
|
|---|
| 320 |
|
|---|
| 321 | //
|
|---|
| 322 | // deliver data to the overlays. we just give the
|
|---|
| 323 | // inner packet to every registered overlay ...
|
|---|
| 324 | //
|
|---|
| 325 |
|
|---|
| 326 | if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_DATA ){
|
|---|
| 327 |
|
|---|
| 328 | logging_debug( "received data message, forwarding to overlay" );
|
|---|
| 329 |
|
|---|
| 330 | //
|
|---|
| 331 | // put the linkid as address into the message
|
|---|
| 332 | // and sent it to the receiver
|
|---|
| 333 | //
|
|---|
| 334 |
|
|---|
| 335 | if( messageReceiver != NULL ) {
|
|---|
| 336 | messageReceiver->receiveMessage(
|
|---|
| 337 | spovmsg,
|
|---|
| 338 | spovmsg->getRemoteLink(),
|
|---|
| 339 | NodeID::UNSPECIFIED
|
|---|
| 340 | );
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | } // LINK_STATE_DATA
|
|---|
| 344 |
|
|---|
| 345 | //
|
|---|
| 346 | // handle link open requests
|
|---|
| 347 | //
|
|---|
| 348 |
|
|---|
| 349 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REQUEST ){
|
|---|
| 350 |
|
|---|
| 351 | logging_debug( "received link open request" );
|
|---|
| 352 |
|
|---|
| 353 | //
|
|---|
| 354 | // create a link context
|
|---|
| 355 | //
|
|---|
| 356 |
|
|---|
| 357 | // in an incoming packet the localLink is from
|
|---|
| 358 | // the sender perspective local and from our
|
|---|
| 359 | // perspective remote
|
|---|
| 360 |
|
|---|
| 361 | logging_debug( "creating local link" );
|
|---|
| 362 |
|
|---|
| 363 | LinkID localLink = LinkID::create();
|
|---|
| 364 | LinkID remoteLink = spovmsg->getLocalLink();
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 | const NetworkLocator* localLocator = dynamic_cast<const NetworkLocator*>(localDescriptor.locator);
|
|---|
| 368 | const NetworkLocator* remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
|
|---|
| 369 |
|
|---|
| 370 | logging_debug( "localLocator=" << localLocator->toString()
|
|---|
| 371 | << " remoteLocator=" << remoteLocator->toString());
|
|---|
| 372 |
|
|---|
| 373 | // ask the registered listeners if this link
|
|---|
| 374 | // creation is fine. we will only allow the
|
|---|
| 375 | // link if all of them agree
|
|---|
| 376 |
|
|---|
| 377 | bool allowlink = true;
|
|---|
| 378 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
|---|
| 379 | allowlink &= i->onLinkRequest( localLink, localLocator, remoteLocator );
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | if( !allowlink ){
|
|---|
| 383 | logging_warn( "overlay denied creation of link" );
|
|---|
| 384 | return true;
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | //
|
|---|
| 388 | // create and save the descriptor for the link
|
|---|
| 389 | //
|
|---|
| 390 |
|
|---|
| 391 | LinkDescriptor linkDescriptor(localLink, localLocator, remoteLink,
|
|---|
| 392 | remoteLocator, EndpointDescriptor(remoteLocator), true);
|
|---|
| 393 |
|
|---|
| 394 | logging_debug( "saving new link descriptor with " <<
|
|---|
| 395 | "[local link " << localLink.toString() << "] " <<
|
|---|
| 396 | "[local locator " << localLocator->toString() << "] " <<
|
|---|
| 397 | "[remote link " << remoteLink.toString() << "] " <<
|
|---|
| 398 | "[remote locator " << remoteLocator->toString() << "]" <<
|
|---|
| 399 | "[link up true]" );
|
|---|
| 400 |
|
|---|
| 401 | addLink( linkDescriptor );
|
|---|
| 402 |
|
|---|
| 403 | //
|
|---|
| 404 | // send out a link reply
|
|---|
| 405 | //
|
|---|
| 406 |
|
|---|
| 407 | logging_debug( "sending back link open reply for " <<
|
|---|
| 408 | "[local link " << localLink.toString() << "] " <<
|
|---|
| 409 | "[remote link " << remoteLink.toString() << "]" );
|
|---|
| 410 |
|
|---|
| 411 | AribaBaseMsg reply(remoteLocator,
|
|---|
| 412 | AribaBaseMsg::LINK_STATE_OPEN_REPLY,
|
|---|
| 413 | localLink,
|
|---|
| 414 | remoteLink);
|
|---|
| 415 |
|
|---|
| 416 | transport->sendMessage( &reply );
|
|---|
| 417 |
|
|---|
| 418 | //
|
|---|
| 419 | // the link is now open
|
|---|
| 420 | //
|
|---|
| 421 |
|
|---|
| 422 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
|---|
| 423 | i->onLinkUp( localLink, localLocator, remoteLocator );
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | } // LINK_STATE_OPEN_REQUEST
|
|---|
| 427 |
|
|---|
| 428 | //
|
|---|
| 429 | // handle link open replies
|
|---|
| 430 | //
|
|---|
| 431 |
|
|---|
| 432 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REPLY ){
|
|---|
| 433 |
|
|---|
| 434 | logging_debug( "received link open reply for a link we initiated" );
|
|---|
| 435 |
|
|---|
| 436 | // this is a reply to a link open request, so we have already
|
|---|
| 437 | // a link mapping and can now set the remote link to valid
|
|---|
| 438 | LinkDescriptor& linkDesc = queryLocalLink( spovmsg->getRemoteLink() );
|
|---|
| 439 |
|
|---|
| 440 | if (linkDesc.isUnspecified()) {
|
|---|
| 441 | logging_warn("Failed to find local link " << spovmsg->getRemoteLink().toString());
|
|---|
| 442 | return false;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | linkDesc.remoteLink = spovmsg->getLocalLink();
|
|---|
| 446 | linkDesc.linkup = true;
|
|---|
| 447 |
|
|---|
| 448 | logging_debug( "the link is now up with local link id " << spovmsg->getRemoteLink().toString() );
|
|---|
| 449 |
|
|---|
| 450 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
|---|
| 451 | i->onLinkUp( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | if( linkDesc.waitingmsg.size() > 0 ){
|
|---|
| 455 | logging_info( "sending out queued messages on link " << linkDesc.localLink.toString() );
|
|---|
| 456 |
|
|---|
| 457 | BOOST_FOREACH( Message* msg, linkDesc.waitingmsg ){
|
|---|
| 458 | sendMessage( linkDesc.localLink, msg );
|
|---|
| 459 | delete msg;
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | linkDesc.waitingmsg.clear();
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | } // LINK_STATE_OPEN_REPLY
|
|---|
| 466 |
|
|---|
| 467 | //
|
|---|
| 468 | // handle link close requests
|
|---|
| 469 | //
|
|---|
| 470 |
|
|---|
| 471 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_CLOSE_REQUEST ){
|
|---|
| 472 |
|
|---|
| 473 | const LinkID& localLink = spovmsg->getRemoteLink();
|
|---|
| 474 | logging_debug( "received link close request for link " << localLink.toString() );
|
|---|
| 475 |
|
|---|
| 476 | //
|
|---|
| 477 | // the link is closed immediately, we
|
|---|
| 478 | // don't need to send out a reply, so we
|
|---|
| 479 | // delete the mapping and inform
|
|---|
| 480 | //
|
|---|
| 481 |
|
|---|
| 482 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
|---|
| 483 | if (linkDesc.isUnspecified()) {
|
|---|
| 484 | logging_warn("Failed to find local link " << localLink.toString());
|
|---|
| 485 | return false;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
|---|
| 489 | i->onLinkDown( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | //
|
|---|
| 493 | // delete all pending messages for the link that has been closed
|
|---|
| 494 | //
|
|---|
| 495 |
|
|---|
| 496 | BOOST_FOREACH( Message* msg, linkDesc.waitingmsg ){
|
|---|
| 497 | delete msg;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | linkDesc.waitingmsg.clear();
|
|---|
| 501 |
|
|---|
| 502 | //
|
|---|
| 503 | // remove the link descriptor
|
|---|
| 504 | //
|
|---|
| 505 |
|
|---|
| 506 | removeLink( localLink );
|
|---|
| 507 |
|
|---|
| 508 | } // LINK_STATE_CLOSE_REQUEST
|
|---|
| 509 |
|
|---|
| 510 | //
|
|---|
| 511 | // handle locator updates
|
|---|
| 512 | //
|
|---|
| 513 |
|
|---|
| 514 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_UPDATE ){
|
|---|
| 515 |
|
|---|
| 516 | const LinkID& localLink = spovmsg->getRemoteLink();
|
|---|
| 517 | logging_debug( "received link update for link " << localLink.toString() );
|
|---|
| 518 |
|
|---|
| 519 | //
|
|---|
| 520 | // find the link description
|
|---|
| 521 | //
|
|---|
| 522 |
|
|---|
| 523 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
|---|
| 524 | if (linkDesc.isUnspecified()) {
|
|---|
| 525 | logging_warn("Failed to update local link " << localLink.toString());
|
|---|
| 526 | return false;
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | //
|
|---|
| 530 | // update the remote locator
|
|---|
| 531 | //
|
|---|
| 532 |
|
|---|
| 533 | const NetworkLocator* oldremote = linkDesc.remoteLocator;
|
|---|
| 534 | linkDesc.remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
|
|---|
| 535 |
|
|---|
| 536 | //
|
|---|
| 537 | // inform the listeners (local link has _not_ changed!)
|
|---|
| 538 | //
|
|---|
| 539 |
|
|---|
| 540 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
|---|
| 541 | i->onLinkChanged(
|
|---|
| 542 | linkDesc.localLink, // linkid
|
|---|
| 543 | linkDesc.localLocator, // old local
|
|---|
| 544 | linkDesc.localLocator, // new local
|
|---|
| 545 | oldremote, // old remote
|
|---|
| 546 | linkDesc.remoteLocator // new remote
|
|---|
| 547 | );
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | } // LINK_STATE_UPDATE
|
|---|
| 551 |
|
|---|
| 552 | return true;
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | void BaseCommunication::addLink( const LinkDescriptor& link ) {
|
|---|
| 556 | linkSet.push_back( link );
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | void BaseCommunication::removeLink( const LinkID& localLink ) {
|
|---|
| 560 |
|
|---|
| 561 | LinkSet::iterator i = linkSet.begin();
|
|---|
| 562 | LinkSet::iterator iend = linkSet.end();
|
|---|
| 563 |
|
|---|
| 564 | for( ; i != iend; i++){
|
|---|
| 565 | if( (*i).localLink != localLink) continue;
|
|---|
| 566 |
|
|---|
| 567 | BOOST_FOREACH( Message* msg, i->waitingmsg ){
|
|---|
| 568 | delete msg;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | i->waitingmsg.clear();
|
|---|
| 572 | linkSet.erase( i );
|
|---|
| 573 |
|
|---|
| 574 | break;
|
|---|
| 575 | }
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | BaseCommunication::LinkDescriptor& BaseCommunication::queryLocalLink( const LinkID& link ) const {
|
|---|
| 579 | for (int i=0; i<linkSet.size();i++)
|
|---|
| 580 | if (linkSet[i].localLink == link) return (LinkDescriptor&)linkSet[i];
|
|---|
| 581 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | BaseCommunication::LinkDescriptor& BaseCommunication::queryRemoteLink( const LinkID& link ) const {
|
|---|
| 585 | for (int i=0; i<linkSet.size();i++)
|
|---|
| 586 | if (linkSet[i].remoteLink == link) return (LinkDescriptor&)linkSet[i];
|
|---|
| 587 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | LinkIDs BaseCommunication::getLocalLinks( const EndpointDescriptor& ep ) const {
|
|---|
| 591 | LinkIDs ids;
|
|---|
| 592 |
|
|---|
| 593 | for (int i=0; i<linkSet.size(); i++){
|
|---|
| 594 | if( ep == EndpointDescriptor::UNSPECIFIED ){
|
|---|
| 595 | ids.push_back( linkSet[i].localLink );
|
|---|
| 596 | } else {
|
|---|
| 597 | if ( linkSet[i].remoteLocator == ep.locator )
|
|---|
| 598 | ids.push_back( linkSet[i].localLink );
|
|---|
| 599 | }
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | return ids;
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | void BaseCommunication::onNetworkChange(const NetworkChangeInterface::NetworkChangeInfo& info){
|
|---|
| 606 |
|
|---|
| 607 | #ifdef UNDERLAY_OMNET
|
|---|
| 608 |
|
|---|
| 609 | // we have no mobility support for simulations
|
|---|
| 610 | return
|
|---|
| 611 |
|
|---|
| 612 | #endif // UNDERLAY_OMNET
|
|---|
| 613 |
|
|---|
| 614 | //
|
|---|
| 615 | // we only care about address changes, not about interface changes
|
|---|
| 616 | // as address changes are triggered by interface changes, we are safe here
|
|---|
| 617 | //
|
|---|
| 618 |
|
|---|
| 619 | if( info.type != NetworkChangeInterface::EventTypeAddressNew &&
|
|---|
| 620 | info.type != NetworkChangeInterface::EventTypeAddressDelete ) return;
|
|---|
| 621 |
|
|---|
| 622 | logging_info( "base communication is handling network address changes" );
|
|---|
| 623 |
|
|---|
| 624 | //
|
|---|
| 625 | // get all now available addresses
|
|---|
| 626 | //
|
|---|
| 627 |
|
|---|
| 628 | NetworkInformation networkInformation;
|
|---|
| 629 | AddressInformation addressInformation;
|
|---|
| 630 |
|
|---|
| 631 | NetworkInterfaceList interfaces = networkInformation.getInterfaces();
|
|---|
| 632 | AddressList addresses;
|
|---|
| 633 |
|
|---|
| 634 | for( NetworkInterfaceList::iterator i = interfaces.begin(); i != interfaces.end(); i++ ){
|
|---|
| 635 | AddressList newaddr = addressInformation.getAddresses(*i);
|
|---|
| 636 | addresses.insert( addresses.end(), newaddr.begin(), newaddr.end() );
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | //
|
|---|
| 640 | // get current locators for the local endpoint
|
|---|
| 641 | // TODO: this code is dublicate of the ctor code!!! cleanup!
|
|---|
| 642 | //
|
|---|
| 643 |
|
|---|
| 644 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
|
|---|
| 645 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
|
|---|
| 646 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
|
|---|
| 647 |
|
|---|
| 648 | //
|
|---|
| 649 | // remember the old local endpoint, in case it changes
|
|---|
| 650 | //
|
|---|
| 651 |
|
|---|
| 652 | EndpointDescriptor oldLocalDescriptor( localDescriptor );
|
|---|
| 653 |
|
|---|
| 654 | //
|
|---|
| 655 | // look for local locators that we can use in communication
|
|---|
| 656 | //
|
|---|
| 657 | // choose the first locator that is not localhost
|
|---|
| 658 | //
|
|---|
| 659 |
|
|---|
| 660 | bool foundLocator = false;
|
|---|
| 661 | bool changedLocator = false;
|
|---|
| 662 |
|
|---|
| 663 | for( ; i != iend; i++){
|
|---|
| 664 | logging_debug( "local locator found " << (*i)->toString() );
|
|---|
| 665 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
|
|---|
| 666 |
|
|---|
| 667 | if( *ipv4locator != IPv4Locator::LOCALHOST &&
|
|---|
| 668 | *ipv4locator != IPv4Locator::ANY &&
|
|---|
| 669 | *ipv4locator != IPv4Locator::BROADCAST ){
|
|---|
| 670 |
|
|---|
| 671 | ipv4locator->setPort( listenport );
|
|---|
| 672 | changedLocator = *localDescriptor.locator != *ipv4locator;
|
|---|
| 673 | localDescriptor.locator = ipv4locator;
|
|---|
| 674 | logging_info( "binding to addr = " << ipv4locator->toString() );
|
|---|
| 675 | foundLocator = true;
|
|---|
| 676 | break;
|
|---|
| 677 | }
|
|---|
| 678 | } // for( ; i != iend; i++)
|
|---|
| 679 |
|
|---|
| 680 | //
|
|---|
| 681 | // if we found no locator, bind to localhost
|
|---|
| 682 | //
|
|---|
| 683 |
|
|---|
| 684 | if( !foundLocator ){
|
|---|
| 685 | changedLocator = *localDescriptor.locator != IPv4Locator::LOCALHOST;
|
|---|
| 686 | localDescriptor.locator = new IPv4Locator( IPv4Locator::LOCALHOST );
|
|---|
| 687 | ((IPv4Locator*)(localDescriptor.locator))->setPort( listenport );
|
|---|
| 688 | logging_info( "found no good local lcoator, binding to addr = " <<
|
|---|
| 689 | localDescriptor.locator->toString() );
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | //
|
|---|
| 693 | // if we have connections that have no more longer endpoints
|
|---|
| 694 | // close these. they will be automatically built up again.
|
|---|
| 695 | // also update the local locator in the linkset mapping
|
|---|
| 696 | //
|
|---|
| 697 |
|
|---|
| 698 | if( changedLocator ){
|
|---|
| 699 |
|
|---|
| 700 | logging_debug( "local endp locator has changed to " << localDescriptor.toString() <<
|
|---|
| 701 | ", resettings connections that end at old locator " <<
|
|---|
| 702 | oldLocalDescriptor.toString());
|
|---|
| 703 |
|
|---|
| 704 | LinkSet::iterator i = linkSet.begin();
|
|---|
| 705 | LinkSet::iterator iend = linkSet.end();
|
|---|
| 706 |
|
|---|
| 707 | for( ; i != iend; i++ ){
|
|---|
| 708 |
|
|---|
| 709 | logging_debug( "checking connection for locator change: " <<
|
|---|
| 710 | " local " << (*i).localLocator->toString() <<
|
|---|
| 711 | " old " << oldLocalDescriptor.locator->toString() );
|
|---|
| 712 |
|
|---|
| 713 | if( *((*i).localLocator) == *(oldLocalDescriptor.locator) ){
|
|---|
| 714 |
|
|---|
| 715 | logging_debug("terminating connection to " << (*i).remoteLocator->toString() );
|
|---|
| 716 | transport->terminate( oldLocalDescriptor.locator, (*i).remoteLocator );
|
|---|
| 717 |
|
|---|
| 718 | (*i).localLocator = localDescriptor.locator;
|
|---|
| 719 | }
|
|---|
| 720 | } // for( ; i != iend; i++ )
|
|---|
| 721 |
|
|---|
| 722 | // wait 500ms to give the sockets time to shut down
|
|---|
| 723 | usleep( 500000 );
|
|---|
| 724 |
|
|---|
| 725 | } else {
|
|---|
| 726 |
|
|---|
| 727 | logging_debug( "locator has not changed, not resetting connections" );
|
|---|
| 728 |
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | //
|
|---|
| 732 | // handle the connections that have no longer any
|
|---|
| 733 | // valid locator. send update messages with the new
|
|---|
| 734 | // locator, so the remote node updates its locator/link mapping
|
|---|
| 735 | //
|
|---|
| 736 |
|
|---|
| 737 | LinkSet::iterator iAffected = linkSet.begin();
|
|---|
| 738 | LinkSet::iterator endAffected = linkSet.end();
|
|---|
| 739 |
|
|---|
| 740 | for( ; iAffected != endAffected; iAffected++ ){
|
|---|
| 741 | LinkDescriptor descr = *iAffected;
|
|---|
| 742 | logging_debug( "sending out link locator update to " << descr.remoteLocator->toString() );
|
|---|
| 743 |
|
|---|
| 744 | AribaBaseMsg updateMsg( descr.remoteLocator,
|
|---|
| 745 | AribaBaseMsg::LINK_STATE_UPDATE,
|
|---|
| 746 | descr.localLink, descr.remoteLink );
|
|---|
| 747 |
|
|---|
| 748 | transport->sendMessage( &updateMsg );
|
|---|
| 749 | }
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | }} // namespace ariba, communication
|
|---|