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

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

-fixes #4 (merge tidy interface with current implementation)

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 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 //
149 // close all links, this will indicate the other nodes that we left
150 //
151
152 OverlayNodeMapping::iterator i = overlayNodes.begin();
153 OverlayNodeMapping::iterator iend = overlayNodes.end();
154
155 for( ; i != iend; i++){
156 if( i->first != nodeid && i->second != LinkID::UNSPECIFIED )
157 baseoverlay.dropLink( i->second );
158 }
159
160 state = OneHopStateInvalid;
161 pendingLinks = 0;
162}
163
164
165void OneHop::onLinkDown(const LinkID& lnk, 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 == lnk ){
178 overlayNodes.erase( i );
179 break;
180 }
181 }
182}
183
184void OneHop::onLinkUp(const LinkID& lnk, 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( lnk != 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, lnk );
204}
205
206void OneHop::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk){
207
208 OneHopMessage* onemsg = msg.getMessage()->convert<OneHopMessage>();
209 if( onemsg == NULL ) return;
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 " << remote.toString() );
220
221 //
222 // first, insert the nodes and the link into our mapping
223 //
224
225 overlayNodes.insert( make_pair(remote, lnk) );
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, lnk );
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( remote );
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 " << remote.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.