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