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  | 
||
| 
2018.5.31
by Robert Collins
 Make RemoteBzrDir tests use a real smart transport connection to a read only server instance for read-only tests.  | 
102  | 
|
| 
2018.5.42
by Robert Collins
 Various hopefully improvements, but wsgi is broken, handing over to spiv :).  | 
103  | 
class SmartTCPServer_for_testing(SmartTCPServer):  | 
| 
2018.5.14
by Andrew Bennetts
 Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.  | 
104  | 
"""Server suitable for use by transport tests.  | 
105  | 
    
 | 
|
| 
2018.5.31
by Robert Collins
 Make RemoteBzrDir tests use a real smart transport connection to a read only server instance for read-only tests.  | 
106  | 
    This server has a _homedir of the current cwd.
 | 
| 
2018.5.14
by Andrew Bennetts
 Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.  | 
107  | 
    """
 | 
108  | 
||
| 
2018.5.42
by Robert Collins
 Various hopefully improvements, but wsgi is broken, handing over to spiv :).  | 
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('chroot+' + backing_transport_server.get_url())  | 
|
117  | 
||
| 
2018.5.47
by Andrew Bennetts
 Make SmartTCPServer_for_Testing.setUp's backing_transport_server argument optional.  | 
118  | 
def setUp(self, backing_transport_server=None):  | 
| 
2018.5.14
by Andrew Bennetts
 Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.  | 
119  | 
"""Set up server for testing"""  | 
| 
2018.5.47
by Andrew Bennetts
 Make SmartTCPServer_for_Testing.setUp's backing_transport_server argument optional.  | 
120  | 
if backing_transport_server is None:  | 
121  | 
from bzrlib.transport.local import LocalURLServer  | 
|
122  | 
backing_transport_server = LocalURLServer()  | 
|
| 
2018.5.42
by Robert Collins
 Various hopefully improvements, but wsgi is broken, handing over to spiv :).  | 
123  | 
self.backing_transport = self.get_backing_transport(backing_transport_server)  | 
| 
2018.5.14
by Andrew Bennetts
 Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.  | 
124  | 
self.start_background_thread()  | 
125  | 
||
126  | 
def tearDown(self):  | 
|
127  | 
self.stop_background_thread()  | 
|
128  | 
||
129  | 
def get_url(self):  | 
|
130  | 
"""Return the url of the server"""  | 
|
131  | 
host, port = self._server_socket.getsockname()  | 
|
| 
2018.5.42
by Robert Collins
 Various hopefully improvements, but wsgi is broken, handing over to spiv :).  | 
132  | 
return "bzr://%s:%d/" % (host, port)  | 
| 
2018.5.14
by Andrew Bennetts
 Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.  | 
133  | 
|
134  | 
def get_bogus_url(self):  | 
|
135  | 
"""Return a URL which will fail to connect"""  | 
|
136  | 
return 'bzr://127.0.0.1:1/'  | 
|
137  | 
||
138  | 
||
| 
2018.5.42
by Robert Collins
 Various hopefully improvements, but wsgi is broken, handing over to spiv :).  | 
139  | 
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing):  | 
140  | 
"""Get a readonly server for testing."""  | 
|
141  | 
||
142  | 
def get_backing_transport(self, backing_transport_server):  | 
|
143  | 
"""Get a backing transport from a server we are decorating."""  | 
|
144  | 
url = 'chroot+readonly+' + backing_transport_server.get_url()  | 
|
145  | 
return transport.get_transport(url)  |