51ffa01c373b5f877c5b1b8094ece39cb023db6e
[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     private:
150         Handle handle_;
151         IOEvent event_;
152         Reader reader_;
153
154         void read();
155     };
156
157
158     template <class Reader=DgramReader<> >
159     class ActiveBurstSocketSource
160         : public Module
161     {
162         SENF_PPI_MODULE(ActiveBurstSocketSource);
163
164     public:
165         typedef typename Reader::Handle Handle; ///< Handle type requested by the reader
166
167         connector::ActiveOutput<typename Reader::PacketType> output;
168                                         ///< Output connector to which the data received is written
169
170         ActiveBurstSocketSource(unsigned max_burst=0);
171         explicit ActiveBurstSocketSource(Reader reader, unsigned max_burst=0);
172         explicit ActiveBurstSocketSource(Handle const & handle, unsigned max_burst=0);
173         ActiveBurstSocketSource(Handle const & handle, Reader reader, unsigned max_burst=0);
174
175         Reader & reader();              ///< Access Reader helper
176         Handle handle();                ///< Access handle
177         void handle(Handle const & handle);
178                                         ///< Set handle
179                                         /**< Assigning an empty or in-valid() handle will disable
180                                              the module until a new, valid handle is assigned. */
181
182         unsigned maxBurst() const;
183         void maxBurst(unsigned max_burst);
184
185     private:
186         Handle handle_;
187         IOEvent event_;
188         Reader reader_;
189         unsigned maxBurst_;
190
191         void read();
192     };
193
194 }}}
195
196 //-/////////////////////////////////////////////////////////////////////////////////////////////////
197 //#include "SocketSource.cci"
198 #include "SocketSource.ct"
199 #include "SocketSource.cti"
200 #endif
201
202 \f
203 // Local Variables:
204 // mode: c++
205 // fill-column: 100
206 // c-file-style: "senf"
207 // indent-tabs-mode: nil
208 // ispell-local-dictionary: "american"
209 // compile-command: "scons -u test"
210 // comment-column: 40
211 // End: