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, const SpoVNetID& _spovnetid, const NodeID& _nodeid){
|
---|
69 | overlay = _overlay;
|
---|
70 | spovnetid = _spovnetid;
|
---|
71 | nodeid = _nodeid;
|
---|
72 |
|
---|
73 | logging_info("starting overlay bootstrap");
|
---|
74 |
|
---|
75 | manager.registerCallback( this );
|
---|
76 | manager.registerModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
|
---|
77 | //manager.registerModule( BootstrapManager::BootstrapTypeBluetoothSdp );
|
---|
78 | //manager.registerModule( BootstrapManager::BootstrapTypeMulticastDns );
|
---|
79 |
|
---|
80 | watchtimer.startWatchdog();
|
---|
81 | }
|
---|
82 |
|
---|
83 | void OverlayBootstrap::stop(){
|
---|
84 | overlay = NULL;
|
---|
85 | spovnetid = SpoVNetID::UNSPECIFIED;
|
---|
86 | nodeid = NodeID::UNSPECIFIED;
|
---|
87 |
|
---|
88 | logging_info("stopping overlay bootstrap");
|
---|
89 |
|
---|
90 | manager.unregisterCallback( this );
|
---|
91 | manager.unregisterModule( BootstrapManager::BootstrapTypePeriodicBroadcast );
|
---|
92 | //manager.unregisterModule( BootstrapManager::BootstrapTypeBluetoothSdp );
|
---|
93 | //manager.unregisterModule( BootstrapManager::BootstrapTypeMulticastDns );
|
---|
94 |
|
---|
95 | watchtimer.stopWatchdog();
|
---|
96 | }
|
---|
97 |
|
---|
98 | void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){
|
---|
99 | JoinData* data = event.getData<JoinData>();
|
---|
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 |
|
---|
105 | // tell the base overlay to join using this endpoint
|
---|
106 | assert( overlay != NULL );
|
---|
107 | overlay->joinSpoVNet( spovnetid, data->endpoint );
|
---|
108 |
|
---|
109 | delete data;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void 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 |
|
---|
116 | //
|
---|
117 | // generate the types
|
---|
118 | //
|
---|
119 |
|
---|
120 | SpoVNetID sid( info1 );
|
---|
121 | NodeID nid( info2 );
|
---|
122 | EndpointDescriptor ep( info3 );
|
---|
123 |
|
---|
124 | //
|
---|
125 | // is this announcement of interest for us?
|
---|
126 | //
|
---|
127 |
|
---|
128 | // announcement for another spovnet
|
---|
129 | if( sid != this->spovnetid ) return;
|
---|
130 |
|
---|
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 |
|
---|
135 | //
|
---|
136 | // send out the bootstrap information as
|
---|
137 | // event to synchronize into the system queue
|
---|
138 | //
|
---|
139 |
|
---|
140 | JoinData* data = new JoinData();
|
---|
141 | data->spovnetid = sid;
|
---|
142 | data->nodeid = nid;
|
---|
143 | data->endpoint = ep;
|
---|
144 |
|
---|
145 | SystemQueue::instance().scheduleEvent(
|
---|
146 | SystemEvent( this, OverlayBootstrapMethodType, data), 0 );
|
---|
147 | }
|
---|
148 |
|
---|
149 | void 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 |
|
---|
158 | void OverlayBootstrap::revoke(){
|
---|
159 | manager.revoke( randname );
|
---|
160 | }
|
---|
161 |
|
---|
162 | void OverlayBootstrap::recordJoin(const EndpointDescriptor& _ep){
|
---|
163 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
164 |
|
---|
165 | JoinData data;
|
---|
166 | data.spovnetid = spovnetid;
|
---|
167 | data.nodeid = nodeid;
|
---|
168 | data.endpoint = _ep;
|
---|
169 |
|
---|
170 | logging_info("recording bootstrap information " << data.endpoint.toString());
|
---|
171 |
|
---|
172 | lastJoines.push_front(data);
|
---|
173 | }
|
---|
174 |
|
---|
175 | bool OverlayBootstrap::haveOverlayConnections(){
|
---|
176 | boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
|
---|
177 | return haveOverlayConnection;
|
---|
178 | }
|
---|
179 |
|
---|
180 | void 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 |
|
---|
191 | {
|
---|
192 | boost::mutex::scoped_lock lock(haveOverlayConnectionMutex);
|
---|
193 | haveOverlayConnection = overlay->getOverlayNeighbors().size() > 0;
|
---|
194 |
|
---|
195 | // we have overlay neighbors -> ok nothing to do
|
---|
196 | if(haveOverlayConnection > 0) return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // no overlay neighbors, see if we can join using old information
|
---|
200 | logging_info("overlay not joined, checking for earlier used bootstrap information");
|
---|
201 | EndpointDescriptor joinep = EndpointDescriptor::UNSPECIFIED();
|
---|
202 |
|
---|
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
|
---|
212 | joinep = (*i).endpoint;
|
---|
213 |
|
---|
214 | if(lastJoines.size() >= 2)
|
---|
215 | swap( *lastJoines.begin(), *(--(lastJoines.end())) );
|
---|
216 | }
|
---|
217 |
|
---|
218 | logging_info("no overlay conenctivity detected, " <<
|
---|
219 | "trying to join using old bootstrap information: " <<
|
---|
220 | joinep.toString());
|
---|
221 |
|
---|
222 | // try to join using this node, if the join is successfull
|
---|
223 | // the endpoint will again be inserted using recordJoin
|
---|
224 | overlay->joinSpoVNet( spovnetid, joinep );
|
---|
225 | }
|
---|
226 |
|
---|
227 | OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
|
---|
228 | }
|
---|
229 |
|
---|
230 | void OverlayBootstrap::WatchdogTimer::startWatchdog(){
|
---|
231 | Timer::setInterval(5000);
|
---|
232 | Timer::start();
|
---|
233 | }
|
---|
234 |
|
---|
235 | void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
|
---|
236 | Timer::stop();
|
---|
237 | }
|
---|
238 |
|
---|
239 | void OverlayBootstrap::WatchdogTimer::eventFunction(){
|
---|
240 | if(obj == NULL) return;
|
---|
241 | obj->checkOverlayStatus();
|
---|
242 | }
|
---|
243 |
|
---|
244 | }} // namespace ariba, overlay
|
---|