PPI: Rename all 'Reader's to 'Source's and 'Writer's to 'Sink's
[senf.git] / PPI / SocketSource.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 SocketSource public header */
23
24 #ifndef HH_SocketSource_
25 #define HH_SocketSource_ 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 "Module.hh"
34 #include "Connectors.hh"
35 #include "IOEvent.hh"
36
37 //#include "SocketSource.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace ppi {
42
43     /** \brief Read helper for module::ActiveSocketSource
44
45         This read helper will read a datagram from a datagram socket. This datagram will then be
46         interpreted as a packet of type \a Packet as defined in the packet library. \a Packet
47         defaults to \ref DataPacket, which will place the data uninterpreted into a packet data
48         structure.
49      */
50     template <class Packet=DataPacket>
51     class PacketSource
52     {
53     public:
54         typedef senf::ClientSocketHandle<
55             senf::MakeSocketPolicy< senf::ReadablePolicy,
56                                     senf::DatagramFramingPolicy >::policy > Handle;
57                                         ///< Handle type supported by this reader
58
59         Packet operator()(Handle handle);
60                                         ///< Read packet from \a handle
61                                         /**< Read a datagram from \a handle and interpret is as
62                                              packet of type \c Packet.
63                                              \param[in] handle Handle to read data from
64                                              \returns Pointer to new packet instance or 0, if no
65                                                  packet could be read */
66     };
67
68 }}
69
70 namespace senf {
71 namespace ppi {
72 namespace module {
73
74     /** \brief Input module reading data from an arbitrary FileHandle
75
76         This input module will read data from a FileHandle object and parse the data according to
77         the \a Source. The default reader is senf::ppi::PacketSource <> which reads the data into a
78         senf::DataPacket. To parse the data according to some other packet type, pass that packet
79         type to senf::ppi::PacketSource:
80         \code
81         senf::ppi::module::ActiveSocketSource< senf::ppi::PacketSource<senf::EthernetPacket> > reader;
82         \endcode
83         declares a \a reader module reading senf::EthrtnetPacket's.
84
85         A \a Source must fulfill the following interface:
86         \code
87         class SomeSource
88         {
89         public:
90             typedef unspecified_type Handle;   // type of handle requested
91             SomeSource();                      // default constructible
92             Packet operator()(Handle handle);  // extraction function
93         };
94         \endcode
95         Whenever the FileHandle object is ready for reading, the \a Source's \c operator() is called
96         to read a packet.
97
98         \ingroup io_modules
99      */
100     template <class Source=PacketSource<> >
101     class ActiveSocketSource 
102         : public Module
103     {
104         SENF_PPI_MODULE(ActiveSocketSource);
105
106     public:
107         typedef typename Source::Handle Handle; ///< Handle type requested by the reader
108
109         connector::ActiveOutput output; ///< Output connector to which the data received is written
110         
111         ActiveSocketSource(Handle handle); ///< Create new reader for the given handle
112                                         /**< Data will be read from \a handle and be parsed by \a
113                                              Source.
114                                              \param[in] handle Handle to read data from */
115
116     private:
117         void read();
118         
119         Handle handle_;
120         IOEvent event_;
121         Source reader_;
122     };
123
124 }}}
125
126 ///////////////////////////////hh.e////////////////////////////////////////
127 //#include "SocketSource.cci"
128 #include "SocketSource.ct"
129 //#include "SocketSource.cti"
130 #endif
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // comment-column: 40
141 // End: