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