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

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

-autolink message buffer von basecomm nach baseoverlay und komplette logik hochgezogen damit mit neuem overlay routing zurecht kommt, damit ist letzer fehler raus

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