2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
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 |
||
2018.5.19
by Andrew Bennetts
Add docstrings to all the new modules, and a few other places. |
17 |
"""The 'medium' layer for the smart servers and clients.
|
18 |
||
19 |
"Medium" here is the noun meaning "a means of transmission", not the adjective
|
|
20 |
for "the quality between big and small."
|
|
21 |
||
22 |
Media carry the bytes of the requests somehow (e.g. via TCP, wrapped in HTTP, or
|
|
23 |
over SSH), and pass them to and from the protocol logic. See the overview in
|
|
24 |
bzrlib/transport/smart/__init__.py.
|
|
25 |
"""
|
|
26 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
27 |
import os |
28 |
import socket |
|
2018.5.162
by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import. |
29 |
import sys |
3431.3.11
by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient. |
30 |
import urllib |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
31 |
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
32 |
from bzrlib.lazy_import import lazy_import |
33 |
lazy_import(globals(), """ |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
34 |
from bzrlib import (
|
35 |
errors,
|
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
36 |
osutils,
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
37 |
symbol_versioning,
|
3431.3.11
by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient. |
38 |
urlutils,
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
39 |
)
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
40 |
from bzrlib.smart import protocol
|
3066.2.1
by John Arbash Meinel
We don't require paramiko for bzr+ssh. |
41 |
from bzrlib.transport import ssh
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
42 |
""") |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
43 |
|
2018.5.17
by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler. |
44 |
|
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
45 |
def _get_protocol_factory_for_bytes(bytes): |
46 |
"""Determine the right protocol factory for 'bytes'.
|
|
47 |
||
48 |
This will return an appropriate protocol factory depending on the version
|
|
49 |
of the protocol being used, as determined by inspecting the given bytes.
|
|
50 |
The bytes should have at least one newline byte (i.e. be a whole line),
|
|
51 |
otherwise it's possible that a request will be incorrectly identified as
|
|
52 |
version 1.
|
|
53 |
||
54 |
Typical use would be::
|
|
55 |
||
56 |
factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
|
|
57 |
server_protocol = factory(transport, write_func, root_client_path)
|
|
58 |
server_protocol.accept_bytes(unused_bytes)
|
|
59 |
||
60 |
:param bytes: a str of bytes of the start of the request.
|
|
61 |
:returns: 2-tuple of (protocol_factory, unused_bytes). protocol_factory is
|
|
62 |
a callable that takes three args: transport, write_func,
|
|
63 |
root_client_path. unused_bytes are any bytes that were not part of a
|
|
64 |
protocol version marker.
|
|
65 |
"""
|
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
66 |
if bytes.startswith(protocol.MESSAGE_VERSION_THREE): |
67 |
protocol_factory = protocol.build_server_protocol_three |
|
68 |
bytes = bytes[len(protocol.MESSAGE_VERSION_THREE):] |
|
69 |
elif bytes.startswith(protocol.REQUEST_VERSION_TWO): |
|
70 |
protocol_factory = protocol.SmartServerRequestProtocolTwo |
|
71 |
bytes = bytes[len(protocol.REQUEST_VERSION_TWO):] |
|
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
72 |
else: |
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
73 |
protocol_factory = protocol.SmartServerRequestProtocolOne |
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
74 |
return protocol_factory, bytes |
75 |
||
76 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
77 |
class SmartServerStreamMedium(object): |
78 |
"""Handles smart commands coming over a stream.
|
|
79 |
||
80 |
The stream may be a pipe connected to sshd, or a tcp socket, or an
|
|
81 |
in-process fifo for testing.
|
|
82 |
||
83 |
One instance is created for each connected client; it can serve multiple
|
|
84 |
requests in the lifetime of the connection.
|
|
85 |
||
86 |
The server passes requests through to an underlying backing transport,
|
|
87 |
which will typically be a LocalTransport looking at the server's filesystem.
|
|
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
88 |
|
89 |
:ivar _push_back_buffer: a str of bytes that have been read from the stream
|
|
90 |
but not used yet, or None if there are no buffered bytes. Subclasses
|
|
91 |
should make sure to exhaust this buffer before reading more bytes from
|
|
92 |
the stream. See also the _push_back method.
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
93 |
"""
|
94 |
||
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
95 |
def __init__(self, backing_transport, root_client_path='/'): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
96 |
"""Construct new server.
|
97 |
||
98 |
:param backing_transport: Transport for the directory served.
|
|
99 |
"""
|
|
100 |
# backing_transport could be passed to serve instead of __init__
|
|
101 |
self.backing_transport = backing_transport |
|
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
102 |
self.root_client_path = root_client_path |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
103 |
self.finished = False |
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
104 |
self._push_back_buffer = None |
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
105 |
|
106 |
def _push_back(self, bytes): |
|
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
107 |
"""Return unused bytes to the medium, because they belong to the next
|
108 |
request(s).
|
|
109 |
||
110 |
This sets the _push_back_buffer to the given bytes.
|
|
111 |
"""
|
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
112 |
if self._push_back_buffer is not None: |
113 |
raise AssertionError( |
|
114 |
"_push_back called when self._push_back_buffer is %r" |
|
115 |
% (self._push_back_buffer,)) |
|
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
116 |
if bytes == '': |
117 |
return
|
|
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
118 |
self._push_back_buffer = bytes |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
119 |
|
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
120 |
def _get_push_back_buffer(self): |
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
121 |
if self._push_back_buffer == '': |
122 |
raise AssertionError( |
|
123 |
'%s._push_back_buffer should never be the empty string, ' |
|
124 |
'which can be confused with EOF' % (self,)) |
|
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
125 |
bytes = self._push_back_buffer |
126 |
self._push_back_buffer = None |
|
127 |
return bytes |
|
128 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
129 |
def serve(self): |
130 |
"""Serve requests until the client disconnects."""
|
|
131 |
# Keep a reference to stderr because the sys module's globals get set to
|
|
132 |
# None during interpreter shutdown.
|
|
133 |
from sys import stderr |
|
134 |
try: |
|
135 |
while not self.finished: |
|
2432.2.3
by Andrew Bennetts
Merge from bzr.dev. |
136 |
server_protocol = self._build_protocol() |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
137 |
self._serve_one_request(server_protocol) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
138 |
except Exception, e: |
139 |
stderr.write("%s terminating on exception %s\n" % (self, e)) |
|
140 |
raise
|
|
141 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
142 |
def _build_protocol(self): |
2432.2.8
by Andrew Bennetts
NEWS entry, greatly improved docstring in bzrlib.smart. |
143 |
"""Identifies the version of the incoming request, and returns an
|
144 |
a protocol object that can interpret it.
|
|
145 |
||
146 |
If more bytes than the version prefix of the request are read, they will
|
|
147 |
be fed into the protocol before it is returned.
|
|
148 |
||
149 |
:returns: a SmartServerRequestProtocol.
|
|
150 |
"""
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
151 |
bytes = self._get_line() |
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
152 |
protocol_factory, unused_bytes = _get_protocol_factory_for_bytes(bytes) |
3245.4.14
by Andrew Bennetts
Merge from bzr.dev (via loom thread). |
153 |
protocol = protocol_factory( |
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
154 |
self.backing_transport, self._write_out, self.root_client_path) |
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
155 |
protocol.accept_bytes(unused_bytes) |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
156 |
return protocol |
157 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
158 |
def _serve_one_request(self, protocol): |
159 |
"""Read one request from input, process, send back a response.
|
|
160 |
|
|
161 |
:param protocol: a SmartServerRequestProtocol.
|
|
162 |
"""
|
|
163 |
try: |
|
164 |
self._serve_one_request_unguarded(protocol) |
|
165 |
except KeyboardInterrupt: |
|
166 |
raise
|
|
167 |
except Exception, e: |
|
168 |
self.terminate_due_to_error() |
|
169 |
||
170 |
def terminate_due_to_error(self): |
|
171 |
"""Called when an unhandled exception from the protocol occurs."""
|
|
172 |
raise NotImplementedError(self.terminate_due_to_error) |
|
173 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
174 |
def _get_bytes(self, desired_count): |
175 |
"""Get some bytes from the medium.
|
|
176 |
||
177 |
:param desired_count: number of bytes we want to read.
|
|
178 |
"""
|
|
179 |
raise NotImplementedError(self._get_bytes) |
|
180 |
||
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
181 |
def _get_line(self): |
182 |
"""Read bytes from this request's response until a newline byte.
|
|
183 |
|
|
184 |
This isn't particularly efficient, so should only be used when the
|
|
185 |
expected size of the line is quite short.
|
|
186 |
||
187 |
:returns: a string of bytes ending in a newline (byte 0x0A).
|
|
188 |
"""
|
|
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
189 |
newline_pos = -1 |
190 |
bytes = '' |
|
191 |
while newline_pos == -1: |
|
192 |
new_bytes = self._get_bytes(1) |
|
193 |
bytes += new_bytes |
|
194 |
if new_bytes == '': |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
195 |
# Ran out of bytes before receiving a complete line.
|
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
196 |
return bytes |
197 |
newline_pos = bytes.find('\n') |
|
198 |
line = bytes[:newline_pos+1] |
|
199 |
self._push_back(bytes[newline_pos+1:]) |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
200 |
return line |
3236.3.2
by Andrew Bennetts
Fix SmartServerPipeStreamMedium._get_line too. |
201 |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
202 |
|
203 |
class SmartServerSocketStreamMedium(SmartServerStreamMedium): |
|
204 |
||
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
205 |
def __init__(self, sock, backing_transport, root_client_path='/'): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
206 |
"""Constructor.
|
207 |
||
208 |
:param sock: the socket the server will read from. It will be put
|
|
209 |
into blocking mode.
|
|
210 |
"""
|
|
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
211 |
SmartServerStreamMedium.__init__( |
212 |
self, backing_transport, root_client_path=root_client_path) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
213 |
sock.setblocking(True) |
214 |
self.socket = sock |
|
215 |
||
216 |
def _serve_one_request_unguarded(self, protocol): |
|
217 |
while protocol.next_read_size(): |
|
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
218 |
bytes = self._get_bytes(4096) |
219 |
if bytes == '': |
|
220 |
self.finished = True |
|
221 |
return
|
|
222 |
protocol.accept_bytes(bytes) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
223 |
|
3245.4.21
by Andrew Bennetts
Remove 'excess_buffer' attribute and another crufty comment. |
224 |
self._push_back(protocol.unused_data) |
3195.3.18
by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though. |
225 |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
226 |
def _get_bytes(self, desired_count): |
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
227 |
if self._push_back_buffer is not None: |
228 |
return self._get_push_back_buffer() |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
229 |
# We ignore the desired_count because on sockets it's more efficient to
|
230 |
# read 4k at a time.
|
|
231 |
return self.socket.recv(4096) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
232 |
|
233 |
def terminate_due_to_error(self): |
|
3245.4.59
by Andrew Bennetts
Various tweaks in response to Martin's review. |
234 |
# TODO: This should log to a server log file, but no such thing
|
235 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
236 |
self.socket.close() |
237 |
self.finished = True |
|
238 |
||
239 |
def _write_out(self, bytes): |
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
240 |
osutils.send_all(self.socket, bytes) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
241 |
|
242 |
||
243 |
class SmartServerPipeStreamMedium(SmartServerStreamMedium): |
|
244 |
||
245 |
def __init__(self, in_file, out_file, backing_transport): |
|
246 |
"""Construct new server.
|
|
247 |
||
248 |
:param in_file: Python file from which requests can be read.
|
|
249 |
:param out_file: Python file to write responses.
|
|
250 |
:param backing_transport: Transport for the directory served.
|
|
251 |
"""
|
|
252 |
SmartServerStreamMedium.__init__(self, backing_transport) |
|
2018.5.161
by Andrew Bennetts
Reinstate forcing binary mode on windows in SmartServerStreamMedium. |
253 |
if sys.platform == 'win32': |
254 |
# force binary mode for files
|
|
255 |
import msvcrt |
|
256 |
for f in (in_file, out_file): |
|
257 |
fileno = getattr(f, 'fileno', None) |
|
258 |
if fileno: |
|
259 |
msvcrt.setmode(fileno(), os.O_BINARY) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
260 |
self._in = in_file |
261 |
self._out = out_file |
|
262 |
||
263 |
def _serve_one_request_unguarded(self, protocol): |
|
264 |
while True: |
|
265 |
bytes_to_read = protocol.next_read_size() |
|
266 |
if bytes_to_read == 0: |
|
267 |
# Finished serving this request.
|
|
268 |
self._out.flush() |
|
269 |
return
|
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
270 |
bytes = self._get_bytes(bytes_to_read) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
271 |
if bytes == '': |
272 |
# Connection has been closed.
|
|
273 |
self.finished = True |
|
274 |
self._out.flush() |
|
275 |
return
|
|
276 |
protocol.accept_bytes(bytes) |
|
277 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
278 |
def _get_bytes(self, desired_count): |
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
279 |
if self._push_back_buffer is not None: |
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
280 |
return self._get_push_back_buffer() |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
281 |
return self._in.read(desired_count) |
282 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
283 |
def terminate_due_to_error(self): |
284 |
# TODO: This should log to a server log file, but no such thing
|
|
285 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
286 |
self._out.close() |
|
287 |
self.finished = True |
|
288 |
||
289 |
def _write_out(self, bytes): |
|
290 |
self._out.write(bytes) |
|
291 |
||
292 |
||
293 |
class SmartClientMediumRequest(object): |
|
294 |
"""A request on a SmartClientMedium.
|
|
295 |
||
296 |
Each request allows bytes to be provided to it via accept_bytes, and then
|
|
297 |
the response bytes to be read via read_bytes.
|
|
298 |
||
299 |
For instance:
|
|
300 |
request.accept_bytes('123')
|
|
301 |
request.finished_writing()
|
|
302 |
result = request.read_bytes(3)
|
|
303 |
request.finished_reading()
|
|
304 |
||
305 |
It is up to the individual SmartClientMedium whether multiple concurrent
|
|
306 |
requests can exist. See SmartClientMedium.get_request to obtain instances
|
|
307 |
of SmartClientMediumRequest, and the concrete Medium you are using for
|
|
308 |
details on concurrency and pipelining.
|
|
309 |
"""
|
|
310 |
||
311 |
def __init__(self, medium): |
|
312 |
"""Construct a SmartClientMediumRequest for the medium medium."""
|
|
313 |
self._medium = medium |
|
314 |
# we track state by constants - we may want to use the same
|
|
315 |
# pattern as BodyReader if it gets more complex.
|
|
316 |
# valid states are: "writing", "reading", "done"
|
|
317 |
self._state = "writing" |
|
318 |
||
319 |
def accept_bytes(self, bytes): |
|
320 |
"""Accept bytes for inclusion in this request.
|
|
321 |
||
322 |
This method may not be be called after finished_writing() has been
|
|
323 |
called. It depends upon the Medium whether or not the bytes will be
|
|
324 |
immediately transmitted. Message based Mediums will tend to buffer the
|
|
325 |
bytes until finished_writing() is called.
|
|
326 |
||
327 |
:param bytes: A bytestring.
|
|
328 |
"""
|
|
329 |
if self._state != "writing": |
|
330 |
raise errors.WritingCompleted(self) |
|
331 |
self._accept_bytes(bytes) |
|
332 |
||
333 |
def _accept_bytes(self, bytes): |
|
334 |
"""Helper for accept_bytes.
|
|
335 |
||
336 |
Accept_bytes checks the state of the request to determing if bytes
|
|
337 |
should be accepted. After that it hands off to _accept_bytes to do the
|
|
338 |
actual acceptance.
|
|
339 |
"""
|
|
340 |
raise NotImplementedError(self._accept_bytes) |
|
341 |
||
342 |
def finished_reading(self): |
|
343 |
"""Inform the request that all desired data has been read.
|
|
344 |
||
345 |
This will remove the request from the pipeline for its medium (if the
|
|
346 |
medium supports pipelining) and any further calls to methods on the
|
|
347 |
request will raise ReadingCompleted.
|
|
348 |
"""
|
|
349 |
if self._state == "writing": |
|
350 |
raise errors.WritingNotComplete(self) |
|
351 |
if self._state != "reading": |
|
352 |
raise errors.ReadingCompleted(self) |
|
353 |
self._state = "done" |
|
354 |
self._finished_reading() |
|
355 |
||
356 |
def _finished_reading(self): |
|
357 |
"""Helper for finished_reading.
|
|
358 |
||
359 |
finished_reading checks the state of the request to determine if
|
|
360 |
finished_reading is allowed, and if it is hands off to _finished_reading
|
|
361 |
to perform the action.
|
|
362 |
"""
|
|
363 |
raise NotImplementedError(self._finished_reading) |
|
364 |
||
365 |
def finished_writing(self): |
|
366 |
"""Finish the writing phase of this request.
|
|
367 |
||
368 |
This will flush all pending data for this request along the medium.
|
|
369 |
After calling finished_writing, you may not call accept_bytes anymore.
|
|
370 |
"""
|
|
371 |
if self._state != "writing": |
|
372 |
raise errors.WritingCompleted(self) |
|
373 |
self._state = "reading" |
|
374 |
self._finished_writing() |
|
375 |
||
376 |
def _finished_writing(self): |
|
377 |
"""Helper for finished_writing.
|
|
378 |
||
379 |
finished_writing checks the state of the request to determine if
|
|
380 |
finished_writing is allowed, and if it is hands off to _finished_writing
|
|
381 |
to perform the action.
|
|
382 |
"""
|
|
383 |
raise NotImplementedError(self._finished_writing) |
|
384 |
||
385 |
def read_bytes(self, count): |
|
386 |
"""Read bytes from this requests response.
|
|
387 |
||
388 |
This method will block and wait for count bytes to be read. It may not
|
|
389 |
be invoked until finished_writing() has been called - this is to ensure
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
390 |
a message-based approach to requests, for compatibility with message
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
391 |
based mediums like HTTP.
|
392 |
"""
|
|
393 |
if self._state == "writing": |
|
394 |
raise errors.WritingNotComplete(self) |
|
395 |
if self._state != "reading": |
|
396 |
raise errors.ReadingCompleted(self) |
|
397 |
return self._read_bytes(count) |
|
398 |
||
399 |
def _read_bytes(self, count): |
|
400 |
"""Helper for read_bytes.
|
|
401 |
||
402 |
read_bytes checks the state of the request to determing if bytes
|
|
403 |
should be read. After that it hands off to _read_bytes to do the
|
|
404 |
actual read.
|
|
405 |
"""
|
|
406 |
raise NotImplementedError(self._read_bytes) |
|
407 |
||
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
408 |
def read_line(self): |
409 |
"""Read bytes from this request's response until a newline byte.
|
|
410 |
|
|
411 |
This isn't particularly efficient, so should only be used when the
|
|
412 |
expected size of the line is quite short.
|
|
413 |
||
414 |
:returns: a string of bytes ending in a newline (byte 0x0A).
|
|
415 |
"""
|
|
416 |
# XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
|
|
417 |
line = '' |
|
418 |
while not line or line[-1] != '\n': |
|
419 |
new_char = self.read_bytes(1) |
|
420 |
line += new_char |
|
421 |
if new_char == '': |
|
3195.2.1
by Andrew Bennetts
Improve test coverage, and fix a bug revealed by the improved coverage. |
422 |
# end of file encountered reading from server
|
423 |
raise errors.ConnectionReset( |
|
424 |
"please check connectivity and permissions", |
|
425 |
"(and try -Dhpss if further diagnosis is required)") |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
426 |
return line |
427 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
428 |
|
429 |
class SmartClientMedium(object): |
|
430 |
"""Smart client is a medium for sending smart protocol requests over."""
|
|
431 |
||
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
432 |
def __init__(self, base): |
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
433 |
super(SmartClientMedium, self).__init__() |
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
434 |
self.base = base |
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
435 |
self._protocol_version_error = None |
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
436 |
self._protocol_version = None |
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
437 |
self._done_hello = False |
3435.1.1
by Andrew Bennetts
Define _remote_is_at_least_1_2 on SmartClientMedium base class, rather than just SmartClientStreamMedium. |
438 |
# Be optimistic: we assume the remote end can accept new remote
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
439 |
# requests until we get an error saying otherwise.
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
440 |
# _remote_version_is_before tracks the bzr version the remote side
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
441 |
# can be based on what we've seen so far.
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
442 |
self._remote_version_is_before = None |
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
443 |
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
444 |
def _is_remote_before(self, version_tuple): |
3502.1.1
by Matt Nordhoff
Fix a docstring typo, and a two-expression ``raise`` statement |
445 |
"""Is it possible the remote side supports RPCs for a given version?
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
446 |
|
447 |
Typical use::
|
|
448 |
||
449 |
needed_version = (1, 2)
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
450 |
if medium._is_remote_before(needed_version):
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
451 |
fallback_to_pre_1_2_rpc()
|
452 |
else:
|
|
453 |
try:
|
|
454 |
do_1_2_rpc()
|
|
455 |
except UnknownSmartMethod:
|
|
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
456 |
medium._remember_remote_is_before(needed_version)
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
457 |
fallback_to_pre_1_2_rpc()
|
458 |
||
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
459 |
:seealso: _remember_remote_is_before
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
460 |
"""
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
461 |
if self._remote_version_is_before is None: |
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
462 |
# So far, the remote side seems to support everything
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
463 |
return False |
464 |
return version_tuple >= self._remote_version_is_before |
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
465 |
|
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
466 |
def _remember_remote_is_before(self, version_tuple): |
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
467 |
"""Tell this medium that the remote side is older the given version.
|
468 |
||
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
469 |
:seealso: _is_remote_before
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
470 |
"""
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
471 |
if (self._remote_version_is_before is not None and |
472 |
version_tuple > self._remote_version_is_before): |
|
3502.1.1
by Matt Nordhoff
Fix a docstring typo, and a two-expression ``raise`` statement |
473 |
raise AssertionError( |
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
474 |
"_remember_remote_is_before(%r) called, but " |
475 |
"_remember_remote_is_before(%r) was called previously." |
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
476 |
% (version_tuple, self._remote_version_is_before)) |
477 |
self._remote_version_is_before = version_tuple |
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
478 |
|
479 |
def protocol_version(self): |
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
480 |
"""Find out if 'hello' smart request works."""
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
481 |
if self._protocol_version_error is not None: |
482 |
raise self._protocol_version_error |
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
483 |
if not self._done_hello: |
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
484 |
try: |
485 |
medium_request = self.get_request() |
|
486 |
# Send a 'hello' request in protocol version one, for maximum
|
|
487 |
# backwards compatibility.
|
|
3530.1.2
by John Arbash Meinel
missed one of the imports |
488 |
client_protocol = protocol.SmartClientRequestProtocolOne(medium_request) |
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
489 |
client_protocol.query_version() |
490 |
self._done_hello = True |
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
491 |
except errors.SmartProtocolError, e: |
492 |
# Cache the error, just like we would cache a successful
|
|
493 |
# result.
|
|
494 |
self._protocol_version_error = e |
|
495 |
raise
|
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
496 |
return '2' |
497 |
||
498 |
def should_probe(self): |
|
499 |
"""Should RemoteBzrDirFormat.probe_transport send a smart request on
|
|
500 |
this medium?
|
|
501 |
||
502 |
Some transports are unambiguously smart-only; there's no need to check
|
|
503 |
if the transport is able to carry smart requests, because that's all
|
|
504 |
it is for. In those cases, this method should return False.
|
|
505 |
||
506 |
But some HTTP transports can sometimes fail to carry smart requests,
|
|
507 |
but still be usuable for accessing remote bzrdirs via plain file
|
|
508 |
accesses. So for those transports, their media should return True here
|
|
509 |
so that RemoteBzrDirFormat can determine if it is appropriate for that
|
|
510 |
transport.
|
|
511 |
"""
|
|
512 |
return False |
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
513 |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
514 |
def disconnect(self): |
515 |
"""If this medium maintains a persistent connection, close it.
|
|
516 |
|
|
517 |
The default implementation does nothing.
|
|
518 |
"""
|
|
519 |
||
3431.3.11
by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient. |
520 |
def remote_path_from_transport(self, transport): |
521 |
"""Convert transport into a path suitable for using in a request.
|
|
522 |
|
|
523 |
Note that the resulting remote path doesn't encode the host name or
|
|
524 |
anything but path, so it is only safe to use it in requests sent over
|
|
525 |
the medium from the matching transport.
|
|
526 |
"""
|
|
527 |
medium_base = urlutils.join(self.base, '/') |
|
528 |
rel_url = urlutils.relative_url(medium_base, transport.base) |
|
529 |
return urllib.unquote(rel_url) |
|
530 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
531 |
|
532 |
class SmartClientStreamMedium(SmartClientMedium): |
|
533 |
"""Stream based medium common class.
|
|
534 |
||
535 |
SmartClientStreamMediums operate on a stream. All subclasses use a common
|
|
536 |
SmartClientStreamMediumRequest for their requests, and should implement
|
|
537 |
_accept_bytes and _read_bytes to allow the request objects to send and
|
|
538 |
receive bytes.
|
|
539 |
"""
|
|
540 |
||
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
541 |
def __init__(self, base): |
542 |
SmartClientMedium.__init__(self, base) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
543 |
self._current_request = None |
544 |
||
545 |
def accept_bytes(self, bytes): |
|
546 |
self._accept_bytes(bytes) |
|
547 |
||
548 |
def __del__(self): |
|
549 |
"""The SmartClientStreamMedium knows how to close the stream when it is
|
|
550 |
finished with it.
|
|
551 |
"""
|
|
552 |
self.disconnect() |
|
553 |
||
554 |
def _flush(self): |
|
555 |
"""Flush the output stream.
|
|
556 |
|
|
557 |
This method is used by the SmartClientStreamMediumRequest to ensure that
|
|
558 |
all data for a request is sent, to avoid long timeouts or deadlocks.
|
|
559 |
"""
|
|
560 |
raise NotImplementedError(self._flush) |
|
561 |
||
562 |
def get_request(self): |
|
563 |
"""See SmartClientMedium.get_request().
|
|
564 |
||
565 |
SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
|
|
566 |
for get_request.
|
|
567 |
"""
|
|
568 |
return SmartClientStreamMediumRequest(self) |
|
569 |
||
570 |
def read_bytes(self, count): |
|
571 |
return self._read_bytes(count) |
|
572 |
||
573 |
||
574 |
class SmartSimplePipesClientMedium(SmartClientStreamMedium): |
|
575 |
"""A client medium using simple pipes.
|
|
576 |
|
|
577 |
This client does not manage the pipes: it assumes they will always be open.
|
|
578 |
"""
|
|
579 |
||
3431.3.1
by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media. |
580 |
def __init__(self, readable_pipe, writeable_pipe, base): |
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
581 |
SmartClientStreamMedium.__init__(self, base) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
582 |
self._readable_pipe = readable_pipe |
583 |
self._writeable_pipe = writeable_pipe |
|
584 |
||
585 |
def _accept_bytes(self, bytes): |
|
586 |
"""See SmartClientStreamMedium.accept_bytes."""
|
|
587 |
self._writeable_pipe.write(bytes) |
|
588 |
||
589 |
def _flush(self): |
|
590 |
"""See SmartClientStreamMedium._flush()."""
|
|
591 |
self._writeable_pipe.flush() |
|
592 |
||
593 |
def _read_bytes(self, count): |
|
594 |
"""See SmartClientStreamMedium._read_bytes."""
|
|
595 |
return self._readable_pipe.read(count) |
|
596 |
||
597 |
||
598 |
class SmartSSHClientMedium(SmartClientStreamMedium): |
|
599 |
"""A client medium using SSH."""
|
|
600 |
||
601 |
def __init__(self, host, port=None, username=None, password=None, |
|
3431.3.1
by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media. |
602 |
base=None, vendor=None, bzr_remote_path=None): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
603 |
"""Creates a client that will connect on the first use.
|
604 |
|
|
605 |
:param vendor: An optional override for the ssh vendor to use. See
|
|
606 |
bzrlib.transport.ssh for details on ssh vendors.
|
|
607 |
"""
|
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
608 |
SmartClientStreamMedium.__init__(self, base) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
609 |
self._connected = False |
610 |
self._host = host |
|
611 |
self._password = password |
|
612 |
self._port = port |
|
613 |
self._username = username |
|
614 |
self._read_from = None |
|
615 |
self._ssh_connection = None |
|
616 |
self._vendor = vendor |
|
617 |
self._write_to = None |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
618 |
self._bzr_remote_path = bzr_remote_path |
619 |
if self._bzr_remote_path is None: |
|
620 |
symbol_versioning.warn( |
|
621 |
'bzr_remote_path is required as of bzr 0.92', |
|
622 |
DeprecationWarning, stacklevel=2) |
|
623 |
self._bzr_remote_path = os.environ.get('BZR_REMOTE_PATH', 'bzr') |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
624 |
|
625 |
def _accept_bytes(self, bytes): |
|
626 |
"""See SmartClientStreamMedium.accept_bytes."""
|
|
627 |
self._ensure_connection() |
|
628 |
self._write_to.write(bytes) |
|
629 |
||
630 |
def disconnect(self): |
|
631 |
"""See SmartClientMedium.disconnect()."""
|
|
632 |
if not self._connected: |
|
633 |
return
|
|
634 |
self._read_from.close() |
|
635 |
self._write_to.close() |
|
636 |
self._ssh_connection.close() |
|
637 |
self._connected = False |
|
638 |
||
639 |
def _ensure_connection(self): |
|
640 |
"""Connect this medium if not already connected."""
|
|
641 |
if self._connected: |
|
642 |
return
|
|
643 |
if self._vendor is None: |
|
644 |
vendor = ssh._get_ssh_vendor() |
|
645 |
else: |
|
646 |
vendor = self._vendor |
|
647 |
self._ssh_connection = vendor.connect_ssh(self._username, |
|
648 |
self._password, self._host, self._port, |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
649 |
command=[self._bzr_remote_path, 'serve', '--inet', |
650 |
'--directory=/', '--allow-writes']) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
651 |
self._read_from, self._write_to = \ |
652 |
self._ssh_connection.get_filelike_channels() |
|
653 |
self._connected = True |
|
654 |
||
655 |
def _flush(self): |
|
656 |
"""See SmartClientStreamMedium._flush()."""
|
|
657 |
self._write_to.flush() |
|
658 |
||
659 |
def _read_bytes(self, count): |
|
660 |
"""See SmartClientStreamMedium.read_bytes."""
|
|
661 |
if not self._connected: |
|
662 |
raise errors.MediumNotConnected(self) |
|
663 |
return self._read_from.read(count) |
|
664 |
||
665 |
||
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
666 |
# Port 4155 is the default port for bzr://, registered with IANA.
|
667 |
BZR_DEFAULT_INTERFACE = '0.0.0.0' |
|
668 |
BZR_DEFAULT_PORT = 4155 |
|
669 |
||
670 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
671 |
class SmartTCPClientMedium(SmartClientStreamMedium): |
672 |
"""A client medium using TCP."""
|
|
673 |
||
3431.3.1
by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media. |
674 |
def __init__(self, host, port, base): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
675 |
"""Creates a client that will connect on the first use."""
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
676 |
SmartClientStreamMedium.__init__(self, base) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
677 |
self._connected = False |
678 |
self._host = host |
|
679 |
self._port = port |
|
680 |
self._socket = None |
|
681 |
||
682 |
def _accept_bytes(self, bytes): |
|
683 |
"""See SmartClientMedium.accept_bytes."""
|
|
684 |
self._ensure_connection() |
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
685 |
osutils.send_all(self._socket, bytes) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
686 |
|
687 |
def disconnect(self): |
|
688 |
"""See SmartClientMedium.disconnect()."""
|
|
689 |
if not self._connected: |
|
690 |
return
|
|
691 |
self._socket.close() |
|
692 |
self._socket = None |
|
693 |
self._connected = False |
|
694 |
||
695 |
def _ensure_connection(self): |
|
696 |
"""Connect this medium if not already connected."""
|
|
697 |
if self._connected: |
|
698 |
return
|
|
699 |
self._socket = socket.socket() |
|
700 |
self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
|
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
701 |
if self._port is None: |
702 |
port = BZR_DEFAULT_PORT |
|
703 |
else: |
|
704 |
port = int(self._port) |
|
3180.1.1
by Andrew Bennetts
Don't traceback on host name errors when connecting to bzr://... |
705 |
try: |
706 |
self._socket.connect((self._host, port)) |
|
707 |
except socket.error, err: |
|
708 |
# socket errors either have a (string) or (errno, string) as their
|
|
709 |
# args.
|
|
710 |
if type(err.args) is str: |
|
711 |
err_msg = err.args |
|
712 |
else: |
|
713 |
err_msg = err.args[1] |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
714 |
raise errors.ConnectionError("failed to connect to %s:%d: %s" % |
3180.1.1
by Andrew Bennetts
Don't traceback on host name errors when connecting to bzr://... |
715 |
(self._host, port, err_msg)) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
716 |
self._connected = True |
717 |
||
718 |
def _flush(self): |
|
719 |
"""See SmartClientStreamMedium._flush().
|
|
720 |
|
|
721 |
For TCP we do no flushing. We may want to turn off TCP_NODELAY and
|
|
722 |
add a means to do a flush, but that can be done in the future.
|
|
723 |
"""
|
|
724 |
||
725 |
def _read_bytes(self, count): |
|
726 |
"""See SmartClientMedium.read_bytes."""
|
|
727 |
if not self._connected: |
|
728 |
raise errors.MediumNotConnected(self) |
|
729 |
return self._socket.recv(count) |
|
730 |
||
731 |
||
732 |
class SmartClientStreamMediumRequest(SmartClientMediumRequest): |
|
733 |
"""A SmartClientMediumRequest that works with an SmartClientStreamMedium."""
|
|
734 |
||
735 |
def __init__(self, medium): |
|
736 |
SmartClientMediumRequest.__init__(self, medium) |
|
737 |
# check that we are safe concurrency wise. If some streams start
|
|
738 |
# allowing concurrent requests - i.e. via multiplexing - then this
|
|
739 |
# assert should be moved to SmartClientStreamMedium.get_request,
|
|
740 |
# and the setting/unsetting of _current_request likewise moved into
|
|
741 |
# that class : but its unneeded overhead for now. RBC 20060922
|
|
742 |
if self._medium._current_request is not None: |
|
743 |
raise errors.TooManyConcurrentRequests(self._medium) |
|
744 |
self._medium._current_request = self |
|
745 |
||
746 |
def _accept_bytes(self, bytes): |
|
747 |
"""See SmartClientMediumRequest._accept_bytes.
|
|
748 |
|
|
749 |
This forwards to self._medium._accept_bytes because we are operating
|
|
750 |
on the mediums stream.
|
|
751 |
"""
|
|
752 |
self._medium._accept_bytes(bytes) |
|
753 |
||
754 |
def _finished_reading(self): |
|
755 |
"""See SmartClientMediumRequest._finished_reading.
|
|
756 |
||
757 |
This clears the _current_request on self._medium to allow a new
|
|
758 |
request to be created.
|
|
759 |
"""
|
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
760 |
if self._medium._current_request is not self: |
761 |
raise AssertionError() |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
762 |
self._medium._current_request = None |
763 |
||
764 |
def _finished_writing(self): |
|
765 |
"""See SmartClientMediumRequest._finished_writing.
|
|
766 |
||
767 |
This invokes self._medium._flush to ensure all bytes are transmitted.
|
|
768 |
"""
|
|
769 |
self._medium._flush() |
|
770 |
||
771 |
def _read_bytes(self, count): |
|
772 |
"""See SmartClientMediumRequest._read_bytes.
|
|
773 |
|
|
774 |
This forwards to self._medium._read_bytes because we are operating
|
|
775 |
on the mediums stream.
|
|
776 |
"""
|
|
777 |
return self._medium._read_bytes(count) |
|
3245.4.32
by Andrew Bennetts
Undo trivial whitespace change relative to bzr.dev. |
778 |