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()
239
217
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',
221
self.create_hook(HookPoint('server_started',
244
222
"Called by the bzr server when it starts serving a directory. "
245
223
"server_started is called with (backing urls, public url), "
246
224
"where backing_url is a list of URLs giving the "
247
225
"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',
226
"public URL for the directory being served.", (0, 16), None))
227
self.create_hook(HookPoint('server_started_ex',
250
228
"Called by the bzr server when it starts serving a directory. "
251
229
"server_started is called with (backing_urls, server_obj).",
253
self.add_hook('server_stopped',
231
self.create_hook(HookPoint('server_stopped',
254
232
"Called by the bzr server when it stops serving a directory. "
255
233
"server_stopped is called with the same parameters as the "
256
"server_started hook: (backing_urls, public_url).", (0, 16))
257
self.add_hook('server_exception',
258
"Called by the bzr server when an exception occurs. "
259
"server_exception is called with the sys.exc_info() tuple "
260
"return true for the hook if the exception has been handled, "
261
"in which case the server will exit normally.", (2, 4))
234
"server_started hook: (backing_urls, public_url).", (0, 16), None))
263
236
SmartTCPServer.hooks = SmartServerHooks()
239
class SmartTCPServer_for_testing(SmartTCPServer):
240
"""Server suitable for use by transport tests.
242
This server is backed by the process's cwd.
245
def __init__(self, thread_name_suffix=''):
246
SmartTCPServer.__init__(self, None)
247
self.client_path_extra = None
248
self.thread_name_suffix = thread_name_suffix
250
def get_backing_transport(self, backing_transport_server):
251
"""Get a backing transport from a server we are decorating."""
252
return transport.get_transport(backing_transport_server.get_url())
254
def start_server(self, backing_transport_server=None,
255
client_path_extra='/extra/'):
256
"""Set up server for testing.
258
:param backing_transport_server: backing server to use. If not
259
specified, a LocalURLServer at the current working directory will
261
:param client_path_extra: a path segment starting with '/' to append to
262
the root URL for this server. For instance, a value of '/foo/bar/'
263
will mean the root of the backing transport will be published at a
264
URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
265
`bzr://127.0.0.1:nnnn/`. Default value is `extra`, so that tests
266
by default will fail unless they do the necessary path translation.
268
if not client_path_extra.startswith('/'):
269
raise ValueError(client_path_extra)
270
from bzrlib.transport.chroot import ChrootServer
271
if backing_transport_server is None:
272
from bzrlib.transport.local import LocalURLServer
273
backing_transport_server = LocalURLServer()
274
self.chroot_server = ChrootServer(
275
self.get_backing_transport(backing_transport_server))
276
self.chroot_server.start_server()
277
self.backing_transport = transport.get_transport(
278
self.chroot_server.get_url())
279
self.root_client_path = self.client_path_extra = client_path_extra
280
self.start_background_thread(self.thread_name_suffix)
282
def stop_server(self):
283
self.stop_background_thread()
284
self.chroot_server.stop_server()
287
url = super(SmartTCPServer_for_testing, self).get_url()
288
return url[:-1] + self.client_path_extra
290
def get_bogus_url(self):
291
"""Return a URL which will fail to connect"""
292
return 'bzr://127.0.0.1:1/'
295
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing):
296
"""Get a readonly server for testing."""
298
def get_backing_transport(self, backing_transport_server):
299
"""Get a backing transport from a server we are decorating."""
300
url = 'readonly+' + backing_transport_server.get_url()
301
return transport.get_transport(url)
304
class SmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing):
305
"""A variation of SmartTCPServer_for_testing that limits the client to
306
using RPCs in protocol v2 (i.e. bzr <= 1.5).
310
url = super(SmartTCPServer_for_testing_v2_only, self).get_url()
311
url = 'bzr-v2://' + url[len('bzr://'):]
315
class ReadonlySmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing_v2_only):
316
"""Get a readonly server for testing."""
318
def get_backing_transport(self, backing_transport_server):
319
"""Get a backing transport from a server we are decorating."""
320
url = 'readonly+' + backing_transport_server.get_url()
321
return transport.get_transport(url)
266
324
def _local_path_for_transport(transport):
267
325
"""Return a local path for transport, if reasonably possible.