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