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

Last change on this file since 6833 was 6266, checked in by mies, 15 years ago

added basic DHT functionality (untested)

File size: 11.3 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
124void OneHop::createOverlay() {
125 // don't need to bootstrap against ourselfs.
126 // the create and join process is completed now.
127 logging_info( "creating onehop overlay structure" );
128}
129
130void OneHop::deleteOverlay(){
131
132 logging_info( "deleting onehop overlay structure" );
133 state = OneHopStateInvalid;
134}
135
136OverlayInterface::NodeList OneHop::getKnownNodes(bool deep) const {
137
138 OverlayInterface::NodeList retlist;
139
140 OverlayNodeMapping::const_iterator i = overlayNodes.begin();
141 OverlayNodeMapping::const_iterator iend = overlayNodes.end();
142
143 for( ; i != iend; i++ )
144 retlist.push_back( i->first );
145
146 return retlist;
147}
148
149void OneHop::joinOverlay(const EndpointDescriptor& bootstrapEp){
150
151 logging_info( "joining onehop overlay structure through end-point " <<
152 (bootstrapEp.isUnspecified() ? "local" : bootstrapEp.toString()) );
153
154 if( bootstrapEp.isUnspecified() ){
155
156 // we are the initiator and we are to bootstrap against
157 // ourselfs. in the onehop overlay this is not an issue
158 // and we can just ignore this call.
159
160 state = OneHopStateCompleted;
161 } else {
162 bootstrapLinks.push_back(
163 baseoverlay.establishDirectLink( bootstrapEp,
164 OverlayInterface::OVERLAY_SERVICE_ID )
165 );
166 }
167}
168
169void OneHop::leaveOverlay(){
170
171 logging_info( "leaving onehop overlay structure" );
172
173 // set the state to invalid, this will prevent from
174 // handling onLinkDown events, as we are traversing the
175 // overlayNodes map and the onLinkDown function is called
176 // from the BaseOverlay and OneHop::onLinkDown will also
177 // try to access the overlayNodes structure.
178 state = OneHopStateInvalid;
179
180 //
181 // send leave messages to all nodes. the nodes
182 // will then drop the links
183 //
184
185 OverlayNodeMapping::iterator i = overlayNodes.begin();
186 OverlayNodeMapping::iterator iend = overlayNodes.end();
187
188 for( ; i != iend; i++){
189 if( i->first != nodeid && i->second != LinkID::UNSPECIFIED ){
190
191 OneHopMessage msg (OneHopMessage::OneHopMessageTypeLeave);
192 baseoverlay.sendMessage( &msg, i->second );
193 }
194 }
195}
196
197
198void OneHop::onLinkDown(const LinkID& lnk, const NodeID& remote){
199
200 // don't handle when we are in state-invalid,
201 // see comment in OneHop::leaveOverlay
202 if( state == OneHopStateInvalid ) return;
203
204 // node went down, remove from overlay mapping
205 logging_debug( "link " << lnk.toString() << " to node " << remote.toString() << " went down, removing node" );
206
207 OverlayNodeMapping::iterator i = overlayNodes.begin();
208 OverlayNodeMapping::iterator iend = overlayNodes.end();
209
210 for( ; i != iend; i++ ){
211 if( i->second == lnk ){
212 overlayNodes.erase( i );
213 break;
214 }
215 }
216
217 vector<LinkID>::iterator it = std::find( bootstrapLinks.begin(), bootstrapLinks.end(), lnk );
218 if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it );
219}
220
221void OneHop::onLinkUp(const LinkID& lnk, const NodeID& remote){
222
223 logging_debug( "link is up, sending out node listing request" );
224
225 NodeListingRequest requestmsg;
226 OneHopMessage onemsg( OneHopMessage::OneHopMessageTypeListingRequest );
227 onemsg.encapsulate( &requestmsg );
228
229 baseoverlay.sendMessage( &onemsg, lnk );
230}
231
232void OneHop::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk){
233
234 OneHopMessage* onemsg = msg.getMessage()->convert<OneHopMessage>();
235 if( onemsg == NULL ) return;
236
237 //
238 // handle node listing request
239 //
240
241 if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingRequest ) ){
242
243 NodeListingRequest* request = onemsg->decapsulate<NodeListingRequest>();
244
245 logging_info( "onehop received node listing request from node " << remote.toString() );
246
247 //
248 // first, insert the nodes and the link into our mapping
249 //
250
251 overlayNodes.insert( make_pair(remote, lnk) );
252
253 //
254 // send back a message with all nodes
255 // and their current EndpointDescriptor
256 //
257
258 OneHopMessage onehopReply( OneHopMessage::OneHopMessageTypeListingReply );
259 NodeListingReply listingReply;
260
261 OverlayNodeMapping::iterator i = overlayNodes.begin();
262 OverlayNodeMapping::iterator iend = overlayNodes.end();
263
264 logging_debug( "sending out node listing reply with the following items" );
265
266 for( ; i != iend; i++ ){
267
268 const NodeID node = i->first;
269 const LinkID link = i->second;
270 const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
271
272 logging_debug( "node: " + node.toString() + ", endp: " + endpoint.toString());
273 listingReply.add( node, const_cast<EndpointDescriptor*>(new EndpointDescriptor(endpoint)) );
274 }
275
276 onehopReply.encapsulate( &listingReply );
277 baseoverlay.sendMessage( &onehopReply, lnk );
278
279 //
280 // now that we know the node, we can tell the baseoverlay
281 // that the node has joined our overlay structure
282 //
283
284 eventsReceiver->onNodeJoin( remote );
285
286 } // OneHopMessageTypeListingRequest
287
288 //
289 // handle node listing reply
290 //
291
292 if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingReply) ){
293
294 NodeListingReply* reply = onemsg->decapsulate<NodeListingReply>();
295
296 logging_debug( "received node listing reply from node " << remote.toString()
297 << " with all overlay nodes. connecting to all of them" );
298
299 //
300 // get out all the EndpointDescriptors from the
301 // overlay nodes and connect to all nodes where
302 // we don't have a link yet
303 //
304
305 const NodeListingReply::NodeEndpointList& endpoints = reply->getList();
306 logging_debug( "received " << endpoints.size() << " nodes in listing" );
307
308 NodeListingReply::NodeEndpointList::const_iterator i = endpoints.begin();
309 NodeListingReply::NodeEndpointList::const_iterator iend = endpoints.end();
310
311 for( ; i != iend; i++ ){
312
313 //
314 // don't connect to nodes that we already have
315 // a link to and don't connect to ourself
316 //
317
318 const NodeID& node = (*i).first;
319 if( overlayNodes.find(node) != overlayNodes.end() ) continue;
320 if( node == nodeid ) continue;
321
322 logging_debug( "building up link to node in overlay " << node.toString() );
323 const LinkID link = baseoverlay.establishDirectLink( *((*i).second),
324 OverlayInterface::OVERLAY_SERVICE_ID );
325
326 overlayNodes.insert( make_pair(node, link) );
327
328 } // for( ; i != iend; i++ )
329
330 } // OneHopMessageTypeListingReply
331
332 //
333 // handle node leaves
334 //
335
336 if( onemsg->isType(OneHopMessage::OneHopMessageTypeLeave) ){
337
338 logging_debug("received leave message from " <<
339 remote.toString() << " on link " << lnk.toString());
340
341 // drop the link to the node
342 baseoverlay.dropLink( lnk );
343
344 } // OneHopMessageTypeLeave
345
346 //
347 // handle kbr route messages
348 //
349
350 if( onemsg->isType( OneHopMessage::OneHopMessageTypeRoute) ){
351 logging_debug( "Route message arrived at destination node -> delegate to BaseOverlay" );
352 baseoverlay.incomingRouteMessage( onemsg, lnk, remote);
353 } // OneHopMessageTypeRoute
354
355}
356
357void OneHop::eventFunction(){
358
359 logging_debug("<<<<<<<<<<<<<<<<onehop-table<<<<<<<<<<<<<<<<<<<");
360
361 OverlayNodeMapping::iterator i = overlayNodes.begin();
362 OverlayNodeMapping::iterator iend = overlayNodes.end();
363
364 for( ; i != iend; i++ ){
365
366 const NodeID node = i->first;
367 const LinkID link = i->second;
368 const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
369
370 logging_debug( "node: " << node.toString() <<
371 ", link_: " << link.toString() << ", endp: " << endpoint.toString());
372 }
373
374 logging_debug(">>>>>>>>>>>>>>>>>onehop-table>>>>>>>>>>>>>>>>>>>>>");
375
376}
377
378}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.