~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/server.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-08-27 02:27:19 UTC
  • mfrom: (4634.3.19 gc-batching)
  • Revision ID: pqm@pqm.ubuntu.com-20090827022719-bl2yoqhpj3fcfczu
(andrew) Fix #402657: 2a fetch over dumb transport reads one group at
        a time.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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
16
16
 
17
17
"""Server for smart-server protocol."""
18
18
 
21
21
import sys
22
22
import threading
23
23
 
24
 
from bzrlib.hooks import Hooks
 
24
from bzrlib.hooks import HookPoint, Hooks
25
25
from bzrlib import (
26
26
    errors,
27
27
    trace,
28
28
    transport,
29
29
)
30
 
from bzrlib.smart.medium import SmartServerSocketStreamMedium
 
30
from bzrlib.lazy_import import lazy_import
 
31
lazy_import(globals(), """
 
32
from bzrlib.smart import medium
 
33
""")
31
34
 
32
35
 
33
36
class SmartTCPServer(object):
59
62
        from socket import error as socket_error
60
63
        self._socket_error = socket_error
61
64
        self._socket_timeout = socket_timeout
62
 
        self._server_socket = socket.socket()
 
65
        addrs = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
 
66
            socket.SOCK_STREAM, 0, socket.AI_PASSIVE)[0]
 
67
 
 
68
        (family, socktype, proto, canonname, sockaddr) = addrs
 
69
 
 
70
        self._server_socket = socket.socket(family, socktype, proto)
63
71
        # SO_REUSERADDR has a different meaning on Windows
64
72
        if sys.platform != 'win32':
65
73
            self._server_socket.setsockopt(socket.SOL_SOCKET,
66
74
                socket.SO_REUSEADDR, 1)
67
75
        try:
68
 
            self._server_socket.bind((host, port))
 
76
            self._server_socket.bind(sockaddr)
69
77
        except self._socket_error, message:
70
78
            raise errors.CannotBindAddress(host, port, message)
71
79
        self._sockname = self._server_socket.getsockname()
90
98
        # We need all three because:
91
99
        #  * other machines see the first
92
100
        #  * local commits on this machine should be able to be mapped to
93
 
        #    this server 
 
101
        #    this server
94
102
        #  * commits the server does itself need to be mapped across to this
95
103
        #    server.
96
104
        # The latter two urls are different aliases to the servers url,
97
 
        # 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
98
106
        # in the future.
99
107
        backing_urls = [self.backing_transport.base]
100
108
        try:
103
111
            pass
104
112
        for hook in SmartTCPServer.hooks['server_started']:
105
113
            hook(backing_urls, self.get_url())
 
114
        for hook in SmartTCPServer.hooks['server_started_ex']:
 
115
            hook(backing_urls, self)
106
116
        self._started.set()
107
117
        try:
108
118
            try:
144
154
 
145
155
    def serve_conn(self, conn, thread_name_suffix):
146
156
        # For WIN32, where the timeout value from the listening socket
147
 
        # propogates to the newly accepted socket.
 
157
        # propagates to the newly accepted socket.
148
158
        conn.setblocking(True)
149
159
        conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
