ba2b5ed28371f8148bef57f7ef4b83ba23ddf4db
[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 chnage 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 /** \fixme this is not nice. The includes and predefs should be restructured */
94 #include "SocketHandle.ih"
95
96 //#include "SocketProtocol.mpp"
97 ///////////////////////////////hh.p////////////////////////////////////////
98
99 namespace senf {
100
101     /// \addtogroup protocol_group
102     /// @{
103
104     class SocketPolicyBase;
105
106     /** \brief Socket protocol base class
107
108         This is the base class of all socket protocol classes. Every protocol class must directly or
109         indirectly inherit from SocketProtocol
110         
111         \attention SocketProtocol must \e always be inherited using public \e virtual inheritance.
112      */
113     class SocketProtocol : boost::noncopyable
114     {
115     public:
116         ///////////////////////////////////////////////////////////////////////////
117         // Types
118
119         ///////////////////////////////////////////////////////////////////////////
120         ///\name Structors and default members
121         ///@{
122
123         SocketProtocol();
124         virtual ~SocketProtocol() = 0;
125
126         // default default constructor
127         // no copy
128         // no conversion constructors
129
130         ///@}
131         ///////////////////////////////////////////////////////////////////////////
132
133         SocketBody & body() const;      ///< Access the socket body
134                                         /**< \todo we don't need body(), we should better provide a
135                                              handle() member which will return a simple FIleHandle
136                                              object (we cannot return some other derived class since
137                                              we don't know the Protocol or Policy at this point) */
138         virtual SocketPolicyBase const & policy() const = 0;
139                                         ///< Access the policy instance
140         
141         ///////////////////////////////////////////////////////////////////////////
142         // Virtual interface
143
144         virtual std::auto_ptr<SocketProtocol> clone() const = 0;
145                                         ///< Polymorphically return a copy of this protocol class
146                                         /**< This member will create a new copy of the protocol
147                                              class on the heap.
148                                              \attention This member must be implemented in every \e
149                                                  leaf protocol class to return a new instance of the
150                                                  appropriate type. */
151         virtual unsigned available() const = 0;
152                                         ///< Return number of bytes available for reading without
153                                         ///< blocking
154                                         /**< This member will check in a (very, sigh) protocol
155                                              deqpendent way, how many bytes are guarateed to be
156                                              readable from the socket without blocking even if the
157                                              socket is blocking. */
158
159         virtual bool eof() const = 0;   ///< Check for end-of-file condition
160                                         /**< This is another check which (like available()) is
161                                              extremely protocol dependent. This member will return
162                                              \c true only, if at end-of-file. If the protocol does
163                                              not support the notion of EOF, this member should
164                                              always return \c false. */
165         virtual void state(SocketStateMap & map, unsigned lod) const;
166                                         ///< Return socket state information
167                                         /**< This member is called to add state information to the
168                                              status \a map. The protocol map should provide as
169                                              detailed information as possible. The amount of
170                                              information to be added to the map is selected by the
171                                              \a lod value with a default value of 0. The
172                                              interpretation of the \a lod value is completely
173                                              implementation defined.
174
175                                              Every class derived from SocketProtocol should
176                                              reimplement state(). The reimplemented method should
177                                              call (all) baseclass-implementations of this
178                                              member.
179
180                                              The \a map Argument is a map which associates
181                                              std:string keys with std:string-like values. The map
182                                              keys are interpreted as hierarchical strings with '.'
183                                              as a separator (like hostnames or struct or class
184                                              members). They are automatically sorted correctly.
185
186                                              The values are std:string with one additional feature:
187                                              they allow assignment or conversion from *any* type as
188                                              long as that type is streamable. This simplifies
189                                              assigning non-string values to the map:
190
191                                              \code
192                                                map["socket.protocol.ip.address"] = peer();
193                                                map["socket.protocol.tcp.backlog"] = backlog();
194                                              \endcode
195
196                                              This will work even if peer() returns an ip-address
197                                              object or backlog() returns an integer. The values are
198                                              automatically converted to their string representation.
199
200                                              The operator "+=" also has been reimplemented to
201                                              simplify adding multiple values to a single entry: It
202                                              will automatically add a ", " separator if the string
203                                              is non-empty. */
204
205     protected:
206
207     private:
208         // backpointer to owning SocketBody instance
209         SocketBody * body_;
210         friend class SocketBody; 
211    };
212
213     
214     /** \brief Concrete socket protocol implementation base class
215         
216         ConcreteSocketProtocol is the base class of a concrete socket protocol implementation. The
217         final protocol class must inherit from ConcreteSocketProtocol. The template argument \a
218         SocketPolicy must be set to the complete socket policy of the protocol.
219
220         A protocol implementation may define the protocol interface directly. It can also
221         (additnally) make use of multiple inheritance to combine a set of protocol facets into a
222         specific protocol implementation (i.e. TCPv4SocketProtocol inherits from
223         ConcreteSocketProtocol and from the protocol facets IPv4Protocol, TCPProtocol,
224         BSDSocketProtocol and AddressableBSDSocketProtocol). The protocol facets are not concrete
225         protocols themselves, they are combined to build concrete protocols. This structure will
226         remove a lot of code duplication. It is important to ensure, that the protocol facets do not
227         overlap, since otherwise there will be problems resolving overlapping members.
228      */
229     template <class SocketPolicy>
230     class ConcreteSocketProtocol
231         : public virtual SocketProtocol
232     {
233     public:
234         ///////////////////////////////////////////////////////////////////////////
235         // Types
236
237         typedef SocketPolicy Policy;    ///< The protocols policy
238
239         ///////////////////////////////////////////////////////////////////////////
240         ///\name Structors and default members
241         ///@{
242
243         ~ConcreteSocketProtocol() = 0;
244
245         // no default constructor
246         // no copy
247         // no conversion constructors
248
249         ///@}
250         ///////////////////////////////////////////////////////////////////////////
251
252         Policy const & policy() const;
253
254     protected:
255
256     private:
257         Policy policy_;
258
259     };
260
261     /// @}
262 }
263
264 ///////////////////////////////hh.e////////////////////////////////////////
265 #include "SocketProtocol.cci"
266 //#include "SocketProtocol.ct"
267 #include "SocketProtocol.cti"
268 #endif
269
270 \f
271 // Local Variables:
272 // mode: c++
273 // c-file-style: "senf"
274 // fill-column: 100
275 // End: