source: source/ariba/overlay/BaseOverlay.h@ 10767

Last change on this file since 10767 was 10653, checked in by Michael Tänzer, 12 years ago

Merge the ASIO branch back into trunk

File size: 15.2 KB
Line 
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
68namespace ariba {
69 class NodeListener;
70 class CommunicationListener;
71 class SideportListener;
72}
73
74using std::vector;
75using std::list;
76using std::map;
77using std::make_pair;
78using std::pair;
79using std::find;
80using std::deque;
81
82// ariba interface
83using ariba::NodeListener;
84using ariba::SideportListener;
85using ariba::CommunicationListener;
86
87// overlay
88using ariba::overlay::OverlayBootstrap;
89
90// communication
91using ariba::communication::EndpointDescriptor;
92using ariba::communication::BaseCommunication;
93using ariba::communication::CommunicationEvents;
94
95// utilities
96using ariba::utility::NodeID;
97using ariba::utility::SpoVNetID;
98using ariba::utility::LinkID;
99using ariba::utility::Identifier;
100using ariba::utility::ServiceID;
101using ariba::utility::QoSParameterSet;
102using ariba::utility::SecurityParameterSet;
103using ariba::utility::Demultiplexer;
104using ariba::utility::MessageReceiver;
105using ariba::utility::MessageSender;
106using ariba::utility::seqnum_t;
107using ariba::utility::Timer;
108
109namespace ariba {
110namespace overlay {
111
112using namespace ariba::addressing;
113
114class LinkDescriptor;
115class OverlayMsg;
116class DHT;
117
118class BaseOverlay: public MessageReceiver,
119 public CommunicationEvents,
120 public OverlayStructureEvents,
121 protected Timer {
122
123 friend class OneHop;
124 friend class Chord;
125 friend class ariba::SideportListener;
126
127 use_logging_h( BaseOverlay );
128
129public:
130
131 /**
132 * Constructs an empty non-functional base overlay instance
133 */
134 BaseOverlay();
135
136 /**
137 * Destructs a base overlay instance
138 */
139 virtual ~BaseOverlay();
140
141 /**
142 * Starts the Base Overlay instance
143 */
144 void start(BaseCommunication& _basecomm, const NodeID& _nodeid);
145
146 /**
147 * Stops the Base Overlay instance
148 */
149 void stop();
150
151 /**
152 * Is the BaseOverlay instance started up yet
153 */
154 bool isStarted();
155
156 /// Tries to establish a direct or overlay link
157 const LinkID establishLink( const EndpointDescriptor& ep,
158 const NodeID& node, const ServiceID& service );
159
160 /**
161 * Starts a link establishment procedure to the specfied node
162 * for the service with id service
163 *
164 * @param node Destination node id
165 * @param service Service to connect to
166 * @param linkid Link identifier to be used with this link
167 */
168 const LinkID establishLink( const NodeID& remote,
169 const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID );
170
171 /**
172 * Starts a link establishment procedure to the specified
173 * endpoint and to the specified service
174 */
175 const LinkID establishDirectLink( const EndpointDescriptor& endpoint,
176 const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID );
177
178 /// drops a link
179 void dropLink( const LinkID& link );
180
181 /// sends a message over an existing link
182 seqnum_t sendMessage(const Message* message, const LinkID& link );
183
184 /// sends a message to a node and a specific service
185 seqnum_t sendMessage(const Message* message, const NodeID& remote,
186 const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID);
187
188 /**
189 * send a message to the closest directly known node to an address
190 *
191 * @return NodeID of the (closest) destination node;
192 */
193 NodeID sendMessageCloserToNodeID(const Message* message, const NodeID& address,
194 const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID);
195
196 /**
197 * Send out a message to all nodes that are known in the overlay structure.
198 * Depending on the structure of the overlay, this can be very different.
199 */
200 void broadcastMessage(Message* message, const ServiceID& service);
201
202 /**
203 * Returns the end-point descriptor of a link.
204 *
205 * @param link the link id of the requested end-point
206 * @return The end-point descriptor of the link's end-point
207 */
208 const EndpointDescriptor& getEndpointDescriptor(
209 const LinkID& link = LinkID::UNSPECIFIED) const;
210
211 /**
212 * Get a list of overlay neighbors.
213 *
214 * @return A list of overlay neighbors.
215 */
216 vector<NodeID> getOverlayNeighbors(bool deep = true) const;
217
218 /**
219 * Returns a end-endpoint descriptor of a overlay neighbor.
220 * If the node is not known -- an unspecified endpoint descriptor is
221 * returned.
222 *
223 * @param node The node identifer of a overlay neighbor.
224 * @return The end-point descriptor of the node or unspecified.
225 */
226 const EndpointDescriptor& getEndpointDescriptor(const NodeID& node) const;
227
228 // TODO: Doc
229 bool bind(CommunicationListener* listener, const ServiceID& sid);
230
231 // TODO: Doc
232 bool unbind(CommunicationListener* listener, const ServiceID& sid);
233
234 // TODO: Doc
235 bool bind(NodeListener* listener);
236
237 // TODO: Doc
238 bool unbind(NodeListener* listener);
239
240 // TODO: Doc
241 bool registerSidePort(SideportListener* _sideport);
242
243 // TODO: Doc
244 bool unregisterSidePort(SideportListener* _sideport);
245
246 /**
247 * Returns the own nodeID or the NodeID of the specified link
248 *
249 * @param lid The link identifier
250 * @return The NodeID of the link
251 */
252 const NodeID& getNodeID(const LinkID& lid = LinkID::UNSPECIFIED) const;
253
254 /**
255 * Return all Links for the specified remote nodeid, or all links when
256 * the node id given is set to unspecified
257 *
258 * @param nid The node id to request links for, or unspecified for all links
259 * @return a vector that contains all the link ids requested
260 */
261 vector<LinkID> getLinkIDs(const NodeID& nid = NodeID::UNSPECIFIED) const;
262
263 /**
264 * Join a existing sponaneous virtual network (spovnet).
265 *
266 * @param id A spovnet identifier
267 * @param boot A bootstrap node
268 */
269 void joinSpoVNet(const SpoVNetID& id, const EndpointDescriptor& boot = EndpointDescriptor::UNSPECIFIED());
270
271 /**
272 * Initiates a new spontaneous virtual network.
273 * This makes this BaseOverlay to the SpoVNet-Initiator.
274 *
275 * @param id The spovnet identifier
276 */
277 void createSpoVNet(const SpoVNetID& id, const OverlayParameterSet& param =
278 OverlayParameterSet::DEFAULT, const SecurityParameterSet& sec =
279 SecurityParameterSet::DEFAULT, const QoSParameterSet& qos =
280 QoSParameterSet::DEFAULT);
281
282 /**
283 * Start the bootstrap modules
284 */
285 void startBootstrapModules(vector<pair<BootstrapManager::BootstrapType,string> > modules);
286
287 /**
288 * Stop the bootstrap modules
289 */
290 void stopBootstrapModules();
291
292 /**
293 * Let the node leave the SpoVNet.
294 */
295 void leaveSpoVNet();
296
297protected:
298 /**
299 * @see ariba::communication::CommunicationEvents.h
300 */
301 virtual void onLinkUp(const LinkID& id, const address_v* local,
302 const address_v* remote);
303
304 /**
305 * @see ariba::communication::CommunicationEvents.h
306 */
307 virtual void onLinkDown(const LinkID& id, const address_v* local,
308 const address_v* remote);
309
310 /**
311 * @see ariba::communication::CommunicationEvents.h
312 */
313 virtual void onLinkChanged(const LinkID& id,
314 const address_v* oldlocal, const address_v* newlocal,
315 const address_v* oldremote, const address_v* newremote);
316
317 /**
318 * @see ariba::communication::CommunicationEvents.h
319 */
320 virtual void onLinkFail(const LinkID& id, const address_v* local,
321 const address_v* remote);
322
323 /**
324 * @see ariba::communication::CommunicationEvents.h
325 */
326 virtual void onLinkQoSChanged(const LinkID& id,
327 const address_v* local, const address_v* remote,
328 const QoSParameterSet& qos);
329
330 /**
331 * @see ariba::communication::CommunicationEvents.h
332 */
333 virtual bool onLinkRequest(const LinkID& id, const address_v* local,
334 const address_v* remote);
335
336 /**
337 * Processes a received message from BaseCommunication
338 *
339 * In case of a message routed by the overlay the source identifies
340 * the node the message came from!
341 */
342 virtual bool receiveMessage( const Message* message, const LinkID& link,
343 const NodeID& );
344
345 /**
346 * This method is called, when a new node joined the network
347 *
348 * @see OverlayStructureEvents.h
349 */
350 virtual void onNodeJoin(const NodeID& node);
351
352 /**
353 * Timer Event method
354 */
355 virtual void eventFunction();
356
357 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358
359 std::string getLinkHTMLInfo();
360
361private:
362 /// is the base overlay started yet
363 bool started;
364
365 /// The state of the BaseOverlay
366 typedef enum _BaseOverlayState {
367 BaseOverlayStateInvalid = 0,
368 BaseOverlayStateCompleted = 1,
369 } BaseOverlayState;
370
371 BaseOverlayState state; ///< Current Base-Overlay state
372 BaseCommunication* bc; ///< reference to the base communication
373 NodeID nodeId; ///< the node id of this node
374 SpoVNetID spovnetId; ///< the spovnet id of the currently joined overlay
375 vector<LinkID> bootstrapLinks; ///< the link id of the link to the initiator node
376 NodeID spovnetInitiator; ///< The initiator node
377
378 /// the service id communication listeners
379 Demultiplexer<CommunicationListener*, ServiceID> communicationListeners;
380 CommunicationListener* getListener( const ServiceID& id );
381
382 /// the node listeners
383 typedef vector<NodeListener*> NodeListenerVector;
384 NodeListenerVector nodeListeners;
385
386 /// the sideport listener
387 SideportListener* sideport;
388
389 /// the used overlay structure
390 OverlayInterface* overlayInterface;
391
392 /// Bootstrapper for our spovnet
393 OverlayBootstrap overlayBootstrap;
394
395 // message handlers --------------------------------------------------------
396
397 /// demultiplexes a incoming message with link descriptor
398 bool handleMessage( const Message* message, LinkDescriptor* ld,
399 const LinkID bcLink = LinkID::UNSPECIFIED );
400
401 // handle data and signalling messages
402 bool handleData( OverlayMsg* msg, LinkDescriptor* ld );
403 bool handleSignaling( OverlayMsg* msg, LinkDescriptor* ld );
404
405 // handle join request / reply messages
406 bool handleJoinRequest( OverlayMsg* msg, const LinkID& bcLink );
407 bool handleJoinReply( OverlayMsg* msg, const LinkID& bcLink );
408
409 // handle link messages
410 bool handleLinkRequest( OverlayMsg* msg, LinkDescriptor* ld );
411 bool handleLinkReply( OverlayMsg* msg, LinkDescriptor* ld );
412 bool handleLinkUpdate( OverlayMsg* msg, LinkDescriptor* ld );
413 bool handleLinkDirect( OverlayMsg* msg, LinkDescriptor* ld );
414 bool handleLinkAlive( OverlayMsg* msg, LinkDescriptor* ld );
415
416
417 // link state handling -----------------------------------------------------
418
419 /// link state information counter
420 int counter;
421
422 /// The link mapping of the node
423 vector<LinkDescriptor*> links;
424
425 /// erases a link descriptor
426 void eraseDescriptor(const LinkID& link, bool communication = false);
427
428 /// returns a link descriptor for the given id
429 LinkDescriptor* getDescriptor(const LinkID& link,
430 bool communication = false);
431
432 /// returns a link descriptor for the given id
433 const LinkDescriptor* getDescriptor(const LinkID& link,
434 bool communication = false) const;
435
436 /// returns a auto-link descriptor for the given node and service id
437 LinkDescriptor* getAutoDescriptor(const NodeID& node, const ServiceID& service);
438
439 /// adds a new link descriptor or uses an existing one
440 LinkDescriptor* addDescriptor(const LinkID& link = LinkID::UNSPECIFIED);
441
442 /// stabilizes link information
443 void stabilizeLinks();
444
445 /// print the currently known links
446 void showLinks();
447
448 /// compares two arbitrary links to the same node
449 int compare( const LinkID& lhs, const LinkID& rhs );
450
451 // relay route management --------------------------------------------------
452
453 /// relay route definitions
454 class relay_route {
455 public:
456 NodeID node;
457 LinkID link;
458 uint8_t hops;
459 time_t used;
460 };
461 vector<relay_route> relay_routes;
462
463 /// stabilize relay information
464 void stabilizeRelays();
465
466 /// refreshes relay information
467 void refreshRelayInformation( const OverlayMsg* message, LinkDescriptor* ld );
468
469 /// returns a known relay link
470 LinkDescriptor* getRelayLinkTo( const NodeID& remote );
471
472 /// removes relay link information
473 void removeRelayLink( const LinkID& link );
474
475 /// removes relay node information
476 void removeRelayNode( const NodeID& link );
477
478 // internal message delivery -----------------------------------------------
479
480 /// routes a message to its destination node
481 void route( OverlayMsg* message );
482
483 /// sends a raw message to another node, delivers it to the base overlay class
484 seqnum_t send( OverlayMsg* message, const NodeID& destination );
485
486 /// send a raw message using a link descriptor, delivers it to the base overlay class
487 seqnum_t send( OverlayMsg* message, LinkDescriptor* ld,
488 bool ignore_down = false );
489
490 /// send a message using a node id using overlay routing
491 /// sets necessary fields in the overlay message!
492 seqnum_t send_node( OverlayMsg* message, const NodeID& remote,
493 const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID);
494
495 /// send a message using a node id using overlay routing using a link
496 /// sets necessary fields in the overlay message!
497 seqnum_t send_link( OverlayMsg* message, const LinkID& link,
498 bool ignore_down = false );
499
500 // misc --------------------------------------------------------------------
501
502 std::string debugInformation();
503
504 /**
505 * nodes with pending joines. TODO: should be cleaned every
506 * some seconds, add timestamps to each, and check on occasion
507 */
508 typedef vector<NodeID> JoiningNodes;
509 JoiningNodes joiningNodes;
510
511 void updateVisual();
512};
513
514}} // namespace ariba, overlay
515
516#endif /*BASEOVERLAY_H_*/
Note: See TracBrowser for help on using the repository browser.