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 "BaseOverlay.h"
|
---|
40 |
|
---|
41 | #include <sstream>
|
---|
42 | #include <iostream>
|
---|
43 | #include <string>
|
---|
44 | #include <boost/foreach.hpp>
|
---|
45 |
|
---|
46 | #include "ariba/NodeListener.h"
|
---|
47 | #include "ariba/CommunicationListener.h"
|
---|
48 | #include "ariba/SideportListener.h"
|
---|
49 |
|
---|
50 | #include "ariba/overlay/LinkDescriptor.h"
|
---|
51 | #include "ariba/overlay/messages/OverlayMsg.h"
|
---|
52 | #include "ariba/overlay/messages/JoinRequest.h"
|
---|
53 | #include "ariba/overlay/messages/JoinReply.h"
|
---|
54 | #include "ariba/overlay/messages/LinkRequest.h"
|
---|
55 | #include "ariba/overlay/messages/RelayMessage.h"
|
---|
56 |
|
---|
57 | #include "ariba/utility/misc/OvlVis.h"
|
---|
58 |
|
---|
59 | namespace ariba {
|
---|
60 | namespace overlay {
|
---|
61 |
|
---|
62 | LinkDescriptor* BaseOverlay::getDescriptor( const LinkID& link, bool communication ) {
|
---|
63 | BOOST_FOREACH( LinkDescriptor* lp, links )
|
---|
64 | if ((communication ? lp->communicationId : lp->overlayId) == link)
|
---|
65 | return lp;
|
---|
66 | return NULL;
|
---|
67 | }
|
---|
68 |
|
---|
69 | const LinkDescriptor* BaseOverlay::getDescriptor( const LinkID& link, bool communication ) const {
|
---|
70 | BOOST_FOREACH( const LinkDescriptor* lp, links )
|
---|
71 | if ((communication ? lp->communicationId : lp->overlayId) == link)
|
---|
72 | return lp;
|
---|
73 | return NULL;
|
---|
74 | }
|
---|
75 |
|
---|
76 | LinkDescriptor* BaseOverlay::getAutoDescriptor( const NodeID& node, const ServiceID& service ) {
|
---|
77 | BOOST_FOREACH( LinkDescriptor* lp, links )
|
---|
78 | if (lp->autolink && lp->remoteNode == node && lp->service == service && lp->up)
|
---|
79 | return lp;
|
---|
80 | BOOST_FOREACH( LinkDescriptor* lp, links )
|
---|
81 | if (lp->autolink && lp->remoteNode == node && lp->service == service )
|
---|
82 | return lp;
|
---|
83 | return NULL;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void BaseOverlay::eraseDescriptor( const LinkID& link, bool communication ) {
|
---|
87 | for ( vector<LinkDescriptor*>::iterator i = links.begin(); i!= links.end(); i++) {
|
---|
88 | LinkDescriptor* ld = *i;
|
---|
89 | if ((communication ? ld->communicationId : ld->overlayId) == link) {
|
---|
90 | delete ld;
|
---|
91 | links.erase(i);
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | LinkDescriptor* BaseOverlay::addDescriptor( const LinkID& link ) {
|
---|
98 | LinkDescriptor* desc = getDescriptor( link );
|
---|
99 | if ( desc == NULL ) {
|
---|
100 | desc = new LinkDescriptor();
|
---|
101 | desc->overlayId = link;
|
---|
102 | links.push_back(desc);
|
---|
103 | }
|
---|
104 | return desc;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /// returns a direct link relay descriptor to the given relay node
|
---|
108 | LinkDescriptor* BaseOverlay::getRelayDescriptor( const NodeID& relayNode ) {
|
---|
109 | BOOST_FOREACH( LinkDescriptor* lp, links )
|
---|
110 | if (lp->remoteNode == relayNode &&
|
---|
111 | lp->service == OverlayInterface::OVERLAY_SERVICE_ID &&
|
---|
112 | lp->relay == false &&
|
---|
113 | lp->up)
|
---|
114 | return lp;
|
---|
115 | return NULL;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /// find a proper relay node
|
---|
119 | const NodeID BaseOverlay::findRelayNode( const NodeID id ) {
|
---|
120 | LinkDescriptor* rld = NULL;
|
---|
121 | NodeID relayNode = NodeID::UNSPECIFIED;
|
---|
122 |
|
---|
123 | // get used next hop towards node
|
---|
124 | LinkID rlid = overlayInterface->getNextLinkId(id);
|
---|
125 | while ( relayNode.isUnspecified() && !rlid.isUnspecified() && rld == NULL ) {
|
---|
126 |
|
---|
127 | // get descriptor of first hop
|
---|
128 | rld = getDescriptor(rlid);
|
---|
129 | logging_info( rld );
|
---|
130 |
|
---|
131 | // is first hop a relay path? yes-> try to find real link!
|
---|
132 | if ( rld->relay )
|
---|
133 | relayNode = getRelayDescriptor(rld->localRelay)->remoteNode;
|
---|
134 |
|
---|
135 | // no-> a proper relay node has been found
|
---|
136 | else relayNode = rld->remoteNode;
|
---|
137 | }
|
---|
138 |
|
---|
139 | // if first relay is unknown choose a arbitrary direct node as relay
|
---|
140 | if ( relayNode.isUnspecified() ) {
|
---|
141 | for (size_t i=0; i<links.size(); i++)
|
---|
142 | if (links[i]->up &&
|
---|
143 | links[i]->communicationUp &&
|
---|
144 | !links[i]->relay &&
|
---|
145 | links[i]->keepAliveMissed <= 1 &&
|
---|
146 | links[i]->service == OverlayInterface::OVERLAY_SERVICE_ID) {
|
---|
147 | relayNode = links[i]->remoteNode;
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | // do not return myself or use the node as relay node
|
---|
153 | if (relayNode == nodeId)
|
---|
154 | return NodeID::UNSPECIFIED;
|
---|
155 | else {
|
---|
156 | logging_info( "Returning relay node " << relayNode.toString() );
|
---|
157 | return relayNode;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /// forwards a message over relays/directly using link descriptor
|
---|
162 | seqnum_t BaseOverlay::sendMessage( Message* message, const LinkDescriptor* ld ) {
|
---|
163 |
|
---|
164 | // directly send message
|
---|
165 | if ( !ld->communicationId.isUnspecified() && ld->communicationUp ) {
|
---|
166 | logging_debug("Send: Sending message via Base Communication");
|
---|
167 | return bc->sendMessage( ld->communicationId, message );
|
---|
168 | }
|
---|
169 |
|
---|
170 | // relay message
|
---|
171 | else if ( ld->relay ) {
|
---|
172 |
|
---|
173 | logging_debug("Send: Relaying message to node "
|
---|
174 | << ld->remoteNode.toString()
|
---|
175 | << " using relay " << ld->localRelay
|
---|
176 | );
|
---|
177 | /*
|
---|
178 | // get local relay link descriptor and mark as used for relaying
|
---|
179 | LinkDescriptor* rld = getRelayDescriptor(ld->localRelay);
|
---|
180 | if (rld==NULL) {
|
---|
181 | logging_error("Send: Relay descriptor for relay " <<
|
---|
182 | ld->localRelay.toString() << " is unknown.");
|
---|
183 | return -1;
|
---|
184 | }
|
---|
185 | rld->markAsRelay();*/
|
---|
186 |
|
---|
187 | // create a information relay message to inform the relay about
|
---|
188 | OverlayMsg overlay_msg( OverlayMsg::typeRelay, ld->service, nodeId );
|
---|
189 | RelayMessage relayMsg( RelayMessage::typeInform, ld->remoteRelay, ld->remoteNode, ld->remoteLinkId );
|
---|
190 | relayMsg.encapsulate( message );
|
---|
191 | overlay_msg.encapsulate( &relayMsg );
|
---|
192 |
|
---|
193 | // route message to relay node in order to inform it!
|
---|
194 | logging_debug("sendMessage: Sending message over relayed link with" << ld );
|
---|
195 | sendOverlay( &overlay_msg, ld->localRelay );
|
---|
196 | return 0;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // error
|
---|
200 | else {
|
---|
201 | logging_error( "Could not send message descriptor=" << ld );
|
---|
202 | return -1;
|
---|
203 | }
|
---|
204 | return -1;
|
---|
205 | }
|
---|
206 |
|
---|
207 | LinkDescriptor* BaseOverlay::getSendDescriptor( const NodeID& nodeid, bool follow ) {
|
---|
208 | for (size_t i=0; i<links.size(); i++)
|
---|
209 | if ( !links[i]->relay &&
|
---|
210 | links[i]->up &&
|
---|
211 | links[i]->communicationUp &&
|
---|
212 | links[i]->keepAliveMissed <= 1 &&
|
---|
213 | links[i]->remoteNode == nodeid &&
|
---|
214 | links[i]->service == OverlayInterface::OVERLAY_SERVICE_ID) {
|
---|
215 | return links[i];
|
---|
216 | }
|
---|
217 | LinkDescriptor* ld = getDescriptor(overlayInterface->getNextLinkId(nodeid));
|
---|
218 | if (ld != NULL && ld->relay && follow)
|
---|
219 | return getSendDescriptor(ld->localRelay, false);
|
---|
220 | return NULL;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /// routes a message over the overlay or directly sends it when a link is open
|
---|
224 | seqnum_t BaseOverlay::sendOverlay( Message* message, const NodeID& nodeid ) {
|
---|
225 | for (size_t i=0; i<links.size(); i++)
|
---|
226 | if ( !links[i]->relay &&
|
---|
227 | links[i]->up &&
|
---|
228 | links[i]->communicationUp &&
|
---|
229 | links[i]->keepAliveMissed <= 1 &&
|
---|
230 | links[i]->remoteNode == nodeid &&
|
---|
231 | links[i]->service == OverlayInterface::OVERLAY_SERVICE_ID) {
|
---|
232 | links[i]->markAsRelay();
|
---|
233 | return sendMessage( message, links[i] );
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | overlayInterface->routeMessage(nodeid, message);
|
---|
237 | return 0;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /// creates a link descriptor, apply relay semantics if possible
|
---|
241 | LinkDescriptor* BaseOverlay::createLinkDescriptor(
|
---|
242 | const NodeID remoteNode, const ServiceID service, const LinkID link_id ) {
|
---|
243 |
|
---|
244 | // find listener
|
---|
245 | if( !communicationListeners.contains( service ) ) {
|
---|
246 | logging_error( "No listener found for service " << service.toString() );
|
---|
247 | return NULL;
|
---|
248 | }
|
---|
249 | CommunicationListener* listener = communicationListeners.get( service );
|
---|
250 | assert( listener != NULL );
|
---|
251 |
|
---|
252 | // copy link id
|
---|
253 | LinkID linkid = link_id;
|
---|
254 |
|
---|
255 | // create link id if necessary
|
---|
256 | if ( linkid.isUnspecified() )
|
---|
257 | linkid = LinkID::create();
|
---|
258 |
|
---|
259 | // create relay link descriptor
|
---|
260 | NodeID relayNode = findRelayNode(remoteNode);
|
---|
261 |
|
---|
262 | // add descriptor
|
---|
263 | LinkDescriptor* ld = addDescriptor( linkid );
|
---|
264 | ld->overlayId = linkid;
|
---|
265 | ld->service = service;
|
---|
266 | ld->listener = listener;
|
---|
267 | ld->remoteNode = remoteNode;
|
---|
268 |
|
---|
269 | // set relay node if available
|
---|
270 | ld->relay = !relayNode.isUnspecified();
|
---|
271 | ld->localRelay = relayNode;
|
---|
272 |
|
---|
273 | if (!ld->relay)
|
---|
274 | logging_error("No relay found!");
|
---|
275 |
|
---|
276 | // debug output
|
---|
277 | logging_debug( "Created link descriptor: " << ld );
|
---|
278 |
|
---|
279 | return ld;
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | // ----------------------------------------------------------------------------
|
---|
284 |
|
---|
285 | use_logging_cpp(BaseOverlay);
|
---|
286 |
|
---|
287 | // ----------------------------------------------------------------------------
|
---|
288 |
|
---|
289 | BaseOverlay::BaseOverlay() :
|
---|
290 | bc(NULL), overlayInterface(NULL), nodeId(NodeID::UNSPECIFIED),
|
---|
291 | spovnetId(SpoVNetID::UNSPECIFIED), state(BaseOverlayStateInvalid),
|
---|
292 | sideport(&SideportListener::DEFAULT), started(false), counter(0) {
|
---|
293 | }
|
---|
294 |
|
---|
295 | BaseOverlay::~BaseOverlay() {
|
---|
296 | }
|
---|
297 |
|
---|
298 | // ----------------------------------------------------------------------------
|
---|
299 |
|
---|
300 | void BaseOverlay::start( BaseCommunication& _basecomm, const NodeID& _nodeid ) {
|
---|
301 | logging_info("Starting...");
|
---|
302 |
|
---|
303 | // set parameters
|
---|
304 | bc = &_basecomm;
|
---|
305 | nodeId = _nodeid;
|
---|
306 |
|
---|
307 | // register at base communication
|
---|
308 | bc->registerMessageReceiver( this );
|
---|
309 | bc->registerEventListener( this );
|
---|
310 |
|
---|
311 | // timer for auto link management
|
---|
312 | Timer::setInterval( 500 );
|
---|
313 | Timer::start();
|
---|
314 |
|
---|
315 | started = true;
|
---|
316 | state = BaseOverlayStateInvalid;
|
---|
317 | }
|
---|
318 |
|
---|
319 | void BaseOverlay::stop() {
|
---|
320 | logging_info("Stopping...");
|
---|
321 |
|
---|
322 | // stop timer
|
---|
323 | Timer::stop();
|
---|
324 |
|
---|
325 | // delete oberlay interface
|
---|
326 | if(overlayInterface != NULL) {
|
---|
327 | delete overlayInterface;
|
---|
328 | overlayInterface = NULL;
|
---|
329 | }
|
---|
330 |
|
---|
331 | // unregister at base communication
|
---|
332 | bc->unregisterMessageReceiver( this );
|
---|
333 | bc->unregisterEventListener( this );
|
---|
334 |
|
---|
335 | started = false;
|
---|
336 | state = BaseOverlayStateInvalid;
|
---|
337 | }
|
---|
338 |
|
---|
339 | bool BaseOverlay::isStarted(){
|
---|
340 | return started;
|
---|
341 | }
|
---|
342 |
|
---|
343 | // ----------------------------------------------------------------------------
|
---|
344 |
|
---|
345 | void BaseOverlay::joinSpoVNet(const SpoVNetID& id,
|
---|
346 | const EndpointDescriptor& bootstrapEp) {
|
---|
347 |
|
---|
348 | if(id != spovnetId){
|
---|
349 | logging_error("attempt to join against invalid spovnet, call initiate first");
|
---|
350 | return;
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | //ovl.visShowNodeBubble ( ovlId, nodeId, "joining..." );
|
---|
355 | logging_info( "Starting to join spovnet " << id.toString() <<
|
---|
356 | " with nodeid " << nodeId.toString());
|
---|
357 |
|
---|
358 | if(bootstrapEp.isUnspecified() && state == BaseOverlayStateInvalid){
|
---|
359 |
|
---|
360 | // bootstrap against ourselfs
|
---|
361 | logging_debug("joining spovnet locally");
|
---|
362 |
|
---|
363 | overlayInterface->joinOverlay();
|
---|
364 | state = BaseOverlayStateCompleted;
|
---|
365 | BOOST_FOREACH( NodeListener* i, nodeListeners )
|
---|
366 | i->onJoinCompleted( spovnetId );
|
---|
367 |
|
---|
368 | //ovl.visChangeNodeIcon ( ovlId, nodeId, OvlVis::ICON_ID_CAMERA );
|
---|
369 | //ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN );
|
---|
370 |
|
---|
371 | logging_debug("starting overlay bootstrap module");
|
---|
372 | overlayBootstrap.start(this, spovnetId, nodeId);
|
---|
373 | overlayBootstrap.publish(bc->getEndpointDescriptor());
|
---|
374 |
|
---|
375 | } else {
|
---|
376 |
|
---|
377 | // bootstrap against another node
|
---|
378 | logging_debug("joining spovnet remotely against " << bootstrapEp.toString());
|
---|
379 |
|
---|
380 | const LinkID& lnk = bc->establishLink( bootstrapEp );
|
---|
381 | bootstrapLinks.push_back(lnk);
|
---|
382 | logging_info("join process initiated for " << id.toString() << "...");
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | void BaseOverlay::leaveSpoVNet() {
|
---|
387 |
|
---|
388 | logging_info( "Leaving spovnet " << spovnetId );
|
---|
389 | bool ret = ( state != this->BaseOverlayStateInvalid );
|
---|
390 |
|
---|
391 | logging_debug("stopping overlay bootstrap module");
|
---|
392 | overlayBootstrap.stop();
|
---|
393 | overlayBootstrap.revoke();
|
---|
394 |
|
---|
395 | logging_debug( "Dropping all auto-links" );
|
---|
396 |
|
---|
397 | // gather all service links
|
---|
398 | vector<LinkID> servicelinks;
|
---|
399 | BOOST_FOREACH( LinkDescriptor* ld, links ) {
|
---|
400 | if( ld->service != OverlayInterface::OVERLAY_SERVICE_ID )
|
---|
401 | servicelinks.push_back( ld->overlayId );
|
---|
402 | }
|
---|
403 |
|
---|
404 | // drop all service links
|
---|
405 | BOOST_FOREACH( LinkID lnk, servicelinks )
|
---|
406 | dropLink( lnk );
|
---|
407 |
|
---|
408 | // let the node leave the spovnet overlay interface
|
---|
409 | logging_debug( "Leaving overlay" );
|
---|
410 | if( overlayInterface != NULL )
|
---|
411 | overlayInterface->leaveOverlay();
|
---|
412 |
|
---|
413 | // drop still open bootstrap links
|
---|
414 | BOOST_FOREACH( LinkID lnk, bootstrapLinks )
|
---|
415 | bc->dropLink( lnk );
|
---|
416 |
|
---|
417 | // change to inalid state
|
---|
418 | state = BaseOverlayStateInvalid;
|
---|
419 | //ovl.visShutdown( ovlId, nodeId, string("") );
|
---|
420 |
|
---|
421 | // inform all registered services of the event
|
---|
422 | BOOST_FOREACH( NodeListener* i, nodeListeners ) {
|
---|
423 | if( ret ) i->onLeaveCompleted( spovnetId );
|
---|
424 | else i->onLeaveFailed( spovnetId );
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | void BaseOverlay::createSpoVNet(const SpoVNetID& id,
|
---|
429 | const OverlayParameterSet& param,
|
---|
430 | const SecurityParameterSet& sec,
|
---|
431 | const QoSParameterSet& qos) {
|
---|
432 |
|
---|
433 | // set the state that we are an initiator, this way incoming messages are
|
---|
434 | // handled correctly
|
---|
435 | logging_info( "creating spovnet " + id.toString() <<
|
---|
436 | " with nodeid " << nodeId.toString() );
|
---|
437 |
|
---|
438 | spovnetId = id;
|
---|
439 |
|
---|
440 | overlayInterface = OverlayFactory::create( *this, param, nodeId, this );
|
---|
441 | if( overlayInterface == NULL ) {
|
---|
442 | logging_fatal( "overlay structure not supported" );
|
---|
443 | state = BaseOverlayStateInvalid;
|
---|
444 |
|
---|
445 | BOOST_FOREACH( NodeListener* i, nodeListeners )
|
---|
446 | i->onJoinFailed( spovnetId );
|
---|
447 |
|
---|
448 | return;
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | // ----------------------------------------------------------------------------
|
---|
453 |
|
---|
454 | const LinkID BaseOverlay::establishLink(
|
---|
455 | const EndpointDescriptor& ep, const NodeID& nodeid,
|
---|
456 | const ServiceID& service, const LinkID& linkid ) {
|
---|
457 |
|
---|
458 | LinkID link_id = linkid;
|
---|
459 |
|
---|
460 | // establish link via overlay
|
---|
461 | if (!nodeid.isUnspecified())
|
---|
462 | link_id = establishLink( nodeid, service, link_id );
|
---|
463 |
|
---|
464 | // establish link directly if only ep is known
|
---|
465 | if (nodeid.isUnspecified())
|
---|
466 | establishLink( ep, service, link_id );
|
---|
467 |
|
---|
468 | return link_id;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /// call base communication's establish link and add link mapping
|
---|
472 | const LinkID BaseOverlay::establishLink( const EndpointDescriptor& ep,
|
---|
473 | const ServiceID& service, const LinkID& linkid ) {
|
---|
474 |
|
---|
475 | // create a new link id if necessary
|
---|
476 | LinkID link_id = linkid;
|
---|
477 | if (link_id.isUnspecified()) link_id = LinkID::create();
|
---|
478 |
|
---|
479 | /// find a service listener
|
---|
480 | if( !communicationListeners.contains( service ) ) {
|
---|
481 | logging_error( "No listener registered for service id=" << service.toString() );
|
---|
482 | return LinkID::UNSPECIFIED;
|
---|
483 | }
|
---|
484 | CommunicationListener* listener = communicationListeners.get( service );
|
---|
485 | assert( listener != NULL );
|
---|
486 |
|
---|
487 | /// establish link and add mapping
|
---|
488 | logging_info("Establishing direct link " << link_id.toString()
|
---|
489 | << " using " << ep.toString());
|
---|
490 |
|
---|
491 | // create descriptor
|
---|
492 | LinkDescriptor* ld = addDescriptor( link_id );
|
---|
493 | ld->overlayId = link_id;
|
---|
494 | ld->communicationId = link_id;
|
---|
495 | ld->listener = listener;
|
---|
496 | ld->service = service;
|
---|
497 | bc->establishLink( ep, link_id );
|
---|
498 |
|
---|
499 | return link_id;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /// establishes a link between two arbitrary nodes
|
---|
503 | const LinkID BaseOverlay::establishLink( const NodeID& node,
|
---|
504 | const ServiceID& service, const LinkID& link_id ) {
|
---|
505 |
|
---|
506 | // do not establish a link to myself!
|
---|
507 | if (node == nodeId) return LinkID::UNSPECIFIED;
|
---|
508 |
|
---|
509 | // create a link descriptor
|
---|
510 | LinkDescriptor* ld = createLinkDescriptor( node, service, link_id );
|
---|
511 |
|
---|
512 | // create link request message with own link id
|
---|
513 | uint32_t nonce = (uint32_t)(rand() ^ (rand() << 16) ^ time(NULL));
|
---|
514 | LinkRequest link_request_msg(
|
---|
515 | nonce, &bc->getEndpointDescriptor(), false,
|
---|
516 | ld->overlayId, ld->localRelay );
|
---|
517 | OverlayMsg overlay_msg( OverlayMsg::typeLinkRequest, service, nodeId );
|
---|
518 | overlay_msg.encapsulate( &link_request_msg );
|
---|
519 | pendingLinks.insert( make_pair(nonce, ld->overlayId) );
|
---|
520 |
|
---|
521 | // debug message
|
---|
522 | logging_debug(
|
---|
523 | "Sending link request with"
|
---|
524 | << " link id=" << ld->overlayId
|
---|
525 | << " node id=" << ld->remoteNode.toString()
|
---|
526 | << " service id=" << ld->service.toString()
|
---|
527 | << " local relay id=" << ld->localRelay.toString()
|
---|
528 | << " nonce= " << nonce
|
---|
529 | );
|
---|
530 |
|
---|
531 | // sending message through new link
|
---|
532 | sendMessage( &overlay_msg, ld );
|
---|
533 |
|
---|
534 | return ld->overlayId;
|
---|
535 | }
|
---|
536 |
|
---|
537 | /// drops an established link
|
---|
538 | void BaseOverlay::dropLink(const LinkID& link) {
|
---|
539 | logging_debug( "Dropping link (initiated locally):" << link.toString() );
|
---|
540 |
|
---|
541 | // find the link item to drop
|
---|
542 | LinkDescriptor* ld = getDescriptor(link);
|
---|
543 | if( ld == NULL ) {
|
---|
544 | logging_warn( "Can't drop link, link is unknown!");
|
---|
545 | return;
|
---|
546 | }
|
---|
547 |
|
---|
548 | // delete all queued messages
|
---|
549 | if( ld->messageQueue.size() > 0 ) {
|
---|
550 | logging_warn( "Dropping link " << ld->overlayId.toString() << " that has "
|
---|
551 | << ld->messageQueue.size() << " waiting messages" );
|
---|
552 | ld->flushQueue();
|
---|
553 | }
|
---|
554 |
|
---|
555 | // inform sideport and listener
|
---|
556 | ld->listener->onLinkDown( ld->overlayId, ld->remoteNode );
|
---|
557 | sideport->onLinkDown(ld->overlayId, this->nodeId, ld->remoteNode, this->spovnetId );
|
---|
558 |
|
---|
559 | // do not drop relay links
|
---|
560 | if (!ld->usedAsRelay) {
|
---|
561 | // drop the link in base communication
|
---|
562 | if (ld->communicationUp) bc->dropLink( ld->communicationId );
|
---|
563 |
|
---|
564 | // erase descriptor
|
---|
565 | eraseDescriptor( ld->overlayId );
|
---|
566 | } else
|
---|
567 | ld->dropWhenRelaysLeft = true;
|
---|
568 | }
|
---|
569 |
|
---|
570 | // ----------------------------------------------------------------------------
|
---|
571 |
|
---|
572 | /// internal send message, always use this functions to send messages over links
|
---|
573 | seqnum_t BaseOverlay::sendMessage( const Message* message, const LinkID& link ) {
|
---|
574 | logging_debug( "Sending data message on link " << link.toString() );
|
---|
575 |
|
---|
576 | // get the mapping for this link
|
---|
577 | LinkDescriptor* ld = getDescriptor(link);
|
---|
578 | if( ld == NULL ) {
|
---|
579 | logging_error("Could not send message. "
|
---|
580 | << "Link not found id=" << link.toString());
|
---|
581 | return -1;
|
---|
582 | }
|
---|
583 |
|
---|
584 | // check if the link is up yet, if its an auto link queue message
|
---|
585 | if( !ld->up ) {
|
---|
586 | ld->markAsUsed();
|
---|
587 | if( ld->autolink ) {
|
---|
588 | logging_info("Auto-link " << link.toString() << " not up, queue message");
|
---|
589 | Data data = data_serialize( message );
|
---|
590 | const_cast<Message*>(message)->dropPayload();
|
---|
591 | ld->messageQueue.push_back( new Message(data) );
|
---|
592 | } else {
|
---|
593 | logging_error("Link " << link.toString() << " not up, drop message");
|
---|
594 | }
|
---|
595 | return -1;
|
---|
596 | }
|
---|
597 |
|
---|
598 | // compile overlay message (has service and node id)
|
---|
599 | OverlayMsg overmsg( OverlayMsg::typeData, ld->service, nodeId );
|
---|
600 | overmsg.encapsulate( const_cast<Message*>(message) );
|
---|
601 |
|
---|
602 | // send message over relay/direct/overlay
|
---|
603 | return sendMessage( &overmsg, ld );
|
---|
604 | }
|
---|
605 |
|
---|
606 | seqnum_t BaseOverlay::sendMessage(const Message* message,
|
---|
607 | const NodeID& node, const ServiceID& service) {
|
---|
608 |
|
---|
609 | // find link for node and service
|
---|
610 | LinkDescriptor* ld = getAutoDescriptor( node, service );
|
---|
611 |
|
---|
612 | // if we found no link, create an auto link
|
---|
613 | if( ld == NULL ) {
|
---|
614 |
|
---|
615 | // debug output
|
---|
616 | logging_info( "No link to send message to node "
|
---|
617 | << node.toString() << " found for service "
|
---|
618 | << service.toString() << ". Creating auto link ..."
|
---|
619 | );
|
---|
620 |
|
---|
621 | // this will call onlinkup on us, if everything worked we now have a mapping
|
---|
622 | LinkID link = LinkID::create();
|
---|
623 |
|
---|
624 | // call base overlay to create a link
|
---|
625 | link = establishLink( node, service, link );
|
---|
626 | ld = getDescriptor( link );
|
---|
627 | if( ld == NULL ) {
|
---|
628 | logging_error( "Failed to establish auto-link.");
|
---|
629 | return -1;
|
---|
630 | }
|
---|
631 | ld->autolink = true;
|
---|
632 |
|
---|
633 | logging_debug( "Auto-link establishment in progress to node "
|
---|
634 | << node.toString() << " with link id=" << link.toString() );
|
---|
635 | }
|
---|
636 | assert(ld != NULL);
|
---|
637 |
|
---|
638 | // mark the link as used, as we now send a message through it
|
---|
639 | ld->markAsUsed();
|
---|
640 |
|
---|
641 | // send / queue message
|
---|
642 | return sendMessage( message, ld->overlayId );
|
---|
643 | }
|
---|
644 |
|
---|
645 | // ----------------------------------------------------------------------------
|
---|
646 |
|
---|
647 | const EndpointDescriptor& BaseOverlay::getEndpointDescriptor(
|
---|
648 | const LinkID& link) const {
|
---|
649 |
|
---|
650 | // return own end-point descriptor
|
---|
651 | if( link == LinkID::UNSPECIFIED )
|
---|
652 | return bc->getEndpointDescriptor();
|
---|
653 |
|
---|
654 | // find link descriptor. not found -> return unspecified
|
---|
655 | const LinkDescriptor* ld = getDescriptor(link);
|
---|
656 | if (ld==NULL) return EndpointDescriptor::UNSPECIFIED();
|
---|
657 |
|
---|
658 | // return endpoint-descriptor from base communication
|
---|
659 | return bc->getEndpointDescriptor( ld->communicationId );
|
---|
660 | }
|
---|
661 |
|
---|
662 | const EndpointDescriptor& BaseOverlay::getEndpointDescriptor(
|
---|
663 | const NodeID& node) const {
|
---|
664 |
|
---|
665 | // return own end-point descriptor
|
---|
666 | if( node == nodeId || node == NodeID::UNSPECIFIED )
|
---|
667 | return bc->getEndpointDescriptor();
|
---|
668 |
|
---|
669 | // no joined and request remote descriptor? -> fail!
|
---|
670 | if( overlayInterface == NULL ) {
|
---|
671 | logging_error( "overlay interface not set, cannot resolve endpoint" );
|
---|
672 | return EndpointDescriptor::UNSPECIFIED();
|
---|
673 | }
|
---|
674 |
|
---|
675 | // resolve end-point descriptor from the base-overlay routing table
|
---|
676 | return overlayInterface->resolveNode( node );
|
---|
677 | }
|
---|
678 |
|
---|
679 | // ----------------------------------------------------------------------------
|
---|
680 |
|
---|
681 | bool BaseOverlay::registerSidePort(SideportListener* _sideport) {
|
---|
682 | sideport = _sideport;
|
---|
683 | _sideport->configure( this );
|
---|
684 | }
|
---|
685 |
|
---|
686 | bool BaseOverlay::unregisterSidePort(SideportListener* _sideport) {
|
---|
687 | sideport = &SideportListener::DEFAULT;
|
---|
688 | }
|
---|
689 |
|
---|
690 | // ----------------------------------------------------------------------------
|
---|
691 |
|
---|
692 | bool BaseOverlay::bind(CommunicationListener* listener, const ServiceID& sid) {
|
---|
693 | logging_debug( "binding communication listener " << listener
|
---|
694 | << " on serviceid " << sid.toString() );
|
---|
695 |
|
---|
696 | if( communicationListeners.contains( sid ) ) {
|
---|
697 | logging_error( "some listener already registered for service id "
|
---|
698 | << sid.toString() );
|
---|
699 | return false;
|
---|
700 | }
|
---|
701 |
|
---|
702 | communicationListeners.registerItem( listener, sid );
|
---|
703 | return true;
|
---|
704 | }
|
---|
705 |
|
---|
706 |
|
---|
707 | bool BaseOverlay::unbind(CommunicationListener* listener, const ServiceID& sid) {
|
---|
708 | logging_debug( "unbinding listener " << listener << " from serviceid " << sid.toString() );
|
---|
709 |
|
---|
710 | if( !communicationListeners.contains( sid ) ) {
|
---|
711 | logging_warn( "cannot unbind listener. no listener registered on service id " << sid.toString() );
|
---|
712 | return false;
|
---|
713 | }
|
---|
714 |
|
---|
715 | if( communicationListeners.get(sid) != listener ) {
|
---|
716 | logging_warn( "listener bound to service id " << sid.toString()
|
---|
717 | << " is different than listener trying to unbind" );
|
---|
718 | return false;
|
---|
719 | }
|
---|
720 |
|
---|
721 | communicationListeners.unregisterItem( sid );
|
---|
722 | return true;
|
---|
723 | }
|
---|
724 |
|
---|
725 | // ----------------------------------------------------------------------------
|
---|
726 |
|
---|
727 | bool BaseOverlay::bind(NodeListener* listener) {
|
---|
728 | logging_debug( "Binding node listener " << listener );
|
---|
729 |
|
---|
730 | // already bound? yes-> warning
|
---|
731 | NodeListenerVector::iterator i =
|
---|
732 | find( nodeListeners.begin(), nodeListeners.end(), listener );
|
---|
733 | if( i != nodeListeners.end() ) {
|
---|
734 | logging_warn("Node listener " << listener << " is already bound!" );
|
---|
735 | return false;
|
---|
736 | }
|
---|
737 |
|
---|
738 | // no-> add
|
---|
739 | nodeListeners.push_back( listener );
|
---|
740 | return true;
|
---|
741 | }
|
---|
742 |
|
---|
743 | bool BaseOverlay::unbind(NodeListener* listener) {
|
---|
744 | logging_debug( "Unbinding node listener " << listener );
|
---|
745 |
|
---|
746 | // already unbound? yes-> warning
|
---|
747 | NodeListenerVector::iterator i = find( nodeListeners.begin(), nodeListeners.end(), listener );
|
---|
748 | if( i == nodeListeners.end() ) {
|
---|
749 | logging_warn( "Node listener " << listener << " is not bound!" );
|
---|
750 | return false;
|
---|
751 | }
|
---|
752 |
|
---|
753 | // no-> remove
|
---|
754 | nodeListeners.erase( i );
|
---|
755 | return true;
|
---|
756 | }
|
---|
757 |
|
---|
758 | // ----------------------------------------------------------------------------
|
---|
759 |
|
---|
760 | void BaseOverlay::onLinkUp(const LinkID& id,
|
---|
761 | const address_v* local, const address_v* remote) {
|
---|
762 | logging_debug( "Link up with base communication link id=" << id );
|
---|
763 |
|
---|
764 | // get descriptor for link
|
---|
765 | LinkDescriptor* ld = getDescriptor(id, true);
|
---|
766 |
|
---|
767 | // handle bootstrap link we initiated
|
---|
768 | if( std::find(bootstrapLinks.begin(), bootstrapLinks.end(), id) != bootstrapLinks.end() ){
|
---|
769 | logging_info(
|
---|
770 | "Join has been initiated by me and the link is now up. " <<
|
---|
771 | "Sending out join request for SpoVNet " << spovnetId.toString()
|
---|
772 | );
|
---|
773 |
|
---|
774 | // send join request message
|
---|
775 | OverlayMsg overlayMsg( OverlayMsg::typeJoinRequest, nodeId );
|
---|
776 | JoinRequest joinRequest( spovnetId, nodeId );
|
---|
777 | overlayMsg.encapsulate( &joinRequest );
|
---|
778 | bc->sendMessage( id, &overlayMsg );
|
---|
779 | return;
|
---|
780 | }
|
---|
781 |
|
---|
782 | // no link found? -> link establishment from remote, add one!
|
---|
783 | if (ld == NULL) {
|
---|
784 | ld = addDescriptor( id );
|
---|
785 | logging_debug( "onLinkUp (remote request) descriptor: " << ld );
|
---|
786 |
|
---|
787 | // update descriptor
|
---|
788 | ld->fromRemote = true;
|
---|
789 | ld->communicationId = id;
|
---|
790 | ld->communicationUp = true;
|
---|
791 | ld->markAsUsed();
|
---|
792 |
|
---|
793 | // in this case, do not inform listener, since service it unknown
|
---|
794 | // -> wait for update message!
|
---|
795 |
|
---|
796 | // link mapping found? -> send update message with node-id and service id
|
---|
797 | } else {
|
---|
798 | logging_debug( "onLinkUp descriptor (initiated locally):" << ld );
|
---|
799 |
|
---|
800 | // note: necessary to validate the link on the remote side!
|
---|
801 | logging_debug( "Sending out update" <<
|
---|
802 | " for service " << ld->service.toString() <<
|
---|
803 | " with local node id " << nodeId.toString() <<
|
---|
804 | " on link " << ld->overlayId.toString() );
|
---|
805 |
|
---|
806 | // update descriptor
|
---|
807 | ld->markAsUsed();
|
---|
808 | ld->communicationUp = true;
|
---|
809 |
|
---|
810 | // if link is a relayed link ->convert to direct link
|
---|
811 | if (ld->relay) {
|
---|
812 | logging_info( "Converting to direct link: " << ld );
|
---|
813 | ld->up = true;
|
---|
814 | ld->relay = false;
|
---|
815 | ld->localRelay = NodeID::UNSPECIFIED;
|
---|
816 | OverlayMsg overMsg( OverlayMsg::typeDirectLink, ld->service, nodeId );
|
---|
817 | overMsg.setRelayLink( ld->remoteLinkId );
|
---|
818 | bc->sendMessage( ld->communicationId, &overMsg );
|
---|
819 | }
|
---|
820 |
|
---|
821 | // compile and send update message
|
---|
822 | OverlayMsg overlayMsg( OverlayMsg::typeUpdate, ld->service, nodeId );
|
---|
823 | overlayMsg.setAutoLink( ld->autolink );
|
---|
824 | bc->sendMessage( ld->communicationId, &overlayMsg );
|
---|
825 | }
|
---|
826 | }
|
---|
827 |
|
---|
828 | void BaseOverlay::onLinkDown(const LinkID& id,
|
---|
829 | const address_v* local, const address_v* remote) {
|
---|
830 |
|
---|
831 | // erase bootstrap links
|
---|
832 | vector<LinkID>::iterator it = std::find( bootstrapLinks.begin(), bootstrapLinks.end(), id );
|
---|
833 | if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it );
|
---|
834 |
|
---|
835 | // get descriptor for link
|
---|
836 | LinkDescriptor* ld = getDescriptor(id, true);
|
---|
837 | if ( ld == NULL ) return; // not found? ->ignore!
|
---|
838 | logging_info( "onLinkDown descriptor: " << ld );
|
---|
839 |
|
---|
840 | // inform listeners about link down
|
---|
841 | ld->communicationUp = false;
|
---|
842 | ld->listener->onLinkDown( ld->overlayId, ld->remoteNode );
|
---|
843 | sideport->onLinkDown( id, this->nodeId, ld->remoteNode, this->spovnetId );
|
---|
844 |
|
---|
845 | // delete all queued messages (auto links)
|
---|
846 | if( ld->messageQueue.size() > 0 ) {
|
---|
847 | logging_warn( "Dropping link " << id.toString() << " that has "
|
---|
848 | << ld->messageQueue.size() << " waiting messages" );
|
---|
849 | ld->flushQueue();
|
---|
850 | }
|
---|
851 |
|
---|
852 | // erase mapping
|
---|
853 | eraseDescriptor(ld->overlayId);
|
---|
854 | }
|
---|
855 |
|
---|
856 | void BaseOverlay::onLinkChanged(const LinkID& id,
|
---|
857 | const address_v* oldlocal, const address_v* newlocal,
|
---|
858 | const address_v* oldremote, const address_v* newremote) {
|
---|
859 |
|
---|
860 | // get descriptor for link
|
---|
861 | LinkDescriptor* ld = getDescriptor(id, true);
|
---|
862 | if ( ld == NULL ) return; // not found? ->ignore!
|
---|
863 | logging_debug( "onLinkChanged descriptor: " << ld );
|
---|
864 |
|
---|
865 | // inform listeners
|
---|
866 | ld->listener->onLinkChanged( ld->overlayId, ld->remoteNode );
|
---|
867 | sideport->onLinkChanged( id, this->nodeId, ld->remoteNode, this->spovnetId );
|
---|
868 |
|
---|
869 | // autolinks: refresh timestamp
|
---|
870 | ld->markAsUsed();
|
---|
871 | }
|
---|
872 |
|
---|
873 | void BaseOverlay::onLinkFail(const LinkID& id,
|
---|
874 | const address_v* local, const address_v* remote) {
|
---|
875 | logging_debug( "Link fail with base communication link id=" << id );
|
---|
876 |
|
---|
877 | // erase bootstrap links
|
---|
878 | vector<LinkID>::iterator it = std::find( bootstrapLinks.begin(), bootstrapLinks.end(), id );
|
---|
879 | if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it );
|
---|
880 |
|
---|
881 | // get descriptor for link
|
---|
882 | LinkDescriptor* ld = getDescriptor(id, true);
|
---|
883 | if ( ld == NULL ) return; // not found? ->ignore!
|
---|
884 | logging_debug( "Link failed id=" << ld->overlayId.toString() );
|
---|
885 |
|
---|
886 | // inform listeners
|
---|
887 | ld->listener->onLinkFail( ld->overlayId, ld->remoteNode );
|
---|
888 | sideport->onLinkFail( id, this->nodeId, ld->remoteNode, this->spovnetId );
|
---|
889 |
|
---|
890 | // autolinks: refresh timestamp
|
---|
891 | ld->markAsUsed();
|
---|
892 | }
|
---|
893 |
|
---|
894 | void BaseOverlay::onLinkQoSChanged(const LinkID& id, const address_v* local,
|
---|
895 | const address_v* remote, const QoSParameterSet& qos) {
|
---|
896 | logging_debug( "Link quality changed with base communication link id=" << id );
|
---|
897 |
|
---|
898 | // get descriptor for link
|
---|
899 | LinkDescriptor* ld = getDescriptor(id, true);
|
---|
900 | if ( ld == NULL ) return; // not found? ->ignore!
|
---|
901 | logging_debug( "Link quality changed id=" << ld->overlayId.toString() );
|
---|
902 |
|
---|
903 | // autolinks: refresh timestamp
|
---|
904 | ld->markAsUsed();
|
---|
905 | }
|
---|
906 |
|
---|
907 | bool BaseOverlay::onLinkRequest( const LinkID& id, const address_v* local,
|
---|
908 | const address_v* remote ) {
|
---|
909 | logging_debug("Accepting link request from " << remote->to_string() );
|
---|
910 | return true;
|
---|
911 | }
|
---|
912 |
|
---|
913 | /// handles a message from base communication
|
---|
914 | bool BaseOverlay::receiveMessage(const Message* message,
|
---|
915 | const LinkID& link, const NodeID& ) {
|
---|
916 | // get descriptor for link
|
---|
917 | LinkDescriptor* ld = getDescriptor( link, true );
|
---|
918 |
|
---|
919 | // link known?
|
---|
920 | if (ld == NULL) { // no-> handle with unspecified params
|
---|
921 | logging_debug("Received message from base communication, link descriptor unknown" );
|
---|
922 | return handleMessage( message, LinkID::UNSPECIFIED, link, NodeID::UNSPECIFIED );
|
---|
923 | } else { // yes -> handle with overlay link id
|
---|
924 | logging_debug("Received message from base communication, link id=" << ld->overlayId.toString() );
|
---|
925 | return handleMessage( message, ld->overlayId, link, NodeID::UNSPECIFIED );
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | // ----------------------------------------------------------------------------
|
---|
930 |
|
---|
931 | /// handles a message from an overlay
|
---|
932 | void BaseOverlay::incomingRouteMessage( Message* msg, const LinkID& link, const NodeID& source ) {
|
---|
933 | logging_debug("Received message from overlay -- "
|
---|
934 | << " link id=" << link.toString()
|
---|
935 | << " node id=" << source.toString() );
|
---|
936 | handleMessage( msg, link, LinkID::UNSPECIFIED, source );
|
---|
937 | }
|
---|
938 |
|
---|
939 | // ----------------------------------------------------------------------------
|
---|
940 |
|
---|
941 | /// handles an incoming message
|
---|
942 | bool BaseOverlay::handleMessage( const Message* message,
|
---|
943 | const LinkID& boLink, const LinkID& bcLink, const NodeID& remoteNode ) {
|
---|
944 | logging_debug( "Handling message: " << message->toString());
|
---|
945 |
|
---|
946 | bool ret = false;
|
---|
947 |
|
---|
948 | // decapsulate overlay message
|
---|
949 | OverlayMsg* overlayMsg =
|
---|
950 | const_cast<Message*>(message)->decapsulate<OverlayMsg>();
|
---|
951 | if( overlayMsg == NULL ) return false;
|
---|
952 |
|
---|
953 | // mark the link as in action
|
---|
954 | LinkDescriptor* ld = getDescriptor(boLink);
|
---|
955 | if (ld == NULL) ld = getDescriptor(bcLink, true);
|
---|
956 | if (ld != NULL) {
|
---|
957 | ld->markAsUsed();
|
---|
958 | ld->markAlive();
|
---|
959 | }
|
---|
960 |
|
---|
961 | switch ( overlayMsg->getType() ) {
|
---|
962 | // ---------------------------------------------------------------------
|
---|
963 | // Handle spovnet instance join requests
|
---|
964 | // ---------------------------------------------------------------------
|
---|
965 | case OverlayMsg::typeJoinRequest: {
|
---|
966 |
|
---|
967 | // decapsulate message
|
---|
968 | JoinRequest* joinReq = overlayMsg->decapsulate<JoinRequest>();
|
---|
969 | logging_info( "Received join request for spovnet " <<
|
---|
970 | joinReq->getSpoVNetID().toString() );
|
---|
971 |
|
---|
972 | // check spovnet id
|
---|
973 | if( joinReq->getSpoVNetID() != spovnetId ) {
|
---|
974 | logging_error(
|
---|
975 | "Received join request for spovnet we don't handle " <<
|
---|
976 | joinReq->getSpoVNetID().toString() );
|
---|
977 | ret = false;
|
---|
978 | break;
|
---|
979 | }
|
---|
980 |
|
---|
981 | // TODO: here you can implement mechanisms to deny joining of a node
|
---|
982 | bool allow = true;
|
---|
983 | logging_info( "Sending join reply for spovnet " <<
|
---|
984 | spovnetId.toString() << " to node " <<
|
---|
985 | overlayMsg->getSourceNode().toString() <<
|
---|
986 | ". Result: " << (allow ? "allowed" : "denied") );
|
---|
987 | joiningNodes.push_back( overlayMsg->getSourceNode() );
|
---|
988 |
|
---|
989 | // return overlay parameters
|
---|
990 | assert( overlayInterface != NULL );
|
---|
991 | logging_debug( "Using bootstrap end-point "
|
---|
992 | << getEndpointDescriptor().toString() )
|
---|
993 | OverlayParameterSet parameters = overlayInterface->getParameters();
|
---|
994 | OverlayMsg retmsg( OverlayMsg::typeJoinReply, nodeId );
|
---|
995 | JoinReply replyMsg( spovnetId, parameters,
|
---|
996 | allow, getEndpointDescriptor() );
|
---|
997 | retmsg.encapsulate(&replyMsg);
|
---|
998 | bc->sendMessage( bcLink, &retmsg );
|
---|
999 | ret = true;
|
---|
1000 | break;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | // ---------------------------------------------------------------------
|
---|
1004 | // handle replies to spovnet instance join requests
|
---|
1005 | // ---------------------------------------------------------------------
|
---|
1006 | case OverlayMsg::typeJoinReply: {
|
---|
1007 |
|
---|
1008 | // decapsulate message
|
---|
1009 | logging_debug("received join reply message");
|
---|
1010 | JoinReply* replyMsg = overlayMsg->decapsulate<JoinReply>();
|
---|
1011 |
|
---|
1012 | // correct spovnet?
|
---|
1013 | if( replyMsg->getSpoVNetID() != spovnetId ) { // no-> fail
|
---|
1014 | logging_error( "Received SpoVNet join reply for " <<
|
---|
1015 | replyMsg->getSpoVNetID().toString() <<
|
---|
1016 | " != " << spovnetId.toString() );
|
---|
1017 | ret = false;
|
---|
1018 | delete replyMsg;
|
---|
1019 | break;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | // access granted? no -> fail
|
---|
1023 | if( !replyMsg->getJoinAllowed() ) {
|
---|
1024 | logging_error( "Our join request has been denied" );
|
---|
1025 |
|
---|
1026 | // drop initiator link
|
---|
1027 |
|
---|
1028 | if(bcLink != LinkID::UNSPECIFIED){
|
---|
1029 | bc->dropLink( bcLink );
|
---|
1030 |
|
---|
1031 | vector<LinkID>::iterator it = std::find(
|
---|
1032 | bootstrapLinks.begin(), bootstrapLinks.end(), bcLink);
|
---|
1033 | if( it != bootstrapLinks.end() )
|
---|
1034 | bootstrapLinks.erase(it);
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | // inform all registered services of the event
|
---|
1038 | BOOST_FOREACH( NodeListener* i, nodeListeners )
|
---|
1039 | i->onJoinFailed( spovnetId );
|
---|
1040 |
|
---|
1041 | ret = true;
|
---|
1042 | delete replyMsg;
|
---|
1043 | break;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | // access has been granted -> continue!
|
---|
1047 | logging_info("Join request has been accepted for spovnet " <<
|
---|
1048 | spovnetId.toString() );
|
---|
1049 |
|
---|
1050 | logging_debug( "Using bootstrap end-point "
|
---|
1051 | << replyMsg->getBootstrapEndpoint().toString() );
|
---|
1052 |
|
---|
1053 | //
|
---|
1054 | // create overlay structure from spovnet parameter set
|
---|
1055 | // if we have not boostrapped yet against some other node
|
---|
1056 | //
|
---|
1057 |
|
---|
1058 | if( overlayInterface == NULL ){
|
---|
1059 |
|
---|
1060 | logging_debug("first-time bootstrapping");
|
---|
1061 |
|
---|
1062 | overlayInterface = OverlayFactory::create(
|
---|
1063 | *this, replyMsg->getParam(), nodeId, this );
|
---|
1064 |
|
---|
1065 | // overlay structure supported? no-> fail!
|
---|
1066 | if( overlayInterface == NULL ) {
|
---|
1067 | logging_error( "overlay structure not supported" );
|
---|
1068 |
|
---|
1069 | if(bcLink != LinkID::UNSPECIFIED){
|
---|
1070 | bc->dropLink( bcLink );
|
---|
1071 |
|
---|
1072 | vector<LinkID>::iterator it = std::find(
|
---|
1073 | bootstrapLinks.begin(), bootstrapLinks.end(), bcLink);
|
---|
1074 | if( it != bootstrapLinks.end() )
|
---|
1075 | bootstrapLinks.erase(it);
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | // inform all registered services of the event
|
---|
1079 | BOOST_FOREACH( NodeListener* i, nodeListeners )
|
---|
1080 | i->onJoinFailed( spovnetId );
|
---|
1081 |
|
---|
1082 | delete replyMsg;
|
---|
1083 | ret = true;
|
---|
1084 | break;
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | // everything ok-> join the overlay!
|
---|
1088 | state = BaseOverlayStateCompleted;
|
---|
1089 | overlayInterface->createOverlay();
|
---|
1090 |
|
---|
1091 | overlayInterface->joinOverlay( replyMsg->getBootstrapEndpoint() );
|
---|
1092 |
|
---|
1093 | // update ovlvis
|
---|
1094 | //ovl.visChangeNodeColor( ovlId, nodeId, OvlVis::NODE_COLORS_GREEN);
|
---|
1095 |
|
---|
1096 | // inform all registered services of the event
|
---|
1097 | BOOST_FOREACH( NodeListener* i, nodeListeners )
|
---|
1098 | i->onJoinCompleted( spovnetId );
|
---|
1099 |
|
---|
1100 | delete replyMsg;
|
---|
1101 |
|
---|
1102 | } else {
|
---|
1103 |
|
---|
1104 | // this is not the first bootstrap, just join the additional node
|
---|
1105 | logging_debug("not first-time bootstrapping");
|
---|
1106 | overlayInterface->joinOverlay( replyMsg->getBootstrapEndpoint() );
|
---|
1107 |
|
---|
1108 | delete replyMsg;
|
---|
1109 |
|
---|
1110 | } // if( overlayInterface == NULL )
|
---|
1111 |
|
---|
1112 | ret = true;
|
---|
1113 | break;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | // ---------------------------------------------------------------------
|
---|
1117 | // handle data forward messages
|
---|
1118 | // ---------------------------------------------------------------------
|
---|
1119 | case OverlayMsg::typeData: {
|
---|
1120 |
|
---|
1121 | // get service
|
---|
1122 | const ServiceID& service = overlayMsg->getService();
|
---|
1123 | logging_debug( "received data for service " << service.toString() );
|
---|
1124 |
|
---|
1125 | // find listener
|
---|
1126 | CommunicationListener* listener =
|
---|
1127 | communicationListeners.get( service );
|
---|
1128 | if( listener == NULL ) {
|
---|
1129 | ret = true;
|
---|
1130 | break;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | // delegate data message
|
---|
1134 | listener->onMessage( overlayMsg,
|
---|
1135 | overlayMsg->getSourceNode(), ld->overlayId );
|
---|
1136 |
|
---|
1137 | ret = true;
|
---|
1138 | break;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | // ---------------------------------------------------------------------
|
---|
1142 | // handle update messages for link establishment
|
---|
1143 | // ---------------------------------------------------------------------
|
---|
1144 | case OverlayMsg::typeUpdate: {
|
---|
1145 | // get info
|
---|
1146 | const NodeID& sourcenode = overlayMsg->getSourceNode();
|
---|
1147 | const ServiceID& service = overlayMsg->getService();
|
---|
1148 |
|
---|
1149 | // no link descriptor available -> error!
|
---|
1150 | if( ld == NULL ) {
|
---|
1151 | logging_warn( "received overlay update message for link for "
|
---|
1152 | << "which we have no mapping" );
|
---|
1153 | ret = false;
|
---|
1154 | break;
|
---|
1155 | }
|
---|
1156 | logging_debug("Received type update message on link " << ld );
|
---|
1157 |
|
---|
1158 | // update our link mapping information for this link
|
---|
1159 | bool changed =
|
---|
1160 | ( ld->remoteNode != sourcenode ) || ( ld->service != service );
|
---|
1161 | ld->remoteNode = sourcenode;
|
---|
1162 | ld->service = service;
|
---|
1163 | ld->autolink = overlayMsg->isAutoLink();
|
---|
1164 |
|
---|
1165 | // if our link information changed, we send out an update, too
|
---|
1166 | if( changed ) {
|
---|
1167 | OverlayMsg overMsg( OverlayMsg::typeUpdate, ld->service, nodeId );
|
---|
1168 | overMsg.setAutoLink(ld->autolink);
|
---|
1169 | bc->sendMessage( ld->communicationId, &overMsg );
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | // service registered? no-> error!
|
---|
1173 | if( !communicationListeners.contains( service ) ) {
|
---|
1174 | logging_warn( "Link up: event listener has not been registered" );
|
---|
1175 | ret = false;
|
---|
1176 | break;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | // default or no service registered?
|
---|
1180 | CommunicationListener* listener = communicationListeners.get( service );
|
---|
1181 | if( listener == NULL || listener == &CommunicationListener::DEFAULT ) {
|
---|
1182 | logging_warn("Link up: event listener is default or null!" );
|
---|
1183 | ret = true;
|
---|
1184 | break;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | // update descriptor
|
---|
1188 | ld->listener = listener;
|
---|
1189 | ld->markAsUsed();
|
---|
1190 | ld->markAlive();
|
---|
1191 |
|
---|
1192 | // ask the service whether it wants to accept this link
|
---|
1193 | if( !listener->onLinkRequest(sourcenode) ) {
|
---|
1194 |
|
---|
1195 | logging_debug("Link id=" << ld->overlayId.toString() <<
|
---|
1196 | " has been denied by service " << service.toString() << ", dropping link");
|
---|
1197 |
|
---|
1198 | // prevent onLinkDown calls to the service
|
---|
1199 | ld->listener = &CommunicationListener::DEFAULT;
|
---|
1200 |
|
---|
1201 | // drop the link
|
---|
1202 | dropLink( ld->overlayId );
|
---|
1203 | ret = true;
|
---|
1204 | break;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | // set link up
|
---|
1208 | ld->up = true;
|
---|
1209 | logging_debug(
|
---|
1210 | "Link " << ld->overlayId.toString()
|
---|
1211 | << " has been accepted by service " << service.toString()
|
---|
1212 | << " and is now up"
|
---|
1213 | );
|
---|
1214 |
|
---|
1215 | // auto links: link has been accepted -> send queued messages
|
---|
1216 | if( ld->messageQueue.size() > 0 ) {
|
---|
1217 | logging_info( "sending out queued messages on link " <<
|
---|
1218 | ld->overlayId.toString() );
|
---|
1219 | BOOST_FOREACH( Message* msg, ld->messageQueue ) {
|
---|
1220 | sendMessage( msg, ld->overlayId );
|
---|
1221 | delete msg;
|
---|
1222 | }
|
---|
1223 | ld->messageQueue.clear();
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | // call the notification functions
|
---|
1227 | listener->onLinkUp( ld->overlayId, sourcenode );
|
---|
1228 | sideport->onLinkUp( ld->overlayId, nodeId, sourcenode, this->spovnetId );
|
---|
1229 |
|
---|
1230 | ret = true;
|
---|
1231 | break;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | // ---------------------------------------------------------------------
|
---|
1235 | // handle link request forwarded through the overlay
|
---|
1236 | // ---------------------------------------------------------------------
|
---|
1237 | case OverlayMsg::typeLinkRequest: {
|
---|
1238 |
|
---|
1239 | logging_debug( "received link request on link" );
|
---|
1240 |
|
---|
1241 | // decapsulate message
|
---|
1242 | LinkRequest* linkReq = overlayMsg->decapsulate<LinkRequest>();
|
---|
1243 | const ServiceID& service = overlayMsg->getService();
|
---|
1244 |
|
---|
1245 | // is request reply?
|
---|
1246 | if ( linkReq->isReply() ) {
|
---|
1247 |
|
---|
1248 | // find link
|
---|
1249 | PendingLinkMap::iterator i = pendingLinks.find( linkReq->getNonce() );
|
---|
1250 | if ( i == pendingLinks.end() ) {
|
---|
1251 | logging_error( "Nonce not found in link request" );
|
---|
1252 | ret = true;
|
---|
1253 | break;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | // debug message
|
---|
1257 | logging_debug( "Link request reply received. Establishing link "
|
---|
1258 | << i->second << " to " << (linkReq->getEndpoint()->toString())
|
---|
1259 | << " for service " << service.toString()
|
---|
1260 | << " with nonce " << linkReq->getNonce()
|
---|
1261 | << " using relay " << linkReq->getRelay().toString()
|
---|
1262 | << " and remote link id=" << linkReq->getRemoteLinkId()
|
---|
1263 | );
|
---|
1264 |
|
---|
1265 | // get descriptor
|
---|
1266 | LinkDescriptor* ldn = getDescriptor(i->second);
|
---|
1267 | if (ldn==NULL) {
|
---|
1268 | delete linkReq;
|
---|
1269 | ret = true;
|
---|
1270 | break;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | // check if link request reply has a relay node ...
|
---|
1274 | if (!linkReq->getRelay().isUnspecified()) { // yes->
|
---|
1275 | ldn->up = true;
|
---|
1276 | ldn->relay = true;
|
---|
1277 | if (ldn->localRelay.isUnspecified()) {
|
---|
1278 | logging_error("On LinkRequest reply: local relay is unspecifed on link " << ldn );
|
---|
1279 | showLinkState();
|
---|
1280 | }
|
---|
1281 | ldn->remoteRelay = linkReq->getRelay();
|
---|
1282 | ldn->remoteLinkId = linkReq->getRemoteLinkId();
|
---|
1283 | ldn->remoteNode = overlayMsg->getSourceNode();
|
---|
1284 |
|
---|
1285 | ldn->markAlive();
|
---|
1286 |
|
---|
1287 | // compile and send update message
|
---|
1288 | OverlayMsg _overlayMsg( OverlayMsg::typeUpdate, ldn->service, nodeId );
|
---|
1289 | _overlayMsg.setAutoLink(ldn->autolink);
|
---|
1290 | sendMessage( &_overlayMsg, ldn );
|
---|
1291 |
|
---|
1292 | // auto links: link has been accepted -> send queued messages
|
---|
1293 | if( ldn->messageQueue.size() > 0 ) {
|
---|
1294 | logging_info( "Sending out queued messages on link " <<
|
---|
1295 | ldn->overlayId.toString() );
|
---|
1296 | BOOST_FOREACH( Message* msg, ldn->messageQueue ) {
|
---|
1297 | sendMessage( msg, ldn->overlayId );
|
---|
1298 | delete msg;
|
---|
1299 | }
|
---|
1300 | ldn->messageQueue.clear();
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | ldn->listener->onLinkUp( ldn->overlayId, ldn->remoteNode );
|
---|
1304 |
|
---|
1305 | // try to establish a direct link
|
---|
1306 | ldn->communicationId =
|
---|
1307 | bc->establishLink( *linkReq->getEndpoint(), i->second );
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | // no relay node-> use overlay routing
|
---|
1311 | else {
|
---|
1312 | ldn->up = true;
|
---|
1313 |
|
---|
1314 | // establish direct link
|
---|
1315 | ldn->communicationId =
|
---|
1316 | bc->establishLink( *linkReq->getEndpoint(), i->second );
|
---|
1317 | }
|
---|
1318 | } else {
|
---|
1319 | logging_debug( "Link request received from node id="
|
---|
1320 | << overlayMsg->getSourceNode() );
|
---|
1321 |
|
---|
1322 | // create link descriptor
|
---|
1323 | LinkDescriptor* ldn =
|
---|
1324 | createLinkDescriptor(overlayMsg->getSourceNode(),
|
---|
1325 | overlayMsg->getService(), LinkID::UNSPECIFIED );
|
---|
1326 | assert(!ldn->overlayId.isUnspecified());
|
---|
1327 |
|
---|
1328 | // create reply message
|
---|
1329 | OverlayMsg overlay_msg( OverlayMsg::typeLinkRequest, service, nodeId );
|
---|
1330 | LinkRequest link_request_msg(
|
---|
1331 | linkReq->getNonce(),
|
---|
1332 | &bc->getEndpointDescriptor(),
|
---|
1333 | true, ldn->overlayId, ldn->localRelay
|
---|
1334 | );
|
---|
1335 | overlay_msg.encapsulate( &link_request_msg );
|
---|
1336 |
|
---|
1337 | // debug message
|
---|
1338 | logging_debug( "Sending LinkRequest reply for link with nonce " <<
|
---|
1339 | linkReq->getNonce() );
|
---|
1340 |
|
---|
1341 | // if this is a relay link-> update information & inform listeners
|
---|
1342 | if (!linkReq->getRelay().isUnspecified()) {
|
---|
1343 | // set flags
|
---|
1344 | ldn->up = true;
|
---|
1345 | ldn->relay = true;
|
---|
1346 | if (ldn->localRelay.isUnspecified()) {
|
---|
1347 | logging_error("On LinkRequest request: local relay is unspecifed on link " << ldn );
|
---|
1348 | showLinkState();
|
---|
1349 | }
|
---|
1350 | ldn->remoteRelay = linkReq->getRelay();
|
---|
1351 | ldn->remoteNode = overlayMsg->getSourceNode();
|
---|
1352 | ldn->remoteLinkId = linkReq->getRemoteLinkId();
|
---|
1353 | ldn->listener->onLinkUp( ldn->overlayId, ldn->remoteNode );
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | // route message back over overlay
|
---|
1357 | sendMessage( &overlay_msg, ldn );
|
---|
1358 | }
|
---|
1359 | delete linkReq;
|
---|
1360 | ret = true;
|
---|
1361 | break;
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | // ---------------------------------------------------------------------
|
---|
1365 | // handle relay message to forward messages
|
---|
1366 | // ---------------------------------------------------------------------
|
---|
1367 | case OverlayMsg::typeRelay: {
|
---|
1368 |
|
---|
1369 | logging_debug( "received relay request on link" );
|
---|
1370 |
|
---|
1371 | // decapsulate message
|
---|
1372 | RelayMessage* relayMsg = overlayMsg->decapsulate<RelayMessage>();
|
---|
1373 |
|
---|
1374 | // is relay message informative?
|
---|
1375 | switch (relayMsg->getType()) {
|
---|
1376 |
|
---|
1377 | // handle relay notification
|
---|
1378 | case RelayMessage::typeInform: {
|
---|
1379 | logging_info("Received relay information message with"
|
---|
1380 | << " relay " << relayMsg->getRelayNode()
|
---|
1381 | << " destination " << relayMsg->getDestNode() );
|
---|
1382 |
|
---|
1383 | // mark incoming link as relay
|
---|
1384 | if (ld!=NULL) ld->markAsRelay();
|
---|
1385 |
|
---|
1386 | // am I the destination of this message? yes->
|
---|
1387 | if (relayMsg->getDestNode() == nodeId ) {
|
---|
1388 | // deliver relay message locally!
|
---|
1389 | logging_debug("Relay message reached destination. Handling the message.");
|
---|
1390 | handleMessage( relayMsg, relayMsg->getDestLink(), LinkID::UNSPECIFIED, remoteNode );
|
---|
1391 | ret = true;
|
---|
1392 | break;
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | // create route message
|
---|
1396 | OverlayMsg _overMsg( *overlayMsg );
|
---|
1397 | RelayMessage _relayMsg( *relayMsg );
|
---|
1398 | _relayMsg.setType( RelayMessage::typeRoute );
|
---|
1399 | _overMsg.encapsulate( &_relayMsg );
|
---|
1400 |
|
---|
1401 | // forward message
|
---|
1402 | if (relayMsg->getRelayNode() == nodeId || relayMsg->getRelayNode().isUnspecified()) {
|
---|
1403 | logging_info("Routing relay message to " << relayMsg->getDestNode().toString() );
|
---|
1404 | sendOverlay( &_overMsg, relayMsg->getDestNode() );
|
---|
1405 | } else {
|
---|
1406 | logging_info("Routing relay message to " << relayMsg->getRelayNode().toString() );
|
---|
1407 | sendOverlay( &_overMsg, relayMsg->getRelayNode() );
|
---|
1408 | }
|
---|
1409 | ret = true;
|
---|
1410 | break;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | // handle relay routing
|
---|
1414 | case RelayMessage::typeRoute: {
|
---|
1415 | logging_info("Received relay route message with"
|
---|
1416 | << " relay " << relayMsg->getRelayNode()
|
---|
1417 | << " destination " << relayMsg->getDestNode() );
|
---|
1418 |
|
---|
1419 | // mark incoming link as relay
|
---|
1420 | if (ld!=NULL) ld->markAsRelay();
|
---|
1421 |
|
---|
1422 | // am I the destination of this message? yes->
|
---|
1423 | if (relayMsg->getDestNode() == nodeId ) {
|
---|
1424 | // deliver relay message locally!
|
---|
1425 | logging_debug("Relay message reached destination. Handling the message.");
|
---|
1426 | handleMessage( relayMsg, relayMsg->getDestLink(), LinkID::UNSPECIFIED, remoteNode );
|
---|
1427 | ret = true;
|
---|
1428 | break;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | // am I the relay for this message? yes->
|
---|
1432 | if (relayMsg->getRelayNode() == nodeId ) {
|
---|
1433 | logging_debug("I'm the relay for this message. Sending to destination.");
|
---|
1434 | OverlayMsg _overMsg( *overlayMsg );
|
---|
1435 | RelayMessage _relayMsg( *relayMsg );
|
---|
1436 | _overMsg.encapsulate(&_relayMsg);
|
---|
1437 |
|
---|
1438 | /// this must be handled by using relay link!
|
---|
1439 | sendOverlay(&_overMsg, relayMsg->getDestNode());
|
---|
1440 | ret = true;
|
---|
1441 | break;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | // error: I'm not a relay or destination!
|
---|
1445 | logging_error("This node is neither relay nor destination. Dropping Message!");
|
---|
1446 | ret = true;
|
---|
1447 | break;
|
---|
1448 | }
|
---|
1449 | default: {
|
---|
1450 | logging_error("RelayMessage Unknown!");
|
---|
1451 | ret = true;
|
---|
1452 | break;
|
---|
1453 | }
|
---|
1454 | }
|
---|
1455 | delete relayMsg;
|
---|
1456 | break;
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | // ---------------------------------------------------------------------
|
---|
1460 | // handle keep-alive messages
|
---|
1461 | // ---------------------------------------------------------------------
|
---|
1462 | case OverlayMsg::typeKeepAlive: {
|
---|
1463 | logging_debug( "received keep-alive on link" );
|
---|
1464 | if ( ld != NULL ) {
|
---|
1465 | logging_info("Keep-Alive for "<< ld->overlayId);
|
---|
1466 | ld->markAlive();
|
---|
1467 | }
|
---|
1468 | break;
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | // ---------------------------------------------------------------------
|
---|
1472 | // handle direct link replacement messages
|
---|
1473 | // ---------------------------------------------------------------------
|
---|
1474 | case OverlayMsg::typeDirectLink: {
|
---|
1475 |
|
---|
1476 | logging_debug( "Received direct link replacement request" );
|
---|
1477 |
|
---|
1478 | LinkDescriptor* rld = getDescriptor( overlayMsg->getRelayLink() );
|
---|
1479 | if (rld == NULL || ld == NULL) {
|
---|
1480 | logging_error("Direct link replacement: Link "
|
---|
1481 | << overlayMsg->getRelayLink() << "not found error." );
|
---|
1482 | break;
|
---|
1483 | }
|
---|
1484 | logging_info( "Received direct link convert notification for " << rld );
|
---|
1485 |
|
---|
1486 | // set communcation link id and set it up
|
---|
1487 | rld->communicationId = ld->communicationId;
|
---|
1488 |
|
---|
1489 | // this is neccessary since this link was a relay link before!
|
---|
1490 | rld->communicationUp = true;
|
---|
1491 |
|
---|
1492 | // this is not a relay link anymore!
|
---|
1493 | rld->relay = false;
|
---|
1494 | rld->localRelay = NodeID::UNSPECIFIED;
|
---|
1495 | rld->remoteRelay = NodeID::UNSPECIFIED;
|
---|
1496 |
|
---|
1497 | // mark used and alive!
|
---|
1498 | rld->markAsUsed();
|
---|
1499 | rld->markAlive();
|
---|
1500 |
|
---|
1501 | // erase the original descriptor
|
---|
1502 | eraseDescriptor(ld->overlayId);
|
---|
1503 | break;
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | // ---------------------------------------------------------------------
|
---|
1507 | // handle unknown message type
|
---|
1508 | // ---------------------------------------------------------------------
|
---|
1509 | default: {
|
---|
1510 | logging_error( "received message in invalid state! don't know " <<
|
---|
1511 | "what to do with this message of type " <<
|
---|
1512 | overlayMsg->getType() );
|
---|
1513 | ret = false;
|
---|
1514 | break;
|
---|
1515 | }
|
---|
1516 | } /* switch */
|
---|
1517 |
|
---|
1518 | delete overlayMsg;
|
---|
1519 | return ret;
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | // ----------------------------------------------------------------------------
|
---|
1523 |
|
---|
1524 | void BaseOverlay::broadcastMessage(Message* message, const ServiceID& service) {
|
---|
1525 |
|
---|
1526 | logging_debug( "broadcasting message to all known nodes " <<
|
---|
1527 | "in the overlay from service " + service.toString() );
|
---|
1528 |
|
---|
1529 | OverlayInterface::NodeList nodes = overlayInterface->getKnownNodes(true);
|
---|
1530 | OverlayInterface::NodeList::iterator i = nodes.begin();
|
---|
1531 | for(; i != nodes.end(); i++ ) {
|
---|
1532 | if( *i == nodeId) continue; // don't send to ourselfs
|
---|
1533 | sendMessage( message, *i, service );
|
---|
1534 | }
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | vector<NodeID> BaseOverlay::getOverlayNeighbors(bool deep) const {
|
---|
1538 | // the known nodes _can_ also include our node, so we remove ourself
|
---|
1539 | vector<NodeID> nodes = overlayInterface->getKnownNodes(deep);
|
---|
1540 |
|
---|
1541 | vector<NodeID>::iterator i = find( nodes.begin(), nodes.end(), this->nodeId );
|
---|
1542 | if( i != nodes.end() ) nodes.erase( i );
|
---|
1543 |
|
---|
1544 | return nodes;
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | const NodeID& BaseOverlay::getNodeID(const LinkID& lid) const {
|
---|
1548 | if( lid == LinkID::UNSPECIFIED ) return nodeId;
|
---|
1549 | const LinkDescriptor* ld = getDescriptor(lid);
|
---|
1550 | if( ld == NULL ) return NodeID::UNSPECIFIED;
|
---|
1551 | else return ld->remoteNode;
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | vector<LinkID> BaseOverlay::getLinkIDs( const NodeID& nid ) const {
|
---|
1555 | vector<LinkID> linkvector;
|
---|
1556 | BOOST_FOREACH( LinkDescriptor* ld, links ) {
|
---|
1557 | if( ld->remoteNode == nid || nid == NodeID::UNSPECIFIED ) {
|
---|
1558 | linkvector.push_back( ld->overlayId );
|
---|
1559 | }
|
---|
1560 | }
|
---|
1561 | return linkvector;
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 |
|
---|
1565 | void BaseOverlay::onNodeJoin(const NodeID& node) {
|
---|
1566 | JoiningNodes::iterator i = std::find( joiningNodes.begin(), joiningNodes.end(), node );
|
---|
1567 | if( i == joiningNodes.end() ) return;
|
---|
1568 |
|
---|
1569 | logging_info( "node has successfully joined baseoverlay and overlay structure "
|
---|
1570 | << node.toString() );
|
---|
1571 |
|
---|
1572 | joiningNodes.erase( i );
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | void BaseOverlay::eventFunction() {
|
---|
1576 |
|
---|
1577 | // send keep-alive messages over established links
|
---|
1578 | BOOST_FOREACH( LinkDescriptor* ld, links ) {
|
---|
1579 | if (!ld->up) continue;
|
---|
1580 | OverlayMsg overMsg( OverlayMsg::typeKeepAlive,
|
---|
1581 | OverlayInterface::OVERLAY_SERVICE_ID, nodeId );
|
---|
1582 | sendMessage( &overMsg, ld );
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | // iterate over all links and check for time boundaries
|
---|
1586 | vector<LinkDescriptor*> oldlinks;
|
---|
1587 | time_t now = time(NULL);
|
---|
1588 | BOOST_FOREACH( LinkDescriptor* ld, links ) {
|
---|
1589 | // remote used as relay flag
|
---|
1590 | if ( ld->usedAsRelay && difftime( now, ld->timeUsedAsRelay ) > 10)
|
---|
1591 | ld->usedAsRelay = false;
|
---|
1592 |
|
---|
1593 | // keep alives and not up? yes-> link connection request is stale!
|
---|
1594 | if ( !ld->up && difftime( now, ld->keepAliveTime ) >= 2 ) {
|
---|
1595 |
|
---|
1596 | // increase counter
|
---|
1597 | ld->keepAliveMissed++;
|
---|
1598 |
|
---|
1599 | // missed more than four keep-alive messages (10 sec)? -> drop link
|
---|
1600 | if (ld->keepAliveMissed > 4) {
|
---|
1601 | logging_info( "Link connection request is stale, closing: " << ld );
|
---|
1602 | oldlinks.push_back( ld );
|
---|
1603 | continue;
|
---|
1604 | }
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | if (!ld->up) continue;
|
---|
1608 |
|
---|
1609 | // drop links that are dropped and not used as relay
|
---|
1610 | if (ld->dropWhenRelaysLeft && !ld->usedAsRelay && !ld->autolink) {
|
---|
1611 | oldlinks.push_back( ld );
|
---|
1612 | continue;
|
---|
1613 | }
|
---|
1614 |
|
---|
1615 | // auto-link time exceeded?
|
---|
1616 | if ( ld->autolink && difftime( now, ld->lastuse ) > 30 ) {
|
---|
1617 | oldlinks.push_back( ld );
|
---|
1618 | continue;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | // keep alives missed? yes->
|
---|
1622 | if ( difftime( now, ld->keepAliveTime ) > 2 ) {
|
---|
1623 |
|
---|
1624 | // increase counter
|
---|
1625 | ld->keepAliveMissed++;
|
---|
1626 |
|
---|
1627 | // missed more than four keep-alive messages (4 sec)? -> drop link
|
---|
1628 | if (ld->keepAliveMissed >= 8) {
|
---|
1629 | logging_info( "Link is stale, closing: " << ld );
|
---|
1630 | oldlinks.push_back( ld );
|
---|
1631 | continue;
|
---|
1632 | }
|
---|
1633 | }
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | // drop links
|
---|
1637 | BOOST_FOREACH( const LinkDescriptor* ld, oldlinks ) {
|
---|
1638 | /*
|
---|
1639 | vector<LinkID>::iterator it = std::find(
|
---|
1640 | bootstrapLinks.begin(), bootstrapLinks.end(), ld->communicationId);
|
---|
1641 |
|
---|
1642 | if (!ld->communicationId.isUnspecified() && it != bootstrapLinks.end() ){
|
---|
1643 | logging_info( "Not dropping initiator link: " << ld );
|
---|
1644 | continue;
|
---|
1645 | }
|
---|
1646 | */
|
---|
1647 | logging_info( "Link timed out. Dropping " << ld );
|
---|
1648 | dropLink( ld->overlayId );
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | // show link state
|
---|
1652 | counter++;
|
---|
1653 | if (counter>=4) showLinkState();
|
---|
1654 | if (counter>=4 || counter<0) counter = 0;
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 | void BaseOverlay::showLinkState() {
|
---|
1658 | int i=0;
|
---|
1659 | logging_info("--- link state -------------------------------");
|
---|
1660 | BOOST_FOREACH( LinkDescriptor* ld, links ) {
|
---|
1661 | logging_info("link " << i << ": " << ld);
|
---|
1662 | i++;
|
---|
1663 | }
|
---|
1664 | logging_info("----------------------------------------------");
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | }} // namespace ariba, overlay
|
---|