Fix SCons 1.2.0 build failure
[senf.git] / PPI / SocketSink.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief SocketSink public header */
25
26 #ifndef HH_SENF_PPI_SocketSink_
27 #define HH_SENF_PPI_SocketSink_ 1
28
29 // Custom includes
30 #include "../Packets/Packets.hh"
31 #include "../Socket/ClientSocketHandle.hh"
32 #include "../Socket/SocketPolicy.hh"
33 #include "../Socket/ReadWritePolicy.hh"
34 #include "../Socket/FramingPolicy.hh"
35 #include "../Socket/CommunicationPolicy.hh"
36 #include "Module.hh"
37 #include "Connectors.hh"
38 #include "../Socket/Protocols/INet/INetAddressing.hh"
39 #include "IOEvent.hh"
40
41 //#include "SocketSink.mpp"
42 ///////////////////////////////hh.p////////////////////////////////////////
43
44 namespace senf {
45 namespace ppi {
46
47     /** \brief Writer for module::ActiveSocketSink / module::PassiveSocketSink
48
49         This writer will write the packets completely as datagrams to the given socket which must be connected.
50      */
51     class ConnectedDgramWriter
52     {
53     public:
54         typedef senf::ClientSocketHandle<
55             senf::MakeSocketPolicy< senf::WriteablePolicy,
56                                     senf::DatagramFramingPolicy,
57                                     senf::ConnectedCommunicationPolicy>::policy > Handle;
58                                         ///< Handle type supported by this writer
59         typedef Packet PacketType;
60
61         void operator()(Handle handle, Packet const & packet);
62                                         ///< Write \a packet to \a handle
63                                         /**< Write the complete \a packet as a datagram to \a
64                                              handle.
65                                              \param[in] handle Handle to write data to
66                                              \param[in] packet Packet to write */
67     };
68
69     /** \brief Writer sending data with ClientSocketHandle::writeto()
70
71         This writer will send out data using ClientSocketHandle::writeto(). The target address can
72         be specified in the writer constructor and can be adjusted at any time.
73
74         If no target address is set, incoming data will be <em>silently dropped</em>.
75      */
76     template <class HandleType>
77     class TargetDgramWriter
78     {
79     public:
80         typedef HandleType Handle;
81         typedef Packet PacketType;
82
83         TargetDgramWriter();            ///< Create TargetDgramWriter with unset target address
84         TargetDgramWriter(typename Handle::Address const & target);
85                                         ///< Create TargetDgramWriter sending to \a target
86
87         typename Handle::Address target() const; ///< Get current target address
88         void target(typename Handle::Address const & target); ///< Set target address
89
90         void operator()(Handle handle, Packet const & packet); ///< Write \a packet to \a handle
91                                         /**< Write the complete \a packet as a datagram to \a
92                                              handle.
93                                              \param[in] handle Handle to write data to
94                                              \param[in] packet Packet to write */
95
96     private:
97         typename Handle::Address target_;
98     };
99
100     class IPv4SourceForcingDgramWriter : ConnectedDgramWriter
101     {
102     public:
103         IPv4SourceForcingDgramWriter();
104         IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr);
105         typedef senf::ClientSocketHandle<
106             senf::MakeSocketPolicy< senf::WriteablePolicy,
107                                     senf::DatagramFramingPolicy>::policy > Handle;
108                                         ///< Handle type supported by this writer
109         typedef Packet PacketType;
110
111         void source(senf::INet4Address & source);
112         senf::INet4Address source();
113         void destination(senf::INet4SocketAddress & dest);
114         senf::INet4SocketAddress destination();
115
116         void operator()(Handle handle, Packet const & packet);
117                                         ///< Write \a packet to \a handle
118                                         /**< Write the complete \a packet as a datagram to \a
119                                              handle.
120                                              \param[in] handle Handle to write data to
121                                              \param[in] packet Packet to write */
122     private:
123         int sendtoandfrom(int sock, const void *data, size_t dataLen, const in_addr *dst, int port, const in_addr *src);
124         senf::INet4Address source_;
125         senf::INet4Address destination_;
126         unsigned int protocolId_;
127     };
128
129     class IPv6SourceForcingDgramWriter : ConnectedDgramWriter
130     {
131     public:
132         IPv6SourceForcingDgramWriter();
133         IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr);
134         typedef senf::ClientSocketHandle<
135             senf::MakeSocketPolicy< senf::WriteablePolicy,
136                                     senf::DatagramFramingPolicy>::policy > Handle;
137                                         ///< Handle type supported by this writer
138         typedef Packet PacketType;
139
140         void source(senf::INet6Address & source);
141         senf::INet6Address source();
142         void destination(senf::INet6SocketAddress & dest);
143         senf::INet6SocketAddress destination();
144
145         void operator()(Handle handle, Packet const & packet);
146                                         ///< Write \a packet to \a handle
147                                         /**< Write the complete \a packet as a datagram to \a
148                                              handle.
149                                              \param[in] handle Handle to write data to
150                                              \param[in] packet Packet to write */
151     private:
152         int sendtoandfrom(int sock, const void *data, size_t dataLen, const in6_addr *dst, int port, const in6_addr *src);
153         senf::INet6Address source_;
154         senf::INet6Address destination_;
155         unsigned int protocolId_;
156 };
157
158
159 }}
160
161 namespace senf {
162 namespace ppi {
163 namespace module {
164
165     /** \brief Output %module writing data to a FileHandle using the provided Writer.
166         If using the default ConnectedDgramWriter the filehandle must be writable, connected and
167         able to handle complete datagrams.
168
169         This output %module will write data to a FileHandle object using a given \a Writer. This
170         output %module is active. This requires the file handle to be able to signal its readiness to
171         accept more data via the Scheduler.
172
173         The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
174         the file handle.
175
176         A \a Writer must fulfill the following interface:
177         \code
178           class SomeWriter
179           {
180           public:
181               typedef unspecified Handle;                          // type of handle requested
182               typedef unspecified_type PacketType                  // type of packet read
183
184               SomeWriter();                                        // EITHER default constructible OR
185               SomeWriter(SomeWriter const & other);                // copy constructible
186
187               void operator()(Handle handle, Packet packet);       // insertion function
188           };
189         \endcode
190         Whenever a packet is received for sending, the \a Writer's \c operator() is called.
191
192         \ingroup io_modules
193      */
194     template <class Writer=ConnectedDgramWriter>
195     class ActiveSocketSink : public Module
196     {
197         SENF_PPI_MODULE(ActiveSocketSink);
198
199     public:
200         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
201
202         connector::ActiveInput<typename Writer::PacketType> input; ///< Input connector from which data is received
203
204         ActiveSocketSink();             ///< Create non-connected writer
205                                         /**< The writer will be disabled until a socket is set
206                                              \pre Requires \a Writer to be default constructible */
207         explicit ActiveSocketSink(Writer const & writer);
208                                         ///< Create non-connected writer
209                                         /**< The writer will be disabled until a socket is set
210                                              \pre Requires \a Writer to be copy constructible
211                                              \param[in] writer Writer helper writing packet date to
212                                                  the socket */
213         explicit ActiveSocketSink(Handle handle); ///< Create new writer for the given handle
214                                         /**< Data will be written to \a handle using \a Writer.
215                                              \pre Requires \a Writer to be default constructible
216                                              \param[in] handle Handle to write data to */
217         ActiveSocketSink(Handle handle, Writer const & writer);
218                                         ///< Create new writer for the given handle
219                                         /**< Data will be written to \a handle using \a Writer.
220                                              \pre Requires \a Writer to be copy constructible
221                                              \param[in] handle Handle to write data to
222                                              \param[in] writer Writer helper writing packet date to
223                                                  the socket */
224
225         Writer & writer();              ///< Access the Writer
226         Handle handle();                ///< Access handle
227         void handle(Handle handle);     ///< Set handle
228                                         /**< Assigning an empty or in-valid() handle will disable
229                                              the module until a new. valid handle is assigned. */
230
231     private:
232         void write();
233
234         Handle handle_;
235         IOEvent event_;
236         Writer writer_;
237     };
238
239     /** \brief Output module writing data to a FileHandle using the provided \a Writer.
240         If using the default ConnectedDgramWriter the filehandle must be writable, connected and
241         able to handle complete datagrams.
242
243         This output module will write data to a FileHandle object using a given \a Writer. This
244         output module is passive. This implies, that the output handle may not block. This also
245         implies, that data will probably get lost if written to fast for the underlying transport
246         mechanism. Either this is desired (like for a UDP socket) or some additional bandwidth
247         shaping needs to be used.
248
249         The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
250         the file handle.
251
252         The \a Writer must fulfill the following interface:
253         \code
254           class SomeWriter
255           {
256           public:
257               typedef unspecified Handle;                          // type of handle requested
258               typedef unspecified_type PacketType                  // type of packet read
259
260               SomeWriter();                                          // EITHER default constructible
261               SomeWriter(SomeWriter const & other);                    // OR copy constructible
262
263               void operator()(Handle handle, Packet packet);       // insertion function
264           };
265         \endcode
266         Whenever a packet is received for sending, the \a Writer's \c operator() is called.
267
268         \ingroup io_modules
269      */
270     template <class Writer=ConnectedDgramWriter>
271     class PassiveSocketSink : public Module
272     {
273         SENF_PPI_MODULE(PassiveSocketSink);
274
275     public:
276         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
277
278         connector::PassiveInput<typename Writer::PacketType> input; ///< Input connector from which data is received
279
280         PassiveSocketSink();            ///< Create non-connected writer
281                                         /**< The writer will be disabled until a socket is set
282                                              \pre Requires \a Writer to be default constructible */
283         explicit PassiveSocketSink(Writer const & writer);
284                                         ///< Create non-connected writer
285                                         /**< The writer will be disabled until a socket is set
286                                              \pre Requires \a Writer to be copy constructible
287                                              \param[in] writer Writer helper writing packet date to
288                                                  the socket */
289         explicit PassiveSocketSink(Handle handle); ///< Create new writer for the given handle
290                                         /**< Data will be written to \a handle using \a Writer.
291                                              \pre Requires \a Writer to be default constructible
292                                              \param[in] handle Handle to write data to */
293         PassiveSocketSink(Handle handle, Writer const & writer);
294                                         ///< Create new writer for the given handle
295                                         /**< Data will be written to \a handle using \a Writer.
296                                              \pre Requires \a Writer to be copy constructible
297                                              \param[in] handle Handle to write data to
298                                              \param[in] writer Writer helper writing packet date to
299                                                  the socket */
300
301         Writer & writer();              ///< Access the Writer
302         Handle & handle();              ///< Access handle
303         void handle(Handle handle);     ///< Set handle
304                                         /**< Assigning an empty or in-valid() handle will disable
305                                              the module until a new. valid handle is assigned. */
306
307 #ifndef DOXYGEN
308         void replaceHandle(Handle newHandle);
309 #endif
310
311     private:
312         void write();
313         void checkThrottle();
314
315         Handle handle_;
316         Writer writer_;
317     };
318
319 }}}
320
321
322 ///////////////////////////////hh.e////////////////////////////////////////
323 #include "SocketSink.cci"
324 #include "SocketSink.ct"
325 #include "SocketSink.cti"
326 #endif
327
328
329 // Local Variables:
330 // mode: c++
331 // fill-column: 100
332 // c-file-style: "senf"
333 // indent-tabs-mode: nil
334 // ispell-local-dictionary: "american"
335 // compile-command: "scons -u test"
336 // comment-column: 40
337 // End: