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