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