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

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

-von außen konfigurierbare bootstrap module, -periodicbroadcast crash fix

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