cb91adc5242f92576c28be1d12bc55f757c55031
[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 DataPacket (type DataPacketType), which will place the data uninterpreted 
48         into a packet data 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
92             SomeSource();                                          // EITHER default constructible
93             SomeSource(SomeSource const & other);                  // OR copy constructible
94
95             Packet operator()(Handle handle);                      // extraction function
96         };
97         \endcode
98         Whenever the FileHandle object is ready for reading, the \a Source's \c operator() is called
99         to read a packet.
100
101         \ingroup io_modules
102      */
103     template <class Source=PacketSource<> >
104     class ActiveSocketSource 
105         : public Module
106     {
107         SENF_PPI_MODULE(ActiveSocketSource);
108
109     public:
110         typedef typename Source::Handle Handle; ///< Handle type requested by the reader
111
112         connector::ActiveOutput output; ///< Output connector to which the data received is written
113         
114         ActiveSocketSource(Handle handle); ///< Create new reader for the given handle
115                                         /**< Data will be read from \a handle and be parsed by \a
116                                              Source.
117                                              \pre Requires \a Source to be default constructible
118                                              \param[in] handle Handle to read data from */
119         ActiveSocketSource(Handle handle, Source source);
120                                         ///< Create new reader for the given handle
121                                         /**< Data will be read from \a handle and be parsed by \a
122                                              Source.
123                                              \pre Requires \a Source to be copy constructible
124                                              \param[in] handle Handle to read data from */
125
126         Source & source();              ///< Access source helper
127
128     private:
129         void read();
130         
131         Handle handle_;
132         IOEvent event_;
133         Source reader_;
134     };
135
136 }}}
137
138 ///////////////////////////////hh.e////////////////////////////////////////
139 //#include "SocketSource.cci"
140 #include "SocketSource.ct"
141 #include "SocketSource.cti"
142 #endif
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // comment-column: 40
153 // End: