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 |
|
---|
42 | namespace ariba {
|
---|
43 | namespace overlay {
|
---|
44 |
|
---|
45 | use_logging_cpp(OverlayBootstrap);
|
---|
46 | SystemEventType OverlayBootstrapMethodType("OverlayBootstrapMethodType");
|
---|
47 |
|
---|
48 | OverlayBootstrap::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 |
|
---|
58 | OverlayBootstrap::~OverlayBootstrap(){
|
---|
59 | }
|
---|
60 |
|
---|
61 | void 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 |
|
---|
75 | void 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 |
|
---|
89 | void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){
|
---|
90 | JoinData* data = event.getData<JoinData>();
|
---|
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 |
|
---|
103 | void 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 | JoinData* data = new JoinData();
|
---|
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 |
|
---|
140 | void 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 |
|
---|
149 | void OverlayBootstrap::revoke(){
|
---|
150 | manager.revoke( randname );
|
---|
151 | }
|
---|
152 |
|
---|
153 | void OverlayBootstrap::recordJoin(const EndpointDescriptor& _ep){
|
---|
154 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
155 |
|
---|
156 | JoinData data;
|
---|
157 | data.spovnetid = spovnetid;
|
---|
158 | data.nodeid = nodeid;
|
---|
159 | data.endpoint = _ep;
|
---|
160 |
|
---|
161 | logging_info("recording bootstrap information " << data.endpoint.toString());
|
---|
162 |
|
---|
163 | lastJoines.push_front(data);
|
---|
164 | }
|
---|
165 |
|
---|
166 | void OverlayBootstrap::checkOverlayStatus(){
|
---|
167 |
|
---|
168 | // if we have no overlay neighbors, try to bootstrap using
|
---|
169 | // bootstrap information that we already used
|
---|
170 |
|
---|
171 | { //limit history to 10 endpoints
|
---|
172 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
173 | while(lastJoines.size() > 10)
|
---|
174 | lastJoines.pop_back();
|
---|
175 | }
|
---|
176 |
|
---|
177 | // we have overlay neighbors -> ok
|
---|
178 | if(overlay->getOverlayNeighbors().size() > 0) return;
|
---|
179 |
|
---|
180 | logging_info("overlay not joined, checking for earlier used bootstrap information");
|
---|
181 | EndpointDescriptor joinep = EndpointDescriptor::UNSPECIFIED();
|
---|
182 |
|
---|
183 | // no overlay neighbors -> try out already
|
---|
184 | // successfully used bootstrap nodes
|
---|
185 | JoinData data;
|
---|
186 | {
|
---|
187 | boost::mutex::scoped_lock lock(lastJoinesMutex);
|
---|
188 | JoinStack::iterator i = lastJoines.begin();
|
---|
189 | if(i == lastJoines.end()) return;
|
---|
190 |
|
---|
191 | // use last used element and then put it into back
|
---|
192 | joinep = (*i).endpoint;
|
---|
193 |
|
---|
194 | if(lastJoines.size() >= 2)
|
---|
195 | swap( *lastJoines.begin(), *(--(lastJoines.end())) );
|
---|
196 | }
|
---|
197 |
|
---|
198 | logging_info("no overlay conenctivity detected, " <<
|
---|
199 | "trying to join using old bootstrap information: " <<
|
---|
200 | joinep.toString());
|
---|
201 |
|
---|
202 | // try to join using this node, if the join is successfull
|
---|
203 | // the endpoint will again be inserted using recordJoin
|
---|
204 | overlay->joinSpoVNet( spovnetid, joinep );
|
---|
205 | }
|
---|
206 |
|
---|
207 | OverlayBootstrap::WatchdogTimer::WatchdogTimer(OverlayBootstrap* _obj) : obj(_obj) {
|
---|
208 | }
|
---|
209 |
|
---|
210 | void OverlayBootstrap::WatchdogTimer::startWatchdog(){
|
---|
211 | Timer::setInterval(5000);
|
---|
212 | Timer::start();
|
---|
213 | }
|
---|
214 |
|
---|
215 | void OverlayBootstrap::WatchdogTimer::stopWatchdog(){
|
---|
216 | Timer::stop();
|
---|
217 | }
|
---|
218 |
|
---|
219 | void OverlayBootstrap::WatchdogTimer::eventFunction(){
|
---|
220 | if(obj == NULL) return;
|
---|
221 | obj->checkOverlayStatus();
|
---|
222 | }
|
---|
223 |
|
---|
224 | }} // namespace ariba, overlay
|
---|