source: source/ariba/communication/BaseCommunication.h@ 5757

Last change on this file since 5757 was 5624, checked in by Christoph Mayer, 15 years ago

maemo segfault wegen static object ctors workaround

File size: 9.6 KB
Line 
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
96namespace ariba {
97 class SideportListener;
98}
99
100namespace ariba {
101namespace communication {
102
103using namespace std;
104using namespace ariba::addressing;
105using namespace ariba::transport;
106using namespace ariba::utility;
107
108// use base ariba types (clarifies multiple definitions)
109using ariba::utility::Message;
110using 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 */
120class BaseCommunication:
121 public NetworkChangeInterface,
122 public SystemEventListener,
123 public transport_listener {
124
125 use_logging_h(BaseCommunication);
126 friend class ariba::SideportListener;
127
128public:
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
204public:
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
214protected:
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
224private:
225 /**
226 * A link descriptor consisting of the end-point descriptor and currently
227 * used underlay address.
228 */
229 class LinkDescriptor {
230 public:
231
232 /// default constructor
233 LinkDescriptor() :
234 localLink(LinkID::UNSPECIFIED), localLocator(NULL),
235 remoteLink(LinkID::UNSPECIFIED), remoteLocator(NULL),
236 up(false) {
237 }
238
239 ~LinkDescriptor() {
240 if (localLocator!=NULL) delete localLocator;
241 if (remoteLocator!=NULL) delete remoteLocator;
242 }
243
244 bool isUnspecified() const {
245 return (this == &UNSPECIFIED());
246 }
247
248 static LinkDescriptor& UNSPECIFIED(){
249 static LinkDescriptor* unspec = NULL;
250 if(unspec == NULL) unspec = new LinkDescriptor();
251 return *unspec;
252 }
253
254 bool unspecified;
255
256 /// link identifiers
257 LinkID localLink;
258 LinkID remoteLink;
259
260 /// used underlay addresses for the link
261 const address_v* localLocator;
262 const address_v* remoteLocator;
263
264 /// the remote end-point descriptor
265 EndpointDescriptor remoteEndpoint;
266
267 /// flag, whether this link is up
268 bool up;
269 };
270
271 /// Link management: list of links
272 typedef vector<LinkDescriptor*> LinkSet;
273
274 /// Link management: the set of currently managed links
275 LinkSet linkSet;
276
277 /// Link management: add a link
278 void addLink( LinkDescriptor* link );
279
280 /// Link management: remove a link
281 void removeLink(const LinkID& localLink);
282
283 /// Link management: get link information using the local link
284 LinkDescriptor& queryLocalLink(const LinkID& localLink) const;
285
286 /// Link management: get link information using the remote link
287 LinkDescriptor& queryRemoteLink(const LinkID& remoteLink) const;
288
289 /// The local end-point descriptor
290 EndpointDescriptor localDescriptor;
291
292#ifndef UNDERLAY_OMNET
293 /// network change detector
294 NetworkChangeDetection networkMonitor;
295#endif
296 /// list of all remote addresses of links to end-points
297 class endpoint_reference {
298 public:
299 int count; ///< the number of open links to this end-point
300 const address_v* endpoint; ///< the end-point itself
301 };
302 vector<endpoint_reference> remote_endpoints;
303
304 /// adds an end-point to the list
305 void add_endpoint( const address_v* endpoint );
306
307 /// removes an end-point from the list
308 void remove_endpoint( const address_v* endpoint );
309
310 /// event listener
311 typedef set<CommunicationEvents*> EventListenerSet;
312 EventListenerSet eventListener;
313
314 /// sequence numbers
315 seqnum_t currentSeqnum;
316
317 /// transport peer
318 transport_peer* transport;
319
320 /// the base overlay message receiver
321 MessageReceiver* messageReceiver;
322
323 /// convenience: send message to peer
324 void send( Message* message, const EndpointDescriptor& endpoint );
325 void send( Message* message, const LinkDescriptor& descriptor );
326
327
328
329 /// state of the base communication
330 bool started;
331
332};
333
334}} // namespace ariba, communication
335
336#endif /* BASECOMMUNICATION_H_ */
Note: See TracBrowser for help on using the repository browser.