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 | #ifndef BASEOVERLAY_H_ |
---|
40 | #define BASEOVERLAY_H_ |
---|
41 | |
---|
42 | #include <map> |
---|
43 | #include <iostream> |
---|
44 | #include <algorithm> |
---|
45 | #include <ctime> |
---|
46 | #include <list> |
---|
47 | #include <vector> |
---|
48 | #include <boost/foreach.hpp> |
---|
49 | |
---|
50 | #include "ariba/utility/messages.h" |
---|
51 | #include "ariba/utility/types.h" |
---|
52 | #include "ariba/utility/misc/Helper.h" |
---|
53 | #include "ariba/utility/misc/Demultiplexer.hpp" |
---|
54 | #include "ariba/utility/logging/Logging.h" |
---|
55 | #include "ariba/utility/system/Timer.h" |
---|
56 | |
---|
57 | #include "ariba/communication/EndpointDescriptor.h" |
---|
58 | #include "ariba/communication/BaseCommunication.h" |
---|
59 | #include "ariba/communication/CommunicationEvents.h" |
---|
60 | |
---|
61 | #include "ariba/interface/ServiceInterface.h" |
---|
62 | |
---|
63 | #include "ariba/overlay/modules/OverlayInterface.h" |
---|
64 | #include "ariba/overlay/modules/OverlayFactory.h" |
---|
65 | #include "ariba/overlay/modules/OverlayStructureEvents.h" |
---|
66 | #include "ariba/overlay/messages/OverlayMsg.h" |
---|
67 | #include "ariba/overlay/messages/JoinRequest.h" |
---|
68 | #include "ariba/overlay/messages/JoinReply.h" |
---|
69 | |
---|
70 | using std::vector; |
---|
71 | using std::list; |
---|
72 | using std::cout; |
---|
73 | using std::map; |
---|
74 | using std::make_pair; |
---|
75 | using std::pair; |
---|
76 | using std::find; |
---|
77 | |
---|
78 | using ariba::communication::EndpointDescriptor; |
---|
79 | using ariba::communication::BaseCommunication; |
---|
80 | using ariba::communication::CommunicationEvents; |
---|
81 | |
---|
82 | using ariba::interface::ServiceInterface; |
---|
83 | |
---|
84 | using ariba::overlay::OverlayMsg; |
---|
85 | using ariba::overlay::JoinRequest; |
---|
86 | using ariba::overlay::JoinReply; |
---|
87 | using ariba::overlay::OverlayInterface; |
---|
88 | using ariba::overlay::OverlayFactory; |
---|
89 | using ariba::overlay::OverlayStructureEvents; |
---|
90 | |
---|
91 | using ariba::utility::NodeID; |
---|
92 | using ariba::utility::SpoVNetID; |
---|
93 | using ariba::utility::LinkID; |
---|
94 | using ariba::utility::Identifier; |
---|
95 | using ariba::utility::ServiceID; |
---|
96 | using ariba::utility::QoSParameterSet; |
---|
97 | using ariba::utility::SecurityParameterSet; |
---|
98 | using ariba::utility::Demultiplexer; |
---|
99 | using ariba::utility::MessageReceiver; |
---|
100 | using ariba::utility::MessageSender; |
---|
101 | using ariba::utility::seqnum_t; |
---|
102 | using ariba::utility::Timer; |
---|
103 | |
---|
104 | #define ovl OvlVis::instance() |
---|
105 | #define ovlId OvlVis::NETWORK_ID_BASE_OVERLAY |
---|
106 | |
---|
107 | // needed for friend decleration |
---|
108 | // in different namespace |
---|
109 | namespace ariba { |
---|
110 | class Node; |
---|
111 | } |
---|
112 | |
---|
113 | namespace ariba { |
---|
114 | namespace overlay { |
---|
115 | |
---|
116 | class BaseOverlay : |
---|
117 | public MessageReceiver, |
---|
118 | public CommunicationEvents, |
---|
119 | public OverlayStructureEvents, |
---|
120 | protected Timer { |
---|
121 | |
---|
122 | use_logging_h( BaseOverlay ); |
---|
123 | friend class ariba::Node; |
---|
124 | |
---|
125 | public: |
---|
126 | |
---|
127 | /** |
---|
128 | * Constructs a Base Overlay instance |
---|
129 | */ |
---|
130 | BaseOverlay( BaseCommunication& _basecomm, const NodeID& _nodeid ); |
---|
131 | |
---|
132 | /** |
---|
133 | * TODO |
---|
134 | */ |
---|
135 | virtual ~BaseOverlay(); |
---|
136 | |
---|
137 | /** |
---|
138 | * Starts a link establishment procedure to the specfied node |
---|
139 | * |
---|
140 | * @param node The node id |
---|
141 | */ |
---|
142 | const LinkID establishLink( const NodeID& node, const ServiceID& service ); |
---|
143 | |
---|
144 | /** |
---|
145 | * TODO |
---|
146 | */ |
---|
147 | const LinkID establishLink( const EndpointDescriptor& ep, const ServiceID& service ); |
---|
148 | |
---|
149 | /** |
---|
150 | * TODO |
---|
151 | */ |
---|
152 | void dropLink( const LinkID& link ); |
---|
153 | |
---|
154 | /** |
---|
155 | * TODO |
---|
156 | */ |
---|
157 | seqnum_t sendMessage( const Message* message, const LinkID& link ); |
---|
158 | |
---|
159 | /** |
---|
160 | * TODO |
---|
161 | */ |
---|
162 | seqnum_t sendMessage( const Message* message, const NodeID& node, const ServiceID& service ); |
---|
163 | |
---|
164 | /** |
---|
165 | * Send out a message to all nodes that are known in the overlay structure. |
---|
166 | * Depending on the structure of the overlay, this can be very different. |
---|
167 | */ |
---|
168 | void broadcastMessage( |
---|
169 | Message* message, |
---|
170 | const ServiceID& service |
---|
171 | ); |
---|
172 | |
---|
173 | /** |
---|
174 | * Get a list of overlay neighboring nodes. |
---|
175 | */ |
---|
176 | vector<NodeID> getOverlayNeighbors() const; |
---|
177 | |
---|
178 | /** |
---|
179 | * Returns the end-point descriptor of a link. |
---|
180 | * |
---|
181 | * @param link the link id of the requested end-point |
---|
182 | * @return The end-point descriptor of the link's end-point |
---|
183 | */ |
---|
184 | const EndpointDescriptor& getEndpointDescriptor( const LinkID& link = LinkID::UNSPECIFIED ) const; |
---|
185 | |
---|
186 | /** |
---|
187 | * TODO |
---|
188 | */ |
---|
189 | const EndpointDescriptor& getEndpointDescriptor( const NodeID& node ) const; |
---|
190 | |
---|
191 | /** |
---|
192 | * Registers a receiver. |
---|
193 | * |
---|
194 | * @param receiver An implementation of the receiver interface |
---|
195 | */ |
---|
196 | bool bind( ServiceInterface* service, const ServiceID& sid ); |
---|
197 | |
---|
198 | /** |
---|
199 | * Unregister a receiver. |
---|
200 | * |
---|
201 | * @param sid The service id to unregister |
---|
202 | */ |
---|
203 | ServiceInterface* unbind( const ServiceID& sid ); |
---|
204 | |
---|
205 | /** |
---|
206 | * Returns the own nodeID or the NodeID of the specified link |
---|
207 | * |
---|
208 | * @param lid The link identifier |
---|
209 | * @return The NodeID of the link |
---|
210 | */ |
---|
211 | const NodeID& getNodeID( const LinkID& lid = LinkID::UNSPECIFIED ) const ; |
---|
212 | |
---|
213 | protected: |
---|
214 | |
---|
215 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
216 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
217 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
218 | |
---|
219 | void joinSpoVNet( |
---|
220 | const SpoVNetID& id, |
---|
221 | const EndpointDescriptor& bootstrapEp |
---|
222 | ); |
---|
223 | |
---|
224 | void createSpoVNet( |
---|
225 | const SpoVNetID& id, |
---|
226 | const OverlayParameterSet& param = OverlayParameterSet::DEFAULT, |
---|
227 | const SecurityParameterSet& sec = SecurityParameterSet::DEFAULT, |
---|
228 | const QoSParameterSet& qos = QoSParameterSet::DEFAULT |
---|
229 | ); |
---|
230 | |
---|
231 | void leaveSpoVNet( |
---|
232 | ); |
---|
233 | |
---|
234 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
235 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
236 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
237 | |
---|
238 | virtual void onLinkUp( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ); |
---|
239 | |
---|
240 | virtual void onLinkDown( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ); |
---|
241 | |
---|
242 | virtual void onLinkChanged( const LinkID& id, const NetworkLocator* oldlocal, const NetworkLocator* newlocal, const NetworkLocator* oldremote, const NetworkLocator* newremote ); |
---|
243 | |
---|
244 | virtual void onLinkFail( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote ); |
---|
245 | |
---|
246 | virtual void onLinkQoSChanged( const LinkID& id, const NetworkLocator* local, const NetworkLocator* remote, const QoSParameterSet& qos ); |
---|
247 | |
---|
248 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
249 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
250 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
251 | |
---|
252 | virtual bool receiveMessage( const Message* message, const LinkID& link, const NodeID& ); // nodeid is not valid in this case! |
---|
253 | |
---|
254 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
255 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
256 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
257 | |
---|
258 | /** |
---|
259 | * see OverlayStructureEvents.h, called from specific OverlayInterface class |
---|
260 | */ |
---|
261 | virtual void incomingRouteMessage( Message* msg ); |
---|
262 | virtual void onNodeJoin( const NodeID& node ); |
---|
263 | |
---|
264 | // for timer events |
---|
265 | virtual void eventFunction(); |
---|
266 | |
---|
267 | private: |
---|
268 | |
---|
269 | /** |
---|
270 | * The BaseCommunication the BaseOverlay |
---|
271 | * communicates over |
---|
272 | */ |
---|
273 | BaseCommunication& bc; |
---|
274 | |
---|
275 | /** |
---|
276 | * The nodeid of this BaseOverlay instance. |
---|
277 | */ |
---|
278 | NodeID nodeId; |
---|
279 | |
---|
280 | /** |
---|
281 | * The SpoVNetID that we are joined to |
---|
282 | * or that we have created. |
---|
283 | */ |
---|
284 | SpoVNetID spovnetId; |
---|
285 | |
---|
286 | /** |
---|
287 | * A demultiplexer that maps listeners to service ids |
---|
288 | * to deliver upcoming messages to the correct service. |
---|
289 | */ |
---|
290 | Demultiplexer<ServiceInterface*, ServiceID> listenerMux; |
---|
291 | |
---|
292 | /** |
---|
293 | * The abstract overlay interface that implements |
---|
294 | * the overlay specific functionality. |
---|
295 | */ |
---|
296 | OverlayInterface* overlayInterface; |
---|
297 | |
---|
298 | /** |
---|
299 | * The special link to the Initiator of the SpoVNet |
---|
300 | * or LinkID::UNDEFINED if we are the Initiator |
---|
301 | */ |
---|
302 | LinkID initiatorLink; |
---|
303 | |
---|
304 | /** |
---|
305 | * The state of the BaseOverlay |
---|
306 | */ |
---|
307 | typedef enum _BaseOverlayState { |
---|
308 | BaseOverlayStateInvalid = 0, |
---|
309 | BaseOverlayStateInitiator = 1, |
---|
310 | BaseOverlayStateJoinInitiated = 2, |
---|
311 | BaseOverlayStateCompleted = 3, |
---|
312 | } BaseOverlayState; |
---|
313 | |
---|
314 | BaseOverlayState state; |
---|
315 | |
---|
316 | /** |
---|
317 | * The initiator node |
---|
318 | */ |
---|
319 | NodeID spovnetInitiator; |
---|
320 | |
---|
321 | /** |
---|
322 | * OvlVis |
---|
323 | */ |
---|
324 | NodeID min, max; |
---|
325 | NodeID succ, pred; |
---|
326 | void updateOvlVis( const NodeID& node ); |
---|
327 | |
---|
328 | /** |
---|
329 | * Link management |
---|
330 | */ |
---|
331 | class LinkItem { |
---|
332 | public: |
---|
333 | static const LinkItem UNSPECIFIED; |
---|
334 | |
---|
335 | LinkItem( const LinkID& _link, const NodeID& _node, |
---|
336 | const ServiceID& _service, ServiceInterface* _interface ) |
---|
337 | : link( _link ), node( _node ), service( _service ), interface( _interface ), |
---|
338 | autolink( false ), lastuse( time(NULL) ) { |
---|
339 | } |
---|
340 | |
---|
341 | // general information about the link |
---|
342 | |
---|
343 | const LinkID link; |
---|
344 | NodeID node; |
---|
345 | ServiceID service; |
---|
346 | ServiceInterface* interface; |
---|
347 | |
---|
348 | // information needed for auto links |
---|
349 | |
---|
350 | void markused(){ |
---|
351 | lastuse = time(NULL); |
---|
352 | } |
---|
353 | |
---|
354 | bool autolink; |
---|
355 | time_t lastuse; |
---|
356 | }; |
---|
357 | |
---|
358 | typedef map<const LinkID, LinkItem> LinkMapping; |
---|
359 | typedef pair<const LinkID,LinkItem> LinkPair; |
---|
360 | LinkMapping linkMapping; |
---|
361 | |
---|
362 | // nodes with pending joines. TODO: should be cleaned every some seconds |
---|
363 | // add timestamps to each, and check on occasion |
---|
364 | typedef vector<NodeID> JoiningNodes; |
---|
365 | JoiningNodes joiningNodes; |
---|
366 | |
---|
367 | }; |
---|
368 | |
---|
369 | }} // namespace ariba, overlay |
---|
370 | |
---|
371 | #endif /*BASEOVERLAY_H_*/ |
---|