1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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
17
"""Server for smart-server protocol."""
23
from bzrlib.smart import medium
31
class SmartTCPServer(object):
32
"""Listens on a TCP socket and accepts connections from smart clients.
34
Each connection will be served by a SmartServerSocketStreamMedium running in
38
def __init__(self, backing_transport, host='127.0.0.1', port=0):
39
"""Construct a new server.
41
To actually start it running, call either start_background_thread or
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.
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
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:
63
self.accept_and_serve()
64
except socket_timeout:
65
# just check if we're asked to stop
67
except socket_error, e:
68
trace.warning("client disconnected: %s", e)
72
"""Return the url of the server"""
73
return "bzr://%s:%d/" % self._server_socket.getsockname()
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()
87
def start_background_thread(self):
88
self._server_thread = threading.Thread(None,
90
name='server-' + self.get_url())
91
self._server_thread.setDaemon(True)
92
self._server_thread.start()
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
99
## self._server_thread.join()
103
class SmartTCPServer_for_testing(SmartTCPServer):
104
"""Server suitable for use by transport tests.
106
This server has a _homedir of the current cwd.
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)
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())
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()
132
self.stop_background_thread()
133
self.chroot_server.tearDown()
135
def get_bogus_url(self):
136
"""Return a URL which will fail to connect"""
137
return 'bzr://127.0.0.1:1/'
140
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing):
141
"""Get a readonly server for testing."""
143
def get_backing_transport(self, backing_transport_server):
144
"""Get a backing transport from a server we are decorating."""
145
url = 'readonly+' + backing_transport_server.get_url()
146
return transport.get_transport(url)