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

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

-kleine fixes

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