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

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

more agressive sdp scan, sdp off when in spovnet, sdp on and agressive when not

File size: 7.2 KB
Line 
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
42namespace ariba {
43namespace overlay {
44
45use_logging_cpp(OverlayBootstrap);
46SystemEventType OverlayBootstrapMethodType("OverlayBootstrapMethodType");
47
48OverlayBootstrap::OverlayBootstrap()
49 : manager( BootstrapManager::instance() ),
50 spovnetid( SpoVNetID::UNSPECIFIED ),
51 nodeid( NodeID::UNSPECIFIED ),
52 overlay( NULL ),
53 watchtimer(this) {
54
55 srand(time(NULL));
56}
57
58OverlayBootstrap::~OverlayBootstrap(){
59}
60
61void OverlayBootstrap::start(BaseOverlay* _overlay, const SpoVNetID& _spovnetid, const NodeID& _nodeid){
62 overlay = _overlay;
63 spovnetid = _spovnetid;
64 nodeid = _nodeid;
65
66 logging_info("starting overlay bootstrap");
67
68 manager.registerCallback( this );
69 manager.registerModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
70 manager.registerModule( BootstrapManager::BootstrapTypeBluetoothSdp );
71
72 watchtimer.startWatchdog();
73}
74
75void OverlayBootstrap::stop(){
76 overlay = NULL;
77 spovnetid = SpoVNetID::UNSPECIFIED;
78 nodeid = NodeID::UNSPECIFIED;
79
80 logging_info("stopping overlay bootstrap");
81
82 manager.unregisterCallback( this );
83 manager.unregisterModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
84 manager.unregisterModule( BootstrapManager::BootstrapTypeBluetoothSdp );
85
86 watchtimer.stopWatchdog();
87}
88
89void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){
90 JoinData* data = event.getData<JoinData>();
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
96 // tell the base overlay to join using this endpoint
97 assert( overlay != NULL );
98 overlay->joinSpoVNet( spovnetid, data->endpoint );
99
100 delete data;
101}
102
103void 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
107 //
108 // generate the types
109 //
110
111 SpoVNetID sid( info1 );
112 NodeID nid( info2 );
113 EndpointDescriptor ep( info3 );
114
115 //
116 // is this announcement of interest for us?
117 //
118
119 // announcement for another spovnet
120 if( sid != this->spovnetid ) return;
121
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
126 //
127 // send out the bootstrap information as
128 // event to synchronize into the system queue
129 //
130
131 JoinData* data = new JoinData();
132 data->spovnetid = sid;
133 data->nodeid = nid;
134 data->endpoint = ep;
135
136 SystemQueue::instance().scheduleEvent(
137 SystemEvent( this, OverlayBootstrapMethodType, data), 0 );
138}
139
140void 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
149void OverlayBootstrap::revoke(){
150 manager.revoke( randname );
151}
152
153void OverlayBootstrap::recordJoin(const EndpointDescriptor& _ep){
154 boost::mutex::scoped_lock lock(lastJoinesMutex);
155
156 JoinData data;
157 data.spovnetid = spovnetid;
158 data.nodeid = nodeid;
159 data.endpoint = _ep;
160
161 logging_info("recording bootstrap information " << data.endpoint.toString());
162
163 lastJoines.push_front(data);
164}
165
166void 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 // TODO: --> SIGCOMM hack <--
178 // if we have no overlay neighbors, make sure sdp is loaded
179 // sdp searching is turned off when we have bootstrapped
180 if(overlay->getOverlayNeighbors().size() > 0){
181
182 // switch off sdp when we we _are_ in the spovnet already
183 if(manager.isModuleRegistered(BootstrapManager::BootstrapTypeBluetoothSdp))
184 manager.unregisterModule(BootstrapManager::BootstrapTypeBluetoothSdp);
185
186 return;
187 }
188
189 // make sure sdp is running when we are _not_ in the spovnet
190 if(!manager.isModuleRegistered(BootstrapManager::BootstrapTypeBluetoothSdp))
191 manager.registerModule(BootstrapManager::BootstrapTypeBluetoothSdp);
192
193 //
194 // we have overlay neighbors -> ok
195 //
196
197 logging_info("overlay not joined, checking for earlier used bootstrap information");
198 EndpointDescriptor joinep = EndpointDescriptor::UNSPECIFIED();
199
200 // no overlay neighbors -> try out already
201 // successfully used bootstrap nodes
202 JoinData data;
203 {
204 boost::mutex::scoped_lock lock(lastJoinesMutex);
205 JoinStack::iterator i = lastJoines.begin();
206 if(i == lastJoines.end()) return;
207
208 // use last used element and then put it into back
209 joinep = (*i).endpoint;
210
211 if(lastJoines.size() >= 2)
212 swap( *lastJoines.begin(), *(--(lastJoines.end())) );
213 }
214
215 logging_info("no overlay conenctivity detected, " <<
216 "trying to join using old bootstrap information: " <<
217 joinep.toString());
218
219 // try to join using this node, if the join is successfull
220 // the endpoint will again be inserted using recordJoin
221 overlay->joinSpoVNet( spovnetid, joinep );
222}
223
224OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
225}
226
227void OverlayBootstrap::WatchdogTimer::startWatchdog(){
228 Timer::setInterval(5000);
229 Timer::start();
230}
231
232void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
233 Timer::stop();
234}
235
236void OverlayBootstrap::WatchdogTimer::eventFunction(){
237 if(obj == NULL) return;
238 obj->checkOverlayStatus();
239}
240
241}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.