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 | #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/overlay/modules/OverlayInterface.h" |
---|
62 | #include "ariba/overlay/modules/OverlayFactory.h" |
---|
63 | #include "ariba/overlay/modules/OverlayStructureEvents.h" |
---|
64 | |
---|
65 | // forward declarations |
---|
66 | namespace ariba { |
---|
67 | class NodeListener; |
---|
68 | class CommunicationListener; |
---|
69 | class SideportListener; |
---|
70 | namespace utility { |
---|
71 | class OvlVis; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | using std::vector; |
---|
76 | using std::list; |
---|
77 | using std::cout; |
---|
78 | using std::map; |
---|
79 | using std::make_pair; |
---|
80 | using std::pair; |
---|
81 | using std::find; |
---|
82 | |
---|
83 | using ariba::NodeListener; |
---|
84 | using ariba::CommunicationListener; |
---|
85 | |
---|
86 | // communication |
---|
87 | using ariba::communication::EndpointDescriptor; |
---|
88 | using ariba::communication::BaseCommunication; |
---|
89 | using ariba::communication::CommunicationEvents; |
---|
90 | |
---|
91 | // utilities |
---|
92 | using ariba::utility::NodeID; |
---|
93 | using ariba::utility::SpoVNetID; |
---|
94 | using ariba::utility::LinkID; |
---|
95 | using ariba::utility::Identifier; |
---|
96 | using ariba::utility::ServiceID; |
---|
97 | using ariba::utility::QoSParameterSet; |
---|
98 | using ariba::utility::SecurityParameterSet; |
---|
99 | using ariba::utility::Demultiplexer; |
---|
100 | using ariba::utility::MessageReceiver; |
---|
101 | using ariba::utility::MessageSender; |
---|
102 | using ariba::utility::seqnum_t; |
---|
103 | using ariba::utility::Timer; |
---|
104 | using ariba::utility::OvlVis; |
---|
105 | |
---|
106 | #define ovl OvlVis::instance() |
---|
107 | #define ovlId OvlVis::NETWORK_ID_BASE_OVERLAY |
---|
108 | |
---|
109 | namespace ariba { |
---|
110 | namespace overlay { |
---|
111 | |
---|
112 | class BaseOverlay: public MessageReceiver, public CommunicationEvents, |
---|
113 | public OverlayStructureEvents, protected Timer { |
---|
114 | |
---|
115 | private: |
---|
116 | friend class OneHop; |
---|
117 | friend class Chord; |
---|
118 | |
---|
119 | use_logging_h( BaseOverlay ); |
---|
120 | |
---|
121 | public: |
---|
122 | |
---|
123 | /** |
---|
124 | * Constructs an empty non-functional base overlay instance |
---|
125 | */ |
---|
126 | BaseOverlay(); |
---|
127 | |
---|
128 | /** |
---|
129 | * Destructs a base overlay instance |
---|
130 | */ |
---|
131 | virtual ~BaseOverlay(); |
---|
132 | |
---|
133 | /** |
---|
134 | * Starts the Base Overlay instance |
---|
135 | */ |
---|
136 | void start(BaseCommunication& _basecomm, const NodeID& _nodeid); |
---|
137 | |
---|
138 | /** |
---|
139 | * Stops the Base Overlay instance |
---|
140 | */ |
---|
141 | void stop(); |
---|
142 | |
---|
143 | /** |
---|
144 | * Starts a link establishment procedure to the specfied node |
---|
145 | * for the service with id service |
---|
146 | * |
---|
147 | * @param node Destination node id |
---|
148 | * @param service Service to connect to |
---|
149 | * @param linkid Link identifier to be used with this link |
---|
150 | */ |
---|
151 | const LinkID establishLink(const NodeID& node, const ServiceID& service, |
---|
152 | const LinkID& linkid = LinkID::UNSPECIFIED); |
---|
153 | |
---|
154 | /** |
---|
155 | * Starts a link establishment procedure to the specified |
---|
156 | * |
---|
157 | * endpoint and to the specified service |
---|
158 | */ |
---|
159 | const LinkID establishLink(const EndpointDescriptor& ep, |
---|
160 | const ServiceID& service, const LinkID& linkid = |
---|
161 | LinkID::UNSPECIFIED); |
---|
162 | |
---|
163 | /// drops a link |
---|
164 | void dropLink(const LinkID& link); |
---|
165 | |
---|
166 | /// sends a message over an existing link |
---|
167 | seqnum_t sendMessage(const Message* message, const LinkID& link); |
---|
168 | |
---|
169 | /// sends a message to a node and a specific service |
---|
170 | seqnum_t sendMessage(const Message* message, const NodeID& node, |
---|
171 | const ServiceID& service); |
---|
172 | |
---|
173 | /** |
---|
174 | * Send out a message to all nodes that are known in the overlay structure. |
---|
175 | * Depending on the structure of the overlay, this can be very different. |
---|
176 | */ |
---|
177 | void broadcastMessage(Message* message, const ServiceID& service); |
---|
178 | |
---|
179 | /** |
---|
180 | * Returns the end-point descriptor of a link. |
---|
181 | * |
---|
182 | * @param link the link id of the requested end-point |
---|
183 | * @return The end-point descriptor of the link's end-point |
---|
184 | */ |
---|
185 | const EndpointDescriptor& getEndpointDescriptor(const LinkID& link = |
---|
186 | LinkID::UNSPECIFIED) const; |
---|
187 | |
---|
188 | /** |
---|
189 | * Get a list of overlay neighbors. |
---|
190 | * |
---|
191 | * @return A list of overlay neighbors. |
---|
192 | */ |
---|
193 | vector<NodeID> getOverlayNeighbors() const; |
---|
194 | |
---|
195 | /** |
---|
196 | * Returns a end-endpoint descriptor of a overlay neighbor. |
---|
197 | * If the node is not known -- an unspecified endpoint descriptor is |
---|
198 | * returned. |
---|
199 | * |
---|
200 | * @param node The node identifer of a overlay neighbor. |
---|
201 | * @return The end-point descriptor of the node or unspecified. |
---|
202 | */ |
---|
203 | const EndpointDescriptor& getEndpointDescriptor(const NodeID& node) const; |
---|
204 | |
---|
205 | // TODO: Doc |
---|
206 | bool bind(CommunicationListener* listener, const ServiceID& sid); |
---|
207 | |
---|
208 | // TODO: Doc |
---|
209 | bool unbind(CommunicationListener* listener, const ServiceID& sid); |
---|
210 | |
---|
211 | // TODO: Doc |
---|
212 | bool bind(NodeListener* listener); |
---|
213 | |
---|
214 | // TODO: Doc |
---|
215 | bool unbind(NodeListener* listener); |
---|
216 | |
---|
217 | // TODO: Doc |
---|
218 | bool registerSidePort(SideportListener* _sideport); |
---|
219 | |
---|
220 | // TODO: Doc |
---|
221 | bool unregisterSidePort(SideportListener* _sideport); |
---|
222 | |
---|
223 | /** |
---|
224 | * Returns the own nodeID or the NodeID of the specified link |
---|
225 | * |
---|
226 | * @param lid The link identifier |
---|
227 | * @return The NodeID of the link |
---|
228 | */ |
---|
229 | const NodeID& getNodeID(const LinkID& lid = LinkID::UNSPECIFIED) const; |
---|
230 | |
---|
231 | /** |
---|
232 | * Return all Links for the specified remote nodeid, or all links when |
---|
233 | * the node id given is set to unspecified |
---|
234 | * |
---|
235 | * @param nid The node id to request links for, or unspecified for all links |
---|
236 | * @return a vector that contains all the link ids requested |
---|
237 | */ |
---|
238 | vector<LinkID> getLinkIDs(const NodeID& nid = NodeID::UNSPECIFIED) const; |
---|
239 | |
---|
240 | /** |
---|
241 | * Join a existing sponaneous virtual network (spovnet). |
---|
242 | * |
---|
243 | * @param id A spovnet identifier |
---|
244 | * @param boot A bootstrap node |
---|
245 | */ |
---|
246 | void joinSpoVNet(const SpoVNetID& id, const EndpointDescriptor& boot ); |
---|
247 | |
---|
248 | /** |
---|
249 | * Initiates a new spontaneous virtual network. |
---|
250 | * This makes this BaseOverlay to the SpoVNet-Initiator. |
---|
251 | * |
---|
252 | * @param id The spovnet identifier |
---|
253 | */ |
---|
254 | void createSpoVNet(const SpoVNetID& id, const OverlayParameterSet& param = |
---|
255 | OverlayParameterSet::DEFAULT, const SecurityParameterSet& sec = |
---|
256 | SecurityParameterSet::DEFAULT, const QoSParameterSet& qos = |
---|
257 | QoSParameterSet::DEFAULT); |
---|
258 | |
---|
259 | /** |
---|
260 | * Let the node leave the SpoVNet. |
---|
261 | */ |
---|
262 | void leaveSpoVNet(); |
---|
263 | |
---|
264 | protected: |
---|
265 | |
---|
266 | /** |
---|
267 | * @see ariba::communication::CommunicationEvents.h |
---|
268 | */ |
---|
269 | virtual void onLinkUp(const LinkID& id, const NetworkLocator* local, |
---|
270 | const NetworkLocator* remote); |
---|
271 | |
---|
272 | /** |
---|
273 | * @see ariba::communication::CommunicationEvents.h |
---|
274 | */ |
---|
275 | virtual void onLinkDown(const LinkID& id, const NetworkLocator* local, |
---|
276 | const NetworkLocator* remote); |
---|
277 | |
---|
278 | /** |
---|
279 | * @see ariba::communication::CommunicationEvents.h |
---|
280 | */ |
---|
281 | virtual void onLinkChanged(const LinkID& id, |
---|
282 | const NetworkLocator* oldlocal, const NetworkLocator* newlocal, |
---|
283 | const NetworkLocator* oldremote, const NetworkLocator* newremote); |
---|
284 | |
---|
285 | /** |
---|
286 | * @see ariba::communication::CommunicationEvents.h |
---|
287 | */ |
---|
288 | virtual void onLinkFail(const LinkID& id, const NetworkLocator* local, |
---|
289 | const NetworkLocator* remote); |
---|
290 | |
---|
291 | /** |
---|
292 | * @see ariba::communication::CommunicationEvents.h |
---|
293 | */ |
---|
294 | virtual void onLinkQoSChanged(const LinkID& id, |
---|
295 | const NetworkLocator* local, const NetworkLocator* remote, |
---|
296 | const QoSParameterSet& qos); |
---|
297 | |
---|
298 | /** |
---|
299 | * @see ariba::communication::CommunicationEvents.h |
---|
300 | */ |
---|
301 | virtual bool onLinkRequest(const LinkID& id, const NetworkLocator* local, |
---|
302 | const NetworkLocator* remote); |
---|
303 | |
---|
304 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
305 | |
---|
306 | /** |
---|
307 | * Processes a received message. |
---|
308 | * |
---|
309 | * Beware: nodeid is not valid in this case! (since this class implements |
---|
310 | * nodeid's in the first place *g*) |
---|
311 | */ |
---|
312 | virtual bool receiveMessage( |
---|
313 | const Message* message, const LinkID& link, const NodeID&); |
---|
314 | |
---|
315 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
316 | |
---|
317 | /** |
---|
318 | * This method is called, when a routed message arrives from the |
---|
319 | * overlay. |
---|
320 | * |
---|
321 | * @see OverlayStructureEvents.h |
---|
322 | */ |
---|
323 | virtual void incomingRouteMessage(Message* msg); |
---|
324 | |
---|
325 | /** |
---|
326 | * This method is called, when a new node joined the network |
---|
327 | * |
---|
328 | * @see OverlayStructureEvents.h |
---|
329 | */ |
---|
330 | virtual void onNodeJoin(const NodeID& node); |
---|
331 | |
---|
332 | /** |
---|
333 | * Timer Event method |
---|
334 | */ |
---|
335 | virtual void eventFunction(); |
---|
336 | |
---|
337 | private: |
---|
338 | /** |
---|
339 | * The BaseCommunication the BaseOverlay |
---|
340 | * communicates over |
---|
341 | */ |
---|
342 | BaseCommunication* bc; |
---|
343 | |
---|
344 | /** |
---|
345 | * The nodeid of this BaseOverlay instance. |
---|
346 | */ |
---|
347 | NodeID nodeId; |
---|
348 | |
---|
349 | /** |
---|
350 | * The SpoVNetID that we are joined to |
---|
351 | * or that we have created. |
---|
352 | */ |
---|
353 | SpoVNetID spovnetId; |
---|
354 | |
---|
355 | /** |
---|
356 | * TODO |
---|
357 | */ |
---|
358 | Demultiplexer<CommunicationListener*, ServiceID> communicationListeners; |
---|
359 | |
---|
360 | /** |
---|
361 | * TODO |
---|
362 | */ |
---|
363 | typedef vector<NodeListener*> NodeListenerVector; |
---|
364 | |
---|
365 | /** |
---|
366 | * TODO |
---|
367 | */ |
---|
368 | NodeListenerVector nodeListeners; |
---|
369 | |
---|
370 | /** |
---|
371 | * TODO |
---|
372 | */ |
---|
373 | SideportListener* sideport; |
---|
374 | |
---|
375 | /** |
---|
376 | * The abstract overlay interface that implements |
---|
377 | * the overlay specific functionality. |
---|
378 | */ |
---|
379 | OverlayInterface* overlayInterface; |
---|
380 | |
---|
381 | /** |
---|
382 | * The special link to the Initiator of the SpoVNet |
---|
383 | * or LinkID::UNDEFINED if we are the Initiator |
---|
384 | */ |
---|
385 | LinkID initiatorLink; |
---|
386 | |
---|
387 | /** |
---|
388 | * The state of the BaseOverlay |
---|
389 | */ |
---|
390 | typedef enum _BaseOverlayState { |
---|
391 | BaseOverlayStateInvalid = 0, |
---|
392 | BaseOverlayStateInitiator = 1, |
---|
393 | BaseOverlayStateJoinInitiated = 2, |
---|
394 | BaseOverlayStateCompleted = 3, |
---|
395 | } BaseOverlayState; |
---|
396 | |
---|
397 | /** |
---|
398 | * TODO |
---|
399 | */ |
---|
400 | BaseOverlayState state; |
---|
401 | |
---|
402 | /** |
---|
403 | * The initiator node |
---|
404 | */ |
---|
405 | NodeID spovnetInitiator; |
---|
406 | |
---|
407 | /** |
---|
408 | * OvlVis |
---|
409 | */ |
---|
410 | NodeID min, max; |
---|
411 | NodeID succ, pred; |
---|
412 | void updateOvlVis(const NodeID& node); |
---|
413 | |
---|
414 | /** |
---|
415 | * Link management |
---|
416 | */ |
---|
417 | class LinkItem { |
---|
418 | public: |
---|
419 | static const LinkItem UNSPECIFIED; |
---|
420 | |
---|
421 | LinkItem() : |
---|
422 | link(LinkID::UNSPECIFIED), node(NodeID::UNSPECIFIED), service( |
---|
423 | ServiceID::UNSPECIFIED), interface( |
---|
424 | &CommunicationListener::DEFAULT), autolink(false), lastuse( |
---|
425 | 0) { |
---|
426 | } |
---|
427 | |
---|
428 | LinkItem(const LinkID& _link, const NodeID& _node, |
---|
429 | const ServiceID& _service, CommunicationListener* _interface) : |
---|
430 | link(_link), node(_node), service(_service), interface(_interface), |
---|
431 | autolink(false), lastuse(time(NULL)) { |
---|
432 | |
---|
433 | assert( _interface != NULL ); |
---|
434 | } |
---|
435 | |
---|
436 | // general information about the link |
---|
437 | |
---|
438 | const LinkID link; |
---|
439 | NodeID node; |
---|
440 | ServiceID service; |
---|
441 | CommunicationListener* interface; |
---|
442 | |
---|
443 | // information needed for auto links |
---|
444 | void markused() { |
---|
445 | lastuse = time(NULL); |
---|
446 | } |
---|
447 | |
---|
448 | bool autolink; |
---|
449 | time_t lastuse; |
---|
450 | }; |
---|
451 | |
---|
452 | typedef map<const LinkID, LinkItem> LinkMapping; |
---|
453 | typedef pair<const LinkID, LinkItem> LinkPair; |
---|
454 | LinkMapping linkMapping; |
---|
455 | |
---|
456 | // map of a link request map a nonce to a LinkID |
---|
457 | typedef map<const uint32_t, LinkID> PendingLinkMap; |
---|
458 | PendingLinkMap pendingLinks; |
---|
459 | |
---|
460 | /** |
---|
461 | * nodes with pending joines. TODO: should be cleaned every |
---|
462 | * some seconds, add timestamps to each, and check on occasion |
---|
463 | */ |
---|
464 | typedef vector<NodeID> JoiningNodes; |
---|
465 | JoiningNodes joiningNodes; |
---|
466 | |
---|
467 | }; |
---|
468 | |
---|
469 | } |
---|
470 | } // namespace ariba, overlay |
---|
471 | |
---|
472 | #endif /*BASEOVERLAY_H_*/ |
---|