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

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