~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/server.py

  • Committer: Andrew Bennetts
  • Date: 2010-02-12 04:33:05 UTC
  • mfrom: (5031 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5032.
  • Revision ID: andrew.bennetts@canonical.com-20100212043305-ujdbsdoviql2t7i3
MergeĀ lp:bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
import sys
23
23
import threading
24
24
 
25
 
from bzrlib.hooks import Hooks
 
25
from bzrlib.hooks import HookPoint, Hooks
26
26
from bzrlib import (
27
27
    errors,
28
28
    trace,
29
 
    transport as _mod_transport,
 
29
    transport,
30
30
)
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 (
35
35
    chroot,
 
36
    get_transport,
36
37
    pathfilter,
37
38
    )
38
39
from bzrlib import (
50
51
    hooks: An instance of SmartServerHooks.
51
52
    """
52
53
 
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.
55
57
 
56
58
        To actually start it running, call either start_background_thread or
57
59
        serve.
58
60
 
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.
62
66
        """
63
 
        self.backing_transport = backing_transport
64
 
        self.root_client_path = root_client_path
65
 
 
66
 
    def start_server(self, host, port):
67
 
        """Create the server listening socket.
68
 
 
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.
71
 
        """
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
99
96
 
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
 
100
        # later stopped).
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
115
115
        # in the future.
116
 
        urls = [self.backing_transport.base]
 
116
        backing_urls = [self.backing_transport.base]
117
117
        try:
118
 
            urls.append(self.backing_transport.external_url())
 
118
            backing_urls.append(self.backing_transport.external_url())
119
119
        except errors.InProcessTransport:
120
120
            pass
121
 
        return urls
122
 
 
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)
130
 
 
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())
136
 
 
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
140
 
        # later stopped).
141
 
        self.run_server_started_hooks()
142
125
        self._started.set()
143
126
        try:
144
127
            try:
155
138
                        if e.args[0] != errno.EBADF:
156
139
                            trace.warning("listening socket error: %s", e)
157
140
                    else:
158
 
                        if self._should_terminate:
159
 
                            break
160
141
                        self.serve_conn(conn, thread_name_suffix)
161
142
            except KeyboardInterrupt:
162
143
                # dont log when CTRL-C'd.
172
153
            except self._socket_error:
173
154
                # ignore errors on close
174
155
                pass
175
 
            self.run_server_stopped_hooks()
 
156
            for hook in SmartTCPServer.hooks['server_stopped']:
 
157
                hook(backing_urls, self.get_url())
176
158
 
177
159
    def get_url(self):
178
160
        """Return the url of the server"""
179
 
        return "bzr://%s:%s/" % (self._sockname[0], self._sockname[1])
 
161
        return "bzr://%s:%d/" % self._sockname
180
162
 
181
163
    def serve_conn(self, conn, thread_name_suffix):
182
164
        # For WIN32, where the timeout value from the listening socket
188
170
        thread_name = 'smart-server-child' + thread_name_suffix
189
171
        connection_thread = threading.Thread(
190
172
            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
173
        connection_thread.setDaemon(True)
195
174
        connection_thread.start()
196
 
        return connection_thread
197
175
 
198
176
    def start_background_thread(self, thread_name_suffix=''):
199
177
        self._started.clear()
239
217
        These are all empty initially, because by default nothing should get
240
218
        notified.
241
219
        """
242
 
        Hooks.__init__(self, "bzrlib.smart.server", "SmartTCPServer.hooks")
243
 
        self.add_hook('server_started',
 
220
        Hooks.__init__(self)
 
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).",
252
 
            (1, 17))
253
 
        self.add_hook('server_stopped',
 
230
            (1, 17), None))
 
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))
262
235
 
263
236
SmartTCPServer.hooks = SmartServerHooks()
264
237
 
330
303
        chroot_server = chroot.ChrootServer(transport)
331
304
        chroot_server.start_server()
332
305
        self.cleanups.append(chroot_server.stop_server)
333
 
        transport = _mod_transport.get_transport_from_url(chroot_server.get_url())
 
306
        transport = get_transport(chroot_server.get_url())
334
307
        if self.base_path is not None:
335
308
            # Decorate the server's backing transport with a filter that can
336
309
            # expand homedirs.
337
310
            expand_userdirs = self._make_expand_userdirs_filter(transport)
338
311
            expand_userdirs.start_server()
339
312
            self.cleanups.append(expand_userdirs.stop_server)
340
 
            transport = _mod_transport.get_transport_from_url(expand_userdirs.get_url())
 
313
            transport = get_transport(expand_userdirs.get_url())
341
314
        self.transport = transport
342
315
 
343
316
    def _make_smart_server(self, host, port, inet):
349
322
                host = medium.BZR_DEFAULT_INTERFACE
350
323
            if port is None:
351
324
                port = medium.BZR_DEFAULT_PORT
352
 
            smart_server = SmartTCPServer(self.transport)
353
 
            smart_server.start_server(host, port)
 
325
            smart_server = SmartTCPServer(self.transport, host=host, port=port)
354
326
            trace.note('listening on port: %s' % smart_server.port)
355
327
        self.smart_server = smart_server
356
328
 
378
350
        for cleanup in reversed(self.cleanups):
379
351
            cleanup()
380
352
 
 
353
 
381
354
def serve_bzr(transport, host=None, port=None, inet=False):
382
355
    """This is the default implementation of 'bzr serve'.
383
356
    
389
362
    try:
390
363
        bzr_server.set_up(transport, host, port, inet)
391
364
        bzr_server.smart_server.serve()
392
 
    except:
393
 
        hook_caught_exception = False
394
 
        for hook in SmartTCPServer.hooks['server_exception']:
395
 
            hook_caught_exception = hook(sys.exc_info())
396
 
        if not hook_caught_exception:
397
 
            raise
398
365
    finally:
399
366
        bzr_server.tear_down()
 
367