25
from bzrlib.hooks import Hooks
25
from bzrlib.hooks import HookPoint, Hooks
26
26
from bzrlib import (
29
transport as _mod_transport,
31
31
from bzrlib.lazy_import import lazy_import
32
32
lazy_import(globals(), """
33
33
from bzrlib.smart import medium
34
34
from bzrlib.transport import (
38
39
from bzrlib import (
50
51
hooks: An instance of SmartServerHooks.
53
def __init__(self, backing_transport, root_client_path='/'):
54
def __init__(self, backing_transport, host='127.0.0.1', port=0,
55
root_client_path='/'):
54
56
"""Construct a new server.
56
58
To actually start it running, call either start_background_thread or
59
61
:param backing_transport: The transport to serve.
62
:param host: Name of the interface to listen on.
63
:param port: TCP port to listen on, or 0 to allocate a transient port.
60
64
:param root_client_path: The client path that will correspond to root
61
65
of backing_transport.
63
self.backing_transport = backing_transport
64
self.root_client_path = root_client_path
66
def start_server(self, host, port):
67
"""Create the server listening socket.
69
:param host: Name of the interface to listen on.
70
:param port: TCP port to listen on, or 0 to allocate a transient port.
72
67
# let connections timeout so that we get a chance to terminate
73
68
# Keep a reference to the exceptions we want to catch because the socket
74
69
# module's globals get set to None during interpreter shutdown.
94
89
self.port = self._sockname[1]
95
90
self._server_socket.listen(1)
96
91
self._server_socket.settimeout(1)
92
self.backing_transport = backing_transport
97
93
self._started = threading.Event()
98
94
self._stopped = threading.Event()
95
self.root_client_path = root_client_path
100
def _backing_urls(self):
97
def serve(self, thread_name_suffix=''):
98
self._should_terminate = False
99
# for hooks we are letting code know that a server has started (and
101
101
# There are three interesting urls:
102
102
# The URL the server can be contacted on. (e.g. bzr://host/)
103
103
# The URL that a commit done on the same machine as the server will
104
104
# have within the servers space. (e.g. file:///home/user/source)
105
105
# The URL that will be given to other hooks in the same process -
106
# the URL of the backing transport itself. (e.g. filtered-36195:///)
106
# the URL of the backing transport itself. (e.g. chroot+:///)
107
107
# We need all three because:
108
108
# * other machines see the first
109
109
# * local commits on this machine should be able to be mapped to
113
113
# The latter two urls are different aliases to the servers url,
114
114
# so we group those in a list - as there might be more aliases
116
urls = [self.backing_transport.base]
116
backing_urls = [self.backing_transport.base]
118
urls.append(self.backing_transport.external_url())
118
backing_urls.append(self.backing_transport.external_url())
119
119
except errors.InProcessTransport:
123
def run_server_started_hooks(self, backing_urls=None):
124
if backing_urls is None:
125
backing_urls = self._backing_urls()
126
121
for hook in SmartTCPServer.hooks['server_started']:
127
122
hook(backing_urls, self.get_url())
128
123
for hook in SmartTCPServer.hooks['server_started_ex']:
129
124
hook(backing_urls, self)
131
def run_server_stopped_hooks(self, backing_urls=None):
132
if backing_urls is None:
133
backing_urls = self._backing_urls()
134
for hook in SmartTCPServer.hooks['server_stopped']:
135
hook(backing_urls, self.get_url())
137
def serve(self, thread_name_suffix=''):
138
self._should_terminate = False
139
# for hooks we are letting code know that a server has started (and
141
self.run_server_started_hooks()
142
125
self._started.set()
172
155
except self._socket_error:
173
156
# ignore errors on close
175
self.run_server_stopped_hooks()
158
for hook in SmartTCPServer.hooks['server_stopped']:
159
hook(backing_urls, self.get_url())
177
161
def get_url(self):
178
162
"""Return the url of the server"""
179
return "bzr://%s:%s/" % (self._sockname[0], self._sockname[1])
163
return "bzr://%s:%d/" % self._sockname
181
165
def serve_conn(self, conn, thread_name_suffix):
182
166
# For WIN32, where the timeout value from the listening socket
188
172
thread_name = 'smart-server-child' + thread_name_suffix
189
173
connection_thread = threading.Thread(
190
174
None, handler.serve, name=thread_name)
191
# FIXME: This thread is never joined, it should at least be collected
192
# somewhere so that tests that want to check for leaked threads can get
193
# rid of them -- vila 20100531
194
175
connection_thread.setDaemon(True)
195
176
connection_thread.start()
196
return connection_thread
198
178
def start_background_thread(self, thread_name_suffix=''):
199
179
self._started.clear()
239
219
These are all empty initially, because by default nothing should get
242
Hooks.__init__(self, "bzrlib.smart.server", "SmartTCPServer.hooks")
243
self.add_hook('server_started',
223
self.create_hook(HookPoint('server_started',
244
224
"Called by the bzr server when it starts serving a directory. "
245
225
"server_started is called with (backing urls, public url), "
246
226
"where backing_url is a list of URLs giving the "
247
227
"server-specific directory locations, and public_url is the "
248
"public URL for the directory being served.", (0, 16))
249
self.add_hook('server_started_ex',
228
"public URL for the directory being served.", (0, 16), None))
229
self.create_hook(HookPoint('server_started_ex',
250
230
"Called by the bzr server when it starts serving a directory. "
251
231
"server_started is called with (backing_urls, server_obj).",
253
self.add_hook('server_stopped',
233
self.create_hook(HookPoint('server_stopped',
254
234
"Called by the bzr server when it stops serving a directory. "
255
235
"server_stopped is called with the same parameters as the "
256
"server_started hook: (backing_urls, public_url).", (0, 16))
236
"server_started hook: (backing_urls, public_url).", (0, 16), None))
258
238
SmartTCPServer.hooks = SmartServerHooks()
325
305
chroot_server = chroot.ChrootServer(transport)
326
306
chroot_server.start_server()
327
307
self.cleanups.append(chroot_server.stop_server)
328
transport = _mod_transport.get_transport(chroot_server.get_url())
308
transport = get_transport(chroot_server.get_url())
329
309
if self.base_path is not None:
330
310
# Decorate the server's backing transport with a filter that can
331
311
# expand homedirs.
332
312
expand_userdirs = self._make_expand_userdirs_filter(transport)
333
313
expand_userdirs.start_server()
334
314
self.cleanups.append(expand_userdirs.stop_server)
335
transport = _mod_transport.get_transport(expand_userdirs.get_url())
315
transport = get_transport(expand_userdirs.get_url())
336
316
self.transport = transport
338
318
def _make_smart_server(self, host, port, inet):
344
324
host = medium.BZR_DEFAULT_INTERFACE
346
326
port = medium.BZR_DEFAULT_PORT
347
smart_server = SmartTCPServer(self.transport)
348
smart_server.start_server(host, port)
327
smart_server = SmartTCPServer(self.transport, host=host, port=port)
349
328
trace.note('listening on port: %s' % smart_server.port)
350
329
self.smart_server = smart_server