added a flush() member to ActiveBurstSocketSource
[senf.git] / senf / PPI / SocketSource.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief SocketSource public header */
30
31 #ifndef HH_SENF_PPI_SocketSource_
32 #define HH_SENF_PPI_SocketSource_ 1
33
34 // Custom includes
35 #include <senf/Packets/Packets.hh>
36 #include <senf/Socket/ClientSocketHandle.hh>
37 #include <senf/Socket/SocketPolicy.hh>
38 #include <senf/Socket/ReadWritePolicy.hh>
39 #include <senf/Socket/FramingPolicy.hh>
40 #include "Module.hh"
41 #include "Connectors.hh"
42 #include "IOEvent.hh"
43
44 //#include "SocketSource.mpp"
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 namespace senf {
48 namespace ppi {
49
50     /** \brief Reader for module::ActiveSocketSource
51
52         This read helper will read a datagram from a datagram socket. This datagram will then be
53         interpreted as a packet of type \a Packet as defined in the packet library. \a Packet
54         defaults to DataPacket (type DataPacketType), which will place the data uninterpreted
55         into a packet data structure.
56      */
57     template <class Packet=DataPacket, unsigned MaxSize=0u>
58     class DgramReader
59     {
60     public:
61         typedef Packet PacketType;
62         typedef senf::ClientSocketHandle<
63             senf::MakeSocketPolicy< senf::ReadablePolicy,
64                                     senf::DatagramFramingPolicy >::policy > Handle;
65                                         ///< Handle type supported by this reader
66
67         Packet operator()(Handle & handle);
68                                         ///< Read packet from \a handle
69                                         /**< Read a datagram from \a handle and interpret is as
70                                              packet of type \c Packet.
71                                              \param[in] handle Handle to read data from
72                                              \returns Pointer to new packet instance or 0, if no
73                                                  packet could be read */
74     };
75
76 }}
77
78 namespace senf {
79 namespace ppi {
80 namespace module {
81
82     /** \brief Input module reading data from an arbitrary FileHandle
83
84         This input module will read data from a FileHandle object and parse the data according to
85         the \a Reader. The default reader is senf::ppi::DgramReader <> which reads the data into a
86         senf::DataPacket. To parse the data according to some other packet type, pass that packet
87         type to senf::ppi::DgramReader:
88         \code
89         senf::ppi::module::ActiveSocketSource< senf::ppi::DgramReader<senf::EthernetPacket> > source;
90         \endcode
91         declares a \a reader module reading senf::EthernetPacket's
92
93         A \a Reader must fulfill the following interface:
94         \code
95         class SomeReader
96         {
97         public:
98             typedef unspecified_type Handle;                       // type of handle requested
99             typedef unspecified_type PacketType                    // type of packet returned
100
101             SomeReader();                                          // EITHER default constructible
102             SomeReader(SomeReader const & other);                  // OR copy constructible
103
104             PacketType operator()(Handle handle);                  // extraction function
105         };
106         \endcode
107         Whenever the FileHandle object is ready for reading, the \a Reader's \c operator() is called
108         to read a packet.
109
110         \ingroup io_modules
111      */
112     template <class Reader=DgramReader<> >
113     class ActiveSocketSource
114         : public Module
115     {
116         SENF_PPI_MODULE(ActiveSocketSource);
117
118     public:
119         typedef typename Reader::Handle Handle; ///< Handle type requested by the reader
120
121         connector::ActiveOutput<typename Reader::PacketType> output;
122                                         ///< Output connector to which the data received is written
123
124         ActiveSocketSource();           ///< Create non-connected reader
125                                         /**< The reader will be disabled until a socket is set
126                                              \pre Requires \a Reader to be default constructible */
127         explicit ActiveSocketSource(Reader reader); ///< Create non-connected reader
128                                         /**< The reader will be disabled until a socket is set
129                                              \pre Requires \a Reader to be copy constructible */
130         explicit ActiveSocketSource(Handle const & handle);
131                                         ///< Create new reader for the given handle
132                                         /**< Data will be read from \a handle and be parsed by \a
133                                              Reader.
134                                              \pre Requires \a Reader to be default constructible
135                                              \param[in] handle Handle to read data from */
136         ActiveSocketSource(Handle const & handle, Reader reader);
137                                         ///< Create new reader for the given handle
138                                         /**< Data will be read from \a handle and be parsed by \a
139                                              Reader.
140                                              \pre Requires \a Reader to be copy constructible
141                                              \param[in] handle Handle to read data from */
142
143         Reader & reader();              ///< Access Reader helper
144         Handle handle();                ///< Access handle
145         void handle(Handle const & handle);
146                                         ///< Set handle
147                                         /**< Assigning an empty or in-valid() handle will disable
148                                              the module until a new, valid handle is assigned. */
149
150     private:
151         Handle handle_;
152         IOEvent event_;
153         Reader reader_;
154
155         void read();
156     };
157
158
159     template <class Reader=DgramReader<> >
160     class ActiveBurstSocketSource
161         : public Module
162     {
163         SENF_PPI_MODULE(ActiveBurstSocketSource);
164
165     public:
166         typedef typename Reader::Handle Handle; ///< Handle type requested by the reader
167
168         connector::ActiveOutput<typename Reader::PacketType> output;
169                                         ///< Output connector to which the data received is written
170
171         ActiveBurstSocketSource(unsigned max_burst=0);
172         explicit ActiveBurstSocketSource(Reader reader, unsigned max_burst=0);
173         explicit ActiveBurstSocketSource(Handle const & handle, unsigned max_burst=0);
174         ActiveBurstSocketSource(Handle const & handle, Reader reader, unsigned max_burst=0);
175
176         Reader & reader();              ///< Access Reader helper
177         Handle handle();                ///< Access handle
178         void handle(Handle const & handle);
179                                         ///< Set handle
180                                         /**< Assigning an empty or in-valid() handle will disable
181                                              the module until a new, valid handle is assigned. */
182
183         unsigned maxBurst() const;
184         void maxBurst(unsigned max_burst);
185
186         void flush();
187
188     private:
189         Handle handle_;
190         IOEvent event_;
191         Reader reader_;
192         unsigned maxBurst_;
193
194         void read();
195     };
196
197 }}}
198
199 //-/////////////////////////////////////////////////////////////////////////////////////////////////
200 //#include "SocketSource.cci"
201 #include "SocketSource.ct"
202 #include "SocketSource.cti"
203 #endif
204
205 \f
206 // Local Variables:
207 // mode: c++
208 // fill-column: 100
209 // c-file-style: "senf"
210 // indent-tabs-mode: nil
211 // ispell-local-dictionary: "american"
212 // compile-command: "scons -u test"
213 // comment-column: 40
214 // End: