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

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