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