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

Last change on this file since 12755 was 10572, checked in by Michael Tänzer, 12 years ago

Fix DHT: messages got lost if not communicating over a direct link.

Also:

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