/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/server.py

  • Committer: Andrew Bennetts
  • Date: 2007-04-10 02:31:42 UTC
  • mto: This revision was merged to the branch mainline in revision 2402.
  • Revision ID: andrew.bennetts@canonical.com-20070410023142-7xgeph6572ikrnu8
Cosmetic changes to minimise the difference between this branch and the hpss branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 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
 
 
17
"""Server for smart-server protocol."""
 
18
 
 
19
import socket
 
20
import os
 
21
import threading
 
22
 
 
23
from bzrlib.smart import medium
 
24
from bzrlib import (
 
25
    trace,
 
26
    transport,
 
27
    urlutils,
 
28
)
 
29
from bzrlib.smart.medium import SmartServerSocketStreamMedium
 
30
 
 
31
 
 
32
class SmartTCPServer(object):
 
33
    """Listens on a TCP socket and accepts connections from smart clients"""
 
34
 
 
35
    def __init__(self, backing_transport, host='127.0.0.1', port=0):
 
36
        """Construct a new server.
 
37
 
 
38
        To actually start it running, call either start_background_thread or
 
39
        serve.
 
40
 
 
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.
 
43
        """
 
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
 
50
 
 
51
    def serve(self):
 
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:
 
59
            try:
 
60
                self.accept_and_serve()
 
61
            except socket_timeout:
 
62
                # just check if we're asked to stop
 
63
                pass
 
64
            except socket_error, e:
 
65
                trace.warning("client disconnected: %s", e)
 
66
                pass
 
67
 
 
68
    def get_url(self):
 
69
        """Return the url of the server"""
 
70
        return "bzr://%s:%d/" % self._server_socket.getsockname()
 
71
 
 
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()
 
82
 
 
83
    def start_background_thread(self):
 
84
        self._server_thread = threading.Thread(None,
 
85
                self.serve,
 
86
                name='server-' + self.get_url())
 
87
        self._server_thread.setDaemon(True)
 
88
        self._server_thread.start()
 
89
 
 
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()
 
98
 
 
99
 
 
100
class SmartTCPServer_for_testing(SmartTCPServer):
 
101
    """Server suitable for use by transport tests.
 
102
    
 
103
    This server is backed by the process's cwd.
 
104
    """
 
105
 
 
106
    def __init__(self):
 
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
 
112
        # directory.
 
113
        SmartTCPServer.__init__(self,
 
114
            transport.get_transport(urlutils.local_path_to_url('/')))
 
115
        
 
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())
 
119
 
 
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()
 
131
 
 
132
    def tearDown(self):
 
133
        self.stop_background_thread()
 
134
 
 
135
    def get_bogus_url(self):
 
136
        """Return a URL which will fail to connect"""
 
137
        return 'bzr://127.0.0.1:1/'
 
138
 
 
139