185aadefc6d187f1cb595d23412bd548c8742467
[senf.git] / senf / PPI / SocketSource.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 SocketSource public header */
25
26 #ifndef HH_SENF_PPI_SocketSource_
27 #define HH_SENF_PPI_SocketSource_ 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 "Module.hh"
36 #include "Connectors.hh"
37 #include "IOEvent.hh"
38
39 //#include "SocketSource.mpp"
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace senf {
43 namespace ppi {
44
45     /** \brief Reader for module::ActiveSocketSource
46
47         This read helper will read a datagram from a datagram socket. This datagram will then be
48         interpreted as a packet of type \a Packet as defined in the packet library. \a Packet
49         defaults to DataPacket (type DataPacketType), which will place the data uninterpreted
50         into a packet data structure.
51      */
52     template <class Packet=DataPacket, unsigned MaxSize=0u>
53     class DgramReader
54     {
55     public:
56         typedef Packet PacketType;
57         typedef senf::ClientSocketHandle<
58             senf::MakeSocketPolicy< senf::ReadablePolicy,
59                                     senf::DatagramFramingPolicy >::policy > Handle;
60                                         ///< Handle type supported by this reader
61
62         Packet operator()(Handle & handle);
63                                         ///< Read packet from \a handle
64                                         /**< Read a datagram from \a handle and interpret is as
65                                              packet of type \c Packet.
66                                              \param[in] handle Handle to read data from
67                                              \returns Pointer to new packet instance or 0, if no
68                                                  packet could be read */
69     };
70
71 }}
72
73 namespace senf {
74 namespace ppi {
75 namespace module {
76
77     /** \brief Input module reading data from an arbitrary FileHandle
78
79         This input module will read data from a FileHandle object and parse the data according to
80         the \a Reader. The default reader is senf::ppi::DgramReader <> which reads the data into a
81         senf::DataPacket. To parse the data according to some other packet type, pass that packet
82         type to senf::ppi::DgramReader:
83         \code
84         senf::ppi::module::ActiveSocketSource< senf::ppi::DgramReader<senf::EthernetPacket> > source;
85         \endcode
86         declares a \a reader module reading senf::EthernetPacket's
87
88         A \a Reader must fulfill the following interface:
89         \code
90         class SomeReader
91         {
92         public:
93             typedef unspecified_type Handle;                       // type of handle requested
94             typedef unspecified_type PacketType                    // type of packet returned
95
96             SomeReader();                                          // EITHER default constructible
97             SomeReader(SomeReader const & other);                  // OR copy constructible
98
99             PacketType operator()(Handle handle);                  // extraction function
100         };
101         \endcode
102         Whenever the FileHandle object is ready for reading, the \a Reader's \c operator() is called
103         to read a packet.
104
105         \ingroup io_modules
106      */
107     template <class Reader=DgramReader<> >
108     class ActiveSocketSource
109         : public Module
110     {
111         SENF_PPI_MODULE(ActiveSocketSource);
112
113     public:
114         typedef typename Reader::Handle Handle; ///< Handle type requested by the reader
115
116         connector::ActiveOutput<typename Reader::PacketType> output;
117                                         ///< Output connector to which the data received is written
118
119         ActiveSocketSource();           ///< Create non-connected reader
120                                         /**< The reader will be disabled until a socket is set
121                                              \pre Requires \a Reader to be default constructible */
122         explicit ActiveSocketSource(Reader reader); ///< Create non-connected reader
123                                         /**< The reader will be disabled until a socket is set
124                                              \pre Requires \a Reader to be copy constructible */
125         explicit ActiveSocketSource(Handle const & handle);
126                                         ///< Create new reader for the given handle
127                                         /**< Data will be read from \a handle and be parsed by \a
128                                              Reader.
129                                              \pre Requires \a Reader to be default constructible
130                                              \param[in] handle Handle to read data from */
131         ActiveSocketSource(Handle const & handle, Reader reader);
132                                         ///< Create new reader for the given handle
133                                         /**< Data will be read from \a handle and be parsed by \a
134                                              Reader.
135                                              \pre Requires \a Reader to be copy constructible
136                                              \param[in] handle Handle to read data from */
137
138         Reader & reader();              ///< Access Reader helper
139         Handle handle();                ///< Access handle
140         void handle(Handle const & handle);
141                                         ///< Set handle
142                                         /**< Assigning an empty or in-valid() handle will disable
143                                              the module until a new, valid handle is assigned. */
144     private:
145         Handle handle_;
146         IOEvent event_;
147         Reader reader_;
148
149         void read();
150     };
151
152
153     template <class Reader=DgramReader<> >
154     class ActiveBurstSocketSource
155         : public Module
156     {
157         SENF_PPI_MODULE(ActiveBurstSocketSource);
158
159     public:
160         typedef typename Reader::Handle Handle; ///< Handle type requested by the reader
161
162         connector::ActiveOutput<typename Reader::PacketType> output;
163                                         ///< Output connector to which the data received is written
164
165         ActiveBurstSocketSource(unsigned max_burst=0);
166         explicit ActiveBurstSocketSource(Reader reader, unsigned max_burst=0);
167         explicit ActiveBurstSocketSource(Handle const & handle, unsigned max_burst=0);
168         ActiveBurstSocketSource(Handle const & handle, Reader reader, unsigned max_burst=0);
169
170         Reader & reader();              ///< Access Reader helper
171         Handle handle();                ///< Access handle
172         void handle(Handle const & handle);
173                                         ///< Set handle
174                                         /**< Assigning an empty or in-valid() handle will disable
175                                              the module until a new, valid handle is assigned. */
176
177         unsigned maxBurst() const;
178         void maxBurst(unsigned max_burst);
179
180     private:
181         Handle handle_;
182         IOEvent event_;
183         Reader reader_;
184         unsigned maxBurst_;
185
186         void read();
187     };
188
189 }}}
190
191 //-/////////////////////////////////////////////////////////////////////////////////////////////////
192 //#include "SocketSource.cci"
193 #include "SocketSource.ct"
194 #include "SocketSource.cti"
195 #endif
196
197 \f
198 // Local Variables:
199 // mode: c++
200 // fill-column: 100
201 // c-file-style: "senf"
202 // indent-tabs-mode: nil
203 // ispell-local-dictionary: "american"
204 // compile-command: "scons -u test"
205 // comment-column: 40
206 // End: