Finished Socket library file handle hierarchy documentation
[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
25     \idea We should optimize the protocol handling. Allocating a
26         protocol instance for every socket body seems quite
27         wasteful. However I have no idea, how to access the socket
28         handle from within the protocol interface if the protocol
29         class is a singleton. Maybe, we'll have some other idea to
30         reduce the number of heap allocations (like deriving the
31         SocketProtocol class (private? protected?) from the
32         SocketBody. (private inheritance is a form of 'has-a' instead
33         of 'is-a' which would fit very well here). This would allow to
34         reduce the number of heap-allocations per socket to one which
35         is good.
36  */
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
43     important socket API methods using the policy framework. To access
44     the complete API, the protocol interface is provided. Access to
45     the protocol interface is only possible via
46     senf::ProtocolClientSocketHandle and
47     senf::ProtocolServerSocketHandle which have the necessary \c
48     protocol() member. This member returns a reference to the protocol
49     class instance which contains members covering all the API
50     functions (mostly setsockopt/getsockopt related calls but there
51     may be more, this is completely up to the implementor of the
52     protocol class) not found in the SocketHandle interface. The
53     protocol interface is specific to the protocol. It's
54     implementation is quite free. The standard protocols are
55     implemented using a simple multiple-inheritance hierarchy as shown
56     above.
57
58     Since the protocol class is protocol specific (how intelligent
59     ...), the protocol class also defines the complete socket policy
60     to be used with it's protocol. Complete meaning, that every policy
61     axis must be assigned it's the most specific (that is derived)
62     policy class to be used with the protocol.
63  */
64
65 #ifndef HH_SocketProtocol_
66 #define HH_SocketProtocol_ 1
67
68 // Custom includes
69 #include <boost/utility.hpp>
70 /** \fixme this is not nice. The includes and predefs should be restructured */
71 #include "SocketHandle.ih"
72
73 //#include "SocketProtocol.mpp"
74 ///////////////////////////////hh.p////////////////////////////////////////
75
76 namespace senf {
77
78     /// \addtogroup protocol_group
79     /// @{
80
81     class SocketPolicyBase;
82
83     class SocketProtocol : boost::noncopyable
84     {
85     public:
86         ///////////////////////////////////////////////////////////////////////////
87         // Types
88
89         ///////////////////////////////////////////////////////////////////////////
90         ///\name Structors and default members
91         ///@{
92
93         SocketProtocol();
94         virtual ~SocketProtocol() = 0;
95
96         // default default constructor
97         // no copy
98         // no conversion constructors
99
100         ///@}
101         ///////////////////////////////////////////////////////////////////////////
102
103         SocketBody & body() const;
104         virtual SocketPolicyBase const & policy() const = 0;
105         
106         ///////////////////////////////////////////////////////////////////////////
107         // Virtual interface
108
109         virtual std::auto_ptr<SocketProtocol> clone() const = 0;
110         virtual unsigned available() const = 0;
111         virtual bool eof() const = 0;
112         virtual void state(SocketStateMap & map, unsigned lod) const;
113
114     protected:
115
116     private:
117         // backpointer to owning SocketBody instance
118         SocketBody * body_;
119         friend class SocketBody; 
120    };
121
122     template <class SocketPolicy>
123     class ConcreteSocketProtocol
124         : public virtual SocketProtocol
125     {
126     public:
127         ///////////////////////////////////////////////////////////////////////////
128         // Types
129
130         typedef SocketPolicy Policy;
131
132         ///////////////////////////////////////////////////////////////////////////
133         ///\name Structors and default members
134         ///@{
135
136         ~ConcreteSocketProtocol() = 0;
137
138         // no default constructor
139         // no copy
140         // no conversion constructors
141
142         ///@}
143         ///////////////////////////////////////////////////////////////////////////
144
145         Policy const & policy() const;
146
147     protected:
148
149     private:
150         Policy policy_;
151
152     };
153
154     /// @}
155 }
156
157 ///////////////////////////////hh.e////////////////////////////////////////
158 #include "SocketProtocol.cci"
159 //#include "SocketProtocol.ct"
160 #include "SocketProtocol.cti"
161 #endif
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // c-file-style: "senf"
167 // End: