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