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