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