added documentation note for SocketProtocol::available()
[senf.git] / Socket / SocketProtocol.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 SocketProtocol and ConcreteSocketProtocol public header
25
26     \idea We should optimize the protocol handling. Allocating a protocol instance for every socket
27         body seems quite wasteful. We could derive SocketPolicy from SocketBody (probably privately,
28         since private inheritance models more of 'has a' than 'is a'). This would allow to reduce
29         the number of heap-allocations per socket to one which is good.
30  */
31
32 // The private inheritance idea should indeed work very well: We just need to change the
33 // implementations of body() and protocol() and that of the ProtocolClient/ServerSocketHandle
34 // constructors and the SocketBody constructor. The body and the protocol would still be visible
35 // like several instances because of the private inheritance but we would save the backwards
36 // pointer.
37
38 /** \defgroup protocol_group The Protocol Classes
39
40     \image html Protocols.png
41
42     The socket handle classes and templates only implement the most important socket API methods
43     using the policy framework. To access the complete API, the protocol interface is
44     provided. Access to the protocol interface is only possible via senf::ProtocolClientSocketHandle
45     and senf::ProtocolServerSocketHandle which have the necessary \c protocol() member. This member
46     returns a reference to the protocol class instance which contains members covering all the API
47     functions (mostly setsockopt/getsockopt related calls but there may be more, this is completely
48     up to the implementor of the protocol class) not found in the SocketHandle interface. The
49     protocol interface is specific to the protocol. It's implementation is quite free. The standard
50     protocols are implemented using a simple multiple-inheritance hierarchy as shown above.
51
52     Since the protocol class is protocol specific (how intelligent ...), the protocol class also
53     defines the complete socket policy to be used with it's protocol. Complete meaning, that every
54     policy axis must be assigned it's the most specific (that is derived) policy class to be used
55     with the protocol.
56
57     \see
58         \ref handle_group \n
59         \ref policy_group
60
61     \todo Complete the protocol interface implementations. Better distribution of members to
62         protocol facets and more precise distribution of functionality among the facets.
63  */
64
65 /** \defgroup concrete_protocol_group Protocol Implementations (Concrete Protocol Classes)
66     \ingroup protocol_group
67
68     Theese protocol classes define concrete and complete protocol implementations. They inherit from
69     ConcreteSocketProtocol and are used with the ProtocolClientSocketHandle and
70     ProtocolServerSocketHandle templates to instantiate socket handles. Appropriate typedefs are
71     always provided.
72
73     Every protocol defines both the protocol and the policy interface provided by that protocol. See
74     the documentation of the protocol classes listed below for more information on the supported
75     protocols. Every protocol class documents it's policy interface. Use the 'list all members' link
76     of the protocol class to find the complete policy interface.
77  */
78
79 /** \defgroup protocol_facets_group Protocol Facets
80     \ingroup protocol_group
81
82     The protocol facets are classes used as building blocks to build concrete protocol classes. Each
83     protocol facet will implement some functional part of the protocol interface. The protocol
84     facets all inherit from SocketProtocol by public \e virtual inheritance. This ensures the
85     accessibility of the socket body from all facets.
86  */
87
88 #ifndef HH_SocketProtocol_
89 #define HH_SocketProtocol_ 1
90
91 // Custom includes
92 #include <boost/utility.hpp>
93 // Hrmpf ... I have tried very hard, but I just can't find a nice, generic way to clean
94 // up this include
95 #include "SocketHandle.ih"
96
97 //#include "SocketProtocol.mpp"
98 ///////////////////////////////hh.p////////////////////////////////////////
99
100 namespace senf {
101
102     /// \addtogroup protocol_group
103     /// @{
104
105     class SocketPolicyBase;
106
107     /** \brief Socket Protocol base class
108
109         This is the base class of all socket protocol classes. Every protocol class must directly or
110         indirectly inherit from SocketProtocol
111
112         \attention SocketProtocol must \e always be inherited using public \e virtual inheritance.
113      */
114     class SocketProtocol : boost::noncopyable
115     {
116     public:
117         ///////////////////////////////////////////////////////////////////////////
118         // Types
119
120         ///////////////////////////////////////////////////////////////////////////
121         ///\name Structors and default members
122         ///@{
123
124         SocketProtocol();
125         virtual ~SocketProtocol() = 0;
126
127         // default default constructor
128         // no copy
129         // no conversion constructors
130
131         ///@}
132         ///////////////////////////////////////////////////////////////////////////
133
134         SocketBody & body() const;      ///< Access the socket body
135                                         /**< \todo we don't need body(), we should better provide a
136                                              handle() member which will return a simple FIleHandle
137                                              object (we cannot return some other derived class since
138                                              we don't know the Protocol or Policy at this point) */
139         virtual SocketPolicyBase const & policy() const = 0;
140                                         ///< Access the policy instance
141
142         ///////////////////////////////////////////////////////////////////////////
143         // Virtual interface
144
145         virtual std::auto_ptr<SocketProtocol> clone() const = 0;
146                                         ///< Polymorphically return a copy of this protocol class
147                                         /**< This member will create a new copy of the protocol
148                                              class on the heap.
149                                              \attention This member must be implemented in every \e
150                                                  leaf protocol class to return a new instance of the
151                                                  appropriate type. */
152         virtual unsigned available() const = 0;
153                                         ///< Return number of bytes available for reading without
154                                         ///< blocking
155                                         /**< This member will check in a (very, sigh) protocol
156                                              dependent way, how many bytes are guaranteed to be
157                                              readable from the socket without blocking even if the
158                                              socket is blocking. If the socket does not support
159                                              reading (viz. NotReadablePolicy is set), this member
160                                              should always return \c 0.*/
161
162         virtual bool eof() const = 0;   ///< Check for end-of-file condition
163                                         /**< This is another check which (like available()) is
164                                              extremely protocol dependent. This member will return
165                                              \c true only, if at end-of-file. If the protocol does
166                                              not support the notion of EOF, this member should
167                                              always return \c false. */
168         virtual void state(SocketStateMap & map, unsigned lod) const;
169                                         ///< Return socket state information
170                                         /**< This member is called to add state information to the
171                                              status \a map. The protocol map should provide as
172                                              detailed information as possible. The amount of
173                                              information to be added to the map is selected by the
174                                              \a lod value with a default value of 0. The
175                                              interpretation of the \a lod value is completely
176                                              implementation defined.
177
178                                              Every class derived from SocketProtocol should
179                                              reimplement state(). The reimplemented method should
180                                              call (all) baseclass-implementations of this
181                                              member.
182
183                                              The \a map Argument is a map which associates
184                                              std:string keys with std:string-like values. The map
185                                              keys are interpreted as hierarchical strings with '.'
186                                              as a separator (like hostnames or struct or class
187                                              members). They are automatically sorted correctly.
188
189                                              The values are std:string with one additional feature:
190                                              they allow assignment or conversion from *any* type as
191                                              long as that type is streamable. This simplifies
192                                              assigning non-string values to the map:
193
194                                              \code
195                                                map["socket.protocol.ip.address"] = peer();
196                                                map["socket.protocol.tcp.backlog"] = backlog();
197                                              \endcode
198
199                                              This will work even if peer() returns an ip-address
200                                              object or backlog() returns an integer. The values are
201                                              automatically converted to their string representation.
202
203                                              The operator "+=" also has been reimplemented to
204                                              simplify adding multiple values to a single entry: It
205                                              will automatically add a ", " separator if the string
206                                              is non-empty. */
207
208     protected:
209
210     private:
211         // backpointer to owning SocketBody instance
212         SocketBody * body_;
213         friend class SocketBody;
214    };
215
216
217     /** \brief Concrete Socket Protocol implementation base class
218
219         ConcreteSocketProtocol is the base class of a concrete socket protocol implementation. The
220         final protocol class must inherit from ConcreteSocketProtocol. The template argument \a
221         SocketPolicy must be set to the complete socket policy of the protocol.
222
223         A protocol implementation may define the protocol interface directly. It can also
224         (additionally) make use of multiple inheritance to combine a set of protocol facets into a
225         specific protocol implementation (i.e. TCPv4SocketProtocol inherits from
226         ConcreteSocketProtocol and from the protocol facets IPv4Protocol, TCPProtocol,
227         BSDSocketProtocol and AddressableBSDSocketProtocol). The protocol facets are not concrete
228         protocols themselves, they are combined to build concrete protocols. This structure will
229         remove a lot of code duplication. It is important to ensure, that the protocol facets do not
230         overlap, since otherwise there will be problems resolving overlapping members.
231         
232         \doc init_client init_server
233      */
234     template <class SocketPolicy>
235     class ConcreteSocketProtocol
236         : public virtual SocketProtocol
237     {
238     public:
239         ///////////////////////////////////////////////////////////////////////////
240         // Types
241
242         typedef SocketPolicy Policy;    ///< The protocols policy
243
244         ///////////////////////////////////////////////////////////////////////////
245         ///\name Structors and default members
246         ///@{
247
248         ~ConcreteSocketProtocol() = 0;
249
250         // no default constructor
251         // no copy
252         // no conversion constructors
253
254         ///@}
255         ///////////////////////////////////////////////////////////////////////////
256
257         Policy const & policy() const;
258
259     protected:
260
261     private:
262         Policy policy_;
263
264     };
265
266     /// @}
267 }
268
269 ///////////////////////////////hh.e////////////////////////////////////////
270 #include "SocketProtocol.cci"
271 //#include "SocketProtocol.ct"
272 #include "SocketProtocol.cti"
273 #endif
274
275 \f
276 // Local Variables:
277 // mode: c++
278 // fill-column: 100
279 // c-file-style: "senf"
280 // indent-tabs-mode: nil
281 // ispell-local-dictionary: "american"
282 // compile-command: "scons -u test"
283 // comment-column: 40
284 // End: