f7f61a55d855cadce6e938ca2d39ee61eded5eb5
[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_SocketSink_
27 #define HH_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         
60         void operator()(Handle handle, Packet packet);
61                                         ///< Write \a packet to \a handle
62                                         /**< Write the complete \a packet as a datagram to \a
63                                              handle.
64                                              \param[in] handle Handle to write data to
65                                              \param[in] packet Packet to write */
66     };
67
68     class IPv6SourceForcingDgramWriter
69     {
70     public:
71         IPv6SourceForcingDgramWriter();
72         IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr);
73         typedef senf::ClientSocketHandle<
74             senf::MakeSocketPolicy< senf::WriteablePolicy,
75                                     senf::DatagramFramingPolicy>::policy > Handle;
76                                         ///< Handle type supported by this writer
77         
78         void source(senf::INet6Address & source);
79         senf::INet6Address source();
80         void destination(senf::INet6SocketAddress & dest);
81         senf::INet6SocketAddress destination();
82
83         void operator()(Handle handle, Packet packet);
84                                         ///< Write \a packet to \a handle
85                                         /**< Write the complete \a packet as a datagram to \a
86                                              handle.
87                                              \param[in] handle Handle to write data to
88                                              \param[in] packet Packet to write */
89     private:
90         int sendtoandfrom(int sock, const void *data, size_t dataLen, const in6_addr *dst, int port, const in6_addr *src);
91         senf::INet6Address source_;
92         senf::INet6Address destination_;
93         unsigned int protocolId_;
94 };    
95     
96     
97 }}
98
99 namespace senf {
100 namespace ppi {
101 namespace module {
102
103     /** \brief Output %module writing data to a FileHandle using the provided Writer.
104         If using the default ConnectedDgramWriter the filehandle must be writable, connected and 
105         able to handle complete datagrams.  
106         
107         This output %module will write data to a FileHandle object using a given \a Writer. This
108         output %module is active. This requires the file handle to be able to signal its readiness to
109         accept more data via the Scheduler.
110
111         The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
112         the file handle.
113
114         A \a Writer must fulfill the following interface:
115         \code
116           class SomeWriter
117           {
118           public:
119               typedef unspecified Handle;                          // type of handle requested
120
121               SomeWriter();                                        // EITHER default constructible OR
122               SomeWriter(SomeWriter const & other);                // copy constructible
123
124               void operator()(Handle handle, Packet packet);       // insertion function
125           };
126         \endcode
127         Whenever a packet is received for sending, the \a Writer's \c operator() is called.
128
129         \ingroup io_modules
130      */
131     template <class Writer=ConnectedDgramWriter>
132     class ActiveSocketSink : public Module
133     {
134         SENF_PPI_MODULE(ActiveSocketSink);
135
136     public:
137         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
138
139         connector::ActiveInput<> input; ///< Input connector from which data is received
140         
141         ActiveSocketSink(Handle handle); ///< Create new writer for the given handle
142                                         /**< Data will be written to \a handle using \a Writer.
143                                              \pre Requires \a Writer to be default constructible
144                                              \param[in] handle Handle to write data to */
145         ActiveSocketSink(Handle handle, Writer const & writer); 
146                                         ///< Create new writer for the given handle
147                                         /**< Data will be written to \a handle using \a Writer.
148                                              \pre Requires \a Writer to be copy constructible
149                                              \param[in] handle Handle to write data to 
150                                              \param[in] writer Writer helper writing packet date to the
151                                                  socket */
152
153         Writer & writer();                  ///< Access the Writer
154     private:
155         void write();
156
157         Handle handle_;
158         IOEvent event_;
159         Writer writer_;
160     };
161
162     /** \brief Output module writing data to a FileHandle using the provided \a Writer.
163         If using the default ConnectedDgramWriter the filehandle must be writable, connected and 
164         able to handle complete datagrams.  
165         
166         This output module will write data to a FileHandle object using a given \a Writer. This
167         output module is passive. This implies, that the output handle may not block. This also
168         implies, that data will probably get lost if written to fast for the underlying transport
169         mechanism. Either this is desired (like for a UDP socket) or some additional bandwidth
170         shaping needs to be used.
171
172         The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
173         the file handle.
174
175         The \a Writer must fulfill the following interface:
176         \code
177           class SomeWriter
178           {
179           public:
180               typedef unspecified Handle;                          // type of handle requested
181
182               SomeWriter();                                          // EITHER default constructible
183               SomeWriter(SomeWriter const & other);                    // OR copy constructible
184
185               void operator()(Handle handle, Packet packet);       // insertion function
186           };
187         \endcode
188         Whenever a packet is received for sending, the \a Writer's \c operator() is called.
189
190         \ingroup io_modules
191      */
192     template <class Writer=ConnectedDgramWriter>
193     class PassiveSocketSink : public Module
194     {
195         SENF_PPI_MODULE(PassiveSocketSink);
196
197     public:
198         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
199
200         connector::PassiveInput<> input; ///< Input connector from which data is received
201         
202         PassiveSocketSink(Handle handle); ///< Create new writer for the given handle
203                                         /**< Data will be written to \a handle using \a Writer.
204                                              \pre Requires \a Writer to be default constructible
205                                              \param[in] handle Handle to write data to */
206         PassiveSocketSink(Handle handle, Writer const & writer);
207                                         ///< Create new writer for the given handle
208                                         /**< Data will be written to \a handle using \a Writer.
209                                              \pre Requires \a Writer to be copy constructible
210                                              \param[in] handle Handle to write data to */
211
212         Writer & writer();      ///< Access the Writer
213         Handle & handle();      /**< Access the handle. This is intendet to be mainly used to reconnect 
214                                      the underlying socket. */
215        /* void reconnect(senf::SocketAddress newAddress);
216         ///< Reconnect the handle to which the packets are written
217        */
218         void replaceHandle(Handle newHandle);
219                                         /**< Replace the handle to which the packets are written
220                                          * Normally you should access the handle and call connect with
221                                          * the new address. This also works for other 
222                                          * (active) ConnectedSocketSinks/Sources */
223                                         
224     private:
225         void write();
226
227         Handle handle_;
228         Writer writer_;
229     };
230
231 }}}
232
233
234 ///////////////////////////////hh.e////////////////////////////////////////
235 #include "SocketSink.cci"
236 #include "SocketSink.ct"
237 #include "SocketSink.cti"
238 #endif
239
240 \f
241 // Local Variables:
242 // mode: c++
243 // fill-column: 100
244 // c-file-style: "senf"
245 // indent-tabs-mode: nil
246 // ispell-local-dictionary: "american"
247 // compile-command: "scons -u test"
248 // comment-column: 40
249 // End: