source: source/ariba/overlay/modules/onehop/OneHop.cpp@ 5308

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

begin merge back from relay branch

File size: 11.4 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 "OneHop.h"
40#include "ariba/overlay/BaseOverlay.h"
41
42#include "ariba/overlay/modules/onehop/messages/OneHopMessage.h"
43#include "ariba/overlay/modules/onehop/messages/NodeListingRequest.h"
44#include "ariba/overlay/modules/onehop/messages/NodeListingReply.h"
45
46namespace ariba {
47namespace overlay {
48
49use_logging_cpp( OneHop );
50
51OneHop::OneHop(BaseOverlay& _baseoverlay, const NodeID& _nodeid,
52 OverlayStructureEvents* _eventsReceiver, const OverlayParameterSet& param)
53 : OverlayInterface( _baseoverlay, _nodeid, _eventsReceiver, param ),
54 state ( OneHopStateInvalid ),
55 bootstrapLink ( LinkID::UNSPECIFIED ),
56 pendingLinks ( 0 ) {
57
58 //
59 // insert us as the first node in the overlay
60 //
61 overlayNodes.insert( make_pair(_nodeid, LinkID::UNSPECIFIED) );
62
63 Timer::setInterval(5000);
64 Timer::start();
65}
66
67OneHop::~OneHop(){
68 Timer::stop();
69 deleteOverlay();
70}
71
72const EndpointDescriptor& OneHop::resolveNode(const NodeID& node){
73
74 OverlayNodeMapping::const_iterator i = overlayNodes.find( node );
75 if (i == overlayNodes.end()) return EndpointDescriptor::UNSPECIFIED;
76
77 const EndpointDescriptor& ep = baseoverlay.getEndpointDescriptor( i->second );
78
79 logging_debug( "resolved node " << node.toString() << " to endpoint " << ep.toString() );
80 return ep;
81}
82
83void OneHop::routeMessage(const NodeID& destnode, Message* msg){
84
85 // in the fullmesh overlay we know every other node
86 // so we also have a link to each other node
87
88 logging_debug( "routing message to node " << destnode.toString() );
89
90 OverlayNodeMapping::const_iterator i = overlayNodes.find( destnode );
91 if (i == overlayNodes.end()) {
92 logging_error( "not able to route message to node " << destnode.toString() );
93 return;
94 }
95 OneHopMessage onehopRoute( OneHopMessage::OneHopMessageTypeRoute );
96 onehopRoute.encapsulate(msg);
97
98 baseoverlay.sendMessage( &onehopRoute, i->second );
99}
100
101void OneHop::routeMessage(const NodeID& node, const LinkID& link, Message* msg) {
102 OneHopMessage onehopRoute( OneHopMessage::OneHopMessageTypeRoute );
103 onehopRoute.encapsulate(msg);
104 baseoverlay.sendMessage( &onehopRoute, link );
105}
106
107/// @see OverlayInterface.h
108const LinkID& OneHop::getNextLinkId( const NodeID& id ) const {
109 OverlayNodeMapping::const_iterator i = overlayNodes.find( id );
110 if (i == overlayNodes.end()) return LinkID::UNSPECIFIED;
111 return i->second;
112}
113
114void OneHop::createOverlay() {
115 // don't need to bootstrap against ourselfs.
116 // the create and join process is completed now.
117 logging_info( "creating onehop overlay structure" );
118 state = OneHopStateCompleted;
119}
120
121void OneHop::deleteOverlay(){
122
123 logging_info( "deleting onehop overlay structure" );
124 state = OneHopStateInvalid;
125 pendingLinks = 0;
126}
127
128OverlayInterface::NodeList OneHop::getKnownNodes() const {
129
130 OverlayInterface::NodeList retlist;
131
132 OverlayNodeMapping::const_iterator i = overlayNodes.begin();
133 OverlayNodeMapping::const_iterator iend = overlayNodes.end();
134
135 for( ; i != iend; i++ )
136 retlist.push_back( i->first );
137
138 return retlist;
139}
140
141void OneHop::joinOverlay(const EndpointDescriptor& bootstrapEp){
142
143 logging_info( "joining onehop overlay structure through end-point " <<
144 (bootstrapEp == EndpointDescriptor::UNSPECIFIED ?
145 "local" : bootstrapEp.toString()) );
146
147 state = OneHopStateJoinInitiated;
148 pendingLinks = 0;
149
150 if( bootstrapEp == EndpointDescriptor::UNSPECIFIED ){
151
152 // we are the initiator and we are to bootstrap against
153 // ourselfs. in the onehop overlay this is not an issue
154 // and we can just ignore this call.
155
156 state = OneHopStateCompleted;
157 } else {
158 bootstrapLink = baseoverlay.establishLink( bootstrapEp,
159 OverlayInterface::OVERLAY_SERVICE_ID );
160 }
161}
162
163void OneHop::leaveOverlay(){
164
165 logging_info( "leaving onehop overlay structure" );
166
167 // set the state to invalid, this will prevent from
168 // handling onLinkDown events, as we are traversing the
169 // overlayNodes map and the onLinkDown function is called
170 // from the BaseOverlay and OneHop::onLinkDown will also
171 // try to access the overlayNodes structure.
172 state = OneHopStateInvalid;
173
174 //
175 // send leave messages to all nodes. the nodes
176 // will then drop the links
177 //
178
179 OverlayNodeMapping::iterator i = overlayNodes.begin();
180 OverlayNodeMapping::iterator iend = overlayNodes.end();
181
182 for( ; i != iend; i++){
183 if( i->first != nodeid && i->second != LinkID::UNSPECIFIED ){
184
185 OneHopMessage msg (OneHopMessage::OneHopMessageTypeLeave);
186 baseoverlay.sendMessage( &msg, i->second );
187 }
188 }
189
190 pendingLinks = 0;
191}
192
193
194void OneHop::onLinkDown(const LinkID& lnk, const NodeID& remote){
195
196 // don't handle when we are in state-invalid,
197 // see comment in OneHop::leaveOverlay
198 if( state == OneHopStateInvalid ) return;
199
200 // node went down, remove from overlay mapping
201 logging_debug( "link " << lnk.toString() << " to node " << remote.toString() << " went down, removing node" );
202
203 OverlayNodeMapping::iterator i = overlayNodes.begin();
204 OverlayNodeMapping::iterator iend = overlayNodes.end();
205
206 for( ; i != iend; i++ ){
207 if( i->second == lnk ){
208 overlayNodes.erase( i );
209 break;
210 }
211 }
212}
213
214void OneHop::onLinkUp(const LinkID& lnk, const NodeID& remote){
215
216 //
217 // as soon as a link goes up, we always request the node listing.
218 // and try to get connections to as much nodes as possible in a greedy way.
219 //
220
221 if( lnk != bootstrapLink ){
222 if( pendingLinks > 0 ) pendingLinks--;
223 if( pendingLinks == 0 ) state = OneHopStateCompleted;
224 }
225
226 logging_debug( "link is up, sending out node listing request" );
227
228 NodeListingRequest requestmsg;
229 OneHopMessage onemsg( OneHopMessage::OneHopMessageTypeListingRequest );
230 onemsg.encapsulate( &requestmsg );
231
232 state = OneHopStateJoinListingRequested;
233 baseoverlay.sendMessage( &onemsg, lnk );
234}
235
236void OneHop::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk){
237
238 OneHopMessage* onemsg = msg.getMessage()->convert<OneHopMessage>();
239 if( onemsg == NULL ) return;
240
241 //
242 // handle node listing request
243 //
244
245 if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingRequest ) ){
246
247 NodeListingRequest* request = onemsg->decapsulate<NodeListingRequest>();
248
249 logging_info( "onehop received node listing request from node " << remote.toString() );
250
251 //
252 // first, insert the nodes and the link into our mapping
253 //
254
255 overlayNodes.insert( make_pair(remote, lnk) );
256
257 //
258 // send back a message with all nodes
259 // and their current EndpointDescriptor
260 //
261
262 OneHopMessage onehopReply( OneHopMessage::OneHopMessageTypeListingReply );
263 NodeListingReply listingReply;
264
265 OverlayNodeMapping::iterator i = overlayNodes.begin();
266 OverlayNodeMapping::iterator iend = overlayNodes.end();
267
268 logging_debug( "sending out node listing reply with the following items" );
269
270 for( ; i != iend; i++ ){
271
272 const NodeID node = i->first;
273 const LinkID link = i->second;
274 const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
275
276 logging_debug( "node: " + node.toString() + ", endp: " + endpoint.toString());
277 listingReply.add( node, const_cast<EndpointDescriptor*>(new EndpointDescriptor(endpoint)) );
278 }
279
280 onehopReply.encapsulate( &listingReply );
281 baseoverlay.sendMessage( &onehopReply, lnk );
282
283 //
284 // now that we know the node, we can tell the baseoverlay
285 // that the node has joined our overlay structure
286 //
287
288 eventsReceiver->onNodeJoin( remote );
289
290 } // OneHopMessageTypeListingRequest
291
292 //
293 // handle node listing reply
294 //
295
296 if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingReply) ){
297
298 NodeListingReply* reply = onemsg->decapsulate<NodeListingReply>();
299
300 logging_debug( "received node listing reply from node " << remote.toString()
301 << " with all overlay nodes. connecting to all of them" );
302
303 //
304 // get out all the EndpointDescriptors from the
305 // overlay nodes and connect to all nodes where
306 // we don't have a link yet
307 //
308
309 const NodeListingReply::NodeEndpointList& endpoints = reply->getList();
310 logging_debug( "received " << endpoints.size() << " nodes in listing" );
311 pendingLinks = 0;
312
313 NodeListingReply::NodeEndpointList::const_iterator i = endpoints.begin();
314 NodeListingReply::NodeEndpointList::const_iterator iend = endpoints.end();
315
316 for( ; i != iend; i++ ){
317
318 //
319 // don't connect to nodes that we already have
320 // a link to and don't connect to ourself
321 //
322
323 const NodeID& node = (*i).first;
324 if( overlayNodes.find(node) != overlayNodes.end() ) continue;
325 if( node == nodeid ) continue;
326
327 logging_debug( "building up link to node in overlay " << node.toString() );
328 const LinkID link = baseoverlay.establishLink( *((*i).second),
329 OverlayInterface::OVERLAY_SERVICE_ID );
330
331 overlayNodes.insert( make_pair(node, link) );
332 pendingLinks++;
333
334 } // for( ; i != iend; i++ )
335
336 } // OneHopMessageTypeListingReply
337
338 //
339 // handle node leaves
340 //
341
342 if( onemsg->isType(OneHopMessage::OneHopMessageTypeLeave) ){
343
344 logging_debug("received leave message from " <<
345 remote.toString() << " on link " << lnk.toString());
346
347 // drop the link to the node
348 baseoverlay.dropLink( lnk );
349
350 } // OneHopMessageTypeLeave
351
352 //
353 // handle kbr route messages
354 //
355
356 if( onemsg->isType( OneHopMessage::OneHopMessageTypeRoute) ){
357 logging_debug( "Route message arrived at destination node -> delegate to BaseOverlay" );
358 baseoverlay.incomingRouteMessage( onemsg );
359 } // OneHopMessageTypeRoute
360
361}
362
363void OneHop::eventFunction(){
364
365 logging_debug("<<<<<<<<<<<<<<<<onehop-table<<<<<<<<<<<<<<<<<<<");
366
367 OverlayNodeMapping::iterator i = overlayNodes.begin();
368 OverlayNodeMapping::iterator iend = overlayNodes.end();
369
370 for( ; i != iend; i++ ){
371
372 const NodeID node = i->first;
373 const LinkID link = i->second;
374 const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
375
376 logging_debug( "node: " << node.toString() <<
377 ", link_: " << link.toString() << ", endp: " << endpoint.toString());
378 }
379
380 logging_debug(">>>>>>>>>>>>>>>>>onehop-table>>>>>>>>>>>>>>>>>>>>>");
381
382}
383
384}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.