/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
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
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
17
"""Server for smart-server protocol."""
18
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
19
import socket
20
import os
21
import threading
22
2018.5.21 by Andrew Bennetts
Move bzrlib.transport.smart to bzrlib.smart
23
from bzrlib.smart import medium
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
24
from bzrlib import (
2018.5.15 by Andrew Bennetts
Tidy some imports, and bugs introduced when adding server.py
25
    trace,
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
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)
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
82
        connection_thread = threading.Thread(
83
            None, handler.serve, name='smart-server-child')
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
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
class SmartTCPServer_for_testing(SmartTCPServer):
103
    """Server suitable for use by transport tests.
104
    
105
    This server is backed by the process's cwd.
106
    """
107
108
    def __init__(self):
109
        self._homedir = urlutils.local_path_to_url(os.getcwd())[7:]
110
        # The server is set up by default like for ssh access: the client
111
        # passes filesystem-absolute paths; therefore the server must look
112
        # them up relative to the root directory.  it might be better to act
113
        # a public server and have the server rewrite paths into the test
114
        # directory.
115
        SmartTCPServer.__init__(self,
116
            transport.get_transport(urlutils.local_path_to_url('/')))
117
        
118
    def setUp(self):
119
        """Set up server for testing"""
120
        self.start_background_thread()
121
122
    def tearDown(self):
123
        self.stop_background_thread()
124
125
    def get_url(self):
126
        """Return the url of the server"""
127
        host, port = self._server_socket.getsockname()
128
        return "bzr://%s:%d%s" % (host, port, urlutils.escape(self._homedir))
129
130
    def get_bogus_url(self):
131
        """Return a URL which will fail to connect"""
132
        return 'bzr://127.0.0.1:1/'
133
134