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