b101a6d55de9d3ad2d24210261918de2550d1509
[senf.git] / PPI / SocketSink.hh
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief SocketSink public header */
23
24 #ifndef HH_SocketSink_
25 #define HH_SocketSink_ 1
26
27 // Custom includes
28 #include "../Packets/Packets.hh"
29 #include "../Socket/ClientSocketHandle.hh"
30 #include "../Socket/SocketPolicy.hh"
31 #include "../Socket/ReadWritePolicy.hh"
32 #include "../Socket/FramingPolicy.hh"
33 #include "../Socket/CommunicationPolicy.hh"
34 #include "Module.hh"
35 #include "Connectors.hh"
36
37 //#include "SocketSink.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace ppi {
42
43     /** \brief Write helper for module::ActiveSocketSink / module::PassiveSocketSink
44         
45         This write helper will write the packets completely as datagrams to the given socket.
46      */
47     class PacketSink
48     {
49     public:
50         typedef senf::ClientSocketHandle<
51             senf::MakeSocketPolicy< senf::WriteablePolicy,
52                                     senf::DatagramFramingPolicy,
53                                     senf::ConnectedCommunicationPolicy>::policy > Handle;
54                                         ///< Handle type supported by this writer
55         
56         void operator()(Handle handle, Packet packet);
57                                         ///< Write \a packet to \a handle
58                                         /**< Write the complete \a packet as a datagram to \a
59                                              handle.
60                                              \param[in] handle Handle to write data to
61                                              \param[in] packet Packet to write */
62     };
63
64 }}
65
66 namespace senf {
67 namespace ppi {
68 namespace module {
69
70     /** \brief Output module writing data to arbitrary FileHandle
71         
72         This output module will write data to a FileHandle object using a given \a Sink. This
73         output module is active. This requires the file handle to be able to signal its readiness to
74         accept more data via the Scheduler.
75
76         The default \a Sink is senf::ppi::PacketSink which will write out the complete packet to
77         the file handle.
78
79         A \a Sink must fulfill the following interface:
80         \code
81           class SomeSink
82           {
83           public:
84               typedef unspecified Handle;                          // type of handle requested
85
86               SomeSink();                                          // EITHER default constructible OR
87               SomeSink(SomeSink const & other);                    // copy constructible
88
89               void operator()(Handle handle, Packet packet);       // insertion function
90           };
91         \endcode
92         Whenever a packet is received for sending, the \a Sink's \c operator() is called.
93
94         \ingroup io_modules
95      */
96     template <class Sink=PacketSink>
97     class ActiveSocketSink : public Module
98     {
99         SENF_PPI_MODULE(ActiveSocketSink);
100
101     public:
102         typedef typename Sink::Handle Handle; ///< Handle type requested by writer
103
104         connector::ActiveInput input; ///< Input connector from which data is received
105         
106         ActiveSocketSink(Handle handle); ///< Create new writer for the given handle
107                                         /**< Data will be written to \a handle using \a Sink.
108                                              \pre Requires \a Sink to be default constructible
109                                              \param[in] handle Handle to write data to */
110         ActiveSocketSink(Handle handle, Sink const & sink); 
111                                         ///< Create new writer for the given handle
112                                         /**< Data will be written to \a handle using \a Sink.
113                                              \pre Requires \a Sink to be copy constructible
114                                              \param[in] handle Handle to write data to 
115                                              \param[in] sink Sink helper writing packet date to the
116                                                  socket */
117
118         Sink & sink();                  ///< Access the sink helper
119
120     private:
121         void write();
122
123         Handle handle_;
124         IOEvent event_;
125         Sink writer_;
126     };
127
128     /** \brief Output module writing data to arbitrary FileHandle
129         
130         This output module will write data to a FileHandle object using a given \a Sink. This
131         output module is passive. This implies, that the output handle may not block. This also
132         implies, that data will probably get lost if written to fast for the underlying transport
133         mechanism. Either this is desired (like for a UDP socket) or some additional bandwidth
134         shaping needs to be used.
135
136         The default \a Sink is senf::ppi::PacketSink which will write out the complete packet to
137         the file handle.
138
139         The \a Sink must fulfill the following interface:
140         \code
141           class SomeSink
142           {
143           public:
144               typedef unspecified Handle;                          // type of handle requested
145
146               SomeSink();                                          // EITHER default constructible
147               SomeSink(SomeSink const & other);                    // OR copy constructible
148
149               void operator()(Handle handle, Packet packet);       // insertion function
150           };
151         \endcode
152         Whenever a packet is received for sending, the \a Sink's \c operator() is called.
153
154         \ingroup io_modules
155      */
156     template <class Sink=PacketSink>
157     class PassiveSocketSink : public Module
158     {
159         SENF_PPI_MODULE(PassiveSocketSink);
160
161     public:
162         typedef typename Sink::Handle Handle; ///< Handle type requested by writer
163
164         connector::PassiveInput input; ///< Input connector from which data is received
165         
166         PassiveSocketSink(Handle handle); ///< Create new writer for the given handle
167                                         /**< Data will be written to \a handle using \a Sink.
168                                              \pre Requires \a Sink to be default constructible
169                                              \param[in] handle Handle to write data to */
170         PassiveSocketSink(Handle handle, Sink const & sink);
171                                         ///< Create new writer for the given handle
172                                         /**< Data will be written to \a handle using \a Sink.
173                                              \pre Requires \a Sink to be copy constructible
174                                              \param[in] handle Handle to write data to */
175
176         Sink & sink();                  ///< Access the sink helper
177
178     private:
179         void write();
180
181         Handle handle_;
182         Sink writer_;
183     };
184
185 }}}
186
187
188 ///////////////////////////////hh.e////////////////////////////////////////
189 #include "SocketSink.cci"
190 #include "SocketSink.ct"
191 #include "SocketSink.cti"
192 #endif
193
194 \f
195 // Local Variables:
196 // mode: c++
197 // fill-column: 100
198 // c-file-style: "senf"
199 // indent-tabs-mode: nil
200 // ispell-local-dictionary: "american"
201 // compile-command: "scons -u test"
202 // comment-column: 40
203 // End: