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

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

merge from bootstrap branch

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