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