PPI: Rename all 'Reader's to 'Source's and 'Writer's to 'Sink's
[senf.git] / Socket / Protocols / GenericAddressingPolicy.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 GenericAddressingPolicy public header
25  */
26
27 /** \defgroup addr_group Addressing classes
28  */
29
30 #ifndef HH_GenericAddressingPolicy_
31 #define HH_GenericAddressingPolicy_ 1
32
33 // Custom includes
34 #include "../../Socket/SocketHandle.hh"
35 #include "../../Socket/FileHandle.hh"
36 #include "../../Socket/SocketPolicy.hh"
37 #include "../../Socket/CommunicationPolicy.hh"
38
39 //#include "GenericAddressingPolicy.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
41
42 namespace senf {
43
44     /// \addtogroup policy_impl_group
45     /// @{
46
47     /** \brief Non-template implementation class of GenericAddressingPolicy template
48
49         \internal
50      */
51     struct GenericAddressingPolicy_Base
52     {
53         static void do_local(FileHandle handle, struct sockaddr * addr, unsigned len);
54         static void do_peer(FileHandle handle, struct sockaddr * addr, unsigned len);
55         static void do_bind(FileHandle handle, struct sockaddr const * addr, unsigned len);
56         static void do_connect(FileHandle handle, struct sockaddr const * addr, unsigned len);
57     };
58
59     /** \brief Template for generic AddressingPolicy implementation based on the BSD socket API
60
61         This template provides an implementation template to implement generic addressing policy
62         classes which rely on the standard BSD socket API for their implementation
63         (connect/bind/getsockname/getpeername).
64
65         The \a Address template parameter specifies the address type of the addressing policy. This
66         type must have two members: \c sockaddr_p() and \c sockaddr_len(). The first must return a
67         <tt>struct sockaddr *</tt> to the address, the second must return the size of the address in
68         bytes. The pointer returned by \c sockaddr_p() must be non-const if called on a non-const
69         address. <em>The underlying socket address stored at that pointer might be
70         modified</em>.
71
72         This template class is inherited into addressing policy classes via private
73         inheritance. Then the members supported by the respective addressing policy are made
74         available via \c using declarations (See INet4AddressingPolicy for an Example).
75
76         \idea We could explicitly provide open_sockaddr_p() and close_sockaddr_p()
77         members. sockaddr_p could always return a const * whereas open_sockaddr_p should return a
78         non-const pointer. The close operation would then explicitly signal, that the new value
79         should be incorporated into the class. With our current implementation, the close member
80         would be a no-op, however this should free us from using the sockaddr values as a direct
81         storage representation of the address.
82      */
83     template <class Address>
84     struct GenericAddressingPolicy
85         : private GenericAddressingPolicy_Base
86     {
87 #       ifndef DOXYGEN
88         template <class Policy>
89         static void peer(SocketHandle<Policy> handle, Address & addr,
90                          typename IfCommunicationPolicyIs<Policy,ConnectedCommunicationPolicy>::type * = 0);
91 #       else
92         template <class Policy>
93         static void peer(SocketHandle<Policy> handle, Address & addr);
94                                         ///< Return address of remote peer on connected sockets
95                                         /**< This member is only available if the socket handles
96                                              communication policy is ConnectedCommunicationPolicy.
97
98                                              \param[in] handle socket handle to get peer address of
99                                              \param[out] addr address of remote peer */
100 #       endif
101         static void local(FileHandle handle, Address & addr);
102                                         ///< Return local of socket
103                                         /**< \param[in] handle socket handle to check
104                                              \param[out] addr local socket address */
105
106 #       ifndef DOXYGEN
107         template <class Policy>
108         static void connect(SocketHandle<Policy> handle, Address const & addr,
109                             typename IfCommunicationPolicyIs<Policy,ConnectedCommunicationPolicy>::type * = 0);
110 #       else
111         template <class Policy>
112         static void connect(SocketHandle<Policy> handle, Address const & addr);
113                                         ///< Connect to remote host
114                                         /**< This member is only available if the socket handles
115                                              communication policy is ConnectedCommunicationPolicy.
116
117                                              \param[in] handle socket handle
118                                              \param[in] addr address of remote peer to connect
119                                                  to */
120 #       endif
121         static void bind(FileHandle handle, Address const & addr);
122                                         ///< Set local socket address
123                                         /**< \param[in] handle socket handle
124                                              \param[in] addr local socket address */
125     };
126
127     /// @}
128
129 }
130
131 ///////////////////////////////hh.e////////////////////////////////////////
132 //#include "GenericAddressingPolicy.cci"
133 //#include "GenericAddressingPolicy.ct"
134 #include "GenericAddressingPolicy.cti"
135 //#include "GenericAddressingPolicy.mpp"
136 #endif
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // comment-column: 40
147 // End: