1
# Copyright (C) 2006, 2007 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
29
from bzrlib.smart.medium import SmartServerSocketStreamMedium
32
class SmartTCPServer(object):
33
"""Listens on a TCP socket and accepts connections from smart clients"""
35
def __init__(self, backing_transport, host='127.0.0.1', port=0):
36
"""Construct a new server.
38
To actually start it running, call either start_background_thread or
41
:param host: Name of the interface to listen on.
42
:param port: TCP port to listen on, or 0 to allocate a transient port.
44
self._server_socket = socket.socket()
45
self._server_socket.bind((host, port))
46
self.port = self._server_socket.getsockname()[1]
47
self._server_socket.listen(1)
48
self._server_socket.settimeout(1)
49
self.backing_transport = backing_transport
52
# let connections timeout so that we get a chance to terminate
53
# Keep a reference to the exceptions we want to catch because the socket
54
# module's globals get set to None during interpreter shutdown.
55
from socket import timeout as socket_timeout
56
from socket import error as socket_error
57
self._should_terminate = False
58
while not self._should_terminate:
60
self.accept_and_serve()
61
except socket_timeout:
62
# just check if we're asked to stop
64
except socket_error, e:
65
trace.warning("client disconnected: %s", e)
69
"""Return the url of the server"""
70
return "bzr://%s:%d/" % self._server_socket.getsockname()
72
def accept_and_serve(self):
73
conn, client_addr = self._server_socket.accept()
74
# For WIN32, where the timeout value from the listening socket
75
# propogates to the newly accepted socket.
76
conn.setblocking(True)
77
conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
78
handler = SmartServerSocketStreamMedium(conn, self.backing_transport)
79
connection_thread = threading.Thread(None, handler.serve, name='smart-server-child')
80
connection_thread.setDaemon(True)
81
connection_thread.start()
83
def start_background_thread(self):
84
self._server_thread = threading.Thread(None,
86
name='server-' + self.get_url())
87
self._server_thread.setDaemon(True)
88
self._server_thread.start()
90
def stop_background_thread(self):
91
self._should_terminate = True
92
# At one point we would wait to join the threads here, but it looks
93
# like they don't actually exit. So now we just leave them running
94
# and expect to terminate the process. -- mbp 20070215
95
# self._server_socket.close()
96
## sys.stderr.write("waiting for server thread to finish...")
97
## self._server_thread.join()
100
class SmartTCPServer_for_testing(SmartTCPServer):
101
"""Server suitable for use by transport tests.
103
This server is backed by the process's cwd.
107
self._homedir = urlutils.local_path_to_url(os.getcwd())[7:]
108
# The server is set up by default like for ssh access: the client
109
# passes filesystem-absolute paths; therefore the server must look
110
# them up relative to the root directory. it might be better to act
111
# a public server and have the server rewrite paths into the test
113
SmartTCPServer.__init__(self,
114
transport.get_transport(urlutils.local_path_to_url('/')))
116
def get_backing_transport(self, backing_transport_server):
117
"""Get a backing transport from a server we are decorating."""
118
return transport.get_transport(backing_transport_server.get_url())
120
def setUp(self, backing_transport_server=None):
121
"""Set up server for testing"""
122
from bzrlib.transport.chroot import TestingChrootServer
123
if backing_transport_server is None:
124
from bzrlib.transport.local import LocalURLServer
125
backing_transport_server = LocalURLServer()
126
self.chroot_server = TestingChrootServer()
127
self.chroot_server.setUp(backing_transport_server)
128
self.backing_transport = transport.get_transport(
129
self.chroot_server.get_url())
130
self.start_background_thread()
133
self.stop_background_thread()
135
def get_bogus_url(self):
136
"""Return a URL which will fail to connect"""
137
return 'bzr://127.0.0.1:1/'