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