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