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

Last change on this file since 5757 was 5743, checked in by mies, 15 years ago
File size: 13.5 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;
123
124class 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
135public:
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 /// Tries to establish a direct or overlay link
163 const LinkID establishLink(const EndpointDescriptor& ep,
164 const NodeID& node, const ServiceID& service,
165 const NodeID& remoteRelay = NodeID::UNSPECIFIED,
166 const LinkID& linkid = LinkID::UNSPECIFIED);
167
168 /**
169 * Starts a link establishment procedure to the specfied node
170 * for the service with id service
171 *
172 * @param node Destination node id
173 * @param service Service to connect to
174 * @param linkid Link identifier to be used with this link
175 */
176 const LinkID establishLink(const NodeID& node, const ServiceID& service,
177 const NodeID& remoteRelay = NodeID::UNSPECIFIED,
178 const LinkID& linkid = LinkID::UNSPECIFIED);
179
180 /**
181 * Starts a link establishment procedure to the specified
182 * endpoint and to the specified service
183 */
184 const LinkID establishDirectLink(const EndpointDescriptor& ep,
185 const ServiceID& service, const LinkID& linkid = LinkID::UNSPECIFIED);
186
187 /// drops a link
188 void dropLink(const LinkID& link);
189
190 /// sends a message over an existing link
191 seqnum_t sendMessage(const Message* message, const LinkID& link);
192
193 /// sends a message to a node and a specific service
194 seqnum_t sendMessage(const Message* message, const NodeID& node,
195 const ServiceID& service);
196
197 /**
198 * Send out a message to all nodes that are known in the overlay structure.
199 * Depending on the structure of the overlay, this can be very different.
200 */
201 void broadcastMessage(Message* message, const ServiceID& service);
202
203 /**
204 * Returns the end-point descriptor of a link.
205 *
206 * @param link the link id of the requested end-point
207 * @return The end-point descriptor of the link's end-point
208 */
209 const EndpointDescriptor& getEndpointDescriptor(const LinkID& link =
210 LinkID::UNSPECIFIED) const;
211
212 /**
213 * Get a list of overlay neighbors.
214 *
215 * @return A list of overlay neighbors.
216 */
217 vector<NodeID> getOverlayNeighbors(bool deep = true) const;
218
219 /**
220 * Returns a end-endpoint descriptor of a overlay neighbor.
221 * If the node is not known -- an unspecified endpoint descriptor is
222 * returned.
223 *
224 * @param node The node identifer of a overlay neighbor.
225 * @return The end-point descriptor of the node or unspecified.
226 */
227 const EndpointDescriptor& getEndpointDescriptor(const NodeID& node) const;
228
229 // TODO: Doc
230 bool bind(CommunicationListener* listener, const ServiceID& sid);
231
232 // TODO: Doc
233 bool unbind(CommunicationListener* listener, const ServiceID& sid);
234
235 // TODO: Doc
236 bool bind(NodeListener* listener);
237
238 // TODO: Doc
239 bool unbind(NodeListener* listener);
240
241 // TODO: Doc
242 bool registerSidePort(SideportListener* _sideport);
243
244 // TODO: Doc
245 bool unregisterSidePort(SideportListener* _sideport);
246
247 /**
248 * Returns the own nodeID or the NodeID of the specified link
249 *
250 * @param lid The link identifier
251 * @return The NodeID of the link
252 */
253 const NodeID& getNodeID(const LinkID& lid = LinkID::UNSPECIFIED) const;
254
255 /**
256 * Return all Links for the specified remote nodeid, or all links when
257 * the node id given is set to unspecified
258 *
259 * @param nid The node id to request links for, or unspecified for all links
260 * @return a vector that contains all the link ids requested
261 */
262 vector<LinkID> getLinkIDs(const NodeID& nid = NodeID::UNSPECIFIED) const;
263
264 /**
265 * Join a existing sponaneous virtual network (spovnet).
266 *
267 * @param id A spovnet identifier
268 * @param boot A bootstrap node
269 */
270 void joinSpoVNet(const SpoVNetID& id, const EndpointDescriptor& boot = EndpointDescriptor::UNSPECIFIED());
271
272 /**
273 * Initiates a new spontaneous virtual network.
274 * This makes this BaseOverlay to the SpoVNet-Initiator.
275 *
276 * @param id The spovnet identifier
277 */
278 void createSpoVNet(const SpoVNetID& id, const OverlayParameterSet& param =
279 OverlayParameterSet::DEFAULT, const SecurityParameterSet& sec =
280 SecurityParameterSet::DEFAULT, const QoSParameterSet& qos =
281 QoSParameterSet::DEFAULT);
282
283 /**
284 * Let the node leave the SpoVNet.
285 */
286 void leaveSpoVNet();
287
288protected:
289
290 /**
291 * @see ariba::communication::CommunicationEvents.h
292 */
293 virtual void onLinkUp(const LinkID& id, const address_v* local,
294 const address_v* remote);
295
296 /**
297 * @see ariba::communication::CommunicationEvents.h
298 */
299 virtual void onLinkDown(const LinkID& id, const address_v* local,
300 const address_v* remote);
301
302 /**
303 * @see ariba::communication::CommunicationEvents.h
304 */
305 virtual void onLinkChanged(const LinkID& id,
306 const address_v* oldlocal, const address_v* newlocal,
307 const address_v* oldremote, const address_v* newremote);
308
309 /**
310 * @see ariba::communication::CommunicationEvents.h
311 */
312 virtual void onLinkFail(const LinkID& id, const address_v* local,
313 const address_v* remote);
314
315 /**
316 * @see ariba::communication::CommunicationEvents.h
317 */
318 virtual void onLinkQoSChanged(const LinkID& id,
319 const address_v* local, const address_v* remote,
320 const QoSParameterSet& qos);
321
322 /**
323 * @see ariba::communication::CommunicationEvents.h
324 */
325 virtual bool onLinkRequest(const LinkID& id, const address_v* local,
326 const address_v* remote);
327
328 /**
329 * Processes a received message from BaseCommunication
330 *
331 * In case of a message routed by the overlay the source identifies
332 * the node the message came from!
333 */
334 virtual bool receiveMessage(const Message* message, const LinkID& link,
335 const NodeID& source = NodeID::UNSPECIFIED);
336
337 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338
339 /// handles an incoming message with link descriptor
340 bool handleMessage(const Message* message,
341 const LinkID& boLink, const LinkID& bcLink, const NodeID& remoteNode );
342
343 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344 /**
345 * This method is called, when a routed message arrives from the
346 * overlay.
347 *
348 * @see OverlayStructureEvents.h
349 */
350 virtual void incomingRouteMessage(Message* msg,
351 const LinkID& link = LinkID::UNSPECIFIED,
352 const NodeID& source = NodeID::UNSPECIFIED);
353
354 /**
355 * This method is called, when a new node joined the network
356 *
357 * @see OverlayStructureEvents.h
358 */
359 virtual void onNodeJoin(const NodeID& node);
360
361 /**
362 * Timer Event method
363 */
364 virtual void eventFunction();
365
366private:
367
368 /// The state of the BaseOverlay
369 typedef enum _BaseOverlayState {
370 BaseOverlayStateInvalid = 0,
371 BaseOverlayStateCompleted = 1,
372 } BaseOverlayState;
373
374 BaseOverlayState state; ///< Current Base-Overlay state
375 BaseCommunication* bc; ///< reference to the base communication
376 NodeID nodeId; ///< the node id of this node
377 SpoVNetID spovnetId; ///< the spovnet id of the currently joined overlay
378 vector<LinkID> bootstrapLinks; ///< the link id of the link to the initiator node
379 NodeID spovnetInitiator; ///< The initiator node
380
381 /// the service id communication listeners
382 Demultiplexer<CommunicationListener*, ServiceID> communicationListeners;
383
384 /// the node listeners
385 typedef vector<NodeListener*> NodeListenerVector;
386 NodeListenerVector nodeListeners;
387
388 /// the sideport listener
389 SideportListener* sideport;
390
391 /// the used overlay structure
392 OverlayInterface* overlayInterface;
393
394 /// The link mapping of the node
395 vector<LinkDescriptor*> links;
396 void eraseDescriptor(const LinkID& link, bool communication = false);
397
398 /// returns a link descriptor for the given id
399 LinkDescriptor* getDescriptor(const LinkID& link,
400 bool communication = false);
401
402 /// returns a link descriptor for the given id
403 const LinkDescriptor* getDescriptor(const LinkID& link,
404 bool communication = false) const;
405
406 /// returns a auto-link descriptor for the given node and service id
407 LinkDescriptor* getAutoDescriptor(const NodeID& node, const ServiceID& service);
408
409 /// adds a new link descriptor or uses an existing one
410 LinkDescriptor* addDescriptor(const LinkID& link = LinkID::UNSPECIFIED);
411
412 /// returns a direct link relay descriptor to the given relay node
413 LinkDescriptor* getRelayDescriptor( const NodeID& remoteNode );
414
415 /// returns the local relay node to a given remote node
416 NodeID getRelayNode( const NodeID& remoteNode );
417
418 /// returns the direct link the message to a neighbor is send to
419 LinkDescriptor* getSendDescriptor( const NodeID& nodeid, bool follow = true );
420
421 /// routes a message over the overlay or directly sends it when a link is open
422 seqnum_t sendOverlay( Message* message, const NodeID& nodeid,
423 const NodeID& remoteRelay = NodeID::UNSPECIFIED );
424
425 /// forwards a message over relays/overlay/directly using link descriptor
426 seqnum_t sendMessage( Message* message, const LinkDescriptor* ld );
427
428 /// creates a link descriptor, applys relay semantics if possible
429 LinkDescriptor* createLinkDescriptor(
430 const NodeID remoteNode, const ServiceID service, const LinkID link_id );
431
432 // map of a link request map a nonce to a LinkID
433 typedef map<const uint32_t, LinkID> PendingLinkMap;
434 PendingLinkMap pendingLinks;
435
436 void showLinkState();
437
438 /**
439 * nodes with pending joines. TODO: should be cleaned every
440 * some seconds, add timestamps to each, and check on occasion
441 */
442 typedef vector<NodeID> JoiningNodes;
443 JoiningNodes joiningNodes;
444
445 int counter;
446
447 /**
448 * Bootstrapper for our spovnet
449 */
450 OverlayBootstrap overlayBootstrap;
451
452 /// is the base overlay started yet
453 bool started;
454};
455
456}} // namespace ariba, overlay
457
458#endif /*BASEOVERLAY_H_*/
Note: See TracBrowser for help on using the repository browser.