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

Last change on this file since 3578 was 3067, checked in by Christoph Mayer, 16 years ago

-fixed #30 (stress test pingpong, has some errors), #24 (autolinks for easy message sending)

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