Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / 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 <senf/Packets/Packets.hh>
31 #include <senf/Socket/ClientSocketHandle.hh>
32 #include <senf/Socket/SocketPolicy.hh>
33 #include <senf/Socket/ReadWritePolicy.hh>
34 #include <senf/Socket/FramingPolicy.hh>
35 #include <senf/Socket/CommunicationPolicy.hh>
36 #include "Module.hh"
37 #include "Connectors.hh"
38 #include <senf/Socket/Protocols/INet/INetAddressing.hh>
39 #include "IOEvent.hh"
40
41 //#include "SocketSink.mpp"
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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         bool 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         bool 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         bool 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         bool 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               bool 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 const & handle);
214                                         ///< Create new writer for the given handle
215                                         /**< Data will be written to \a handle using \a Writer.
216                                              \pre Requires \a Writer to be default constructible
217                                              \param[in] handle Handle to write data to */
218         ActiveSocketSink(Handle const & handle, Writer const & writer);
219                                         ///< Create new writer for the given handle
220                                         /**< Data will be written to \a handle using \a Writer.
221                                              \pre Requires \a Writer to be copy constructible
222                                              \param[in] handle Handle to write data to
223                                              \param[in] writer Writer helper writing packet date to
224                                                  the socket */
225
226         Writer & writer();              ///< Access the Writer
227         Handle handle();                ///< Access handle
228         void handle(Handle const & handle);
229                                         ///< Set handle
230                                         /**< Assigning an empty or in-valid() handle will disable
231                                              the module until a new. valid handle is assigned. */
232
233     private:
234         void write();
235
236         Handle handle_;
237         IOEvent event_;
238         Writer writer_;
239     };
240
241     /** \brief Output module writing data to a FileHandle using the provided \a Writer.
242         If using the default ConnectedDgramWriter the filehandle must be writable, connected and
243         able to handle complete datagrams.
244
245         This output module will write data to a FileHandle object using a given \a Writer. This
246         output module is passive. This implies, that <em>the output handle may not block</em>. This
247         also implies, that data will probably get lost if written to fast for the underlying
248         transport mechanism. Either this is desired (like for a UDP socket) or some additional
249         bandwidth shaping needs to be used.
250
251         The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
252         the file handle.
253
254         The \a Writer must fulfill the following interface:
255         \code
256           class SomeWriter
257           {
258           public:
259               typedef unspecified Handle;                          // type of handle requested
260               typedef unspecified_type PacketType                  // type of packet read
261
262               SomeWriter();                                          // EITHER default constructible
263               SomeWriter(SomeWriter const & other);                    // OR copy constructible
264
265               bool operator()(Handle handle, Packet packet);       // insertion function
266           };
267         \endcode
268         Whenever a packet is received for sending, the \a Writer's \c operator() is called.
269
270         \ingroup io_modules
271      */
272     template <class Writer=ConnectedDgramWriter>
273     class PassiveSocketSink : public Module
274     {
275         SENF_PPI_MODULE(PassiveSocketSink);
276
277     public:
278         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
279
280         connector::PassiveInput<typename Writer::PacketType> input; ///< Input connector from which data is received
281
282         PassiveSocketSink();            ///< Create non-connected writer
283                                         /**< The writer will be disabled until a socket is set
284                                              \pre Requires \a Writer to be default constructible */
285         explicit PassiveSocketSink(Writer const & writer);
286                                         ///< Create non-connected writer
287                                         /**< The writer will be disabled until a socket is set
288                                              \pre Requires \a Writer to be copy constructible
289                                              \param[in] writer Writer helper writing packet date to
290                                                  the socket */
291         explicit PassiveSocketSink(Handle const & handle);
292                                         ///< Create new writer for the given handle
293                                         /**< Data will be written to \a handle using \a Writer.
294                                              \pre Requires \a Writer to be default constructible
295                                              \param[in] handle Handle to write data to */
296         PassiveSocketSink(Handle const & handle, Writer const & writer);
297                                         ///< Create new writer for the given handle
298                                         /**< Data will be written to \a handle using \a Writer.
299                                              \pre Requires \a Writer to be copy constructible
300                                              \param[in] handle Handle to write data to
301                                              \param[in] writer Writer helper writing packet date to
302                                                  the socket */
303
304         Writer & writer();              ///< Access the Writer
305         Handle & handle();              ///< Access handle
306         void handle(Handle const & handle);
307                                         ///< Set handle
308                                         /**< Assigning an empty or in-valid() handle will disable
309                                              the module until a new valid handle is assigned. */
310
311 #ifndef DOXYGEN
312         void replaceHandle(Handle newHandle);
313 #endif
314
315     private:
316         void write();
317         void checkThrottle();
318
319         Handle handle_;
320         Writer writer_;
321     };
322
323 }}}
324
325
326 //-/////////////////////////////////////////////////////////////////////////////////////////////////
327 #include "SocketSink.cci"
328 #include "SocketSink.ct"
329 #include "SocketSink.cti"
330 #endif
331
332 \f
333 // Local Variables:
334 // mode: c++
335 // fill-column: 100
336 // c-file-style: "senf"
337 // indent-tabs-mode: nil
338 // ispell-local-dictionary: "american"
339 // compile-command: "scons -u test"
340 // comment-column: 40
341 // End: