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