00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "OverlayBootstrap.h"
00040
00041 #include "BaseOverlay.h"
00042 #include "ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.h"
00043
00044 using ariba::utility::BluetoothSdp;
00045
00046 namespace ariba {
00047 namespace overlay {
00048
00049 use_logging_cpp(OverlayBootstrap);
00050 SystemEventType OverlayBootstrapMethodType("OverlayBootstrapMethodType");
00051
00052 OverlayBootstrap::OverlayBootstrap()
00053 : manager( BootstrapManager::instance() ),
00054 spovnetid( SpoVNetID::UNSPECIFIED ),
00055 nodeid( NodeID::UNSPECIFIED ),
00056 overlay( NULL ),
00057 watchtimer(this),
00058 haveOverlayConnection(false){
00059
00060 srand(time(NULL));
00061
00062 BluetoothSdp::CONNECTION_CHECKER = this;
00063 }
00064
00065 OverlayBootstrap::~OverlayBootstrap(){
00066 }
00067
00068 void OverlayBootstrap::start(BaseOverlay* _overlay,
00069 const SpoVNetID& _spovnetid, const NodeID& _nodeid,
00070 vector<pair<BootstrapManager::BootstrapType,string> > modules){
00071 overlay = _overlay;
00072 spovnetid = _spovnetid;
00073 nodeid = _nodeid;
00074
00075 logging_info("starting overlay bootstrap");
00076
00077 manager.registerCallback( this );
00078
00079 typedef pair<BootstrapManager::BootstrapType,string> X;
00080 BOOST_FOREACH( X i, modules){
00081 manager.registerModule( i.first, i.second );
00082 }
00083
00084 watchtimer.startWatchdog();
00085 }
00086
00087 void OverlayBootstrap::stop(){
00088 overlay = NULL;
00089 spovnetid = SpoVNetID::UNSPECIFIED;
00090 nodeid = NodeID::UNSPECIFIED;
00091
00092 logging_info("stopping overlay bootstrap");
00093
00094 manager.unregisterCallback( this );
00095 manager.unregisterAllModules();
00096
00097 watchtimer.stopWatchdog();
00098 }
00099
00100 void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){
00101 JoinData* data = event.getData<JoinData>();
00102
00103
00104 logging_info( "found bootstrap node for our SpoVNetID " << data->spovnetid.toString()
00105 << " on NodeID " << data->nodeid.toString() << " with endpoint " << data->endpoint.toString() );
00106
00107
00108 assert( overlay != NULL );
00109 overlay->joinSpoVNet( spovnetid, data->endpoint );
00110
00111 delete data;
00112 }
00113
00114 void OverlayBootstrap::onBootstrapServiceFound(string name, string info1, string info2, string info3){
00115 if( overlay == NULL ) return;
00116 if(name.length() <= 0 || info1.length() <= 0 || info2.length() <= 0 || info3.length() <= 0) return;
00117
00118
00119
00120
00121
00122 SpoVNetID sid( info1 );
00123 NodeID nid( info2 );
00124 EndpointDescriptor ep( info3 );
00125
00126
00127
00128
00129
00130
00131 if( sid != this->spovnetid ) return;
00132
00133
00134
00135 if( nid == this->nodeid ) return;
00136
00137
00138
00139
00140
00141
00142 JoinData* data = new JoinData();
00143 data->spovnetid = sid;
00144 data->nodeid = nid;
00145 data->endpoint = ep;
00146
00147 SystemQueue::instance().scheduleEvent(
00148 SystemEvent( this, OverlayBootstrapMethodType, data), 0 );
00149 }
00150
00151 void OverlayBootstrap::publish(const EndpointDescriptor& _ep){
00152
00153 ostringstream r;
00154 r << std::hex << rand();
00155
00156 randname = r.str();
00157 manager.publish( randname, spovnetid.toString(), nodeid.toString(), _ep.toString() );
00158 }
00159
00160 void OverlayBootstrap::revoke(){
00161 manager.revoke( randname );
00162 }
00163
00164 void OverlayBootstrap::recordJoin(const EndpointDescriptor& _ep){
00165 boost::mutex::scoped_lock lock(lastJoinesMutex);
00166
00167 JoinData data;
00168 data.spovnetid = spovnetid;
00169 data.nodeid = nodeid;
00170 data.endpoint = _ep;
00171
00172 logging_info("recording bootstrap information " << data.endpoint.toString());
00173
00174 lastJoines.push_front(data);
00175 }
00176
00177 bool OverlayBootstrap::haveOverlayConnections(){
00178 boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
00179 return haveOverlayConnection;
00180 }
00181
00182 void OverlayBootstrap::checkOverlayStatus(){
00183
00184
00185
00186
00187 {
00188 boost::mutex::scoped_lock lock(lastJoinesMutex);
00189 while(lastJoines.size() > 10)
00190 lastJoines.pop_back();
00191 }
00192
00193 {
00194 boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
00195 haveOverlayConnection = overlay->getOverlayNeighbors().size() > 0;
00196
00197
00198 if(haveOverlayConnection > 0) return;
00199 }
00200
00201
00202 logging_info("overlay not joined, checking for earlier used bootstrap information");
00203 EndpointDescriptor joinep = EndpointDescriptor::UNSPECIFIED();
00204
00205
00206
00207 JoinData data;
00208 {
00209 boost::mutex::scoped_lock lock(lastJoinesMutex);
00210 JoinStack::iterator i = lastJoines.begin();
00211 if(i == lastJoines.end()) return;
00212
00213
00214 joinep = (*i).endpoint;
00215
00216 if(lastJoines.size() >= 2)
00217 swap( *lastJoines.begin(), *(--(lastJoines.end())) );
00218 }
00219
00220 logging_info("no overlay conenctivity detected, " <<
00221 "trying to join using old bootstrap information: " <<
00222 joinep.toString());
00223
00224
00225
00226 overlay->joinSpoVNet( spovnetid, joinep );
00227 }
00228
00229 OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
00230 }
00231
00232 void OverlayBootstrap::WatchdogTimer::startWatchdog(){
00233 Timer::setInterval(5000);
00234 Timer::start();
00235 }
00236
00237 void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
00238 Timer::stop();
00239 }
00240
00241 void OverlayBootstrap::WatchdogTimer::eventFunction(){
00242 if(obj == NULL) return;
00243 obj->checkOverlayStatus();
00244 }
00245
00246 }}