| 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 BASECOMMUNICATION_H_
|
|---|
| 40 | #define BASECOMMUNICATION_H_
|
|---|
| 41 |
|
|---|
| 42 | #include <ext/hash_map>
|
|---|
| 43 | #include <ext/hash_set>
|
|---|
| 44 | #include <map>
|
|---|
| 45 | #include <set>
|
|---|
| 46 | #include <vector>
|
|---|
| 47 | #include <iostream>
|
|---|
| 48 | #include <deque>
|
|---|
| 49 | #include <algorithm>
|
|---|
| 50 | #include <boost/foreach.hpp>
|
|---|
| 51 |
|
|---|
| 52 | #include "ariba/utility/types.h"
|
|---|
| 53 | #include "ariba/utility/messages.h"
|
|---|
| 54 | #include "ariba/utility/logging/Logging.h"
|
|---|
| 55 | #include "ariba/utility/misc/Demultiplexer.hpp"
|
|---|
| 56 |
|
|---|
| 57 | #include "ariba/communication/CommunicationEvents.h"
|
|---|
| 58 | #include "ariba/communication/EndpointDescriptor.h"
|
|---|
| 59 | #include "ariba/communication/networkinfo/NetworkChangeInterface.h"
|
|---|
| 60 | #include "ariba/communication/networkinfo/NetworkChangeDetection.h"
|
|---|
| 61 | #include "ariba/communication/networkinfo/NetworkInformation.h"
|
|---|
| 62 | #include "ariba/communication/networkinfo/AddressInformation.h"
|
|---|
| 63 | #include "ariba/communication/messages/AribaBaseMsg.h"
|
|---|
| 64 | #include "ariba/communication/modules/transport/TransportProtocol.h"
|
|---|
| 65 | #include "ariba/communication/modules/network/NetworkProtocol.h"
|
|---|
| 66 | #include "ariba/communication/modules/network/NetworkLocator.h"
|
|---|
| 67 |
|
|---|
| 68 | #ifndef UNDERLAY_OMNET
|
|---|
| 69 | #include "ariba/communication/modules/transport/tcp/TCPTransport.h"
|
|---|
| 70 | #include "ariba/communication/modules/network/ip/IPv4NetworkProtocol.h"
|
|---|
| 71 | #endif
|
|---|
| 72 |
|
|---|
| 73 | using __gnu_cxx::hash_set;
|
|---|
| 74 | using __gnu_cxx::hash_map;
|
|---|
| 75 |
|
|---|
| 76 | using std::cout;
|
|---|
| 77 | using std::set;
|
|---|
| 78 | using std::map;
|
|---|
| 79 | using std::vector;
|
|---|
| 80 | using std::deque;
|
|---|
| 81 | using std::pair;
|
|---|
| 82 | using std::make_pair;
|
|---|
| 83 | using std::find;
|
|---|
| 84 |
|
|---|
| 85 | using ariba::communication::NetworkChangeDetection;
|
|---|
| 86 | using ariba::communication::NetworkChangeInterface;
|
|---|
| 87 | using ariba::communication::NetworkInterfaceList;
|
|---|
| 88 | using ariba::communication::NetworkInformation;
|
|---|
| 89 | using ariba::communication::AddressInformation;
|
|---|
| 90 | using ariba::communication::AddressList;
|
|---|
| 91 | using ariba::communication::AribaBaseMsg;
|
|---|
| 92 | using ariba::communication::CommunicationEvents;
|
|---|
| 93 |
|
|---|
| 94 | using ariba::utility::Demultiplexer;
|
|---|
| 95 | using ariba::utility::QoSParameterSet;
|
|---|
| 96 | using ariba::utility::SecurityParameterSet;
|
|---|
| 97 | using ariba::utility::Address;
|
|---|
| 98 | using ariba::utility::LinkID;
|
|---|
| 99 | using ariba::utility::LinkIDs;
|
|---|
| 100 | using ariba::utility::Message;
|
|---|
| 101 | using ariba::utility::MessageReceiver;
|
|---|
| 102 | using ariba::utility::seqnum_t;
|
|---|
| 103 |
|
|---|
| 104 | using ariba::communication::TransportProtocol;
|
|---|
| 105 | using ariba::communication::NetworkProtocol;
|
|---|
| 106 | using ariba::communication::NetworkLocator;
|
|---|
| 107 | #ifndef UNDERLAY_OMNET
|
|---|
| 108 | using ariba::communication::IPv4NetworkProtocol;
|
|---|
| 109 | using ariba::communication::TCPTransport;
|
|---|
| 110 | #endif
|
|---|
| 111 |
|
|---|
| 112 | namespace ariba {
|
|---|
| 113 | namespace communication {
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * This class implements the Ariba Base Communication<br />
|
|---|
| 117 | *
|
|---|
| 118 | * Its primary task is to provide an abstraction to existing
|
|---|
| 119 | * protocols and addressing schemes.
|
|---|
| 120 | *
|
|---|
| 121 | * @author Sebastian Mies, Christoph Mayer
|
|---|
| 122 | */
|
|---|
| 123 | class BaseCommunication : public MessageReceiver, NetworkChangeInterface {
|
|---|
| 124 |
|
|---|
| 125 | use_logging_h(BaseCommunication);
|
|---|
| 126 |
|
|---|
| 127 | public:
|
|---|
| 128 | /**
|
|---|
| 129 | * Constructs a Base Communication instance.
|
|---|
| 130 | * Listens default on port number 41402
|
|---|
| 131 | */
|
|---|
| 132 | BaseCommunication(const NetworkLocator* _locallocator, const uint16_t _listenport);
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Destructs a Base Communication instance
|
|---|
| 136 | */
|
|---|
| 137 | virtual ~BaseCommunication();
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Establishes a link to another end-point
|
|---|
| 141 | */
|
|---|
| 142 | const LinkID establishLink(
|
|---|
| 143 | const EndpointDescriptor& descriptor,
|
|---|
| 144 | const QoSParameterSet& qos = QoSParameterSet::DEFAULT,
|
|---|
| 145 | const SecurityParameterSet& sec = SecurityParameterSet::DEFAULT
|
|---|
| 146 | );
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Drops a link
|
|---|
| 150 | *
|
|---|
| 151 | * @param The link id of the link that should be dropped
|
|---|
| 152 | */
|
|---|
| 153 | void dropLink(const LinkID link);
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Sends a message though an existing link to an end-point.
|
|---|
| 157 | *
|
|---|
| 158 | * @param lid The link identifier
|
|---|
| 159 | * @param message The message to be sent
|
|---|
| 160 | * @return A sequence number for this message
|
|---|
| 161 | */
|
|---|
| 162 | seqnum_t sendMessage(const LinkID lid, const Message* message);
|
|---|
| 163 |
|
|---|
| 164 | /**
|
|---|
| 165 | * Returns the end-point descriptor
|
|---|
| 166 | *
|
|---|
| 167 | * @param link the link id of the requested end-point
|
|---|
| 168 | * @return The end-point descriptor of the link's end-point
|
|---|
| 169 | */
|
|---|
| 170 | const EndpointDescriptor& getEndpointDescriptor( const LinkID link = LinkID::UNSPECIFIED ) const;
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * Get local links to the given endpoint of all local link
|
|---|
| 174 | * using the default parameter EndpointDescriptor::UNSPECIFIED
|
|---|
| 175 | * @param ep The remote endpoint to get all links to.
|
|---|
| 176 | * @return List of LinkID
|
|---|
| 177 | */
|
|---|
| 178 | LinkIDs getLocalLinks( const EndpointDescriptor& ep = EndpointDescriptor::UNSPECIFIED ) const;
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Registers a receiver.
|
|---|
| 182 | *
|
|---|
| 183 | * @param _receiver The receiving side
|
|---|
| 184 | */
|
|---|
| 185 | void registerMessageReceiver( MessageReceiver* _receiver );
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * Unregister a receiver.
|
|---|
| 189 | *
|
|---|
| 190 | * @param _receiver The receiving side
|
|---|
| 191 | */
|
|---|
| 192 | void unregisterMessageReceiver( MessageReceiver* _receiver );
|
|---|
| 193 |
|
|---|
| 194 | void registerEventListener( CommunicationEvents* _events );
|
|---|
| 195 | void unregisterEventListener( CommunicationEvents* _events );
|
|---|
| 196 |
|
|---|
| 197 | protected:
|
|---|
| 198 |
|
|---|
| 199 | /**
|
|---|
| 200 | * Called from the Transport when async items
|
|---|
| 201 | * from the SystemQueue are delivered
|
|---|
| 202 | */
|
|---|
| 203 | virtual bool receiveMessage( const Message* message, const LinkID& link, const NodeID& node );
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * Called when a network interface change happens
|
|---|
| 207 | */
|
|---|
| 208 | virtual void onNetworkChange( const NetworkChangeInterface::NetworkChangeInfo& info );
|
|---|
| 209 |
|
|---|
| 210 | private:
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * A link descriptor consisting of the
|
|---|
| 214 | * end-point descriptor and currently used locator and
|
|---|
| 215 | * message receiver
|
|---|
| 216 | */
|
|---|
| 217 | class LinkDescriptor {
|
|---|
| 218 | public:
|
|---|
| 219 | static const LinkDescriptor UNSPECIFIED;
|
|---|
| 220 |
|
|---|
| 221 | LinkDescriptor() :
|
|---|
| 222 | localLink(),
|
|---|
| 223 | localLocator(NULL),
|
|---|
| 224 | remoteLink(),
|
|---|
| 225 | remoteLocator(NULL),
|
|---|
| 226 | remoteEndpoint(EndpointDescriptor::UNSPECIFIED),
|
|---|
| 227 | linkup(false) {
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | LinkDescriptor(const LinkID& _localLink, const NetworkLocator*& _localLocator,
|
|---|
| 231 | const LinkID& _remoteLink, const NetworkLocator*& _remoteLocator,
|
|---|
| 232 | const EndpointDescriptor& _remoteEndpoint, bool _linkup ) :
|
|---|
| 233 | localLink(_localLink),
|
|---|
| 234 | localLocator(_localLocator),
|
|---|
| 235 | remoteLink(_remoteLink),
|
|---|
| 236 | remoteLocator(_remoteLocator),
|
|---|
| 237 | remoteEndpoint(_remoteEndpoint),
|
|---|
| 238 | linkup(_linkup) {
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | LinkDescriptor( const LinkDescriptor& desc ) :
|
|---|
| 242 | localLink(desc.localLink),
|
|---|
| 243 | localLocator(desc.localLocator),
|
|---|
| 244 | remoteLink(desc.remoteLink),
|
|---|
| 245 | remoteLocator(desc.remoteLocator),
|
|---|
| 246 | remoteEndpoint(desc.remoteEndpoint),
|
|---|
| 247 | linkup(desc.linkup) {
|
|---|
| 248 |
|
|---|
| 249 | BOOST_FOREACH( Message* msg, desc.waitingmsg ){
|
|---|
| 250 | waitingmsg.push_back( msg );
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | bool isUnspecified() const {
|
|---|
| 255 | return (this == &UNSPECIFIED);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | LinkID localLink;
|
|---|
| 259 | const NetworkLocator* localLocator;
|
|---|
| 260 | LinkID remoteLink;
|
|---|
| 261 | const NetworkLocator* remoteLocator;
|
|---|
| 262 | EndpointDescriptor remoteEndpoint;
|
|---|
| 263 |
|
|---|
| 264 | bool linkup;
|
|---|
| 265 | deque<Message*> waitingmsg;
|
|---|
| 266 | };
|
|---|
| 267 |
|
|---|
| 268 | /**
|
|---|
| 269 | * Link management: add a link
|
|---|
| 270 | */
|
|---|
| 271 | void addLink( const LinkDescriptor& link );
|
|---|
| 272 |
|
|---|
| 273 | /**
|
|---|
| 274 | * Link management: remove alink
|
|---|
| 275 | */
|
|---|
| 276 | void removeLink( const LinkID& localLink );
|
|---|
| 277 |
|
|---|
| 278 | /**
|
|---|
| 279 | * Link management: get link information using the local link
|
|---|
| 280 | */
|
|---|
| 281 | LinkDescriptor& queryLocalLink( const LinkID& localLink ) const;
|
|---|
| 282 |
|
|---|
| 283 | /**
|
|---|
| 284 | * Link management: get link information using the remote link
|
|---|
| 285 | */
|
|---|
| 286 | LinkDescriptor& queryRemoteLink( const LinkID& remoteLink ) const;
|
|---|
| 287 |
|
|---|
| 288 | /**
|
|---|
| 289 | * Link management: list of links
|
|---|
| 290 | */
|
|---|
| 291 | typedef vector<LinkDescriptor> LinkSet;
|
|---|
| 292 |
|
|---|
| 293 | /**
|
|---|
| 294 | * Link management: the set of currently managed links
|
|---|
| 295 | */
|
|---|
| 296 | LinkSet linkSet;
|
|---|
| 297 |
|
|---|
| 298 | /**
|
|---|
| 299 | * The message receiver
|
|---|
| 300 | */
|
|---|
| 301 | MessageReceiver* messageReceiver;
|
|---|
| 302 |
|
|---|
| 303 | /**
|
|---|
| 304 | * The local end-point descriptor
|
|---|
| 305 | */
|
|---|
| 306 | EndpointDescriptor localDescriptor;
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * Network information and protocol
|
|---|
| 310 | */
|
|---|
| 311 | NetworkProtocol* network;
|
|---|
| 312 |
|
|---|
| 313 | /**
|
|---|
| 314 | * Transport information and protocol
|
|---|
| 315 | */
|
|---|
| 316 | TransportProtocol* transport;
|
|---|
| 317 |
|
|---|
| 318 | #ifndef UNDERLAY_OMNET
|
|---|
| 319 | /**
|
|---|
| 320 | * Detect changes in the routing/interface, etc.
|
|---|
| 321 | * stuff needed for mobility detection
|
|---|
| 322 | */
|
|---|
| 323 | NetworkChangeDetection networkMonitor;
|
|---|
| 324 | #endif
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * The local listen port
|
|---|
| 328 | */
|
|---|
| 329 | uint16_t listenport;
|
|---|
| 330 |
|
|---|
| 331 | typedef set<CommunicationEvents*> EventListenerSet;
|
|---|
| 332 | EventListenerSet eventListener;
|
|---|
| 333 |
|
|---|
| 334 | seqnum_t currentSeqnum;
|
|---|
| 335 | };
|
|---|
| 336 |
|
|---|
| 337 | }} // namespace ariba, communication
|
|---|
| 338 |
|
|---|
| 339 | #endif /*BASECOMMUNICATION_H_*/
|
|---|