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