| 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 BASECOMMUNICATION_H_
|
---|
| 40 | #define BASECOMMUNICATION_H_
|
---|
| 41 |
|
---|
| 42 | // boost & std includes
|
---|
| 43 | #include <ext/hash_map>
|
---|
| 44 | #include <ext/hash_set>
|
---|
| 45 | #include <map>
|
---|
| 46 | #include <set>
|
---|
| 47 | #include <vector>
|
---|
| 48 | #include <iostream>
|
---|
| 49 | #include <algorithm>
|
---|
| 50 | #include <boost/foreach.hpp>
|
---|
| 51 |
|
---|
| 52 | // utilities
|
---|
| 53 | #include "ariba/utility/types.h"
|
---|
| 54 | #include "ariba/utility/messages.h"
|
---|
| 55 | #include "ariba/utility/logging/Logging.h"
|
---|
| 56 | #include "ariba/utility/misc/Demultiplexer.hpp"
|
---|
| 57 | #include "ariba/utility/system/SystemEventListener.h"
|
---|
| 58 |
|
---|
| 59 | // new transport and addressing
|
---|
| 60 | #include "ariba/utility/addressing/addressing.hpp"
|
---|
| 61 | #include "ariba/utility/transport/transport.hpp"
|
---|
| 62 |
|
---|
| 63 | // communication
|
---|
| 64 | #include "ariba/communication/CommunicationEvents.h"
|
---|
| 65 | #include "ariba/communication/EndpointDescriptor.h"
|
---|
| 66 | #include "ariba/communication/messages/AribaBaseMsg.h"
|
---|
| 67 |
|
---|
| 68 | // network changes
|
---|
| 69 | #include "ariba/communication/networkinfo/NetworkChangeInterface.h"
|
---|
| 70 | #include "ariba/communication/networkinfo/NetworkChangeDetection.h"
|
---|
| 71 | #include "ariba/communication/networkinfo/NetworkInformation.h"
|
---|
| 72 | #include "ariba/communication/networkinfo/AddressInformation.h"
|
---|
| 73 |
|
---|
| 74 | // deprecated
|
---|
| 75 | //#include "ariba/communication/modules/transport/TransportProtocol.h"
|
---|
| 76 | //#include "ariba/communication/modules/network/NetworkProtocol.h"
|
---|
| 77 | //#include "ariba/communication/modules/network/NetworkLocator.h"
|
---|
| 78 |
|
---|
| 79 | // disabled
|
---|
| 80 | //#ifndef UNDERLAY_OMNET
|
---|
| 81 | // #include "ariba/communication/modules/transport/tcp/TCPTransport.h"
|
---|
| 82 | // #include "ariba/communication/modules/network/ip/IPv4NetworkProtocol.h"
|
---|
| 83 | //#endif
|
---|
| 84 |
|
---|
| 85 | // deprecated
|
---|
| 86 | //using ariba::communication::TransportProtocol;
|
---|
| 87 | //using ariba::communication::NetworkProtocol;
|
---|
| 88 | //using ariba::communication::NetworkLocator;
|
---|
| 89 |
|
---|
| 90 | // disabled
|
---|
| 91 | //#ifndef UNDERLAY_OMNET
|
---|
| 92 | // using ariba::communication::IPv4NetworkProtocol;
|
---|
| 93 | // using ariba::communication::TCPTransport;
|
---|
| 94 | //#endif
|
---|
| 95 |
|
---|
| 96 | namespace ariba {
|
---|
| 97 | namespace communication {
|
---|
| 98 |
|
---|
| 99 | using namespace std;
|
---|
| 100 | using namespace ariba::addressing;
|
---|
| 101 | using namespace ariba::transport;
|
---|
| 102 | using namespace ariba::utility;
|
---|
| 103 |
|
---|
| 104 | // use base ariba types (clarifies multiple definitions)
|
---|
| 105 | using ariba::utility::Message;
|
---|
| 106 | using ariba::utility::seqnum_t;
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * This class implements the Ariba Base Communication<br />
|
---|
| 110 | *
|
---|
| 111 | * Its primary task is to provide an abstraction to existing
|
---|
| 112 | * protocols and addressing schemes.
|
---|
| 113 | *
|
---|
| 114 | * @author Sebastian Mies, Christoph Mayer
|
---|
| 115 | */
|
---|
| 116 | class BaseCommunication: public NetworkChangeInterface,
|
---|
| 117 | public SystemEventListener, public transport_listener {
|
---|
| 118 | use_logging_h(BaseCommunication);
|
---|
| 119 |
|
---|
| 120 | public:
|
---|
| 121 | /// Default ctor that just creates an non-functional base communication
|
---|
| 122 | BaseCommunication();
|
---|
| 123 |
|
---|
| 124 | /// Default dtor that does nothing
|
---|
| 125 | virtual ~BaseCommunication();
|
---|
| 126 |
|
---|
| 127 | /// Startup the base communication, start modules etc.
|
---|
| 128 | void start();
|
---|
| 129 |
|
---|
| 130 | /// Stops the base communication, stop modules etc.
|
---|
| 131 | void stop();
|
---|
| 132 |
|
---|
| 133 | /// Sets the endpoints
|
---|
| 134 | void setEndpoints( string& endpoints );
|
---|
| 135 |
|
---|
| 136 | /// Check whether the base communication has been started up
|
---|
| 137 | bool isStarted();
|
---|
| 138 |
|
---|
| 139 | /// Establishes a link to another end-point.
|
---|
| 140 | const LinkID establishLink(const EndpointDescriptor& descriptor,
|
---|
| 141 | const LinkID& linkid = LinkID::UNSPECIFIED, const QoSParameterSet& qos =
|
---|
| 142 | QoSParameterSet::DEFAULT, const SecurityParameterSet& sec =
|
---|
| 143 | SecurityParameterSet::DEFAULT);
|
---|
| 144 |
|
---|
| 145 | /// Drops a link
|
---|
| 146 | void dropLink(const LinkID link);
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Sends a message though an existing link to an end-point.
|
---|
| 150 | *
|
---|
| 151 | * @param lid The link identifier
|
---|
| 152 | * @param message The message to be sent
|
---|
| 153 | * @return A sequence number for this message
|
---|
| 154 | */
|
---|
| 155 | seqnum_t sendMessage(const LinkID lid, const Message* message);
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Returns the end-point descriptor
|
---|
| 159 | *
|
---|
| 160 | * @param link the link id of the requested end-point
|
---|
| 161 | * @return The end-point descriptor of the link's end-point
|
---|
| 162 | */
|
---|
| 163 | const EndpointDescriptor& getEndpointDescriptor(const LinkID link =
|
---|
| 164 | LinkID::UNSPECIFIED) const;
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
| 167 | * Get local links to the given endpoint of all local link
|
---|
| 168 | * using the default parameter EndpointDescriptor::UNSPECIFIED
|
---|
| 169 | * @param ep The remote endpoint to get all links to.
|
---|
| 170 | * @return List of LinkID
|
---|
| 171 | */
|
---|
| 172 | LinkIDs getLocalLinks(const address_v* addr) const;
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * Registers a receiver.
|
---|
| 176 | *
|
---|
| 177 | * @param _receiver The receiving side
|
---|
| 178 | */
|
---|
| 179 | void registerMessageReceiver(MessageReceiver* receiver) {
|
---|
| 180 | messageReceiver = receiver;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
| 184 | * Unregister a receiver.
|
---|
| 185 | *
|
---|
| 186 | * @param _receiver The receiving side
|
---|
| 187 | */
|
---|
| 188 | void unregisterMessageReceiver(MessageReceiver* receiver) {
|
---|
| 189 | messageReceiver = NULL;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | void registerEventListener(CommunicationEvents* _events);
|
---|
| 193 |
|
---|
| 194 | void unregisterEventListener(CommunicationEvents* _events);
|
---|
| 195 |
|
---|
| 196 | public:
|
---|
| 197 |
|
---|
| 198 | /// called when a system event is emitted by system queue
|
---|
| 199 | virtual void handleSystemEvent(const SystemEvent& event);
|
---|
| 200 |
|
---|
| 201 | /// called when a message is received form transport_peer
|
---|
| 202 | virtual void receive_message(transport_protocol* transport,
|
---|
| 203 | const address_vf local, const address_vf remote, const uint8_t* data,
|
---|
| 204 | size_t size);
|
---|
| 205 |
|
---|
| 206 | protected:
|
---|
| 207 |
|
---|
| 208 | /// handle received message from a transport module
|
---|
| 209 | void receiveMessage(const Message* message,
|
---|
| 210 | const address_v* local, const address_v* remote );
|
---|
| 211 |
|
---|
| 212 | /// called when a network interface change happens
|
---|
| 213 | virtual void onNetworkChange(
|
---|
| 214 | const NetworkChangeInterface::NetworkChangeInfo& info);
|
---|
| 215 |
|
---|
| 216 | private:
|
---|
| 217 | /**
|
---|
| 218 | * A link descriptor consisting of the end-point descriptor and currently
|
---|
| 219 | * used underlay address.
|
---|
| 220 | */
|
---|
| 221 | class LinkDescriptor {
|
---|
| 222 | public:
|
---|
| 223 | static const LinkDescriptor UNSPECIFIED;
|
---|
| 224 |
|
---|
| 225 | /// default constructor
|
---|
| 226 | LinkDescriptor() :
|
---|
| 227 | localLink(LinkID::UNSPECIFIED), localLocator(NULL),
|
---|
| 228 | remoteLink(LinkID::UNSPECIFIED), remoteLocator(NULL),
|
---|
| 229 | remoteEndpoint(EndpointDescriptor::UNSPECIFIED), up(false) {
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | ~LinkDescriptor() {
|
---|
| 233 | if (localLocator!=NULL) delete localLocator;
|
---|
| 234 | if (remoteLocator!=NULL) delete remoteLocator;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /// returns true if this is the UNSPECIFIED object
|
---|
| 238 | bool isUnspecified() const {
|
---|
| 239 | return this == &UNSPECIFIED;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /// link identifiers
|
---|
| 243 | LinkID localLink;
|
---|
| 244 | LinkID remoteLink;
|
---|
| 245 |
|
---|
| 246 | /// used underlay addresses for the link
|
---|
| 247 | const address_v* localLocator;
|
---|
| 248 | const address_v* remoteLocator;
|
---|
| 249 |
|
---|
| 250 | /// the remote end-point descriptor
|
---|
| 251 | EndpointDescriptor remoteEndpoint;
|
---|
| 252 |
|
---|
| 253 | /// flag, whether this link is up
|
---|
| 254 | bool up;
|
---|
| 255 | };
|
---|
| 256 |
|
---|
| 257 | /// Link management: list of links
|
---|
| 258 | typedef vector<LinkDescriptor*> LinkSet;
|
---|
| 259 |
|
---|
| 260 | /// Link management: the set of currently managed links
|
---|
| 261 | LinkSet linkSet;
|
---|
| 262 |
|
---|
| 263 | /// Link management: add a link
|
---|
| 264 | void addLink( LinkDescriptor* link );
|
---|
| 265 |
|
---|
| 266 | /// Link management: remove a link
|
---|
| 267 | void removeLink(const LinkID& localLink);
|
---|
| 268 |
|
---|
| 269 | /// Link management: get link information using the local link
|
---|
| 270 | LinkDescriptor& queryLocalLink(const LinkID& localLink) const;
|
---|
| 271 |
|
---|
| 272 | /// Link management: get link information using the remote link
|
---|
| 273 | LinkDescriptor& queryRemoteLink(const LinkID& remoteLink) const;
|
---|
| 274 |
|
---|
| 275 | /// The local end-point descriptor
|
---|
| 276 | EndpointDescriptor localDescriptor;
|
---|
| 277 |
|
---|
| 278 | #ifndef UNDERLAY_OMNET
|
---|
| 279 | /// network change detector
|
---|
| 280 | NetworkChangeDetection networkMonitor;
|
---|
| 281 | #endif
|
---|
| 282 | /// event listener
|
---|
| 283 | typedef set<CommunicationEvents*> EventListenerSet;
|
---|
| 284 | EventListenerSet eventListener;
|
---|
| 285 |
|
---|
| 286 | /// sequence numbers
|
---|
| 287 | seqnum_t currentSeqnum;
|
---|
| 288 |
|
---|
| 289 | /// transport peer
|
---|
| 290 | transport_peer* transport;
|
---|
| 291 |
|
---|
| 292 | /// the base overlay message receiver
|
---|
| 293 | MessageReceiver* messageReceiver;
|
---|
| 294 |
|
---|
| 295 | /// convenience: send message to peer
|
---|
| 296 | void send( Message* message, const EndpointDescriptor& endpoint );
|
---|
| 297 | void send( Message* message, const LinkDescriptor& descriptor );
|
---|
| 298 |
|
---|
| 299 | /// state of the base communication
|
---|
| 300 | bool started;
|
---|
| 301 |
|
---|
| 302 | };
|
---|
| 303 |
|
---|
| 304 | }} // namespace ariba, communication
|
---|
| 305 |
|
---|
| 306 | #endif /* BASECOMMUNICATION_H_ */
|
---|