source: source/ariba/overlay/OverlayBootstrap.cpp@ 6185

Last change on this file since 6185 was 5967, checked in by Christoph Mayer, 15 years ago

rfcomm sdp fixing

File size: 7.1 KB
RevLine 
[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"
[5967]40
[4836]41#include "BaseOverlay.h"
[5967]42#include "ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.h"
[4836]43
[5967]44using ariba::utility::BluetoothSdp;
45
[4836]46namespace ariba {
47namespace overlay {
48
49use_logging_cpp(OverlayBootstrap);
[4866]50SystemEventType OverlayBootstrapMethodType("OverlayBootstrapMethodType");
[4836]51
52OverlayBootstrap::OverlayBootstrap()
53 : manager( BootstrapManager::instance() ),
54 spovnetid( SpoVNetID::UNSPECIFIED ),
55 nodeid( NodeID::UNSPECIFIED ),
[5838]56 overlay( NULL ),
[5967]57 watchtimer(this),
58 haveOverlayConnection(false){
[5838]59
60 srand(time(NULL));
[5967]61
62 BluetoothSdp::CONNECTION_CHECKER = this;
[4836]63}
64
65OverlayBootstrap::~OverlayBootstrap(){
66}
67
68void OverlayBootstrap::start(BaseOverlay* _overlay, const SpoVNetID& _spovnetid, const NodeID& _nodeid){
69 overlay = _overlay;
70 spovnetid = _spovnetid;
71 nodeid = _nodeid;
72
[5316]73 logging_info("starting overlay bootstrap");
74
[4836]75 manager.registerCallback( this );
[5405]76 manager.registerModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
[5412]77 manager.registerModule( BootstrapManager::BootstrapTypeBluetoothSdp );
[5838]78
79 watchtimer.startWatchdog();
[4836]80}
[4935]81
[4836]82void OverlayBootstrap::stop(){
83 overlay = NULL;
84 spovnetid = SpoVNetID::UNSPECIFIED;
85 nodeid = NodeID::UNSPECIFIED;
86
[5316]87 logging_info("stopping overlay bootstrap");
88
[4836]89 manager.unregisterCallback( this );
[5405]90 manager.unregisterModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
[5412]91 manager.unregisterModule( BootstrapManager::BootstrapTypeBluetoothSdp );
[5838]92
93 watchtimer.stopWatchdog();
[4836]94}
95
[4866]96void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){
[5860]97 JoinData* data = event.getData<JoinData>();
[4866]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
[4935]103 // tell the base overlay to join using this endpoint
104 assert( overlay != NULL );
[5316]105 overlay->joinSpoVNet( spovnetid, data->endpoint );
[4866]106
107 delete data;
108}
109
[4836]110void 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
[4837]114 //
115 // generate the types
116 //
117
[4836]118 SpoVNetID sid( info1 );
119 NodeID nid( info2 );
120 EndpointDescriptor ep( info3 );
121
[4837]122 //
123 // is this announcement of interest for us?
124 //
125
[4838]126 // announcement for another spovnet
[4850]127 if( sid != this->spovnetid ) return;
[4837]128
[4850]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
[4866]133 //
134 // send out the bootstrap information as
135 // event to synchronize into the system queue
136 //
137
[5860]138 JoinData* data = new JoinData();
[4866]139 data->spovnetid = sid;
140 data->nodeid = nid;
141 data->endpoint = ep;
142
143 SystemQueue::instance().scheduleEvent(
144 SystemEvent( this, OverlayBootstrapMethodType, data), 0 );
[4836]145}
146
147void 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
156void OverlayBootstrap::revoke(){
157 manager.revoke( randname );
158}
159
[5838]160void OverlayBootstrap::recordJoin(const EndpointDescriptor& _ep){
161 boost::mutex::scoped_lock lock(lastJoinesMutex);
162
[5860]163 JoinData data;
[5838]164 data.spovnetid = spovnetid;
165 data.nodeid = nodeid;
166 data.endpoint = _ep;
167
[5860]168 logging_info("recording bootstrap information " << data.endpoint.toString());
169
170 lastJoines.push_front(data);
[5838]171}
172
[5967]173bool OverlayBootstrap::haveOverlayConnections(){
174 boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
175 return haveOverlayConnection;
176}
177
[5838]178void 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
[5967]189 {
190 boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
191 haveOverlayConnection = overlay->getOverlayNeighbors().size() > 0;
[5953]192
[5967]193 // we have overlay neighbors -> ok nothing to do
194 if(haveOverlayConnection > 0) return;
[5953]195 }
196
[5967]197 // no overlay neighbors, see if we can join using old information
[5860]198 logging_info("overlay not joined, checking for earlier used bootstrap information");
199 EndpointDescriptor joinep = EndpointDescriptor::UNSPECIFIED();
200
[5838]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
[5860]210 joinep = (*i).endpoint;
211
212 if(lastJoines.size() >= 2)
213 swap( *lastJoines.begin(), *(--(lastJoines.end())) );
[5838]214 }
215
[5860]216 logging_info("no overlay conenctivity detected, " <<
217 "trying to join using old bootstrap information: " <<
218 joinep.toString());
[5838]219
220 // try to join using this node, if the join is successfull
221 // the endpoint will again be inserted using recordJoin
[5860]222 overlay->joinSpoVNet( spovnetid, joinep );
[5838]223}
224
225OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
226}
227
228void OverlayBootstrap::WatchdogTimer::startWatchdog(){
[5860]229 Timer::setInterval(5000);
[5838]230 Timer::start();
231}
232
233void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
234 Timer::stop();
235}
236
237void OverlayBootstrap::WatchdogTimer::eventFunction(){
238 if(obj == NULL) return;
239 obj->checkOverlayStatus();
240}
241
[4836]242}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.