/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/server.py

Record merge against split out urlutils work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Server for smart-server protocol."""
 
18
 
 
19
import socket
 
20
import os
 
21
import threading
 
22
 
 
23
from bzrlib.smart import medium
 
24
from bzrlib import (
 
25
    trace,
 
26
    transport,
 
27
    urlutils,
 
28
)
 
29
 
 
30
 
 
31
class SmartTCPServer(object):
 
32
    """Listens on a TCP socket and accepts connections from smart clients.
 
33
    
 
34
    Each connection will be served by a SmartServerSocketStreamMedium running in
 
35
    thread.
 
36
    """
 
37
 
 
38
    def __init__(self, backing_transport, host='127.0.0.1', port=0):
 
39
        """Construct a new server.
 
40
 
 
41
        To actually start it running, call either start_background_thread or
 
42
        serve.
 
43
 
 
44
        :param host: Name of the interface to listen on.
 
45
        :param port: TCP port to listen on, or 0 to allocate a transient port.
 
46
        """
 
47
        self._server_socket = socket.socket()
 
48
        self._server_socket.bind((host, port))
 
49
        self.port = self._server_socket.getsockname()[1]
 
50
        self._server_socket.listen(1)
 
51
        self._server_socket.settimeout(1)
 
52
        self.backing_transport = backing_transport
 
53
 
 
54
    def serve(self):
 
55
        # let connections timeout so that we get a chance to terminate
 
56
        # Keep a reference to the exceptions we want to catch because the socket
 
57
        # module's globals get set to None during interpreter shutdown.
 
58
        from socket import timeout as socket_timeout
 
59
        from socket import error as socket_error
 
60
        self._should_terminate = False
 
61
        while not self._should_terminate:
 
62
            try:
 
63
                self.accept_and_serve()
 
64
            except socket_timeout:
 
65
                # just check if we're asked to stop
 
66
                pass
 
67
            except socket_error, e:
 
68
                trace.warning("client disconnected: %s", e)
 
69
                pass
 
70
 
 
71
    def get_url(self):
 
72
        """Return the url of the server"""
 
73
        return "bzr://%s:%d/" % self._server_socket.getsockname()
 
74
 
 
75
    def accept_and_serve(self):
 
76
        conn, client_addr = self._server_socket.accept()
 
77
        # For WIN32, where the timeout value from the listening socket
 
78
        # propogates to the newly accepted socket.
 
79
        conn.setblocking(True)
 
80
        conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
 
81
        handler = medium.SmartServerSocketStreamMedium(conn, self.backing_transport)
 
82
        connection_thread = threading.Thread(
 
83
            None, handler.serve, name='smart-server-child')
 
84
        connection_thread.setDaemon(True)
 
85
        connection_thread.start()
 
86
 
 
87
    def start_background_thread(self):
 
88
        self._server_thread = threading.Thread(None,
 
89
                self.serve,
 
90
                name='server-' + self.get_url())
 
91
        self._server_thread.setDaemon(True)
 
92
        self._server_thread.start()
 
93
 
 
94
    def stop_background_thread(self):
 
95
        self._should_terminate = True
 
96
        # self._server_socket.close()
 
97
        # we used to join the thread, but it's not really necessary; it will
 
98
        # terminate in time
 
99
        ## self._server_thread.join()
 
100
 
 
101
 
 
102
 
 
103
class SmartTCPServer_for_testing(SmartTCPServer):
 
104
    """Server suitable for use by transport tests.
 
105
    
 
106
    This server has a _homedir of the current cwd.
 
107
    """
 
108
 
 
109
    def __init__(self):
 
110
        # The server is set up by default like for inetd access: the backing
 
111
        # transport is connected to a local path that is not '/'.
 
112
        SmartTCPServer.__init__(self, None)
 
113
 
 
114
    def get_backing_transport(self, backing_transport_server):
 
115
        """Get a backing transport from a server we are decorating."""
 
116
        return transport.get_transport(backing_transport_server.get_url())
 
117
 
 
118
    def setUp(self, backing_transport_server=None):
 
119
        """Set up server for testing"""
 
120
        from bzrlib.transport.chroot import ChrootServer
 
121
        if backing_transport_server is None:
 
122
            from bzrlib.transport.local import LocalURLServer
 
123
            backing_transport_server = LocalURLServer()
 
124
        self.chroot_server = ChrootServer(
 
125
            self.get_backing_transport(backing_transport_server))
 
126
        self.chroot_server.setUp()
 
127
        self.backing_transport = transport.get_transport(
 
128
            self.chroot_server.get_url())
 
129
        self.start_background_thread()
 
130
 
 
131
    def tearDown(self):
 
132
        self.stop_background_thread()
 
133
        self.chroot_server.tearDown()
 
134
 
 
135
    def get_url(self):
 
136
        """Return the url of the server"""
 
137
        host, port = self._server_socket.getsockname()
 
138
        return "bzr://%s:%d/" % (host, port)
 
139
 
 
140
    def get_bogus_url(self):
 
141
        """Return a URL which will fail to connect"""
 
142
        return 'bzr://127.0.0.1:1/'
 
143
 
 
144
 
 
145
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing):
 
146
    """Get a readonly server for testing."""
 
147
 
 
148
    def get_backing_transport(self, backing_transport_server):
 
149
        """Get a backing transport from a server we are decorating."""
 
150
        url = 'readonly+' + backing_transport_server.get_url()
 
151
        return transport.get_transport(url)
 
152