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 |
|
---|
41 | #include "BaseOverlay.h"
|
---|
42 | #include "ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.h"
|
---|
43 |
|
---|
44 | using ariba::utility::BluetoothSdp;
|
---|
45 |
|
---|
46 | namespace ariba {
|
---|
47 | namespace overlay {
|
---|
48 |
|
---|
49 | use_logging_cpp(OverlayBootstrap);
|
---|
50 | SystemEventType OverlayBootstrapMethodType("OverlayBootstrapMethodType");
|
---|
51 |
|
---|
52 | OverlayBootstrap::OverlayBootstrap()
|
---|
53 | : manager( BootstrapManager::instance() ),
|
---|
54 | spovnetid( SpoVNetID::UNSPECIFIED ),
|
---|
55 | nodeid( NodeID::UNSPECIFIED ),
|
---|
56 | overlay( NULL ),
|
---|
57 | watchtimer(this),
|
---|
58 | haveOverlayConnection(false){
|
---|
59 |
|
---|
60 | srand(time(NULL));
|
---|
61 |
|
---|
62 | BluetoothSdp::CONNECTION_CHECKER = this;
|
---|
63 | }
|
---|
64 |
|
---|
65 | OverlayBootstrap::~OverlayBootstrap(){
|
---|
66 | }
|
---|
67 |
|
---|
68 | void OverlayBootstrap::start(BaseOverlay* _overlay,
|
---|
69 | const SpoVNetID& _spovnetid, const NodeID& _nodeid,
|
---|
70 | vector<pair<BootstrapManager::BootstrapType,string> > modules){
|
---|
71 | overlay = _overlay;
|
---|
72 | spovnetid = _spovnetid;
|
---|
73 | nodeid = _nodeid;
|
---|
74 |
|
---|
75 | logging_info("starting overlay bootstrap");
|
---|
76 |
|
---|
77 | manager.registerCallback( this );
|
---|
78 |
|
---|
79 | typedef pair<BootstrapManager::BootstrapType,string> X;
|
---|
80 | BOOST_FOREACH( X i, modules){
|
---|
81 | manager.registerModule( i.first, i.second );
|
---|
82 | }
|
---|
83 |
|
---|
84 | watchtimer.startWatchdog();
|
---|
85 | }
|
---|
86 |
|
---|
87 | void OverlayBootstrap::stop(){
|
---|
88 | overlay = NULL;
|
---|
89 | spovnetid = SpoVNetID::UNSPECIFIED;
|
---|
90 | nodeid = NodeID::UNSPECIFIED;
|
---|
91 |
|
---|
92 | logging_info("stopping overlay bootstrap");
|
---|
93 |
|
---|
94 | manager.unregisterCallback( this );
|
---|
95 | manager.unregisterAllModules();
|
---|
96 |
|
---|
97 | watchtimer.stopWatchdog();
|
---|
98 | }
|
---|
99 |
|
---|
100 | void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){
|
---|
101 | JoinData* data = event.getData<JoinData>();
|
---|
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 |
|
---|
107 | // tell the base overlay to join using this endpoint
|
---|
108 | assert( overlay != NULL );
|
---|
109 | overlay->joinSpoVNet( spovnetid, data->endpoint );
|
---|
110 |
|
---|
111 | delete data;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void 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 |
|
---|
118 | //
|
---|
119 | // generate the types
|
---|
120 | //
|
---|
121 |
|
---|
122 | SpoVNetID sid( info1 );
|
---|
123 | NodeID nid( info2 );
|
---|
124 | EndpointDescriptor ep( info3 );
|
---|
125 |
|
---|
126 | //
|
---|
127 | // is this announcement of interest for us?
|
---|
128 | //
|
---|
129 |
|
---|
130 | // announcement for another spovnet
|
---|
131 | if( sid != this->spovnetid ) return;
|
---|
132 |
|
---|
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 |
|
---|
137 | //
|
---|
138 | // send out the bootstrap information as
|
---|
139 | // event to synchronize into the system queue
|
---|
140 | //
|
---|
141 |
|
---|
142 | JoinData* data = new JoinData();
|
---|
143 | data->spovnetid = sid;
|
---|
144 | data->nodeid = nid;
|
---|
145 | data->endpoint = ep;
|
---|
146 |
|
---|
147 | SystemQueue::instance().scheduleEvent(
|
---|
148 | SystemEvent( this, OverlayBootstrapMethodType, data), 0 );
|
---|
149 | }
|
---|
150 |
|
---|
151 | void 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 |
|
---|
160 | void OverlayBootstrap::revoke(){
|
---|
161 | manager.revoke( randname );
|
---|
162 | }
|
---|
163 |
|
---|
164 | void OverlayBootstrap::recordJoin(const EndpointDescriptor& _ep){
|
---|
165 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
166 |
|
---|
167 | JoinData data;
|
---|
168 | data.spovnetid = spovnetid;
|
---|
169 | data.nodeid = nodeid;
|
---|
170 | data.endpoint = _ep;
|
---|
171 |
|
---|
172 | logging_info("recording bootstrap information " << data.endpoint.toString());
|
---|
173 |
|
---|
174 | lastJoines.push_front(data);
|
---|
175 | }
|
---|
176 |
|
---|
177 | bool OverlayBootstrap::haveOverlayConnections(){
|
---|
178 | boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
|
---|
179 | return haveOverlayConnection;
|
---|
180 | }
|
---|
181 |
|
---|
182 | void 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 |
|
---|
193 | {
|
---|
194 | boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
|
---|
195 | haveOverlayConnection = overlay->getOverlayNeighbors().size() > 0;
|
---|
196 |
|
---|
197 | // we have overlay neighbors -> ok nothing to do
|
---|
198 | if(haveOverlayConnection > 0) return;
|
---|
199 | }
|
---|
200 |
|
---|
201 | // no overlay neighbors, see if we can join using old information
|
---|
202 | logging_info("overlay not joined, checking for earlier used bootstrap information");
|
---|
203 | EndpointDescriptor joinep = EndpointDescriptor::UNSPECIFIED();
|
---|
204 |
|
---|
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
|
---|
214 | joinep = (*i).endpoint;
|
---|
215 |
|
---|
216 | if(lastJoines.size() >= 2)
|
---|
217 | swap( *lastJoines.begin(), *(--(lastJoines.end())) );
|
---|
218 | }
|
---|
219 |
|
---|
220 | logging_info("no overlay conenctivity detected, " <<
|
---|
221 | "trying to join using old bootstrap information: " <<
|
---|
222 | joinep.toString());
|
---|
223 |
|
---|
224 | // try to join using this node, if the join is successfull
|
---|
225 | // the endpoint will again be inserted using recordJoin
|
---|
226 | overlay->joinSpoVNet( spovnetid, joinep );
|
---|
227 | }
|
---|
228 |
|
---|
229 | OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
|
---|
230 | }
|
---|
231 |
|
---|
232 | void OverlayBootstrap::WatchdogTimer::startWatchdog(){
|
---|
233 | Timer::setInterval(5000);
|
---|
234 | Timer::start();
|
---|
235 | }
|
---|
236 |
|
---|
237 | void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
|
---|
238 | Timer::stop();
|
---|
239 | }
|
---|
240 |
|
---|
241 | void OverlayBootstrap::WatchdogTimer::eventFunction(){
|
---|
242 | if(obj == NULL) return;
|
---|
243 | obj->checkOverlayStatus();
|
---|
244 | }
|
---|
245 |
|
---|
246 | }} // namespace ariba, overlay
|
---|