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