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