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