263f3493ad7503524e6f7c7e5e7172f90ba4e2d6
[senf.git] / PPI / SocketWriter.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 SocketWriter public header */
23
24 #ifndef HH_SocketWriter_
25 #define HH_SocketWriter_ 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 "SocketWriter.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace ppi {
42
43     /** \brief Write helper for module::ActiveSocketWriter / module::PassiveSocketWriter
44         
45         This write helper will write the packets completely as datagrams to the given socket.
46      */
47     class PacketWriter
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 Writer. 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 \a Writer must fulfill the following interface:
77         \code
78           class SomeWriter
79           {
80           public:
81               typedef unspecified Handle;                          // type of handle requested
82               SomeWriter();                                        // default constructible
83               void operator()(Handle handle, Packet packet);       // insertion function
84           };
85         \endcode
86      */
87     template <class Writer=PacketWriter>
88     class ActiveSocketWriter : public Module
89     {
90         SENF_PPI_MODULE(ActiveSocketWriter);
91
92     public:
93         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
94
95         connector::ActiveInput input; ///< Input connector from which data is received
96         
97         ActiveSocketWriter(Handle handle); ///< Create new writer for the given handle
98                                         /**< Data will be written to \a handle using \a Writer.
99                                              \param[in] handle Handle to write data to */
100     private:
101         void write();
102
103         Handle handle_;
104         IOEvent event_;
105         Writer writer_;
106     };
107
108     /** \brief Output module writing data to arbitrary FileHandle
109         
110         This output module will write data to a FileHandle object using a given \a Writer. This
111         output module is passive. This implies, that the output handle may not block. This also
112         implies, that data will probably get lost if written to fast for the underlying transport
113         mechanism. Either this is desired (like for a UDP socket) or some additional bandwidth
114         shaping needs to be used.
115
116         The \a Writer must fulfill the following interface:
117         \code
118           class SomeWriter
119           {
120           public:
121               typedef unspecified Handle;                          // type of handle requested
122               SomeWriter();                                        // default constructible
123               void operator()(Handle handle, Packet packet);       // insertion function
124           };
125         \endcode
126      */
127     template <class Writer=PacketWriter>
128     class PassiveSocketWriter : public Module
129     {
130         SENF_PPI_MODULE(PassiveSocketWriter);
131
132     public:
133         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
134
135         connector::PassiveInput input; ///< Input connector from which data is received
136         
137         PassiveSocketWriter(Handle handle); ///< Create new writer for the given handle
138                                         /**< Data will be written to \a handle using \a Writer.
139                                              \param[in] handle Handle to write data to */
140
141     private:
142         void write();
143
144         Handle handle_;
145         Writer writer_;
146     };
147
148 }}}
149
150
151 ///////////////////////////////hh.e////////////////////////////////////////
152 #include "SocketWriter.cci"
153 #include "SocketWriter.ct"
154 //#include "SocketWriter.cti"
155 #endif
156
157 \f
158 // Local Variables:
159 // mode: c++
160 // fill-column: 100
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"
165 // comment-column: 40
166 // End: