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->local;
|
---|
297 | delete dmsg->remote;
|
---|
298 | delete msg;
|
---|
299 | delete dmsg;
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /// called when a message is received from transport_peer
|
---|
304 | void BaseCommunication::receive_message(transport_protocol* transport,
|
---|
305 | const address_vf local, const address_vf remote, const uint8_t* data,
|
---|
306 | size_t size) {
|
---|
307 |
|
---|
308 | // logging_debug( "Dispatching message" );
|
---|
309 |
|
---|
310 | // convert data
|
---|
311 | Data data_( const_cast<uint8_t*>(data), size * 8 );
|
---|
312 | DispatchMsg* dmsg = new DispatchMsg();
|
---|
313 |
|
---|
314 | Message* msg = new Message(data_);
|
---|
315 | dmsg->local = local->clone();
|
---|
316 | dmsg->remote = remote->clone();
|
---|
317 | dmsg->message = msg;
|
---|
318 |
|
---|
319 | SystemQueue::instance().scheduleEvent(
|
---|
320 | SystemEvent( this, MessageDispatchEvent, dmsg )
|
---|
321 | );
|
---|
322 | }
|
---|
323 |
|
---|
324 | /// handles a message from the underlay transport
|
---|
325 | void BaseCommunication::receiveMessage(const Message* message,
|
---|
326 | const address_v* local, const address_v* remote ){
|
---|
327 |
|
---|
328 | /// decapsulate message
|
---|
329 | AribaBaseMsg* msg = ((Message*)message)->decapsulate<AribaBaseMsg>();
|
---|
330 | logging_debug( "Receiving message of type " << msg->getTypeString() );
|
---|
331 |
|
---|
332 | // handle message
|
---|
333 | switch (msg->getType()) {
|
---|
334 |
|
---|
335 | // ---------------------------------------------------------------------
|
---|
336 | // data message
|
---|
337 | // ---------------------------------------------------------------------
|
---|
338 | case AribaBaseMsg::typeData: {
|
---|
339 | logging_debug( "Received data message, forwarding to overlay" );
|
---|
340 | if( messageReceiver != NULL ) {
|
---|
341 | messageReceiver->receiveMessage(
|
---|
342 | msg, msg->getRemoteLink(), NodeID::UNSPECIFIED
|
---|
343 | );
|
---|
344 | }
|
---|
345 | break;
|
---|
346 | }
|
---|
347 |
|
---|
348 | // ---------------------------------------------------------------------
|
---|
349 | // handle link request from remote
|
---|
350 | // ---------------------------------------------------------------------
|
---|
351 | case AribaBaseMsg::typeLinkRequest: {
|
---|
352 | logging_debug( "Received link open request" );
|
---|
353 |
|
---|
354 | /// not the correct peer id-> skip request
|
---|
355 | if (!msg->getRemoteDescriptor().getPeerId().isUnspecified()
|
---|
356 | && msg->getRemoteDescriptor().getPeerId() != localDescriptor.getPeerId()) {
|
---|
357 | logging_info("Received link request for "
|
---|
358 | << msg->getRemoteDescriptor().getPeerId().toString()
|
---|
359 | << "but i'm "
|
---|
360 | << localDescriptor.getPeerId()
|
---|
361 | << ": Ignoring!");
|
---|
362 | break;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /// only answer the first request
|
---|
366 | if (!queryRemoteLink(msg->getLocalLink()).isUnspecified()) {
|
---|
367 | logging_debug("Link request already received. Ignore!");
|
---|
368 | break;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /// create link ids
|
---|
372 | LinkID localLink = LinkID::create();
|
---|
373 | LinkID remoteLink = msg->getLocalLink();
|
---|
374 | logging_debug( "local=" << local->to_string()
|
---|
375 | << " remote=" << remote->to_string()
|
---|
376 | );
|
---|
377 |
|
---|
378 | // check if link creation is allowed by ALL listeners
|
---|
379 | bool allowlink = true;
|
---|
380 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
381 | allowlink &= i->onLinkRequest( localLink, local, remote );
|
---|
382 | }
|
---|
383 |
|
---|
384 | // not allowed-> warn
|
---|
385 | if( !allowlink ){
|
---|
386 | logging_warn( "Overlay denied creation of link" );
|
---|
387 | return;
|
---|
388 | }
|
---|
389 |
|
---|
390 | // create descriptor
|
---|
391 | LinkDescriptor* ld = new LinkDescriptor();
|
---|
392 | ld->localLink = localLink;
|
---|
393 | ld->remoteLink = remoteLink;
|
---|
394 | ld->localLocator = local->clone();
|
---|
395 | ld->remoteLocator = remote->clone();
|
---|
396 | ld->remoteEndpoint = msg->getLocalDescriptor();
|
---|
397 | add_endpoint(ld->remoteLocator);
|
---|
398 |
|
---|
399 | // add layer 1-3 addresses
|
---|
400 | ld->remoteEndpoint.getEndpoints().add(
|
---|
401 | ld->remoteLocator, endpoint_set::Layer1_3 | endpoint_set::NoLoopback);
|
---|
402 | localDescriptor.getEndpoints().add(
|
---|
403 | local, endpoint_set::Layer1_3 | endpoint_set::NoLoopback);
|
---|
404 |
|
---|
405 | // link is now up-> add it
|
---|
406 | ld->up = true;
|
---|
407 | addLink(ld);
|
---|
408 |
|
---|
409 | // link is up!
|
---|
410 | logging_debug( "Link (initiated from remote) is up with "
|
---|
411 | << "local(id=" << ld->localLink.toString() << ","
|
---|
412 | << "locator=" << ld->localLocator->to_string() << ") "
|
---|
413 | << "remote(id=" << ld->remoteLink.toString() << ", "
|
---|
414 | << "locator=" << ld->remoteLocator->to_string() << ")"
|
---|
415 | );
|
---|
416 |
|
---|
417 | // sending link request reply
|
---|
418 | logging_debug( "Sending link request reply with ids "
|
---|
419 | << "local=" << localLink.toString() << ", "
|
---|
420 | << "remote=" << remoteLink.toString() );
|
---|
421 | AribaBaseMsg reply( AribaBaseMsg::typeLinkReply, localLink, remoteLink );
|
---|
422 | reply.getLocalDescriptor() = localDescriptor;
|
---|
423 | reply.getRemoteDescriptor() = ld->remoteEndpoint;
|
---|
424 |
|
---|
425 | send( &reply, *ld );
|
---|
426 |
|
---|
427 | // inform listeners about new open link
|
---|
428 | BOOST_FOREACH( CommunicationEvents* i, eventListener ) {
|
---|
429 | i->onLinkUp( localLink, ld->localLocator, ld->remoteLocator);
|
---|
430 | }
|
---|
431 |
|
---|
432 | // done
|
---|
433 | break;
|
---|
434 | }
|
---|
435 |
|
---|
436 | // ---------------------------------------------------------------------
|
---|
437 | // handle link request reply
|
---|
438 | // ---------------------------------------------------------------------
|
---|
439 | case AribaBaseMsg::typeLinkReply: {
|
---|
440 | logging_debug( "Received link open reply for a link we initiated" );
|
---|
441 |
|
---|
442 | // this is a reply to a link open request, so we have already
|
---|
443 | // a link mapping and can now set the remote link to valid
|
---|
444 | LinkDescriptor& ld = queryLocalLink( msg->getRemoteLink() );
|
---|
445 |
|
---|
446 | // no link found-> warn!
|
---|
447 | if (ld.isUnspecified()) {
|
---|
448 | logging_warn("Failed to find local link " << msg->getRemoteLink().toString());
|
---|
449 | return;
|
---|
450 | }
|
---|
451 |
|
---|
452 | // set remote locator and link id
|
---|
453 | ld.remoteLink = msg->getLocalLink();
|
---|
454 | ld.remoteLocator = remote->clone();
|
---|
455 | ld.remoteEndpoint.getEndpoints().add(
|
---|
456 | msg->getLocalDescriptor().getEndpoints(),
|
---|
457 | endpoint_set::Layer1_4
|
---|
458 | );
|
---|
459 |
|
---|
460 |
|
---|
461 | localDescriptor.getEndpoints().add(
|
---|
462 | msg->getRemoteDescriptor().getEndpoints(),
|
---|
463 | endpoint_set::Layer1_3
|
---|
464 | );
|
---|
465 | ld.up = true;
|
---|
466 | add_endpoint(ld.remoteLocator);
|
---|
467 |
|
---|
468 | logging_debug( "Link is now up with local id "
|
---|
469 | << ld.localLink.toString() << " and remote id "
|
---|
470 | << ld.remoteLink.toString() );
|
---|
471 |
|
---|
472 |
|
---|
473 | // inform lisneters about link up event
|
---|
474 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
475 | i->onLinkUp( ld.localLink, ld.localLocator, ld.remoteLocator );
|
---|
476 | }
|
---|
477 |
|
---|
478 | // done
|
---|
479 | break;
|
---|
480 | }
|
---|
481 |
|
---|
482 | // ---------------------------------------------------------------------
|
---|
483 | // handle link close requests
|
---|
484 | // ---------------------------------------------------------------------
|
---|
485 | case AribaBaseMsg::typeLinkClose: {
|
---|
486 | // get remote link
|
---|
487 | const LinkID& localLink = msg->getRemoteLink();
|
---|
488 | logging_debug( "Received link close request for link " << localLink.toString() );
|
---|
489 |
|
---|
490 | // searching for link, not found-> warn
|
---|
491 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
492 | if (linkDesc.isUnspecified()) {
|
---|
493 | logging_warn("Failed to find local link " << localLink.toString());
|
---|
494 | return;
|
---|
495 | }
|
---|
496 |
|
---|
497 | // inform listeners
|
---|
498 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
499 | i->onLinkDown( linkDesc.localLink,
|
---|
500 | linkDesc.localLocator, linkDesc.remoteLocator );
|
---|
501 | }
|
---|
502 |
|
---|
503 | // remove the link descriptor
|
---|
504 | removeLink( localLink );
|
---|
505 |
|
---|
506 | // done
|
---|
507 | break;
|
---|
508 | }
|
---|
509 |
|
---|
510 | // ---------------------------------------------------------------------
|
---|
511 | // handle link locator changes
|
---|
512 | // ---------------------------------------------------------------------
|
---|
513 | case AribaBaseMsg::typeLinkUpdate: {
|
---|
514 | const LinkID& localLink = msg->getRemoteLink();
|
---|
515 | logging_debug( "Received link update for link "
|
---|
516 | << localLink.toString() );
|
---|
517 |
|
---|
518 | // find the link description
|
---|
519 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
520 | if (linkDesc.isUnspecified()) {
|
---|
521 | logging_warn("Failed to update local link "
|
---|
522 | << localLink.toString());
|
---|
523 | return;
|
---|
524 | }
|
---|
525 |
|
---|
526 | // update the remote locator
|
---|
527 | const address_v* oldremote = linkDesc.remoteLocator;
|
---|
528 | linkDesc.remoteLocator = remote->clone();
|
---|
529 |
|
---|
530 | // inform the listeners (local link has _not_ changed!)
|
---|
531 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
532 | i->onLinkChanged(
|
---|
533 | linkDesc.localLink, // linkid
|
---|
534 | linkDesc.localLocator, // old local
|
---|
535 | linkDesc.localLocator, // new local
|
---|
536 | oldremote, // old remote
|
---|
537 | linkDesc.remoteLocator // new remote
|
---|
538 | );
|
---|
539 | }
|
---|
540 |
|
---|
541 | // done
|
---|
542 | break;
|
---|
543 | }
|
---|
544 | }
|
---|
545 | delete msg;
|
---|
546 | }
|
---|
547 |
|
---|
548 | /// add a newly allocated link to the set of links
|
---|
549 | void BaseCommunication::addLink( LinkDescriptor* link ) {
|
---|
550 | linkSet.push_back( link );
|
---|
551 | }
|
---|
552 |
|
---|
553 | /// remove a link from set
|
---|
554 | void BaseCommunication::removeLink( const LinkID& localLink ) {
|
---|
555 | for(LinkSet::iterator i=linkSet.begin(); i != linkSet.end(); i++){
|
---|
556 | if( (*i)->localLink != localLink) continue;
|
---|
557 | remove_endpoint((*i)->remoteLocator);
|
---|
558 | delete *i;
|
---|
559 | linkSet.erase( i );
|
---|
560 | break;
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 | /// query a descriptor by local link id
|
---|
565 | BaseCommunication::LinkDescriptor& BaseCommunication::queryLocalLink( const LinkID& link ) const {
|
---|
566 | for (size_t i=0; i<linkSet.size();i++)
|
---|
567 | if (linkSet[i]->localLink == link) return (LinkDescriptor&)*linkSet[i];
|
---|
568 |
|
---|
569 | return LinkDescriptor::UNSPECIFIED();
|
---|
570 | }
|
---|
571 |
|
---|
572 | /// query a descriptor by remote link id
|
---|
573 | BaseCommunication::LinkDescriptor& BaseCommunication::queryRemoteLink( const LinkID& link ) const {
|
---|
574 | for (size_t i=0; i<linkSet.size();i++)
|
---|
575 | if (linkSet[i]->remoteLink == link) return (LinkDescriptor&)*linkSet[i];
|
---|
576 |
|
---|
577 | return LinkDescriptor::UNSPECIFIED();
|
---|
578 | }
|
---|
579 |
|
---|
580 | LinkIDs BaseCommunication::getLocalLinks( const address_v* addr ) const {
|
---|
581 | LinkIDs ids;
|
---|
582 | for (size_t i=0; i<linkSet.size(); i++){
|
---|
583 | if( addr == NULL ){
|
---|
584 | ids.push_back( linkSet[i]->localLink );
|
---|
585 | } else {
|
---|
586 | if ( *linkSet[i]->remoteLocator == *addr )
|
---|
587 | ids.push_back( linkSet[i]->localLink );
|
---|
588 | }
|
---|
589 | }
|
---|
590 | return ids;
|
---|
591 | }
|
---|
592 |
|
---|
593 | void BaseCommunication::onNetworkChange(const NetworkChangeInterface::NetworkChangeInfo& info){
|
---|
594 |
|
---|
595 | #ifdef UNDERLAY_OMNET
|
---|
596 |
|
---|
597 | // we have no mobility support for simulations
|
---|
598 | return
|
---|
599 |
|
---|
600 | #endif // UNDERLAY_OMNET
|
---|
601 |
|
---|
602 | /*- disabled!
|
---|
603 |
|
---|
604 | // we only care about address changes, not about interface changes
|
---|
605 | // as address changes are triggered by interface changes, we are safe here
|
---|
606 | if( info.type != NetworkChangeInterface::EventTypeAddressNew &&
|
---|
607 | info.type != NetworkChangeInterface::EventTypeAddressDelete ) return;
|
---|
608 |
|
---|
609 | logging_info( "base communication is handling network address changes" );
|
---|
610 |
|
---|
611 | // get all now available addresses
|
---|
612 | NetworkInformation networkInformation;
|
---|
613 | AddressInformation addressInformation;
|
---|
614 |
|
---|
615 | NetworkInterfaceList interfaces = networkInformation.getInterfaces();
|
---|
616 | AddressList addresses;
|
---|
617 |
|
---|
618 | for( NetworkInterfaceList::iterator i = interfaces.begin(); i != interfaces.end(); i++ ){
|
---|
619 | AddressList newaddr = addressInformation.getAddresses(*i);
|
---|
620 | addresses.insert( addresses.end(), newaddr.begin(), newaddr.end() );
|
---|
621 | }
|
---|
622 |
|
---|
623 | //
|
---|
624 | // get current locators for the local endpoint
|
---|
625 | // TODO: this code is dublicate of the ctor code!!! cleanup!
|
---|
626 | //
|
---|
627 |
|
---|
628 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
|
---|
629 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
|
---|
630 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
|
---|
631 |
|
---|
632 | //
|
---|
633 | // remember the old local endpoint, in case it changes
|
---|
634 | //
|
---|
635 |
|
---|
636 | EndpointDescriptor oldLocalDescriptor( localDescriptor );
|
---|
637 |
|
---|
638 | //
|
---|
639 | // look for local locators that we can use in communication
|
---|
640 | //
|
---|
641 | // choose the first locator that is not localhost
|
---|
642 | //
|
---|
643 |
|
---|
644 | bool foundLocator = false;
|
---|
645 | bool changedLocator = false;
|
---|
646 |
|
---|
647 | for( ; i != iend; i++){
|
---|
648 | logging_debug( "local locator found " << (*i)->toString() );
|
---|
649 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
|
---|
650 |
|
---|
651 | if( *ipv4locator != IPv4Locator::LOCALHOST &&
|
---|
652 | *ipv4locator != IPv4Locator::ANY &&
|
---|
653 | *ipv4locator != IPv4Locator::BROADCAST ){
|
---|
654 |
|
---|
655 | ipv4locator->setPort( listenport );
|
---|
656 | changedLocator = *localDescriptor.locator != *ipv4locator;
|
---|
657 | localDescriptor.locator = ipv4locator;
|
---|
658 | logging_info( "binding to addr = " << ipv4locator->toString() );
|
---|
659 | foundLocator = true;
|
---|
660 | break;
|
---|
661 | }
|
---|
662 | } // for( ; i != iend; i++)
|
---|
663 |
|
---|
664 | //
|
---|
665 | // if we found no locator, bind to localhost
|
---|
666 | //
|
---|
667 |
|
---|
668 | if( !foundLocator ){
|
---|
669 | changedLocator = *localDescriptor.locator != IPv4Locator::LOCALHOST;
|
---|
670 | localDescriptor.locator = new IPv4Locator( IPv4Locator::LOCALHOST );
|
---|
671 | ((IPv4Locator*)(localDescriptor.locator))->setPort( listenport );
|
---|
672 | logging_info( "found no good local lcoator, binding to addr = " <<
|
---|
673 | localDescriptor.locator->toString() );
|
---|
674 | }
|
---|
675 |
|
---|
676 | //
|
---|
677 | // if we have connections that have no more longer endpoints
|
---|
678 | // close these. they will be automatically built up again.
|
---|
679 | // also update the local locator in the linkset mapping
|
---|
680 | //
|
---|
681 |
|
---|
682 | if( changedLocator ){
|
---|
683 |
|
---|
684 | logging_debug( "local endp locator has changed to " << localDescriptor.toString() <<
|
---|
685 | ", resettings connections that end at old locator " <<
|
---|
686 | oldLocalDescriptor.toString());
|
---|
687 |
|
---|
688 | LinkSet::iterator i = linkSet.begin();
|
---|
689 | LinkSet::iterator iend = linkSet.end();
|
---|
690 |
|
---|
691 | for( ; i != iend; i++ ){
|
---|
692 |
|
---|
693 | logging_debug( "checking connection for locator change: " <<
|
---|
694 | " local " << (*i).localLocator->toString() <<
|
---|
695 | " old " << oldLocalDescriptor.locator->toString() );
|
---|
696 |
|
---|
697 | if( *((*i).localLocator) == *(oldLocalDescriptor.locator) ){
|
---|
698 |
|
---|
699 | logging_debug("terminating connection to " << (*i).remoteLocator->toString() );
|
---|
700 | transport->terminate( oldLocalDescriptor.locator, (*i).remoteLocator );
|
---|
701 |
|
---|
702 | (*i).localLocator = localDescriptor.locator;
|
---|
703 | }
|
---|
704 | } // for( ; i != iend; i++ )
|
---|
705 |
|
---|
706 | // wait 500ms to give the sockets time to shut down
|
---|
707 | usleep( 500000 );
|
---|
708 |
|
---|
709 | } else {
|
---|
710 |
|
---|
711 | logging_debug( "locator has not changed, not resetting connections" );
|
---|
712 |
|
---|
713 | }
|
---|
714 |
|
---|
715 | //
|
---|
716 | // handle the connections that have no longer any
|
---|
717 | // valid locator. send update messages with the new
|
---|
718 | // locator, so the remote node updates its locator/link mapping
|
---|
719 | //
|
---|
720 |
|
---|
721 | LinkSet::iterator iAffected = linkSet.begin();
|
---|
722 | LinkSet::iterator endAffected = linkSet.end();
|
---|
723 |
|
---|
724 | for( ; iAffected != endAffected; iAffected++ ){
|
---|
725 | LinkDescriptor descr = *iAffected;
|
---|
726 | logging_debug( "sending out link locator update to " << descr.remoteLocator->toString() );
|
---|
727 |
|
---|
728 | AribaBaseMsg updateMsg( descr.remoteLocator,
|
---|
729 | AribaBaseMsg::LINK_STATE_UPDATE,
|
---|
730 | descr.localLink, descr.remoteLink );
|
---|
731 |
|
---|
732 | transport->sendMessage( &updateMsg );
|
---|
733 | }
|
---|
734 | */
|
---|
735 | }
|
---|
736 |
|
---|
737 | /// sends a message to all end-points in the end-point descriptor
|
---|
738 | void BaseCommunication::send(Message* message, const EndpointDescriptor& endpoint) {
|
---|
739 | Data data = data_serialize( message, DEFAULT_V );
|
---|
740 | transport->send( endpoint.getEndpoints(), data.getBuffer(), data.getLength() / 8);
|
---|
741 | data.release();
|
---|
742 | }
|
---|
743 |
|
---|
744 | /// sends a message to the remote locator inside the link descriptor
|
---|
745 | void BaseCommunication::send(Message* message, const LinkDescriptor& desc) {
|
---|
746 | if (desc.remoteLocator==NULL) return;
|
---|
747 | Data data = data_serialize( message, DEFAULT_V );
|
---|
748 | transport->send( desc.remoteLocator, data.getBuffer(), data.getLength() / 8);
|
---|
749 | data.release();
|
---|
750 | }
|
---|
751 |
|
---|
752 | }} // namespace ariba, communication
|
---|