switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / ServerSocketHandle.cti
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ServerSocketHandle inline template implementation
30  */
31
32 // Definition of inline template functions
33
34 //#include "ServerSocketHandle.ih"
35
36 // Custom includes
37 #include <typeinfo>
38
39 #define prefix_ inline
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 template <class SPolicy>
43 prefix_ senf::ServerSocketHandle<SPolicy>::ServerSocketHandle()
44 {}
45
46 template <class SPolicy>
47 template <class OtherPolicy>
48 prefix_ senf::ServerSocketHandle<SPolicy>::
49 ServerSocketHandle(ServerSocketHandle<OtherPolicy> other,
50                    typename SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type *)
51     : SocketHandle<SPolicy>(other,true)
52 {}
53
54 template <class SPolicy>
55 prefix_  senf::ServerSocketHandle<SPolicy>::
56 ServerSocketHandle(std::auto_ptr<SocketBody> body)
57     : SocketHandle<SPolicy>(body)
58 {}
59
60 template <class SPolicy>
61 template <class OtherPolicy>
62 prefix_ typename senf::SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type const &
63 senf::ServerSocketHandle<SPolicy>::operator=(ServerSocketHandle<OtherPolicy> other)
64 {
65     assign(other);
66     return *this;
67 }
68
69 //-/////////////////////////////////////////////////////////////////////////////////////////////////
70 // Server socket interface
71
72 template <class SPolicy>
73 prefix_ void senf::ServerSocketHandle<SPolicy>::bind(AddressParam addr)
74 {
75     SPolicy::AddressingPolicy::bind(*this,addr);
76 }
77
78 template <class SPolicy>
79 prefix_ void senf::ServerSocketHandle<SPolicy>::listen(unsigned backlog)
80 {
81     SPolicy::CommunicationPolicy::listen(*this,backlog);
82 }
83
84 template <class SPolicy>
85 prefix_ typename senf::ServerSocketHandle<SPolicy>::Address
86 senf::ServerSocketHandle<SPolicy>::local()
87 {
88     typename SPolicy::AddressingPolicy::Address addr;
89     this->local(addr);
90     return addr;
91 }
92
93 template <class SPolicy>
94 prefix_ void senf::ServerSocketHandle<SPolicy>::local(Address & addr)
95 {
96     SPolicy::AddressingPolicy::local(*this,addr);
97 }
98
99 template <class SPolicy>
100 prefix_ typename senf::ServerSocketHandle<SPolicy>::ClientHandle
101 senf::ServerSocketHandle<SPolicy>::accept()
102 {
103     return ClientHandle(this->body().clone(
104                             SPolicy::CommunicationPolicy::accept(*this), false));
105 }
106
107 template <class SPolicy>
108 prefix_ std::pair<typename senf::ServerSocketHandle<SPolicy>::ClientHandle,
109                   typename senf::ServerSocketHandle<SPolicy>::Address>
110 senf::ServerSocketHandle<SPolicy>::acceptfrom()
111 {
112
113     Address address;
114     ClientHandle handle = acceptfrom(address);
115     return std::make_pair(handle,address);
116 }
117
118 template <class SPolicy>
119 prefix_ typename senf::ServerSocketHandle<SPolicy>::ClientHandle
120 senf::ServerSocketHandle<SPolicy>::acceptfrom(Address & addr)
121 {
122     return ClientHandle(this->body().clone(
123                             SPolicy::CommunicationPolicy::accept(*this,addr), false));
124 }
125
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127
128 template <class SPolicy>
129 prefix_ senf::ServerSocketHandle<SPolicy>::ServerSocketHandle(FileHandle other,
130                                                                     bool isChecked)
131     : SocketHandle<SPolicy>(other, isChecked)
132 {}
133
134 template <class SPolicy>
135 prefix_ senf::ServerSocketHandle<SPolicy>
136 senf::ServerSocketHandle<SPolicy>::cast_static(FileHandle handle)
137 {
138     return ServerSocketHandle(handle,true);
139 }
140
141 template <class SPolicy>
142 prefix_ senf::ServerSocketHandle<SPolicy>
143 senf::ServerSocketHandle<SPolicy>::cast_dynamic(FileHandle handle)
144 {
145     SocketHandle<SPolicy> h (SocketHandle<SPolicy>::cast_dynamic(handle));
146     if (! static_cast<SocketBody&>(FileHandle::body(handle)).isServer())
147         throw std::bad_cast();
148     return cast_static(handle);
149 }
150
151 template <class SPolicy>
152 prefix_ void senf::ServerSocketHandle<SPolicy>::state(SocketStateMap & map, unsigned lod)
153 {
154     map["handle"] = prettyName(typeid(*this));
155     if (this->valid()) {
156         map["valid"] << "true";
157         this->body().state(map,lod);
158     } else
159         map["valid"] << "false";
160 }
161
162 template <class SPolicy>
163 prefix_ std::string senf::ServerSocketHandle<SPolicy>::dumpState(unsigned lod)
164 {
165     SocketStateMap map;
166     state(map,lod);
167     return detail::dumpState(map);
168 }
169
170 //-/////////////////////////////////////////////////////////////////////////////////////////////////
171 #undef prefix_
172
173 \f
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // comment-column: 40
182 // End: