/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/transport/smart/server.py

Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
17
import socket
 
18
import os
 
19
import threading
 
20
 
 
21
from bzrlib.transport.smart import medium
 
22
from bzrlib import (
 
23
    transport,
 
24
    urlutils,
 
25
)
 
26
 
 
27
 
 
28
class SmartTCPServer(object):
 
29
    """Listens on a TCP socket and accepts connections from smart clients.
 
30
    
 
31
    Each connection will be served by a SmartServerSocketStreamMedium running in
 
32
    thread.
 
33
    """
 
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 = 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()
 
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
        # self._server_socket.close()
 
93
        # we used to join the thread, but it's not really necessary; it will
 
94
        # terminate in time
 
95
        ## self._server_thread.join()
 
96
 
 
97
 
 
98
class SmartTCPServer_for_testing(SmartTCPServer):
 
99
    """Server suitable for use by transport tests.
 
100
    
 
101
    This server is backed by the process's cwd.
 
102
    """
 
103
 
 
104
    def __init__(self):
 
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
 
110
        # directory.
 
111
        SmartTCPServer.__init__(self,
 
112
            transport.get_transport(urlutils.local_path_to_url('/')))
 
113
        
 
114
    def setUp(self):
 
115
        """Set up server for testing"""
 
116
        self.start_background_thread()
 
117
 
 
118
    def tearDown(self):
 
119
        self.stop_background_thread()
 
120
 
 
121
    def get_url(self):
 
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))
 
125
 
 
126
    def get_bogus_url(self):
 
127
        """Return a URL which will fail to connect"""
 
128
        return 'bzr://127.0.0.1:1/'
 
129
 
 
130