set keyword svn property on more files
[senf.git] / 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_SocketSource_
27 #define HH_SocketSource_ 1
28
29 // Custom includes
30 #include "../Packets/Packets.hh"
31 #include "../Socket/ClientSocketHandle.hh"
32 #include "../Socket/SocketPolicy.hh"
33 #include "../Socket/ReadWritePolicy.hh"
34 #include "../Socket/FramingPolicy.hh"
35 #include "Module.hh"
36 #include "Connectors.hh"
37 #include "IOEvent.hh"
38
39 //#include "SocketSource.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
41
42 namespace senf {
43 namespace ppi {
44
45     /** \brief Read helper 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>
53     class PacketSource
54     {
55     public:
56         typedef senf::ClientSocketHandle<
57             senf::MakeSocketPolicy< senf::ReadablePolicy,
58                                     senf::DatagramFramingPolicy >::policy > Handle;
59                                         ///< Handle type supported by this reader
60
61         Packet operator()(Handle handle);
62                                         ///< Read packet from \a handle
63                                         /**< Read a datagram from \a handle and interpret is as
64                                              packet of type \c Packet.
65                                              \param[in] handle Handle to read data from
66                                              \returns Pointer to new packet instance or 0, if no
67                                                  packet could be read */
68     };
69
70 }}
71
72 namespace senf {
73 namespace ppi {
74 namespace module {
75
76     /** \brief Input module reading data from an arbitrary FileHandle
77
78         This input module will read data from a FileHandle object and parse the data according to
79         the \a Source. The default reader is senf::ppi::PacketSource <> which reads the data into a
80         senf::DataPacket. To parse the data according to some other packet type, pass that packet
81         type to senf::ppi::PacketSource:
82         \code
83         senf::ppi::module::ActiveSocketSource< senf::ppi::PacketSource<senf::EthernetPacket> > reader;
84         \endcode
85         declares a \a reader module reading senf::EthrtnetPacket's.
86
87         A \a Source must fulfill the following interface:
88         \code
89         class SomeSource
90         {
91         public:
92             typedef unspecified_type Handle;                       // type of handle requested
93
94             SomeSource();                                          // EITHER default constructible
95             SomeSource(SomeSource const & other);                  // OR copy constructible
96
97             Packet operator()(Handle handle);                      // extraction function
98         };
99         \endcode
100         Whenever the FileHandle object is ready for reading, the \a Source's \c operator() is called
101         to read a packet.
102
103         \ingroup io_modules
104      */
105     template <class Source=PacketSource<> >
106     class ActiveSocketSource 
107         : public Module
108     {
109         SENF_PPI_MODULE(ActiveSocketSource);
110
111     public:
112         typedef typename Source::Handle Handle; ///< Handle type requested by the reader
113
114         connector::ActiveOutput output; ///< Output connector to which the data received is written
115         
116         ActiveSocketSource(Handle handle); ///< Create new reader for the given handle
117                                         /**< Data will be read from \a handle and be parsed by \a
118                                              Source.
119                                              \pre Requires \a Source to be default constructible
120                                              \param[in] handle Handle to read data from */
121         ActiveSocketSource(Handle handle, Source source);
122                                         ///< Create new reader for the given handle
123                                         /**< Data will be read from \a handle and be parsed by \a
124                                              Source.
125                                              \pre Requires \a Source to be copy constructible
126                                              \param[in] handle Handle to read data from */
127
128         Source & source();              ///< Access source helper
129
130     private:
131         void read();
132         
133         Handle handle_;
134         IOEvent event_;
135         Source reader_;
136     };
137
138 }}}
139
140 ///////////////////////////////hh.e////////////////////////////////////////
141 //#include "SocketSource.cci"
142 #include "SocketSource.ct"
143 #include "SocketSource.cti"
144 #endif
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // comment-column: 40
155 // End: