| 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 | #include "networkinfo/AddressDiscovery.h"
|
---|
| 41 |
|
---|
| 42 | #ifdef UNDERLAY_OMNET
|
---|
| 43 | #include "ariba/communication/modules/transport/omnet/AribaOmnetModule.h"
|
---|
| 44 | #include "ariba/communication/modules/network/omnet/OmnetNetworkProtocol.h"
|
---|
| 45 | #include "ariba/utility/system/StartupWrapper.h"
|
---|
| 46 |
|
---|
| 47 | using ariba::communication::AribaOmnetModule;
|
---|
| 48 | using ariba::communication::OmnetNetworkProtocol;
|
---|
| 49 | using ariba::utility::StartupWrapper;
|
---|
| 50 | #endif
|
---|
| 51 |
|
---|
| 52 | namespace ariba {
|
---|
| 53 | namespace communication {
|
---|
| 54 |
|
---|
| 55 | use_logging_cpp(BaseCommunication);
|
---|
| 56 |
|
---|
| 57 | /// adds an endpoint to the list
|
---|
| 58 | void BaseCommunication::add_endpoint( const address_v* endpoint ) {
|
---|
| 59 | if (endpoint==NULL) return;
|
---|
| 60 | BOOST_FOREACH( endpoint_reference& ref, remote_endpoints ) {
|
---|
| 61 | if (ref.endpoint->type_id() == endpoint->type_id() && *ref.endpoint == *endpoint) {
|
---|
| 62 | ref.count++;
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | endpoint_reference ref;
|
---|
| 67 | ref.endpoint = endpoint->clone();
|
---|
| 68 | ref.count = 1;
|
---|
| 69 | remote_endpoints.push_back(ref);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /// removes an endpoint from the list
|
---|
| 73 | void BaseCommunication::remove_endpoint( const address_v* endpoint ) {
|
---|
| 74 | if (endpoint==NULL) return;
|
---|
| 75 | for (vector<endpoint_reference>::iterator i = remote_endpoints.begin();
|
---|
| 76 | i != remote_endpoints.end(); i++) {
|
---|
| 77 | if ((*i->endpoint).type_id() == endpoint->type_id() && (*i->endpoint) == *endpoint) {
|
---|
| 78 | i->count--;
|
---|
| 79 | if (i->count==0) {
|
---|
| 80 | logging_info("No more links to " << i->endpoint->to_string() << ": terminating transports!");
|
---|
| 81 | transport->terminate(i->endpoint);
|
---|
| 82 | delete i->endpoint;
|
---|
| 83 | remote_endpoints.erase(i);
|
---|
| 84 | }
|
---|
| 85 | return;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | BaseCommunication::BaseCommunication() {
|
---|
| 91 | this->transport = NULL;
|
---|
| 92 | this->started = false;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | BaseCommunication::~BaseCommunication(){
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void BaseCommunication::start() {
|
---|
| 99 | logging_info( "Starting up ..." );
|
---|
| 100 | currentSeqnum = 0;
|
---|
| 101 |
|
---|
| 102 | // creating transports
|
---|
| 103 | logging_info( "Creating transports ..." );
|
---|
| 104 |
|
---|
| 105 | #ifdef UNDERLAY_OMNET
|
---|
| 106 | AribaOmnetModule* module = StartupWrapper::getCurrentModule();
|
---|
| 107 | module->setServerPort( listenport );
|
---|
| 108 |
|
---|
| 109 | transport = module;
|
---|
| 110 | network = new OmnetNetworkProtocol( module );
|
---|
| 111 | #else
|
---|
| 112 | transport = new transport_peer( localDescriptor.getEndpoints() );
|
---|
| 113 | #endif
|
---|
| 114 |
|
---|
| 115 | logging_info( "Searching for local locators ..." );
|
---|
| 116 | if(localDescriptor.getEndpoints().to_string().length() == 0)
|
---|
| 117 | AddressDiscovery::discover_endpoints( localDescriptor.getEndpoints() );
|
---|
| 118 | logging_info( "Done. Local endpoints = " << localDescriptor.toString() );
|
---|
| 119 |
|
---|
| 120 | transport->register_listener( this );
|
---|
| 121 | transport->start();
|
---|
| 122 |
|
---|
| 123 | #ifndef UNDERLAY_OMNET
|
---|
| 124 | // bind to the network change detection
|
---|
| 125 | networkMonitor.registerNotification( this );
|
---|
| 126 | #endif
|
---|
| 127 |
|
---|
| 128 | // base comm startup done
|
---|
| 129 | started = true;
|
---|
| 130 | logging_info( "Started up." );
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | void BaseCommunication::stop() {
|
---|
| 134 | logging_info( "Stopping transports ..." );
|
---|
| 135 |
|
---|
| 136 | transport->stop();
|
---|
| 137 | delete transport;
|
---|
| 138 | started = false;
|
---|
| 139 |
|
---|
| 140 | logging_info( "Stopped." );
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | bool BaseCommunication::isStarted(){
|
---|
| 144 | return started;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /// Sets the endpoints
|
---|
| 148 | void BaseCommunication::setEndpoints( string& _endpoints ) {
|
---|
| 149 | localDescriptor.getEndpoints().assign(_endpoints);
|
---|
| 150 | logging_info("Setting local end-points: "
|
---|
| 151 | << localDescriptor.getEndpoints().to_string());
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | const LinkID BaseCommunication::establishLink(
|
---|
| 155 | const EndpointDescriptor& descriptor,
|
---|
| 156 | const LinkID& link_id,
|
---|
| 157 | const QoSParameterSet& qos,
|
---|
| 158 | const SecurityParameterSet& sec) {
|
---|
| 159 |
|
---|
| 160 | // copy link id
|
---|
| 161 | LinkID linkid = link_id;
|
---|
| 162 |
|
---|
| 163 | // debug
|
---|
| 164 | logging_debug( "Request to establish link" );
|
---|
| 165 |
|
---|
| 166 | // create link identifier
|
---|
| 167 | if (linkid.isUnspecified()) linkid = LinkID::create();
|
---|
| 168 |
|
---|
| 169 | // create link descriptor
|
---|
| 170 | logging_debug( "Creating new descriptor entry with local link id=" << linkid.toString() );
|
---|
| 171 | LinkDescriptor* ld = new LinkDescriptor();
|
---|
| 172 | ld->localLink = linkid;
|
---|
| 173 | addLink( ld );
|
---|
| 174 |
|
---|
| 175 | // send a message to request new link to remote
|
---|
| 176 | logging_debug( "Send messages with request to open link to " << descriptor.toString() );
|
---|
| 177 | AribaBaseMsg baseMsg( AribaBaseMsg::typeLinkRequest, linkid );
|
---|
| 178 | baseMsg.getLocalDescriptor() = localDescriptor;
|
---|
| 179 |
|
---|
| 180 | // serialize and send message
|
---|
| 181 | send( &baseMsg, descriptor );
|
---|
| 182 |
|
---|
| 183 | return linkid;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | void BaseCommunication::dropLink(const LinkID link) {
|
---|
| 187 |
|
---|
| 188 | logging_debug( "Starting to drop link " + link.toString() );
|
---|
| 189 |
|
---|
| 190 | // see if we have the link
|
---|
| 191 | LinkDescriptor& ld = queryLocalLink( link );
|
---|
| 192 | if( ld.isUnspecified() ) {
|
---|
| 193 | logging_error( "Don't know the link you want to drop "+ link.toString() );
|
---|
| 194 | return;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | // tell the registered listeners
|
---|
| 198 | BOOST_FOREACH( CommunicationEvents* i, eventListener ) {
|
---|
| 199 | i->onLinkDown( link, ld.localLocator, ld.remoteLocator );
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | // create message to drop the link
|
---|
| 203 | logging_debug( "Sending out link close request. for us, the link is closed now" );
|
---|
| 204 | AribaBaseMsg msg( AribaBaseMsg::typeLinkClose, ld.localLink, ld.remoteLink );
|
---|
| 205 |
|
---|
| 206 | // send message to drop the link
|
---|
| 207 | send( &msg, ld );
|
---|
| 208 |
|
---|
| 209 | // remove from map
|
---|
| 210 | removeLink(link);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | seqnum_t BaseCommunication::sendMessage( const LinkID lid, const Message* message) {
|
---|
| 214 |
|
---|
| 215 | logging_debug( "Sending out message to link " << lid.toString() );
|
---|
| 216 |
|
---|
| 217 | // query local link info
|
---|
| 218 | LinkDescriptor& ld = queryLocalLink(lid);
|
---|
| 219 | if( ld.isUnspecified() ){
|
---|
| 220 | logging_error( "Don't know the link with id " << lid.toString() );
|
---|
| 221 | return -1;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | // link not up-> error
|
---|
| 225 | if( !ld.up ) {
|
---|
| 226 | logging_error("Can not send on link " << lid.toString() << ": link not up");
|
---|
| 227 | return -1;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | // create message
|
---|
| 231 | AribaBaseMsg msg( AribaBaseMsg::typeData, ld.localLink, ld.remoteLink );
|
---|
| 232 |
|
---|
| 233 | // encapsulate the payload message
|
---|
| 234 | msg.encapsulate( const_cast<Message*>(message) );
|
---|
| 235 |
|
---|
| 236 | // send message
|
---|
| 237 | send( &msg, ld );
|
---|
| 238 |
|
---|
| 239 | // return sequence number
|
---|
| 240 | return ++currentSeqnum;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | const EndpointDescriptor& BaseCommunication::getEndpointDescriptor(const LinkID link) const {
|
---|
| 244 | if( link.isUnspecified() ){
|
---|
| 245 | return localDescriptor;
|
---|
| 246 | } else {
|
---|
| 247 | LinkDescriptor& linkDesc = queryLocalLink(link);
|
---|
| 248 | if (linkDesc.isUnspecified()) return EndpointDescriptor::UNSPECIFIED();
|
---|
| 249 | return linkDesc.remoteEndpoint;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | void BaseCommunication::registerEventListener(CommunicationEvents* _events){
|
---|
| 254 | if( eventListener.find( _events ) == eventListener.end() )
|
---|
| 255 | eventListener.insert( _events );
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | void BaseCommunication::unregisterEventListener(CommunicationEvents* _events){
|
---|
| 259 | EventListenerSet::iterator i = eventListener.find( _events );
|
---|
| 260 | if( i != eventListener.end() )
|
---|
| 261 | eventListener.erase( i );
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | SystemEventType TransportEvent("Transport");
|
---|
| 265 | SystemEventType MessageDispatchEvent("MessageDispatchEvent", TransportEvent );
|
---|
| 266 |
|
---|
| 267 | class DispatchMsg {
|
---|
| 268 | public:
|
---|
| 269 | address_v* local;
|
---|
| 270 | address_v* remote;
|
---|
| 271 | Message* message;
|
---|
| 272 | };
|
---|
| 273 |
|
---|
| 274 | /// called when a system event is emitted by system queue
|
---|
| 275 | void BaseCommunication::handleSystemEvent(const SystemEvent& event) {
|
---|
| 276 |
|
---|
| 277 | // dispatch received messages
|
---|
| 278 | if ( event.getType() == MessageDispatchEvent ){
|
---|
| 279 | logging_debug( "Forwarding message receiver" );
|
---|
| 280 | DispatchMsg* dmsg = event.getData<DispatchMsg>();
|
---|
| 281 | Message* msg = dmsg->message;
|
---|
| 282 | receiveMessage(msg, dmsg->local, dmsg->remote);
|
---|
| 283 | msg->dropPayload();
|
---|
| 284 | delete dmsg;
|
---|
| 285 | delete msg;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /// called when a message is received from transport_peer
|
---|
| 290 | void BaseCommunication::receive_message(transport_protocol* transport,
|
---|
| 291 | const address_vf local, const address_vf remote, const uint8_t* data,
|
---|
| 292 | size_t size) {
|
---|
| 293 |
|
---|
| 294 | // logging_debug( "Dispatching message" );
|
---|
| 295 |
|
---|
| 296 | // convert data
|
---|
| 297 | Data data_( const_cast<uint8_t*>(data), size * 8 );
|
---|
| 298 | DispatchMsg* dmsg = new DispatchMsg();
|
---|
| 299 |
|
---|
| 300 | Message* msg = new Message(data_);
|
---|
| 301 | dmsg->local = local->clone();
|
---|
| 302 | dmsg->remote = remote->clone();
|
---|
| 303 | dmsg->message = msg;
|
---|
| 304 |
|
---|
| 305 | SystemQueue::instance().scheduleEvent(
|
---|
| 306 | SystemEvent( this, MessageDispatchEvent, dmsg )
|
---|
| 307 | );
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | /// handles a message from the underlay transport
|
---|
| 311 | void BaseCommunication::receiveMessage(const Message* message,
|
---|
| 312 | const address_v* local, const address_v* remote ){
|
---|
| 313 |
|
---|
| 314 | /// decapsulate message
|
---|
| 315 | AribaBaseMsg* msg = ((Message*)message)->decapsulate<AribaBaseMsg>();
|
---|
| 316 | logging_debug( "Receiving message of type " << msg->getTypeString() );
|
---|
| 317 |
|
---|
| 318 | // handle message
|
---|
| 319 | switch (msg->getType()) {
|
---|
| 320 |
|
---|
| 321 | // ---------------------------------------------------------------------
|
---|
| 322 | // data message
|
---|
| 323 | // ---------------------------------------------------------------------
|
---|
| 324 | case AribaBaseMsg::typeData: {
|
---|
| 325 | logging_debug( "Received data message, forwarding to overlay" );
|
---|
| 326 | if( messageReceiver != NULL ) {
|
---|
| 327 | messageReceiver->receiveMessage(
|
---|
| 328 | msg, msg->getRemoteLink(), NodeID::UNSPECIFIED
|
---|
| 329 | );
|
---|
| 330 | }
|
---|
| 331 | break;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | // ---------------------------------------------------------------------
|
---|
| 335 | // handle link request from remote
|
---|
| 336 | // ---------------------------------------------------------------------
|
---|
| 337 | case AribaBaseMsg::typeLinkRequest: {
|
---|
| 338 | logging_debug( "Received link open request" );
|
---|
| 339 |
|
---|
| 340 | /// only answer the first request
|
---|
| 341 | if (!queryRemoteLink(msg->getLocalLink()).isUnspecified()) {
|
---|
| 342 | logging_debug("Link request already received. Ignore!");
|
---|
| 343 | break;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | /// create link ids
|
---|
| 347 | LinkID localLink = LinkID::create();
|
---|
| 348 | LinkID remoteLink = msg->getLocalLink();
|
---|
| 349 | logging_debug( "local=" << local->to_string()
|
---|
| 350 | << " remote=" << remote->to_string()
|
---|
| 351 | );
|
---|
| 352 |
|
---|
| 353 | // check if link creation is allowed by ALL listeners
|
---|
| 354 | bool allowlink = true;
|
---|
| 355 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 356 | allowlink &= i->onLinkRequest( localLink, local, remote );
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | // not allowed-> warn
|
---|
| 360 | if( !allowlink ){
|
---|
| 361 | logging_warn( "Overlay denied creation of link" );
|
---|
| 362 | return;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | // create descriptor
|
---|
| 366 | LinkDescriptor* ld = new LinkDescriptor();
|
---|
| 367 | ld->localLink = localLink;
|
---|
| 368 | ld->remoteLink = remoteLink;
|
---|
| 369 | ld->localLocator = local->clone();
|
---|
| 370 | ld->remoteLocator = remote->clone();
|
---|
| 371 | ld->remoteEndpoint = msg->getLocalDescriptor();
|
---|
| 372 | add_endpoint(ld->remoteLocator);
|
---|
| 373 |
|
---|
| 374 | // add layer 1-3 addresses
|
---|
| 375 | ld->remoteEndpoint.getEndpoints().add(
|
---|
| 376 | ld->remoteLocator, endpoint_set::Layer1_3 | endpoint_set::NoLoopback);
|
---|
| 377 | localDescriptor.getEndpoints().add(
|
---|
| 378 | local, endpoint_set::Layer1_3 | endpoint_set::NoLoopback
|
---|
| 379 | );
|
---|
| 380 |
|
---|
| 381 | // link is now up-> add it
|
---|
| 382 | ld->up = true;
|
---|
| 383 | addLink(ld);
|
---|
| 384 |
|
---|
| 385 | // link is up!
|
---|
| 386 | logging_debug( "Link (initiated from remote) is up with "
|
---|
| 387 | << "local(id=" << ld->localLink.toString() << ","
|
---|
| 388 | << "locator=" << ld->localLocator->to_string() << ") "
|
---|
| 389 | << "remote(id=" << ld->remoteLink.toString() << ", "
|
---|
| 390 | << "locator=" << ld->remoteLocator->to_string() << ")"
|
---|
| 391 | );
|
---|
| 392 |
|
---|
| 393 | // sending link request reply
|
---|
| 394 | logging_debug( "Sending link request reply with ids "
|
---|
| 395 | << "local=" << localLink.toString() << ", "
|
---|
| 396 | << "remote=" << remoteLink.toString() );
|
---|
| 397 | AribaBaseMsg reply( AribaBaseMsg::typeLinkReply, localLink, remoteLink );
|
---|
| 398 | reply.getLocalDescriptor() = localDescriptor;
|
---|
| 399 | reply.getRemoteDescriptor() = ld->remoteEndpoint;
|
---|
| 400 |
|
---|
| 401 | send( &reply, *ld );
|
---|
| 402 |
|
---|
| 403 | // inform listeners about new open link
|
---|
| 404 | BOOST_FOREACH( CommunicationEvents* i, eventListener ) {
|
---|
| 405 | i->onLinkUp( localLink, ld->localLocator, ld->remoteLocator);
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | // done
|
---|
| 409 | break;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | // ---------------------------------------------------------------------
|
---|
| 413 | // handle link request reply
|
---|
| 414 | // ---------------------------------------------------------------------
|
---|
| 415 | case AribaBaseMsg::typeLinkReply: {
|
---|
| 416 | logging_debug( "Received link open reply for a link we initiated" );
|
---|
| 417 |
|
---|
| 418 | // this is a reply to a link open request, so we have already
|
---|
| 419 | // a link mapping and can now set the remote link to valid
|
---|
| 420 | LinkDescriptor& ld = queryLocalLink( msg->getRemoteLink() );
|
---|
| 421 |
|
---|
| 422 | // no link found-> warn!
|
---|
| 423 | if (ld.isUnspecified()) {
|
---|
| 424 | logging_warn("Failed to find local link " << msg->getRemoteLink().toString());
|
---|
| 425 | return;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | // set remote locator and link id
|
---|
| 429 | ld.remoteLink = msg->getLocalLink();
|
---|
| 430 | ld.remoteLocator = remote->clone();
|
---|
| 431 | ld.remoteEndpoint.getEndpoints().add(
|
---|
| 432 | msg->getLocalDescriptor().getEndpoints(),
|
---|
| 433 | endpoint_set::Layer1_4
|
---|
| 434 | );
|
---|
| 435 |
|
---|
| 436 |
|
---|
| 437 | localDescriptor.getEndpoints().add(
|
---|
| 438 | msg->getRemoteDescriptor().getEndpoints(),
|
---|
| 439 | endpoint_set::Layer1_3
|
---|
| 440 | );
|
---|
| 441 | ld.up = true;
|
---|
| 442 | add_endpoint(ld.remoteLocator);
|
---|
| 443 |
|
---|
| 444 | logging_debug( "Link is now up with local id "
|
---|
| 445 | << ld.localLink.toString() << " and remote id "
|
---|
| 446 | << ld.remoteLink.toString() );
|
---|
| 447 |
|
---|
| 448 |
|
---|
| 449 | // inform lisneters about link up event
|
---|
| 450 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 451 | i->onLinkUp( ld.localLink, ld.localLocator, ld.remoteLocator );
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | // done
|
---|
| 455 | break;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | // ---------------------------------------------------------------------
|
---|
| 459 | // handle link close requests
|
---|
| 460 | // ---------------------------------------------------------------------
|
---|
| 461 | case AribaBaseMsg::typeLinkClose: {
|
---|
| 462 | // get remote link
|
---|
| 463 | const LinkID& localLink = msg->getRemoteLink();
|
---|
| 464 | logging_debug( "Received link close request for link " << localLink.toString() );
|
---|
| 465 |
|
---|
| 466 | // searching for link, not found-> warn
|
---|
| 467 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
| 468 | if (linkDesc.isUnspecified()) {
|
---|
| 469 | logging_warn("Failed to find local link " << localLink.toString());
|
---|
| 470 | return;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | // inform listeners
|
---|
| 474 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 475 | i->onLinkDown( linkDesc.localLink,
|
---|
| 476 | linkDesc.localLocator, linkDesc.remoteLocator );
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | // remove the link descriptor
|
---|
| 480 | removeLink( localLink );
|
---|
| 481 |
|
---|
| 482 | // done
|
---|
| 483 | break;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | // ---------------------------------------------------------------------
|
---|
| 487 | // handle link locator changes
|
---|
| 488 | // ---------------------------------------------------------------------
|
---|
| 489 | case AribaBaseMsg::typeLinkUpdate: {
|
---|
| 490 | const LinkID& localLink = msg->getRemoteLink();
|
---|
| 491 | logging_debug( "Received link update for link "
|
---|
| 492 | << localLink.toString() );
|
---|
| 493 |
|
---|
| 494 | // find the link description
|
---|
| 495 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
| 496 | if (linkDesc.isUnspecified()) {
|
---|
| 497 | logging_warn("Failed to update local link "
|
---|
| 498 | << localLink.toString());
|
---|
| 499 | return;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | // update the remote locator
|
---|
| 503 | const address_v* oldremote = linkDesc.remoteLocator;
|
---|
| 504 | linkDesc.remoteLocator = remote->clone();
|
---|
| 505 |
|
---|
| 506 | // inform the listeners (local link has _not_ changed!)
|
---|
| 507 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
| 508 | i->onLinkChanged(
|
---|
| 509 | linkDesc.localLink, // linkid
|
---|
| 510 | linkDesc.localLocator, // old local
|
---|
| 511 | linkDesc.localLocator, // new local
|
---|
| 512 | oldremote, // old remote
|
---|
| 513 | linkDesc.remoteLocator // new remote
|
---|
| 514 | );
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | // done
|
---|
| 518 | break;
|
---|
| 519 | }
|
---|
| 520 | }
|
---|
| 521 | delete msg;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | /// add a newly allocated link to the set of links
|
---|
| 525 | void BaseCommunication::addLink( LinkDescriptor* link ) {
|
---|
| 526 | linkSet.push_back( link );
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | /// remove a link from set
|
---|
| 530 | void BaseCommunication::removeLink( const LinkID& localLink ) {
|
---|
| 531 | for(LinkSet::iterator i=linkSet.begin(); i != linkSet.end(); i++){
|
---|
| 532 | if( (*i)->localLink != localLink) continue;
|
---|
| 533 | remove_endpoint((*i)->remoteLocator);
|
---|
| 534 | delete *i;
|
---|
| 535 | linkSet.erase( i );
|
---|
| 536 | break;
|
---|
| 537 | }
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | /// query a descriptor by local link id
|
---|
| 541 | BaseCommunication::LinkDescriptor& BaseCommunication::queryLocalLink( const LinkID& link ) const {
|
---|
| 542 | for (size_t i=0; i<linkSet.size();i++)
|
---|
| 543 | if (linkSet[i]->localLink == link) return (LinkDescriptor&)*linkSet[i];
|
---|
| 544 |
|
---|
| 545 | return LinkDescriptor::UNSPECIFIED();
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | /// query a descriptor by remote link id
|
---|
| 549 | BaseCommunication::LinkDescriptor& BaseCommunication::queryRemoteLink( const LinkID& link ) const {
|
---|
| 550 | for (size_t i=0; i<linkSet.size();i++)
|
---|
| 551 | if (linkSet[i]->remoteLink == link) return (LinkDescriptor&)*linkSet[i];
|
---|
| 552 |
|
---|
| 553 | return LinkDescriptor::UNSPECIFIED();
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | LinkIDs BaseCommunication::getLocalLinks( const address_v* addr ) const {
|
---|
| 557 | LinkIDs ids;
|
---|
| 558 | for (size_t i=0; i<linkSet.size(); i++){
|
---|
| 559 | if( addr == NULL ){
|
---|
| 560 | ids.push_back( linkSet[i]->localLink );
|
---|
| 561 | } else {
|
---|
| 562 | if ( *linkSet[i]->remoteLocator == *addr )
|
---|
| 563 | ids.push_back( linkSet[i]->localLink );
|
---|
| 564 | }
|
---|
| 565 | }
|
---|
| 566 | return ids;
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | void BaseCommunication::onNetworkChange(const NetworkChangeInterface::NetworkChangeInfo& info){
|
---|
| 570 |
|
---|
| 571 | #ifdef UNDERLAY_OMNET
|
---|
| 572 |
|
---|
| 573 | // we have no mobility support for simulations
|
---|
| 574 | return
|
---|
| 575 |
|
---|
| 576 | #endif // UNDERLAY_OMNET
|
---|
| 577 |
|
---|
| 578 | /*- disabled!
|
---|
| 579 |
|
---|
| 580 | // we only care about address changes, not about interface changes
|
---|
| 581 | // as address changes are triggered by interface changes, we are safe here
|
---|
| 582 | if( info.type != NetworkChangeInterface::EventTypeAddressNew &&
|
---|
| 583 | info.type != NetworkChangeInterface::EventTypeAddressDelete ) return;
|
---|
| 584 |
|
---|
| 585 | logging_info( "base communication is handling network address changes" );
|
---|
| 586 |
|
---|
| 587 | // get all now available addresses
|
---|
| 588 | NetworkInformation networkInformation;
|
---|
| 589 | AddressInformation addressInformation;
|
---|
| 590 |
|
---|
| 591 | NetworkInterfaceList interfaces = networkInformation.getInterfaces();
|
---|
| 592 | AddressList addresses;
|
---|
| 593 |
|
---|
| 594 | for( NetworkInterfaceList::iterator i = interfaces.begin(); i != interfaces.end(); i++ ){
|
---|
| 595 | AddressList newaddr = addressInformation.getAddresses(*i);
|
---|
| 596 | addresses.insert( addresses.end(), newaddr.begin(), newaddr.end() );
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | //
|
---|
| 600 | // get current locators for the local endpoint
|
---|
| 601 | // TODO: this code is dublicate of the ctor code!!! cleanup!
|
---|
| 602 | //
|
---|
| 603 |
|
---|
| 604 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
|
---|
| 605 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
|
---|
| 606 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
|
---|
| 607 |
|
---|
| 608 | //
|
---|
| 609 | // remember the old local endpoint, in case it changes
|
---|
| 610 | //
|
---|
| 611 |
|
---|
| 612 | EndpointDescriptor oldLocalDescriptor( localDescriptor );
|
---|
| 613 |
|
---|
| 614 | //
|
---|
| 615 | // look for local locators that we can use in communication
|
---|
| 616 | //
|
---|
| 617 | // choose the first locator that is not localhost
|
---|
| 618 | //
|
---|
| 619 |
|
---|
| 620 | bool foundLocator = false;
|
---|
| 621 | bool changedLocator = false;
|
---|
| 622 |
|
---|
| 623 | for( ; i != iend; i++){
|
---|
| 624 | logging_debug( "local locator found " << (*i)->toString() );
|
---|
| 625 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
|
---|
| 626 |
|
---|
| 627 | if( *ipv4locator != IPv4Locator::LOCALHOST &&
|
---|
| 628 | *ipv4locator != IPv4Locator::ANY &&
|
---|
| 629 | *ipv4locator != IPv4Locator::BROADCAST ){
|
---|
| 630 |
|
---|
| 631 | ipv4locator->setPort( listenport );
|
---|
| 632 | changedLocator = *localDescriptor.locator != *ipv4locator;
|
---|
| 633 | localDescriptor.locator = ipv4locator;
|
---|
| 634 | logging_info( "binding to addr = " << ipv4locator->toString() );
|
---|
| 635 | foundLocator = true;
|
---|
| 636 | break;
|
---|
| 637 | }
|
---|
| 638 | } // for( ; i != iend; i++)
|
---|
| 639 |
|
---|
| 640 | //
|
---|
| 641 | // if we found no locator, bind to localhost
|
---|
| 642 | //
|
---|
| 643 |
|
---|
| 644 | if( !foundLocator ){
|
---|
| 645 | changedLocator = *localDescriptor.locator != IPv4Locator::LOCALHOST;
|
---|
| 646 | localDescriptor.locator = new IPv4Locator( IPv4Locator::LOCALHOST );
|
---|
| 647 | ((IPv4Locator*)(localDescriptor.locator))->setPort( listenport );
|
---|
| 648 | logging_info( "found no good local lcoator, binding to addr = " <<
|
---|
| 649 | localDescriptor.locator->toString() );
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | //
|
---|
| 653 | // if we have connections that have no more longer endpoints
|
---|
| 654 | // close these. they will be automatically built up again.
|
---|
| 655 | // also update the local locator in the linkset mapping
|
---|
| 656 | //
|
---|
| 657 |
|
---|
| 658 | if( changedLocator ){
|
---|
| 659 |
|
---|
| 660 | logging_debug( "local endp locator has changed to " << localDescriptor.toString() <<
|
---|
| 661 | ", resettings connections that end at old locator " <<
|
---|
| 662 | oldLocalDescriptor.toString());
|
---|
| 663 |
|
---|
| 664 | LinkSet::iterator i = linkSet.begin();
|
---|
| 665 | LinkSet::iterator iend = linkSet.end();
|
---|
| 666 |
|
---|
| 667 | for( ; i != iend; i++ ){
|
---|
| 668 |
|
---|
| 669 | logging_debug( "checking connection for locator change: " <<
|
---|
| 670 | " local " << (*i).localLocator->toString() <<
|
---|
| 671 | " old " << oldLocalDescriptor.locator->toString() );
|
---|
| 672 |
|
---|
| 673 | if( *((*i).localLocator) == *(oldLocalDescriptor.locator) ){
|
---|
| 674 |
|
---|
| 675 | logging_debug("terminating connection to " << (*i).remoteLocator->toString() );
|
---|
| 676 | transport->terminate( oldLocalDescriptor.locator, (*i).remoteLocator );
|
---|
| 677 |
|
---|
| 678 | (*i).localLocator = localDescriptor.locator;
|
---|
| 679 | }
|
---|
| 680 | } // for( ; i != iend; i++ )
|
---|
| 681 |
|
---|
| 682 | // wait 500ms to give the sockets time to shut down
|
---|
| 683 | usleep( 500000 );
|
---|
| 684 |
|
---|
| 685 | } else {
|
---|
| 686 |
|
---|
| 687 | logging_debug( "locator has not changed, not resetting connections" );
|
---|
| 688 |
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | //
|
---|
| 692 | // handle the connections that have no longer any
|
---|
| 693 | // valid locator. send update messages with the new
|
---|
| 694 | // locator, so the remote node updates its locator/link mapping
|
---|
| 695 | //
|
---|
| 696 |
|
---|
| 697 | LinkSet::iterator iAffected = linkSet.begin();
|
---|
| 698 | LinkSet::iterator endAffected = linkSet.end();
|
---|
| 699 |
|
---|
| 700 | for( ; iAffected != endAffected; iAffected++ ){
|
---|
| 701 | LinkDescriptor descr = *iAffected;
|
---|
| 702 | logging_debug( "sending out link locator update to " << descr.remoteLocator->toString() );
|
---|
| 703 |
|
---|
| 704 | AribaBaseMsg updateMsg( descr.remoteLocator,
|
---|
| 705 | AribaBaseMsg::LINK_STATE_UPDATE,
|
---|
| 706 | descr.localLink, descr.remoteLink );
|
---|
| 707 |
|
---|
| 708 | transport->sendMessage( &updateMsg );
|
---|
| 709 | }
|
---|
| 710 | */
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | /// sends a message to all end-points in the end-point descriptor
|
---|
| 714 | void BaseCommunication::send(Message* message, const EndpointDescriptor& endpoint) {
|
---|
| 715 | Data data = data_serialize( message, DEFAULT_V );
|
---|
| 716 | transport->send( endpoint.getEndpoints(), data.getBuffer(), data.getLength() / 8);
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | /// sends a message to the remote locator inside the link descriptor
|
---|
| 720 | void BaseCommunication::send(Message* message, const LinkDescriptor& desc) {
|
---|
| 721 | if (desc.remoteLocator==NULL) return;
|
---|
| 722 | Data data = data_serialize( message, DEFAULT_V );
|
---|
| 723 | transport->send( desc.remoteLocator, data.getBuffer(), data.getLength() / 8);
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | }} // namespace ariba, communication
|
---|