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