[4836] | 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 "OverlayBootstrap.h"
|
---|
| 40 | #include "BaseOverlay.h"
|
---|
| 41 |
|
---|
| 42 | namespace ariba {
|
---|
| 43 | namespace overlay {
|
---|
| 44 |
|
---|
| 45 | use_logging_cpp(OverlayBootstrap);
|
---|
[4866] | 46 | SystemEventType OverlayBootstrapMethodType("OverlayBootstrapMethodType");
|
---|
[4836] | 47 |
|
---|
| 48 | OverlayBootstrap::OverlayBootstrap()
|
---|
| 49 | : manager( BootstrapManager::instance() ),
|
---|
| 50 | spovnetid( SpoVNetID::UNSPECIFIED ),
|
---|
| 51 | nodeid( NodeID::UNSPECIFIED ),
|
---|
[5838] | 52 | overlay( NULL ),
|
---|
| 53 | watchtimer(this) {
|
---|
| 54 |
|
---|
| 55 | srand(time(NULL));
|
---|
[4836] | 56 | }
|
---|
| 57 |
|
---|
| 58 | OverlayBootstrap::~OverlayBootstrap(){
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void OverlayBootstrap::start(BaseOverlay* _overlay, const SpoVNetID& _spovnetid, const NodeID& _nodeid){
|
---|
| 62 | overlay = _overlay;
|
---|
| 63 | spovnetid = _spovnetid;
|
---|
| 64 | nodeid = _nodeid;
|
---|
| 65 |
|
---|
[5316] | 66 | logging_info("starting overlay bootstrap");
|
---|
| 67 |
|
---|
[4836] | 68 | manager.registerCallback( this );
|
---|
[5405] | 69 | manager.registerModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
|
---|
[5412] | 70 | manager.registerModule( BootstrapManager::BootstrapTypeBluetoothSdp );
|
---|
[5838] | 71 |
|
---|
| 72 | watchtimer.startWatchdog();
|
---|
[4836] | 73 | }
|
---|
[4935] | 74 |
|
---|
[4836] | 75 | void OverlayBootstrap::stop(){
|
---|
| 76 | overlay = NULL;
|
---|
| 77 | spovnetid = SpoVNetID::UNSPECIFIED;
|
---|
| 78 | nodeid = NodeID::UNSPECIFIED;
|
---|
| 79 |
|
---|
[5316] | 80 | logging_info("stopping overlay bootstrap");
|
---|
| 81 |
|
---|
[4836] | 82 | manager.unregisterCallback( this );
|
---|
[5405] | 83 | manager.unregisterModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
|
---|
[5412] | 84 | manager.unregisterModule( BootstrapManager::BootstrapTypeBluetoothSdp );
|
---|
[5838] | 85 |
|
---|
| 86 | watchtimer.stopWatchdog();
|
---|
[4836] | 87 | }
|
---|
| 88 |
|
---|
[4866] | 89 | void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){
|
---|
[5860] | 90 | JoinData* data = event.getData<JoinData>();
|
---|
[4866] | 91 |
|
---|
| 92 | // announcement for our spovnet
|
---|
| 93 | logging_info( "found bootstrap node for our SpoVNetID " << data->spovnetid.toString()
|
---|
| 94 | << " on NodeID " << data->nodeid.toString() << " with endpoint " << data->endpoint.toString() );
|
---|
| 95 |
|
---|
[4935] | 96 | // tell the base overlay to join using this endpoint
|
---|
| 97 | assert( overlay != NULL );
|
---|
[5316] | 98 | overlay->joinSpoVNet( spovnetid, data->endpoint );
|
---|
[4866] | 99 |
|
---|
| 100 | delete data;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[4836] | 103 | void OverlayBootstrap::onBootstrapServiceFound(string name, string info1, string info2, string info3){
|
---|
| 104 | if( overlay == NULL ) return;
|
---|
| 105 | if(name.length() <= 0 || info1.length() <= 0 || info2.length() <= 0 || info3.length() <= 0) return;
|
---|
| 106 |
|
---|
[4837] | 107 | //
|
---|
| 108 | // generate the types
|
---|
| 109 | //
|
---|
| 110 |
|
---|
[4836] | 111 | SpoVNetID sid( info1 );
|
---|
| 112 | NodeID nid( info2 );
|
---|
| 113 | EndpointDescriptor ep( info3 );
|
---|
| 114 |
|
---|
[4837] | 115 | //
|
---|
| 116 | // is this announcement of interest for us?
|
---|
| 117 | //
|
---|
| 118 |
|
---|
[4838] | 119 | // announcement for another spovnet
|
---|
[4850] | 120 | if( sid != this->spovnetid ) return;
|
---|
[4837] | 121 |
|
---|
[4850] | 122 | // announcement with our nodeid (either our announcement
|
---|
| 123 | // or a node with the same id, any way -> ignore)
|
---|
| 124 | if( nid == this->nodeid ) return;
|
---|
| 125 |
|
---|
[4866] | 126 | //
|
---|
| 127 | // send out the bootstrap information as
|
---|
| 128 | // event to synchronize into the system queue
|
---|
| 129 | //
|
---|
| 130 |
|
---|
[5860] | 131 | JoinData* data = new JoinData();
|
---|
[4866] | 132 | data->spovnetid = sid;
|
---|
| 133 | data->nodeid = nid;
|
---|
| 134 | data->endpoint = ep;
|
---|
| 135 |
|
---|
| 136 | SystemQueue::instance().scheduleEvent(
|
---|
| 137 | SystemEvent( this, OverlayBootstrapMethodType, data), 0 );
|
---|
[4836] | 138 | }
|
---|
| 139 |
|
---|
| 140 | void OverlayBootstrap::publish(const EndpointDescriptor& _ep){
|
---|
| 141 |
|
---|
| 142 | ostringstream r;
|
---|
| 143 | r << std::hex << rand();
|
---|
| 144 |
|
---|
| 145 | randname = r.str();
|
---|
| 146 | manager.publish( randname, spovnetid.toString(), nodeid.toString(), _ep.toString() );
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | void OverlayBootstrap::revoke(){
|
---|
| 150 | manager.revoke( randname );
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[5838] | 153 | void OverlayBootstrap::recordJoin(const EndpointDescriptor& _ep){
|
---|
| 154 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
| 155 |
|
---|
[5860] | 156 | JoinData data;
|
---|
[5838] | 157 | data.spovnetid = spovnetid;
|
---|
| 158 | data.nodeid = nodeid;
|
---|
| 159 | data.endpoint = _ep;
|
---|
| 160 |
|
---|
[5860] | 161 | logging_info("recording bootstrap information " << data.endpoint.toString());
|
---|
| 162 |
|
---|
| 163 | lastJoines.push_front(data);
|
---|
[5838] | 164 | }
|
---|
| 165 |
|
---|
| 166 | void OverlayBootstrap::checkOverlayStatus(){
|
---|
| 167 |
|
---|
| 168 | // if we have no overlay neighbors, try to bootstrap using
|
---|
| 169 | // bootstrap information that we already used
|
---|
| 170 |
|
---|
| 171 | { //limit history to 10 endpoints
|
---|
| 172 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
| 173 | while(lastJoines.size() > 10)
|
---|
| 174 | lastJoines.pop_back();
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | // we have overlay neighbors -> ok
|
---|
| 178 | if(overlay->getOverlayNeighbors().size() > 0) return;
|
---|
| 179 |
|
---|
[5860] | 180 | logging_info("overlay not joined, checking for earlier used bootstrap information");
|
---|
| 181 | EndpointDescriptor joinep = EndpointDescriptor::UNSPECIFIED();
|
---|
| 182 |
|
---|
[5838] | 183 | // no overlay neighbors -> try out already
|
---|
| 184 | // successfully used bootstrap nodes
|
---|
| 185 | JoinData data;
|
---|
| 186 | {
|
---|
| 187 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
| 188 | JoinStack::iterator i = lastJoines.begin();
|
---|
| 189 | if(i == lastJoines.end()) return;
|
---|
| 190 |
|
---|
| 191 | // use last used element and then put it into back
|
---|
[5860] | 192 | joinep = (*i).endpoint;
|
---|
| 193 |
|
---|
| 194 | if(lastJoines.size() >= 2)
|
---|
| 195 | swap( *lastJoines.begin(), *(--(lastJoines.end())) );
|
---|
[5838] | 196 | }
|
---|
| 197 |
|
---|
[5860] | 198 | logging_info("no overlay conenctivity detected, " <<
|
---|
| 199 | "trying to join using old bootstrap information: " <<
|
---|
| 200 | joinep.toString());
|
---|
[5838] | 201 |
|
---|
| 202 | // try to join using this node, if the join is successfull
|
---|
| 203 | // the endpoint will again be inserted using recordJoin
|
---|
[5860] | 204 | overlay->joinSpoVNet( spovnetid, joinep );
|
---|
[5838] | 205 | }
|
---|
| 206 |
|
---|
| 207 | OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | void OverlayBootstrap::WatchdogTimer::startWatchdog(){
|
---|
[5860] | 211 | Timer::setInterval(5000);
|
---|
[5838] | 212 | Timer::start();
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
|
---|
| 216 | Timer::stop();
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | void OverlayBootstrap::WatchdogTimer::eventFunction(){
|
---|
| 220 | if(obj == NULL) return;
|
---|
| 221 | obj->checkOverlayStatus();
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[4836] | 224 | }} // namespace ariba, overlay
|
---|