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

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

watchdog bootstrapping und kleine relay fixes

File size: 6.3 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 EventData* data = event.getData<EventData>();
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 EventData* data = new EventData();
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 EventData data;
157 data.spovnetid = spovnetid;
158 data.nodeid = nodeid;
159 data.endpoint = _ep;
160
161 lastJoines.push_front(JoinData(data));
162}
163
164void OverlayBootstrap::checkOverlayStatus(){
165
166 // if we have no overlay neighbors, try to bootstrap using
167 // bootstrap information that we already used
168
169 { //limit history to 10 endpoints
170 boost::mutex::scoped_lock lock(lastJoinesMutex);
171 while(lastJoines.size() > 10)
172 lastJoines.pop_back();
173 }
174
175 // we have overlay neighbors -> ok
176 if(overlay->getOverlayNeighbors().size() > 0) return;
177
178 // no overlay neighbors -> try out already
179 // successfully used bootstrap nodes
180 JoinData data;
181 {
182 boost::mutex::scoped_lock lock(lastJoinesMutex);
183 JoinStack::iterator i = lastJoines.begin();
184 if(i == lastJoines.end()) return;
185
186 // use last used element and then put it into back
187 JoinData data = *i;
188 lastJoines.pop_front();
189 lastJoines.push_back(data);
190 }
191
192 logging_info("no overlay connections detected, " <<
193 "trying to join using old bootstrap information");
194
195 // try to join using this node, if the join is successfull
196 // the endpoint will again be inserted using recordJoin
197 overlay->joinSpoVNet( spovnetid, data.data.endpoint );
198}
199
200OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
201}
202
203void OverlayBootstrap::WatchdogTimer::startWatchdog(){
204 Timer::setInterval(2000);
205 Timer::start();
206}
207
208void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
209 Timer::stop();
210}
211
212void OverlayBootstrap::WatchdogTimer::eventFunction(){
213 if(obj == NULL) return;
214 obj->checkOverlayStatus();
215}
216
217}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.