13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Server for smart-server protocol."""
23
from bzrlib.hooks import Hooks
24
from bzrlib.hooks import HookPoint, Hooks
24
25
from bzrlib import (
29
from bzrlib.smart.medium import SmartServerSocketStreamMedium
30
from bzrlib.lazy_import import lazy_import
31
lazy_import(globals(), """
32
from bzrlib.smart import medium
32
36
class SmartTCPServer(object):
38
42
hooks: An instance of SmartServerHooks.
41
def __init__(self, backing_transport, host='127.0.0.1', port=0):
45
def __init__(self, backing_transport, host='127.0.0.1', port=0,
46
root_client_path='/'):
42
47
"""Construct a new server.
44
49
To actually start it running, call either start_background_thread or
52
:param backing_transport: The transport to serve.
47
53
:param host: Name of the interface to listen on.
48
54
:param port: TCP port to listen on, or 0 to allocate a transient port.
55
:param root_client_path: The client path that will correspond to root
50
58
# let connections timeout so that we get a chance to terminate
51
59
# Keep a reference to the exceptions we want to catch because the socket
54
62
from socket import error as socket_error
55
63
self._socket_error = socket_error
56
64
self._socket_timeout = socket_timeout
57
self._server_socket = socket.socket()
58
self._server_socket.bind((host, port))
65
addrs = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
66
socket.SOCK_STREAM, 0, socket.AI_PASSIVE)[0]
68
(family, socktype, proto, canonname, sockaddr) = addrs
70
self._server_socket = socket.socket(family, socktype, proto)
71
# SO_REUSERADDR has a different meaning on Windows
72
if sys.platform != 'win32':
73
self._server_socket.setsockopt(socket.SOL_SOCKET,
74
socket.SO_REUSEADDR, 1)
76
self._server_socket.bind(sockaddr)
77
except self._socket_error, message:
78
raise errors.CannotBindAddress(host, port, message)
59
79
self._sockname = self._server_socket.getsockname()
60
80
self.port = self._sockname[1]
61
81
self._server_socket.listen(1)
77
98
# We need all three because:
78
99
# * other machines see the first
79
100
# * local commits on this machine should be able to be mapped to
81
102
# * commits the server does itself need to be mapped across to this
83
104
# The latter two urls are different aliases to the servers url,
84
# so we group those in a list - as there might be more aliases
105
# so we group those in a list - as there might be more aliases
86
107
backing_urls = [self.backing_transport.base]
129
150
"""Return the url of the server"""
130
151
return "bzr://%s:%d/" % self._sockname
132
def serve_conn(self, conn):
153
def serve_conn(self, conn, thread_name_suffix):
133
154
# For WIN32, where the timeout value from the listening socket
134
# propogates to the newly accepted socket.
155
# propagates to the newly accepted socket.
135
156
conn.setblocking(True)
136
157
conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
137
handler = SmartServerSocketStreamMedium(conn, self.backing_transport)
138
connection_thread = threading.Thread(None, handler.serve, name='smart-server-child')
158
handler = medium.SmartServerSocketStreamMedium(
159
conn, self.backing_transport, self.root_client_path)
160
thread_name = 'smart-server-child' + thread_name_suffix
161
connection_thread = threading.Thread(
162
None, handler.serve, name=thread_name)
139
163
connection_thread.setDaemon(True)
140
164
connection_thread.start()
142
def start_background_thread(self):
166
def start_background_thread(self, thread_name_suffix=''):
143
167
self._started.clear()
144
168
self._server_thread = threading.Thread(None,
169
self.serve, args=(thread_name_suffix,),
146
170
name='server-' + self.get_url())
147
171
self._server_thread.setDaemon(True)
148
172
self._server_thread.start()
186
210
Hooks.__init__(self)
187
# Introduced in 0.16:
188
# invoked whenever the server starts serving a directory.
189
# The api signature is (backing urls, public url).
190
self['server_started'] = []
191
# Introduced in 0.16:
192
# invoked whenever the server stops serving a directory.
193
# The api signature is (backing urls, public url).
194
self['server_stopped'] = []
211
self.create_hook(HookPoint('server_started',
212
"Called by the bzr server when it starts serving a directory. "
213
"server_started is called with (backing urls, public url), "
214
"where backing_url is a list of URLs giving the "
215
"server-specific directory locations, and public_url is the "
216
"public URL for the directory being served.", (0, 16), None))
217
self.create_hook(HookPoint('server_stopped',
218
"Called by the bzr server when it stops serving a directory. "
219
"server_stopped is called with the same parameters as the "
220
"server_started hook: (backing_urls, public_url).", (0, 16), None))
196
222
SmartTCPServer.hooks = SmartServerHooks()
199
225
class SmartTCPServer_for_testing(SmartTCPServer):
200
226
"""Server suitable for use by transport tests.
202
228
This server is backed by the process's cwd.
231
def __init__(self, thread_name_suffix=''):
206
232
SmartTCPServer.__init__(self, None)
233
self.client_path_extra = None
234
self.thread_name_suffix = thread_name_suffix
208
236
def get_backing_transport(self, backing_transport_server):
209
237
"""Get a backing transport from a server we are decorating."""
210
238
return transport.get_transport(backing_transport_server.get_url())
212
def setUp(self, backing_transport_server=None):
213
"""Set up server for testing"""
240
def setUp(self, backing_transport_server=None,
241
client_path_extra='/extra/'):
242
"""Set up server for testing.
244
:param backing_transport_server: backing server to use. If not
245
specified, a LocalURLServer at the current working directory will
247
:param client_path_extra: a path segment starting with '/' to append to
248
the root URL for this server. For instance, a value of '/foo/bar/'
249
will mean the root of the backing transport will be published at a
250
URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
251
`bzr://127.0.0.1:nnnn/`. Default value is `extra`, so that tests
252
by default will fail unless they do the necessary path translation.
254
if not client_path_extra.startswith('/'):
255
raise ValueError(client_path_extra)
214
256
from bzrlib.transport.chroot import ChrootServer
215
257
if backing_transport_server is None:
216
258
from bzrlib.transport.local import LocalURLServer
220
262
self.chroot_server.setUp()
221
263
self.backing_transport = transport.get_transport(
222
264
self.chroot_server.get_url())
223
self.start_background_thread()
265
self.root_client_path = self.client_path_extra = client_path_extra
266
self.start_background_thread(self.thread_name_suffix)
225
268
def tearDown(self):
226
269
self.stop_background_thread()
227
270
self.chroot_server.tearDown()
273
url = super(SmartTCPServer_for_testing, self).get_url()
274
return url[:-1] + self.client_path_extra
229
276
def get_bogus_url(self):
230
277
"""Return a URL which will fail to connect"""
231
278
return 'bzr://127.0.0.1:1/'
239
286
url = 'readonly+' + backing_transport_server.get_url()
240
287
return transport.get_transport(url)
290
class SmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing):
291
"""A variation of SmartTCPServer_for_testing that limits the client to
292
using RPCs in protocol v2 (i.e. bzr <= 1.5).
296
url = super(SmartTCPServer_for_testing_v2_only, self).get_url()
297
url = 'bzr-v2://' + url[len('bzr://'):]
301
class ReadonlySmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing_v2_only):
302
"""Get a readonly server for testing."""
304
def get_backing_transport(self, backing_transport_server):
305
"""Get a backing transport from a server we are decorating."""
306
url = 'readonly+' + backing_transport_server.get_url()
307
return transport.get_transport(url)