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

Last change on this file since 4872 was 4836, checked in by Christoph Mayer, 15 years ago

einige avahi fixes und ablauf

File size: 12.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::CommunicationListener;
89
90// overlay
91using ariba::overlay::OverlayBootstrap;
92
93// communication
94using ariba::communication::EndpointDescriptor;
95using ariba::communication::BaseCommunication;
96using ariba::communication::CommunicationEvents;
97
98// utilities
99using ariba::utility::NodeID;
100using ariba::utility::SpoVNetID;
101using ariba::utility::LinkID;
102using ariba::utility::Identifier;
103using ariba::utility::ServiceID;
104using ariba::utility::QoSParameterSet;
105using ariba::utility::SecurityParameterSet;
106using ariba::utility::Demultiplexer;
107using ariba::utility::MessageReceiver;
108using ariba::utility::MessageSender;
109using ariba::utility::seqnum_t;
110using ariba::utility::Timer;
111using ariba::utility::OvlVis;
112
113#define ovl OvlVis::instance()
114#define ovlId OvlVis::NETWORK_ID_BASE_OVERLAY
115
116namespace ariba {
117namespace overlay {
118
119class BaseOverlay: public MessageReceiver, public CommunicationEvents,
120 public OverlayStructureEvents, protected Timer {
121
122private:
123 friend class OneHop;
124 friend class Chord;
125
126 use_logging_h( BaseOverlay );
127
128public:
129
130 /**
131 * Constructs an empty non-functional base overlay instance
132 */
133 BaseOverlay();
134
135 /**
136 * Destructs a base overlay instance
137 */
138 virtual ~BaseOverlay();
139
140 /**
141 * Starts the Base Overlay instance
142 */
143 void start(BaseCommunication& _basecomm, const NodeID& _nodeid);
144
145 /**
146 * Stops the Base Overlay instance
147 */
148 void stop();
149
150 /**
151 * Starts a link establishment procedure to the specfied node
152 * for the service with id service
153 *
154 * @param node Destination node id
155 * @param service Service to connect to
156 * @param linkid Link identifier to be used with this link
157 */
158 const LinkID establishLink(const NodeID& node, const ServiceID& service,
159 const LinkID& linkid = LinkID::UNSPECIFIED);
160
161 /**
162 * Starts a link establishment procedure to the specified
163 *
164 * endpoint and to the specified service
165 */
166 const LinkID establishLink(const EndpointDescriptor& ep,
167 const ServiceID& service, const LinkID& linkid =
168 LinkID::UNSPECIFIED);
169
170 /// drops a link
171 void dropLink(const LinkID& link);
172
173 /// sends a message over an existing link
174 seqnum_t sendMessage(const Message* message, const LinkID& link);
175
176 /// sends a message to a node and a specific service
177 seqnum_t sendMessage(const Message* message, const NodeID& node,
178 const ServiceID& service);
179
180 /**
181 * Send out a message to all nodes that are known in the overlay structure.
182 * Depending on the structure of the overlay, this can be very different.
183 */
184 void broadcastMessage(Message* message, const ServiceID& service);
185
186 /**
187 * Returns the end-point descriptor of a link.
188 *
189 * @param link the link id of the requested end-point
190 * @return The end-point descriptor of the link's end-point
191 */
192 const EndpointDescriptor& getEndpointDescriptor(const LinkID& link =
193 LinkID::UNSPECIFIED) const;
194
195 /**
196 * Get a list of overlay neighbors.
197 *
198 * @return A list of overlay neighbors.
199 */
200 vector<NodeID> getOverlayNeighbors() const;
201
202 /**
203 * Returns a end-endpoint descriptor of a overlay neighbor.
204 * If the node is not known -- an unspecified endpoint descriptor is
205 * returned.
206 *
207 * @param node The node identifer of a overlay neighbor.
208 * @return The end-point descriptor of the node or unspecified.
209 */
210 const EndpointDescriptor& getEndpointDescriptor(const NodeID& node) const;
211
212 // TODO: Doc
213 bool bind(CommunicationListener* listener, const ServiceID& sid);
214
215 // TODO: Doc
216 bool unbind(CommunicationListener* listener, const ServiceID& sid);
217
218 // TODO: Doc
219 bool bind(NodeListener* listener);
220
221 // TODO: Doc
222 bool unbind(NodeListener* listener);
223
224 // TODO: Doc
225 bool registerSidePort(SideportListener* _sideport);
226
227 // TODO: Doc
228 bool unregisterSidePort(SideportListener* _sideport);
229
230 /**
231 * Returns the own nodeID or the NodeID of the specified link
232 *
233 * @param lid The link identifier
234 * @return The NodeID of the link
235 */
236 const NodeID& getNodeID(const LinkID& lid = LinkID::UNSPECIFIED) const;
237
238 /**
239 * Return all Links for the specified remote nodeid, or all links when
240 * the node id given is set to unspecified
241 *
242 * @param nid The node id to request links for, or unspecified for all links
243 * @return a vector that contains all the link ids requested
244 */
245 vector<LinkID> getLinkIDs(const NodeID& nid = NodeID::UNSPECIFIED) const;
246
247 /**
248 * Join a existing sponaneous virtual network (spovnet).
249 *
250 * @param id A spovnet identifier
251 * @param boot A bootstrap node
252 */
253 void joinSpoVNet(const SpoVNetID& id, const EndpointDescriptor& boot );
254
255 /**
256 * Initiates a new spontaneous virtual network.
257 * This makes this BaseOverlay to the SpoVNet-Initiator.
258 *
259 * @param id The spovnet identifier
260 */
261 void createSpoVNet(const SpoVNetID& id, const OverlayParameterSet& param =
262 OverlayParameterSet::DEFAULT, const SecurityParameterSet& sec =
263 SecurityParameterSet::DEFAULT, const QoSParameterSet& qos =
264 QoSParameterSet::DEFAULT);
265
266 /**
267 * Let the node leave the SpoVNet.
268 */
269 void leaveSpoVNet();
270
271protected:
272
273 /**
274 * @see ariba::communication::CommunicationEvents.h
275 */
276 virtual void onLinkUp(const LinkID& id, const NetworkLocator* local,
277 const NetworkLocator* remote);
278
279 /**
280 * @see ariba::communication::CommunicationEvents.h
281 */
282 virtual void onLinkDown(const LinkID& id, const NetworkLocator* local,
283 const NetworkLocator* remote);
284
285 /**
286 * @see ariba::communication::CommunicationEvents.h
287 */
288 virtual void onLinkChanged(const LinkID& id,
289 const NetworkLocator* oldlocal, const NetworkLocator* newlocal,
290 const NetworkLocator* oldremote, const NetworkLocator* newremote);
291
292 /**
293 * @see ariba::communication::CommunicationEvents.h
294 */
295 virtual void onLinkFail(const LinkID& id, const NetworkLocator* local,
296 const NetworkLocator* remote);
297
298 /**
299 * @see ariba::communication::CommunicationEvents.h
300 */
301 virtual void onLinkQoSChanged(const LinkID& id,
302 const NetworkLocator* local, const NetworkLocator* remote,
303 const QoSParameterSet& qos);
304
305 /**
306 * @see ariba::communication::CommunicationEvents.h
307 */
308 virtual bool onLinkRequest(const LinkID& id, const NetworkLocator* local,
309 const NetworkLocator* remote);
310
311 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312
313 /**
314 * Processes a received message.
315 *
316 * Beware: nodeid is not valid in this case! (since this class implements
317 * nodeid's in the first place *g*)
318 */
319 virtual bool receiveMessage(
320 const Message* message, const LinkID& link, const NodeID&);
321
322 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
323
324 /**
325 * This method is called, when a routed message arrives from the
326 * overlay.
327 *
328 * @see OverlayStructureEvents.h
329 */
330 virtual void incomingRouteMessage(Message* msg);
331
332 /**
333 * This method is called, when a new node joined the network
334 *
335 * @see OverlayStructureEvents.h
336 */
337 virtual void onNodeJoin(const NodeID& node);
338
339 /**
340 * Timer Event method
341 */
342 virtual void eventFunction();
343
344private:
345 /**
346 * The BaseCommunication the BaseOverlay
347 * communicates over
348 */
349 BaseCommunication* bc;
350
351 /**
352 * The nodeid of this BaseOverlay instance.
353 */
354 NodeID nodeId;
355
356 /**
357 * The SpoVNetID that we are joined to
358 * or that we have created.
359 */
360 SpoVNetID spovnetId;
361
362 /**
363 * TODO
364 */
365 Demultiplexer<CommunicationListener*, ServiceID> communicationListeners;
366
367 /**
368 * TODO
369 */
370 typedef vector<NodeListener*> NodeListenerVector;
371
372 /**
373 * TODO
374 */
375 NodeListenerVector nodeListeners;
376
377 /**
378 * TODO
379 */
380 SideportListener* sideport;
381
382 /**
383 * The abstract overlay interface that implements
384 * the overlay specific functionality.
385 */
386 OverlayInterface* overlayInterface;
387
388 /**
389 * The special link to the Initiator of the SpoVNet
390 * or LinkID::UNDEFINED if we are the Initiator
391 */
392 LinkID initiatorLink;
393
394 /**
395 * The state of the BaseOverlay
396 */
397 typedef enum _BaseOverlayState {
398 BaseOverlayStateInvalid = 0,
399 BaseOverlayStateInitiator = 1,
400 BaseOverlayStateJoinInitiated = 2,
401 BaseOverlayStateCompleted = 3,
402 } BaseOverlayState;
403
404 /**
405 * TODO
406 */
407 BaseOverlayState state;
408
409 /**
410 * The initiator node
411 */
412 NodeID spovnetInitiator;
413
414 /**
415 * OvlVis
416 */
417 NodeID min, max;
418 NodeID succ, pred;
419 void updateOvlVis(const NodeID& node);
420
421 /**
422 * Link management
423 */
424 class LinkItem {
425 public:
426 static const LinkItem UNSPECIFIED;
427
428 LinkItem() :
429 link(LinkID::UNSPECIFIED),
430 node(NodeID::UNSPECIFIED),
431 service(ServiceID::UNSPECIFIED),
432 interface(&CommunicationListener::DEFAULT),
433 autolink(false),
434 lastuse(0),
435 linkup(false){
436 }
437
438 LinkItem(const LinkID& _link, const NodeID& _node,
439 const ServiceID& _service, CommunicationListener* _interface) :
440 link(_link),
441 node(_node),
442 service(_service),
443 interface(_interface),
444 autolink(false),
445 lastuse(time(NULL)),
446 linkup(false){
447
448 assert( _interface != NULL );
449 }
450
451 LinkItem(const LinkItem& rh) :
452 link(rh.link),
453 node(rh.node),
454 service(rh.service),
455 interface(rh.interface),
456 autolink(rh.autolink),
457 lastuse(rh.lastuse),
458 linkup(rh.linkup){
459
460 BOOST_FOREACH( Message* msg, rh.waitingmsg ){
461 waitingmsg.push_back( msg );
462 }
463 }
464
465 void deleteWaiting(){
466 BOOST_FOREACH( Message* msg, waitingmsg ){
467 delete msg;
468 }
469 waitingmsg.clear();
470 }
471
472 // general information about the link
473 const LinkID link;
474 NodeID node;
475 ServiceID service;
476 CommunicationListener* interface;
477 bool linkup;
478
479 // information needed for auto links
480 void markused() {
481 lastuse = time(NULL);
482 }
483
484 bool autolink;
485 time_t lastuse;
486 deque<Message*> waitingmsg;
487 };
488
489 typedef map<const LinkID, LinkItem> LinkMapping;
490 typedef pair<const LinkID, LinkItem> LinkPair;
491 LinkMapping linkMapping;
492
493 // map of a link request map a nonce to a LinkID
494 typedef map<const uint32_t, LinkID> PendingLinkMap;
495 PendingLinkMap pendingLinks;
496
497 /**
498 * nodes with pending joines. TODO: should be cleaned every
499 * some seconds, add timestamps to each, and check on occasion
500 */
501 typedef vector<NodeID> JoiningNodes;
502 JoiningNodes joiningNodes;
503
504 /**
505 * Bootstrapper for our spovnet
506 */
507 OverlayBootstrap overlayBootstrap;
508};
509
510}} // namespace ariba, overlay
511
512#endif /*BASEOVERLAY_H_*/
Note: See TracBrowser for help on using the repository browser.