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
21
from bzrlib.transport.smart import medium
28
class SmartTCPServer(object):
29
"""Listens on a TCP socket and accepts connections from smart clients.
31
Each connection will be served by a SmartServerSocketStreamMedium running in
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 = medium.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
# self._server_socket.close()
93
# we used to join the thread, but it's not really necessary; it will
95
## self._server_thread.join()
98
class SmartTCPServer_for_testing(SmartTCPServer):
99
"""Server suitable for use by transport tests.
101
This server is backed by the process's cwd.
105
self._homedir = urlutils.local_path_to_url(os.getcwd())[7:]
106
# The server is set up by default like for ssh access: the client
107
# passes filesystem-absolute paths; therefore the server must look
108
# them up relative to the root directory. it might be better to act
109
# a public server and have the server rewrite paths into the test
111
SmartTCPServer.__init__(self,
112
transport.get_transport(urlutils.local_path_to_url('/')))
115
"""Set up server for testing"""
116
self.start_background_thread()
119
self.stop_background_thread()
122
"""Return the url of the server"""
123
host, port = self._server_socket.getsockname()
124
return "bzr://%s:%d%s" % (host, port, urlutils.escape(self._homedir))
126
def get_bogus_url(self):
127
"""Return a URL which will fail to connect"""
128
return 'bzr://127.0.0.1:1/'