150
 
        handler = SmartServerSocketStreamMedium(
 
160
        handler = medium.SmartServerSocketStreamMedium(
151
161
            conn, self.backing_transport, self.root_client_path)
152
162
        thread_name = 'smart-server-child' + thread_name_suffix
153
163
        connection_thread = threading.Thread(
170
180
        self._should_terminate = True
171
181
        # close the socket - gives error to connections from here on in,
172
182
        # rather than a connection reset error to connections made during
173
 
        # the period between setting _should_terminate = True and 
 
183
        # the period between setting _should_terminate = True and
174
184
        # the current request completing/aborting. It may also break out the
175
185
        # main loop if it was currently in accept() (on some platforms).
176
186
        try:
200
210
        notified.
201
211
        """
202
212
        Hooks.__init__(self)
203
 
        # Introduced in 0.16:
204
 
        # invoked whenever the server starts serving a directory.
205
 
        # The api signature is (backing urls, public url).
206
 
        self['server_started'] = []
207
 
        # Introduced in 0.16:
208
 
        # invoked whenever the server stops serving a directory.
209
 
        # The api signature is (backing urls, public url).
210
 
        self['server_stopped'] = []
 
213
        self.create_hook(HookPoint('server_started',
 
214
            "Called by the bzr server when it starts serving a directory. "
 
215
            "server_started is called with (backing urls, public url), "
 
216
            "where backing_url is a list of URLs giving the "
 
217
            "server-specific directory locations, and public_url is the "
 
218
            "public URL for the directory being served.", (0, 16), None))
 
219
        self.create_hook(HookPoint('server_started_ex',
 
220
            "Called by the bzr server when it starts serving a directory. "
 
221
            "server_started is called with (backing_urls, server_obj).",
 
222
            (1, 17), None))
 
223
        self.create_hook(HookPoint('server_stopped',
 
224
            "Called by the bzr server when it stops serving a directory. "
 
225
            "server_stopped is called with the same parameters as the "
 
226
            "server_started hook: (backing_urls, public_url).", (0, 16), None))
211
227
 
212
228
SmartTCPServer.hooks = SmartServerHooks()
213
229
 
214
230
 
215
231
class SmartTCPServer_for_testing(SmartTCPServer):
216
232
    """Server suitable for use by transport tests.
217
 
    
 
233
 
218
234
    This server is backed by the process's cwd.
219
235
    """
220
236
 
222
238
        SmartTCPServer.__init__(self, None)
223
239
        self.client_path_extra = None
224
240
        self.thread_name_suffix = thread_name_suffix
225
 
        
 
241
 
226
242
    def get_backing_transport(self, backing_transport_server):
227
243
        """Get a backing transport from a server we are decorating."""
228
244
        return transport.get_transport(backing_transport_server.get_url())
230
246
    def setUp(self, backing_transport_server=None,
231
247
              client_path_extra='/extra/'):
232
248
        """Set up server for testing.
233
 
        
 
249
 
234
250
        :param backing_transport_server: backing server to use.  If not
235
251
            specified, a LocalURLServer at the current working directory will
236
252
            be used.
297
313
        return transport.get_transport(url)
298
314
 
299
315
 
 
316
def serve_bzr(transport, host=None, port=None, inet=False):
 
317
    from bzrlib import lockdir, ui
 
318
    from bzrlib.transport import get_transport
 
319
    from bzrlib.transport.chroot import ChrootServer
 
320
    chroot_server = ChrootServer(transport)
 
321
    chroot_server.setUp()
 
322
    transport = get_transport(chroot_server.get_url())
 
323
    if inet:
 
324
        smart_server = medium.SmartServerPipeStreamMedium(
 
325
            sys.stdin, sys.stdout, transport)
 
326
    else:
 
327
        if host is None:
 
328
            host = medium.BZR_DEFAULT_INTERFACE
 
329
        if port is None:
 
330
            port = medium.BZR_DEFAULT_PORT
 
331
        smart_server = SmartTCPServer(transport, host=host, port=port)
 
332
        trace.note('listening on port: %s' % smart_server.port)
 
333
    # For the duration of this server, no UI output is permitted. note
 
334
    # that this may cause problems with blackbox tests. This should be
 
335
    # changed with care though, as we dont want to use bandwidth sending
 
336
    # progress over stderr to smart server clients!
 
337
    old_factory = ui.ui_factory
 
338
    old_lockdir_timeout = lockdir._DEFAULT_TIMEOUT_SECONDS
 
339
    try:
 
340
        ui.ui_factory = ui.SilentUIFactory()
 
341
        lockdir._DEFAULT_TIMEOUT_SECONDS = 0
 
342
        smart_server.serve()
 
343
    finally:
 
344
        ui.ui_factory = old_factory
 
345
        lockdir._DEFAULT_TIMEOUT_SECONDS = old_lockdir_timeout
 
346