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 | #ifdef UNDERLAY_OMNET
|
---|
42 | #include "ariba/communication/modules/transport/omnet/AribaOmnetModule.h"
|
---|
43 | #include "ariba/communication/modules/network/omnet/OmnetNetworkProtocol.h"
|
---|
44 | #include "ariba/utility/system/StartupWrapper.h"
|
---|
45 |
|
---|
46 | using ariba::communication::AribaOmnetModule;
|
---|
47 | using ariba::communication::OmnetNetworkProtocol;
|
---|
48 | using ariba::utility::StartupWrapper;
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | namespace ariba {
|
---|
52 | namespace communication {
|
---|
53 |
|
---|
54 | use_logging_cpp(BaseCommunication);
|
---|
55 | const BaseCommunication::LinkDescriptor BaseCommunication::LinkDescriptor::UNSPECIFIED;
|
---|
56 |
|
---|
57 | BaseCommunication::BaseCommunication()
|
---|
58 | : messageReceiver(NULL), network(NULL), transport(NULL){
|
---|
59 | }
|
---|
60 |
|
---|
61 | BaseCommunication::~BaseCommunication(){
|
---|
62 | }
|
---|
63 |
|
---|
64 | void BaseCommunication::start(const NetworkLocator* _locallocator, const uint16_t _listenport){
|
---|
65 |
|
---|
66 | currentSeqnum = 0;
|
---|
67 | listenport = _listenport;
|
---|
68 |
|
---|
69 | logging_info( "starting up base communication and creating transports ..." );
|
---|
70 | logging_info( "using port " << listenport );
|
---|
71 |
|
---|
72 | #ifdef UNDERLAY_OMNET
|
---|
73 | AribaOmnetModule* module = StartupWrapper::getCurrentModule();
|
---|
74 | module->setServerPort( listenport );
|
---|
75 |
|
---|
76 | transport = module;
|
---|
77 | network = new OmnetNetworkProtocol( module );
|
---|
78 | #else
|
---|
79 | transport = new TCPTransport( listenport );
|
---|
80 | network = new IPv4NetworkProtocol();
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | logging_debug( "searching for local locators ..." );
|
---|
84 |
|
---|
85 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
|
---|
86 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
|
---|
87 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
|
---|
88 |
|
---|
89 | //
|
---|
90 | // choose the first locator that is not localhost
|
---|
91 | //
|
---|
92 |
|
---|
93 | bool foundLocator = false;
|
---|
94 |
|
---|
95 | for( ; i != iend; i++){
|
---|
96 | logging_debug( "local locator found " << (*i)->toString() );
|
---|
97 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
|
---|
98 |
|
---|
99 | // TODO: which locators are find to bind to?
|
---|
100 | // localhost is not too bad, works when testing locally
|
---|
101 | // with several instances. the manual override currently
|
---|
102 | // enables to use an arbitrary address, guess this is fine.
|
---|
103 | // so the manual override also can use ANY, LOCALHOST, BROADCAST
|
---|
104 |
|
---|
105 | if( *ipv4locator != IPv4Locator::LOCALHOST &&
|
---|
106 | *ipv4locator != IPv4Locator::ANY &&
|
---|
107 | *ipv4locator != IPv4Locator::BROADCAST ){
|
---|
108 |
|
---|
109 | ipv4locator->setPort(listenport);
|
---|
110 | localDescriptor.locator = ipv4locator;
|
---|
111 | localDescriptor.isUnspec = false;
|
---|
112 | logging_info( "binding to addr = " << ipv4locator->toString() );
|
---|
113 | foundLocator = true;
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | } // for( ; i != iend; i++)
|
---|
117 |
|
---|
118 |
|
---|
119 | if( _locallocator != NULL ) {
|
---|
120 | if( localDescriptor.locator != NULL) delete localDescriptor.locator;
|
---|
121 | localDescriptor.locator = new IPv4Locator( IPv4Locator::fromString( _locallocator->toString()) );
|
---|
122 | localDescriptor.isUnspec = false;
|
---|
123 | logging_debug( "manual locator override, using locator=" <<
|
---|
124 | localDescriptor.locator->toString() );
|
---|
125 | foundLocator = true;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // if we found no local locator, exit using logging fatal
|
---|
129 | if( !foundLocator )
|
---|
130 | logging_fatal( "did not find a useable local locator!" );
|
---|
131 |
|
---|
132 | transport->addMessageReceiver( this );
|
---|
133 | transport->start();
|
---|
134 |
|
---|
135 | #ifndef UNDERLAY_OMNET
|
---|
136 | //
|
---|
137 | // bind to the network change detection
|
---|
138 | //
|
---|
139 |
|
---|
140 | networkMonitor.registerNotification( this );
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | //
|
---|
144 | // base comm startup done
|
---|
145 | //
|
---|
146 |
|
---|
147 | logging_info( "base communication started up" );
|
---|
148 | }
|
---|
149 |
|
---|
150 | void BaseCommunication::stop() {
|
---|
151 |
|
---|
152 | logging_info( "stopping base communication and transport ..." );
|
---|
153 |
|
---|
154 | transport->stop();
|
---|
155 | delete transport;
|
---|
156 | delete network;
|
---|
157 |
|
---|
158 | logging_info( "base communication stopped" );
|
---|
159 | }
|
---|
160 |
|
---|
161 | const LinkID BaseCommunication::establishLink(
|
---|
162 | const EndpointDescriptor& descriptor,
|
---|
163 | const LinkID& link_id,
|
---|
164 | const QoSParameterSet& qos,
|
---|
165 | const SecurityParameterSet& sec) {
|
---|
166 |
|
---|
167 | // copy link id
|
---|
168 | LinkID linkid = link_id;
|
---|
169 |
|
---|
170 | // debug
|
---|
171 | logging_debug( "request to establish link" );
|
---|
172 |
|
---|
173 | //
|
---|
174 | // just use the first locator in the endp descriptors
|
---|
175 | //
|
---|
176 | if( descriptor.locator == NULL ){
|
---|
177 | logging_error( "invalid destination endpoint" );
|
---|
178 | return LinkID::UNSPECIFIED;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if( localDescriptor.locator == NULL ){
|
---|
182 | logging_error( "invalid local endpoint" );
|
---|
183 | return LinkID::UNSPECIFIED;
|
---|
184 | }
|
---|
185 |
|
---|
186 | const NetworkLocator* remote = descriptor.locator;
|
---|
187 | const NetworkLocator* local = localDescriptor.locator;
|
---|
188 |
|
---|
189 | // create link identifier and link descriptor
|
---|
190 | if (linkid.isUnspecified()) linkid = LinkID::create();
|
---|
191 | logging_debug( "creating new local descriptor entry with local link id " << linkid.toString() );
|
---|
192 | LinkDescriptor linkDescriptor( linkid, local, LinkID::UNSPECIFIED, remote, descriptor, false );
|
---|
193 | addLink( linkDescriptor );
|
---|
194 |
|
---|
195 | //
|
---|
196 | // create a base msg with our link id and
|
---|
197 | // a request to open a link on the other side
|
---|
198 | //
|
---|
199 |
|
---|
200 | logging_debug( "sending out base messages with request to open link to " << remote->toString() );
|
---|
201 | AribaBaseMsg baseMsg( remote, AribaBaseMsg::LINK_STATE_OPEN_REQUEST, linkid,
|
---|
202 | LinkID::UNSPECIFIED );
|
---|
203 | transport->sendMessage(&baseMsg);
|
---|
204 |
|
---|
205 | return linkid;
|
---|
206 | }
|
---|
207 |
|
---|
208 | void BaseCommunication::dropLink(const LinkID link) {
|
---|
209 |
|
---|
210 | logging_debug( "starting to drop link " + link.toString() );
|
---|
211 |
|
---|
212 | // see if we have the link
|
---|
213 | LinkDescriptor& descriptor = queryLocalLink( link );
|
---|
214 | if( descriptor.isUnspecified() ){
|
---|
215 | logging_error( "don't know the link you want to drop" );
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | // warn if this link has some queued messages attached
|
---|
220 | if( descriptor.waitingmsg.size() > 0 ){
|
---|
221 | logging_warn( "dropping link " << link.toString() <<
|
---|
222 | " that has " << descriptor.waitingmsg.size() << " waiting messages" );
|
---|
223 | }
|
---|
224 |
|
---|
225 | // create message to drop the link
|
---|
226 | logging_debug( "sending out link close request. for us, the link is closed now" );
|
---|
227 | AribaBaseMsg msg(
|
---|
228 | descriptor.remoteLocator,
|
---|
229 | AribaBaseMsg::LINK_STATE_CLOSE_REQUEST,
|
---|
230 | descriptor.localLink,
|
---|
231 | descriptor.remoteLink
|
---|
232 | );
|
---|
233 |
|
---|
234 | // send message to drop the link
|
---|
235 | transport->sendMessage( &msg );
|
---|
236 |
|
---|
237 | // tell the registered listeners
|
---|
238 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
239 | i->onLinkDown( link, descriptor.localLocator, descriptor.remoteLocator );
|
---|
240 | }
|
---|
241 |
|
---|
242 | // remove from map
|
---|
243 | removeLink(link);
|
---|
244 | }
|
---|
245 |
|
---|
246 | seqnum_t BaseCommunication::sendMessage( const LinkID lid, const Message* message) {
|
---|
247 |
|
---|
248 | logging_debug( "sending out message to link " << lid.toString() );
|
---|
249 |
|
---|
250 | // query local link info
|
---|
251 | LinkDescriptor& linkDesc = queryLocalLink(lid);
|
---|
252 | if( linkDesc.isUnspecified() ){
|
---|
253 | logging_error( "don't know the link with id " << lid.toString() );
|
---|
254 | return -1;
|
---|
255 | }
|
---|
256 |
|
---|
257 | // create message
|
---|
258 | AribaBaseMsg msg(
|
---|
259 | linkDesc.remoteLocator,
|
---|
260 | AribaBaseMsg::LINK_STATE_DATA,
|
---|
261 | linkDesc.localLink,
|
---|
262 | linkDesc.remoteLink
|
---|
263 | );
|
---|
264 |
|
---|
265 | // encapsulate the payload message
|
---|
266 | msg.encapsulate( const_cast<Message*>(message) );
|
---|
267 |
|
---|
268 | if( linkDesc.linkup ){
|
---|
269 |
|
---|
270 | // send message
|
---|
271 | transport->sendMessage( &msg );
|
---|
272 | return ++currentSeqnum;
|
---|
273 |
|
---|
274 | } else {
|
---|
275 |
|
---|
276 | // queue message
|
---|
277 | logging_info( "link " << lid.toString() << " is not up yet, queueing message" );
|
---|
278 | linkDesc.waitingmsg.push_back( new Message(msg) ); // TODO ooooo
|
---|
279 |
|
---|
280 | return 0;
|
---|
281 |
|
---|
282 | } // if( linkDesc.linkup )
|
---|
283 | }
|
---|
284 |
|
---|
285 | const EndpointDescriptor& BaseCommunication::getEndpointDescriptor(const LinkID link) const {
|
---|
286 |
|
---|
287 | if( link == LinkID::UNSPECIFIED){
|
---|
288 | return localDescriptor;
|
---|
289 | } else {
|
---|
290 | LinkDescriptor& linkDesc = queryLocalLink(link);
|
---|
291 | if (linkDesc.isUnspecified()) return EndpointDescriptor::UNSPECIFIED;
|
---|
292 | return linkDesc.remoteEndpoint;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | void BaseCommunication::registerMessageReceiver(MessageReceiver* _receiver) {
|
---|
297 | messageReceiver = _receiver;
|
---|
298 | }
|
---|
299 |
|
---|
300 | void BaseCommunication::unregisterMessageReceiver(MessageReceiver* _receiver) {
|
---|
301 | messageReceiver = NULL;
|
---|
302 | }
|
---|
303 |
|
---|
304 | void BaseCommunication::registerEventListener(CommunicationEvents* _events){
|
---|
305 |
|
---|
306 | if( eventListener.find( _events ) == eventListener.end() )
|
---|
307 | eventListener.insert( _events );
|
---|
308 | }
|
---|
309 |
|
---|
310 | void BaseCommunication::unregisterEventListener(CommunicationEvents* _events){
|
---|
311 |
|
---|
312 | EventListenerSet::iterator i = eventListener.find( _events );
|
---|
313 | if( i != eventListener.end() )
|
---|
314 | eventListener.erase( i );
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | bool BaseCommunication::receiveMessage(const Message* message, const LinkID& link, const NodeID& node){
|
---|
319 |
|
---|
320 | //
|
---|
321 | // these messages arrive from the Transport module
|
---|
322 | // and are incoming network messages. Unpack the
|
---|
323 | // AribaBaseMsg and handle control packets,
|
---|
324 | // deliver data packets to the overlay
|
---|
325 | //
|
---|
326 |
|
---|
327 | AribaBaseMsg* spovmsg = ((Message*)message)->decapsulate<AribaBaseMsg>();
|
---|
328 | logging_debug( "receiving base comm message of type " << spovmsg->getTypeString() );
|
---|
329 |
|
---|
330 | //
|
---|
331 | // deliver data to the overlays. we just give the
|
---|
332 | // inner packet to every registered overlay ...
|
---|
333 | //
|
---|
334 |
|
---|
335 | if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_DATA ){
|
---|
336 |
|
---|
337 | logging_debug( "received data message, forwarding to overlay" );
|
---|
338 |
|
---|
339 | //
|
---|
340 | // put the linkid as address into the message
|
---|
341 | // and sent it to the receiver
|
---|
342 | //
|
---|
343 |
|
---|
344 | if( messageReceiver != NULL ) {
|
---|
345 | messageReceiver->receiveMessage(
|
---|
346 | spovmsg,
|
---|
347 | spovmsg->getRemoteLink(),
|
---|
348 | NodeID::UNSPECIFIED
|
---|
349 | );
|
---|
350 | }
|
---|
351 |
|
---|
352 | } // LINK_STATE_DATA
|
---|
353 |
|
---|
354 | //
|
---|
355 | // handle link open requests
|
---|
356 | //
|
---|
357 |
|
---|
358 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REQUEST ){
|
---|
359 |
|
---|
360 | logging_debug( "received link open request" );
|
---|
361 |
|
---|
362 | //
|
---|
363 | // create a link context
|
---|
364 | //
|
---|
365 |
|
---|
366 | // in an incoming packet the localLink is from
|
---|
367 | // the sender perspective local and from our
|
---|
368 | // perspective remote
|
---|
369 |
|
---|
370 | logging_debug( "creating local link" );
|
---|
371 |
|
---|
372 | LinkID localLink = LinkID::create();
|
---|
373 | LinkID remoteLink = spovmsg->getLocalLink();
|
---|
374 |
|
---|
375 |
|
---|
376 | const NetworkLocator* localLocator = dynamic_cast<const NetworkLocator*>(localDescriptor.locator);
|
---|
377 | const NetworkLocator* remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
|
---|
378 |
|
---|
379 | logging_debug( "localLocator=" << localLocator->toString()
|
---|
380 | << " remoteLocator=" << remoteLocator->toString());
|
---|
381 |
|
---|
382 | // ask the registered listeners if this link
|
---|
383 | // creation is fine. we will only allow the
|
---|
384 | // link if all of them agree
|
---|
385 |
|
---|
386 | bool allowlink = true;
|
---|
387 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
388 | allowlink &= i->onLinkRequest( localLink, localLocator, remoteLocator );
|
---|
389 | }
|
---|
390 |
|
---|
391 | if( !allowlink ){
|
---|
392 | logging_warn( "overlay denied creation of link" );
|
---|
393 | return true;
|
---|
394 | }
|
---|
395 |
|
---|
396 | //
|
---|
397 | // create and save the descriptor for the link
|
---|
398 | //
|
---|
399 |
|
---|
400 | LinkDescriptor linkDescriptor(localLink, localLocator, remoteLink,
|
---|
401 | remoteLocator, EndpointDescriptor(remoteLocator), true);
|
---|
402 |
|
---|
403 | logging_debug( "saving new link descriptor with " <<
|
---|
404 | "[local link " << localLink.toString() << "] " <<
|
---|
405 | "[local locator " << localLocator->toString() << "] " <<
|
---|
406 | "[remote link " << remoteLink.toString() << "] " <<
|
---|
407 | "[remote locator " << remoteLocator->toString() << "]" <<
|
---|
408 | "[link up true]" );
|
---|
409 |
|
---|
410 | addLink( linkDescriptor );
|
---|
411 |
|
---|
412 | //
|
---|
413 | // send out a link reply
|
---|
414 | //
|
---|
415 |
|
---|
416 | logging_debug( "sending back link open reply for " <<
|
---|
417 | "[local link " << localLink.toString() << "] " <<
|
---|
418 | "[remote link " << remoteLink.toString() << "]" );
|
---|
419 |
|
---|
420 | AribaBaseMsg reply(remoteLocator,
|
---|
421 | AribaBaseMsg::LINK_STATE_OPEN_REPLY,
|
---|
422 | localLink,
|
---|
423 | remoteLink);
|
---|
424 |
|
---|
425 | transport->sendMessage( &reply );
|
---|
426 |
|
---|
427 | //
|
---|
428 | // the link is now open
|
---|
429 | //
|
---|
430 |
|
---|
431 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
432 | i->onLinkUp( localLink, localLocator, remoteLocator );
|
---|
433 | }
|
---|
434 |
|
---|
435 | } // LINK_STATE_OPEN_REQUEST
|
---|
436 |
|
---|
437 | //
|
---|
438 | // handle link open replies
|
---|
439 | //
|
---|
440 |
|
---|
441 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REPLY ){
|
---|
442 |
|
---|
443 | logging_debug( "received link open reply for a link we initiated" );
|
---|
444 |
|
---|
445 | // this is a reply to a link open request, so we have already
|
---|
446 | // a link mapping and can now set the remote link to valid
|
---|
447 | LinkDescriptor& linkDesc = queryLocalLink( spovmsg->getRemoteLink() );
|
---|
448 |
|
---|
449 | if (linkDesc.isUnspecified()) {
|
---|
450 | logging_warn("Failed to find local link " << spovmsg->getRemoteLink().toString());
|
---|
451 | return false;
|
---|
452 | }
|
---|
453 |
|
---|
454 | linkDesc.remoteLink = spovmsg->getLocalLink();
|
---|
455 | linkDesc.linkup = true;
|
---|
456 |
|
---|
457 | logging_debug( "the link is now up with local link id " << spovmsg->getRemoteLink().toString() );
|
---|
458 |
|
---|
459 | // notify the baseoverlay that the link is up, so
|
---|
460 | // it can exchange nodeids over this link. then we
|
---|
461 | // can send the queued messages, as both nodes have
|
---|
462 | // to know their nodeids first
|
---|
463 |
|
---|
464 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
465 | i->onLinkUp( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
|
---|
466 | }
|
---|
467 |
|
---|
468 | if( linkDesc.waitingmsg.size() > 0 ){
|
---|
469 | logging_info( "sending out queued messages on link " << linkDesc.localLink.toString() );
|
---|
470 |
|
---|
471 | BOOST_FOREACH( Message* msg, linkDesc.waitingmsg ){
|
---|
472 | sendMessage( linkDesc.localLink, msg );
|
---|
473 | delete msg;
|
---|
474 | }
|
---|
475 |
|
---|
476 | linkDesc.waitingmsg.clear();
|
---|
477 | }
|
---|
478 |
|
---|
479 | } // LINK_STATE_OPEN_REPLY
|
---|
480 |
|
---|
481 | //
|
---|
482 | // handle link close requests
|
---|
483 | //
|
---|
484 |
|
---|
485 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_CLOSE_REQUEST ){
|
---|
486 |
|
---|
487 | const LinkID& localLink = spovmsg->getRemoteLink();
|
---|
488 | logging_debug( "received link close request for link " << localLink.toString() );
|
---|
489 |
|
---|
490 | //
|
---|
491 | // the link is closed immediately, we
|
---|
492 | // don't need to send out a reply, so we
|
---|
493 | // delete the mapping and inform
|
---|
494 | //
|
---|
495 |
|
---|
496 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
497 | if (linkDesc.isUnspecified()) {
|
---|
498 | logging_warn("Failed to find local link " << localLink.toString());
|
---|
499 | return false;
|
---|
500 | }
|
---|
501 |
|
---|
502 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
503 | i->onLinkDown( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator );
|
---|
504 | }
|
---|
505 |
|
---|
506 | //
|
---|
507 | // delete all pending messages for the link that has been closed
|
---|
508 | //
|
---|
509 |
|
---|
510 | BOOST_FOREACH( Message* msg, linkDesc.waitingmsg ){
|
---|
511 | delete msg;
|
---|
512 | }
|
---|
513 |
|
---|
514 | linkDesc.waitingmsg.clear();
|
---|
515 |
|
---|
516 | //
|
---|
517 | // remove the link descriptor
|
---|
518 | //
|
---|
519 |
|
---|
520 | removeLink( localLink );
|
---|
521 |
|
---|
522 | } // LINK_STATE_CLOSE_REQUEST
|
---|
523 |
|
---|
524 | //
|
---|
525 | // handle locator updates
|
---|
526 | //
|
---|
527 |
|
---|
528 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_UPDATE ){
|
---|
529 |
|
---|
530 | const LinkID& localLink = spovmsg->getRemoteLink();
|
---|
531 | logging_debug( "received link update for link " << localLink.toString() );
|
---|
532 |
|
---|
533 | //
|
---|
534 | // find the link description
|
---|
535 | //
|
---|
536 |
|
---|
537 | LinkDescriptor& linkDesc = queryLocalLink( localLink );
|
---|
538 | if (linkDesc.isUnspecified()) {
|
---|
539 | logging_warn("Failed to update local link " << localLink.toString());
|
---|
540 | return false;
|
---|
541 | }
|
---|
542 |
|
---|
543 | //
|
---|
544 | // update the remote locator
|
---|
545 | //
|
---|
546 |
|
---|
547 | const NetworkLocator* oldremote = linkDesc.remoteLocator;
|
---|
548 | linkDesc.remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress());
|
---|
549 |
|
---|
550 | //
|
---|
551 | // inform the listeners (local link has _not_ changed!)
|
---|
552 | //
|
---|
553 |
|
---|
554 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){
|
---|
555 | i->onLinkChanged(
|
---|
556 | linkDesc.localLink, // linkid
|
---|
557 | linkDesc.localLocator, // old local
|
---|
558 | linkDesc.localLocator, // new local
|
---|
559 | oldremote, // old remote
|
---|
560 | linkDesc.remoteLocator // new remote
|
---|
561 | );
|
---|
562 | }
|
---|
563 |
|
---|
564 | } // LINK_STATE_UPDATE
|
---|
565 |
|
---|
566 | return true;
|
---|
567 | }
|
---|
568 |
|
---|
569 | void BaseCommunication::addLink( const LinkDescriptor& link ) {
|
---|
570 | linkSet.push_back( link );
|
---|
571 | }
|
---|
572 |
|
---|
573 | void BaseCommunication::removeLink( const LinkID& localLink ) {
|
---|
574 |
|
---|
575 | LinkSet::iterator i = linkSet.begin();
|
---|
576 | LinkSet::iterator iend = linkSet.end();
|
---|
577 |
|
---|
578 | for( ; i != iend; i++){
|
---|
579 | if( (*i).localLink != localLink) continue;
|
---|
580 |
|
---|
581 | BOOST_FOREACH( Message* msg, i->waitingmsg ){
|
---|
582 | delete msg;
|
---|
583 | }
|
---|
584 |
|
---|
585 | i->waitingmsg.clear();
|
---|
586 | linkSet.erase( i );
|
---|
587 |
|
---|
588 | break;
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | BaseCommunication::LinkDescriptor& BaseCommunication::queryLocalLink( const LinkID& link ) const {
|
---|
593 | for (int i=0; i<linkSet.size();i++)
|
---|
594 | if (linkSet[i].localLink == link) return (LinkDescriptor&)linkSet[i];
|
---|
595 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
|
---|
596 | }
|
---|
597 |
|
---|
598 | BaseCommunication::LinkDescriptor& BaseCommunication::queryRemoteLink( const LinkID& link ) const {
|
---|
599 | for (int i=0; i<linkSet.size();i++)
|
---|
600 | if (linkSet[i].remoteLink == link) return (LinkDescriptor&)linkSet[i];
|
---|
601 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED;
|
---|
602 | }
|
---|
603 |
|
---|
604 | LinkIDs BaseCommunication::getLocalLinks( const EndpointDescriptor& ep ) const {
|
---|
605 | LinkIDs ids;
|
---|
606 |
|
---|
607 | for (int i=0; i<linkSet.size(); i++){
|
---|
608 | if( ep == EndpointDescriptor::UNSPECIFIED ){
|
---|
609 | ids.push_back( linkSet[i].localLink );
|
---|
610 | } else {
|
---|
611 | if ( linkSet[i].remoteLocator == ep.locator )
|
---|
612 | ids.push_back( linkSet[i].localLink );
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | return ids;
|
---|
617 | }
|
---|
618 |
|
---|
619 | void BaseCommunication::onNetworkChange(const NetworkChangeInterface::NetworkChangeInfo& info){
|
---|
620 |
|
---|
621 | #ifdef UNDERLAY_OMNET
|
---|
622 |
|
---|
623 | // we have no mobility support for simulations
|
---|
624 | return
|
---|
625 |
|
---|
626 | #endif // UNDERLAY_OMNET
|
---|
627 |
|
---|
628 | //
|
---|
629 | // we only care about address changes, not about interface changes
|
---|
630 | // as address changes are triggered by interface changes, we are safe here
|
---|
631 | //
|
---|
632 |
|
---|
633 | if( info.type != NetworkChangeInterface::EventTypeAddressNew &&
|
---|
634 | info.type != NetworkChangeInterface::EventTypeAddressDelete ) return;
|
---|
635 |
|
---|
636 | logging_info( "base communication is handling network address changes" );
|
---|
637 |
|
---|
638 | //
|
---|
639 | // get all now available addresses
|
---|
640 | //
|
---|
641 |
|
---|
642 | NetworkInformation networkInformation;
|
---|
643 | AddressInformation addressInformation;
|
---|
644 |
|
---|
645 | NetworkInterfaceList interfaces = networkInformation.getInterfaces();
|
---|
646 | AddressList addresses;
|
---|
647 |
|
---|
648 | for( NetworkInterfaceList::iterator i = interfaces.begin(); i != interfaces.end(); i++ ){
|
---|
649 | AddressList newaddr = addressInformation.getAddresses(*i);
|
---|
650 | addresses.insert( addresses.end(), newaddr.begin(), newaddr.end() );
|
---|
651 | }
|
---|
652 |
|
---|
653 | //
|
---|
654 | // get current locators for the local endpoint
|
---|
655 | // TODO: this code is dublicate of the ctor code!!! cleanup!
|
---|
656 | //
|
---|
657 |
|
---|
658 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses();
|
---|
659 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin();
|
---|
660 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end();
|
---|
661 |
|
---|
662 | //
|
---|
663 | // remember the old local endpoint, in case it changes
|
---|
664 | //
|
---|
665 |
|
---|
666 | EndpointDescriptor oldLocalDescriptor( localDescriptor );
|
---|
667 |
|
---|
668 | //
|
---|
669 | // look for local locators that we can use in communication
|
---|
670 | //
|
---|
671 | // choose the first locator that is not localhost
|
---|
672 | //
|
---|
673 |
|
---|
674 | bool foundLocator = false;
|
---|
675 | bool changedLocator = false;
|
---|
676 |
|
---|
677 | for( ; i != iend; i++){
|
---|
678 | logging_debug( "local locator found " << (*i)->toString() );
|
---|
679 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i);
|
---|
680 |
|
---|
681 | if( *ipv4locator != IPv4Locator::LOCALHOST &&
|
---|
682 | *ipv4locator != IPv4Locator::ANY &&
|
---|
683 | *ipv4locator != IPv4Locator::BROADCAST ){
|
---|
684 |
|
---|
685 | ipv4locator->setPort( listenport );
|
---|
686 | changedLocator = *localDescriptor.locator != *ipv4locator;
|
---|
687 | localDescriptor.locator = ipv4locator;
|
---|
688 | logging_info( "binding to addr = " << ipv4locator->toString() );
|
---|
689 | foundLocator = true;
|
---|
690 | break;
|
---|
691 | }
|
---|
692 | } // for( ; i != iend; i++)
|
---|
693 |
|
---|
694 | //
|
---|
695 | // if we found no locator, bind to localhost
|
---|
696 | //
|
---|
697 |
|
---|
698 | if( !foundLocator ){
|
---|
699 | changedLocator = *localDescriptor.locator != IPv4Locator::LOCALHOST;
|
---|
700 | localDescriptor.locator = new IPv4Locator( IPv4Locator::LOCALHOST );
|
---|
701 | ((IPv4Locator*)(localDescriptor.locator))->setPort( listenport );
|
---|
702 | logging_info( "found no good local lcoator, binding to addr = " <<
|
---|
703 | localDescriptor.locator->toString() );
|
---|
704 | }
|
---|
705 |
|
---|
706 | //
|
---|
707 | // if we have connections that have no more longer endpoints
|
---|
708 | // close these. they will be automatically built up again.
|
---|
709 | // also update the local locator in the linkset mapping
|
---|
710 | //
|
---|
711 |
|
---|
712 | if( changedLocator ){
|
---|
713 |
|
---|
714 | logging_debug( "local endp locator has changed to " << localDescriptor.toString() <<
|
---|
715 | ", resettings connections that end at old locator " <<
|
---|
716 | oldLocalDescriptor.toString());
|
---|
717 |
|
---|
718 | LinkSet::iterator i = linkSet.begin();
|
---|
719 | LinkSet::iterator iend = linkSet.end();
|
---|
720 |
|
---|
721 | for( ; i != iend; i++ ){
|
---|
722 |
|
---|
723 | logging_debug( "checking connection for locator change: " <<
|
---|
724 | " local " << (*i).localLocator->toString() <<
|
---|
725 | " old " << oldLocalDescriptor.locator->toString() );
|
---|
726 |
|
---|
727 | if( *((*i).localLocator) == *(oldLocalDescriptor.locator) ){
|
---|
728 |
|
---|
729 | logging_debug("terminating connection to " << (*i).remoteLocator->toString() );
|
---|
730 | transport->terminate( oldLocalDescriptor.locator, (*i).remoteLocator );
|
---|
731 |
|
---|
732 | (*i).localLocator = localDescriptor.locator;
|
---|
733 | }
|
---|
734 | } // for( ; i != iend; i++ )
|
---|
735 |
|
---|
736 | // wait 500ms to give the sockets time to shut down
|
---|
737 | usleep( 500000 );
|
---|
738 |
|
---|
739 | } else {
|
---|
740 |
|
---|
741 | logging_debug( "locator has not changed, not resetting connections" );
|
---|
742 |
|
---|
743 | }
|
---|
744 |
|
---|
745 | //
|
---|
746 | // handle the connections that have no longer any
|
---|
747 | // valid locator. send update messages with the new
|
---|
748 | // locator, so the remote node updates its locator/link mapping
|
---|
749 | //
|
---|
750 |
|
---|
751 | LinkSet::iterator iAffected = linkSet.begin();
|
---|
752 | LinkSet::iterator endAffected = linkSet.end();
|
---|
753 |
|
---|
754 | for( ; iAffected != endAffected; iAffected++ ){
|
---|
755 | LinkDescriptor descr = *iAffected;
|
---|
756 | logging_debug( "sending out link locator update to " << descr.remoteLocator->toString() );
|
---|
757 |
|
---|
758 | AribaBaseMsg updateMsg( descr.remoteLocator,
|
---|
759 | AribaBaseMsg::LINK_STATE_UPDATE,
|
---|
760 | descr.localLink, descr.remoteLink );
|
---|
761 |
|
---|
762 | transport->sendMessage( &updateMsg );
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | }} // namespace ariba, communication
|
---|