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

Last change on this file since 2455 was 2455, checked in by mies, 16 years ago

fixed bugs concerning bootstrap

File size: 8.9 KB
Line 
1// [Licence]
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// [Licence]
38
39#include "OneHop.h"
40#include "ariba/overlay/BaseOverlay.h"
41
42namespace ariba {
43namespace overlay {
44
45use_logging_cpp( OneHop );
46
47OneHop::OneHop(BaseOverlay& _baseoverlay, const NodeID& _nodeid, OverlayStructureEvents* _eventsReceiver)
48 : OverlayInterface( _baseoverlay, _nodeid, _eventsReceiver ),
49 state ( OneHopStateInvalid ),
50 bootstrapLink ( LinkID::UNSPECIFIED ),
51 pendingLinks ( 0 ) {
52
53 //
54 // insert us as the first node in the overlay
55 //
56 overlayNodes.insert( make_pair(_nodeid, LinkID::UNSPECIFIED) );
57}
58
59OneHop::~OneHop(){
60
61 deleteOverlay();
62}
63
64const EndpointDescriptor& OneHop::resolveNode(const NodeID& node){
65
66 OverlayNodeMapping::const_iterator i = overlayNodes.find( node );
67 if (i == overlayNodes.end()) return EndpointDescriptor::UNSPECIFIED;
68
69 const EndpointDescriptor& ep = baseoverlay.getEndpointDescriptor( i->second );
70
71 logging_debug( "resolved node " << node.toString() << " to endpoint " << ep.toString() );
72 return ep;
73}
74
75void OneHop::routeMessage(const NodeID& destnode, Message* msg){
76
77 // in the fullmesh overlay we know every other node
78 // so we also have a link to each other node
79
80 logging_debug( "routing message to node " << destnode.toString() );
81
82 OverlayNodeMapping::const_iterator i = overlayNodes.find( destnode );
83 if (i == overlayNodes.end()) {
84 logging_error( "not able to route message to node " << destnode.toString() );
85 return;
86 }
87
88 baseoverlay.sendMessage( msg, i->second );
89}
90
91void OneHop::createOverlay(){
92
93 // don't need to bootstrap against ourselfs.
94 // the create and join process is completed now.
95
96 logging_info( "creating onehop overlay structure" );
97 state = OneHopStateCompleted;
98}
99
100void OneHop::deleteOverlay(){
101
102 logging_info( "deleting onehop overlay structure" );
103 state = OneHopStateInvalid;
104 pendingLinks = 0;
105}
106
107OverlayInterface::NodeList OneHop::getKnownNodes() const {
108
109 OverlayInterface::NodeList retlist;
110
111 OverlayNodeMapping::const_iterator i = overlayNodes.begin();
112 OverlayNodeMapping::const_iterator iend = overlayNodes.end();
113
114 for( ; i != iend; i++ )
115 retlist.push_back( i->first );
116
117 return retlist;
118}
119
120void OneHop::joinOverlay(const EndpointDescriptor& bootstrapEp){
121
122 logging_info( "joining onehop overlay structure through endpoint " <<
123 (bootstrapEp == EndpointDescriptor::UNSPECIFIED ?
124 "local" : bootstrapEp.toString()) );
125
126 state = OneHopStateJoinInitiated;
127 pendingLinks = 0;
128
129 if( bootstrapEp == EndpointDescriptor::UNSPECIFIED ){
130
131 // we are the initiator and we are to bootstrap against
132 // ourselfs. in the onehop overlay this is not an issue
133 // and we can just ignore this call.
134
135 state = OneHopStateCompleted;
136
137
138 } else {
139
140 bootstrapLink = baseoverlay.establishLink( bootstrapEp,
141 OverlayInterface::OVERLAY_SERVICE_ID );
142 }
143}
144
145void OneHop::leaveOverlay(){
146
147 logging_info( "leaving onehop overlay structure" );
148
149 //
150 // close all links, this will indicate the other nodes that we left
151 //
152
153 OverlayNodeMapping::iterator i = overlayNodes.begin();
154 OverlayNodeMapping::iterator iend = overlayNodes.end();
155
156 for( ; i != iend; i++){
157 if( i->first != nodeid && i->second != LinkID::UNSPECIFIED )
158 baseoverlay.dropLink( i->second );
159 }
160
161 state = OneHopStateInvalid;
162 pendingLinks = 0;
163}
164
165void OneHop::onLinkDown( const LinkID& link, const NodeID& local, const NodeID& remote ){
166
167 //
168 // node went down, remove from overlay mapping
169 //
170
171 logging_debug( "node " << remote.toString() << " left overlay structure" );
172
173 OverlayNodeMapping::iterator i = overlayNodes.begin();
174 OverlayNodeMapping::iterator iend = overlayNodes.end();
175
176 for( ; i != iend; i++ ){
177 if( i->second == link ){
178 overlayNodes.erase( i );
179 break;
180 }
181 }
182}
183
184void OneHop::onLinkUp(const LinkID& link, const NodeID& local, const NodeID& remote) {
185
186 //
187 // as soon as a link goes up, we always request the node listing.
188 // and try to get connections to as much nodes as possible in a greedy way.
189 //
190
191 if( link != bootstrapLink ){
192 if( pendingLinks > 0 ) pendingLinks--;
193 if( pendingLinks == 0 ) state = OneHopStateCompleted;
194 }
195
196 logging_debug( "link is up, sending out node listing request" );
197
198 NodeListingRequest requestmsg;
199 OneHopMessage onemsg( OneHopMessage::OneHopMessageTypeListingRequest );
200 onemsg.encapsulate( &requestmsg );
201
202 state = OneHopStateJoinListingRequested;
203 baseoverlay.sendMessage( &onemsg, link );
204}
205
206bool OneHop::receiveMessage(const Message* message, const LinkID& link, const NodeID& node){
207
208 OneHopMessage* onemsg = const_cast<Message*>(message)->decapsulate<OneHopMessage>();
209 if( onemsg == NULL ) return false;
210
211 //
212 // handle node listing request
213 //
214
215 if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingRequest) ){
216
217 NodeListingRequest* request = onemsg->decapsulate<NodeListingRequest>();
218
219 logging_info( "onehop received node listing request from node " << node.toString() );
220
221 //
222 // first, insert the nodes and the link into our mapping
223 //
224
225 overlayNodes.insert( make_pair(node, link) );
226
227 //
228 // send back a message with all nodes
229 // and their current EndpointDescriptor
230 //
231
232 OneHopMessage onehopReply( OneHopMessage::OneHopMessageTypeListingReply );
233 NodeListingReply listingReply;
234
235 OverlayNodeMapping::iterator i = overlayNodes.begin();
236 OverlayNodeMapping::iterator iend = overlayNodes.end();
237
238 logging_debug( "sending out node listing reply with the following items" );
239
240 for( ; i != iend; i++ ){
241
242 const NodeID node = i->first;
243 const LinkID link = i->second;
244 const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
245
246 logging_debug( "node: " + node.toString() + ", endp: " + endpoint.toString());
247 listingReply.add( node, const_cast<EndpointDescriptor*>(new EndpointDescriptor(endpoint)) );
248 }
249
250 onehopReply.encapsulate( &listingReply );
251 baseoverlay.sendMessage( &onehopReply, link );
252
253 //
254 // now that we know the node, we can tell the baseoverlay
255 // that the node has joined our overlay structure
256 //
257
258 eventsReceiver->onNodeJoin( node );
259
260 } // if( request != NULL )
261
262 //
263 // handle node listing reply
264 //
265
266 if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingReply) ){
267
268 NodeListingReply* reply = onemsg->decapsulate<NodeListingReply>();
269
270 logging_debug( "received node listing reply from node " << node.toString()
271 << " with all overlay nodes. connecting to all of them" );
272
273 //
274 // get out all the EndpointDescriptors from the
275 // overlay nodes and connect to all nodes where
276 // we don't have a link yet
277 //
278
279 const NodeListingReply::NodeEndpointList& endpoints = reply->getList();
280 logging_debug( "received " << endpoints.size() << " nodes in listing" );
281 pendingLinks = 0;
282
283 NodeListingReply::NodeEndpointList::const_iterator i = endpoints.begin();
284 NodeListingReply::NodeEndpointList::const_iterator iend = endpoints.end();
285
286 for( ; i != iend; i++ ){
287
288 //
289 // don't connect to nodes that we already have
290 // a link to and don't connect to ourself
291 //
292
293 const NodeID& node = (*i).first;
294 if( overlayNodes.find(node) != overlayNodes.end() ) continue;
295 if( node == nodeid ) continue;
296
297 logging_debug( "building up link to node in overlay " << node.toString() );
298 const LinkID link = baseoverlay.establishLink( *((*i).second),
299 OverlayInterface::OVERLAY_SERVICE_ID );
300
301 overlayNodes.insert( make_pair(node, link) );
302 pendingLinks++;
303
304 } // for( ; i != iend; i++ )
305
306 } // if( reply != NULL )
307
308}
309
310}} // namespace ariba, overlay
Note: See TracBrowser for help on using the repository browser.