2018.5.157
by Andrew Bennetts
Remove unnecessary trivial divergences from bzr.dev. |
1 |
# Copyright (C) 2006, 2007 Canonical Ltd
|
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
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 |
"""Wire-level encoding and decoding of requests and responses for the smart
|
18 |
client and server.
|
|
19 |
"""
|
|
20 |
||
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
21 |
from cStringIO import StringIO |
2664.4.1
by John Arbash Meinel
Add timing information for call/response groups for hpss |
22 |
import time |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
23 |
|
2593.3.1
by Andrew Bennetts
Add a -Dhpss debug flag. |
24 |
from bzrlib import debug |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
25 |
from bzrlib import errors |
2018.5.23
by Andrew Bennetts
Use a Registry for smart server command handlers. |
26 |
from bzrlib.smart import request |
2621.3.1
by Andrew Bennetts
Log errors from the smart server in the trace file, to make debugging test failures (and live failures!) easier. |
27 |
from bzrlib.trace import log_exception_quietly, mutter |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
28 |
|
29 |
||
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
30 |
# Protocol version strings. These are sent as prefixes of bzr requests and
|
31 |
# responses to identify the protocol version being used. (There are no version
|
|
32 |
# one strings because that version doesn't send any).
|
|
33 |
REQUEST_VERSION_TWO = 'bzr request 2\n' |
|
34 |
RESPONSE_VERSION_TWO = 'bzr response 2\n' |
|
35 |
||
36 |
||
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
37 |
def _recv_tuple(from_file): |
38 |
req_line = from_file.readline() |
|
39 |
return _decode_tuple(req_line) |
|
40 |
||
41 |
||
42 |
def _decode_tuple(req_line): |
|
43 |
if req_line == None or req_line == '': |
|
44 |
return None |
|
45 |
if req_line[-1] != '\n': |
|
46 |
raise errors.SmartProtocolError("request %r not terminated" % req_line) |
|
47 |
return tuple(req_line[:-1].split('\x01')) |
|
48 |
||
49 |
||
50 |
def _encode_tuple(args): |
|
51 |
"""Encode the tuple args to a bytestream."""
|
|
52 |
return '\x01'.join(args) + '\n' |
|
53 |
||
54 |
||
55 |
class SmartProtocolBase(object): |
|
56 |
"""Methods common to client and server"""
|
|
57 |
||
58 |
# TODO: this only actually accomodates a single block; possibly should
|
|
59 |
# support multiple chunks?
|
|
60 |
def _encode_bulk_data(self, body): |
|
61 |
"""Encode body as a bulk data chunk."""
|
|
62 |
return ''.join(('%d\n' % len(body), body, 'done\n')) |
|
63 |
||
64 |
def _serialise_offsets(self, offsets): |
|
65 |
"""Serialise a readv offset list."""
|
|
66 |
txt = [] |
|
67 |
for start, length in offsets: |
|
68 |
txt.append('%d,%d' % (start, length)) |
|
69 |
return '\n'.join(txt) |
|
70 |
||
71 |
||
72 |
class SmartServerRequestProtocolOne(SmartProtocolBase): |
|
73 |
"""Server-side encoding and decoding logic for smart version 1."""
|
|
74 |
||
75 |
def __init__(self, backing_transport, write_func): |
|
76 |
self._backing_transport = backing_transport |
|
77 |
self.excess_buffer = '' |
|
78 |
self._finished = False |
|
79 |
self.in_buffer = '' |
|
80 |
self.has_dispatched = False |
|
81 |
self.request = None |
|
82 |
self._body_decoder = None |
|
2664.4.6
by John Arbash Meinel
Restore a line that shouldn't have been removed |
83 |
self._write_func = write_func |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
84 |
|
85 |
def accept_bytes(self, bytes): |
|
86 |
"""Take bytes, and advance the internal state machine appropriately.
|
|
87 |
|
|
88 |
:param bytes: must be a byte string
|
|
89 |
"""
|
|
90 |
assert isinstance(bytes, str) |
|
91 |
self.in_buffer += bytes |
|
92 |
if not self.has_dispatched: |
|
93 |
if '\n' not in self.in_buffer: |
|
94 |
# no command line yet
|
|
95 |
return
|
|
96 |
self.has_dispatched = True |
|
97 |
try: |
|
98 |
first_line, self.in_buffer = self.in_buffer.split('\n', 1) |
|
99 |
first_line += '\n' |
|
100 |
req_args = _decode_tuple(first_line) |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
101 |
self.request = request.SmartServerRequestHandler( |
2018.5.23
by Andrew Bennetts
Use a Registry for smart server command handlers. |
102 |
self._backing_transport, commands=request.request_handlers) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
103 |
self.request.dispatch_command(req_args[0], req_args[1:]) |
104 |
if self.request.finished_reading: |
|
105 |
# trivial request
|
|
106 |
self.excess_buffer = self.in_buffer |
|
107 |
self.in_buffer = '' |
|
2432.4.3
by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body. |
108 |
self._send_response(self.request.response) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
109 |
except KeyboardInterrupt: |
110 |
raise
|
|
111 |
except Exception, exception: |
|
112 |
# everything else: pass to client, flush, and quit
|
|
2621.3.1
by Andrew Bennetts
Log errors from the smart server in the trace file, to make debugging test failures (and live failures!) easier. |
113 |
log_exception_quietly() |
2432.4.3
by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body. |
114 |
self._send_response(request.FailedSmartServerResponse( |
115 |
('error', str(exception)))) |
|
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
116 |
return
|
117 |
||
118 |
if self.has_dispatched: |
|
119 |
if self._finished: |
|
120 |
# nothing to do.XXX: this routine should be a single state
|
|
121 |
# machine too.
|
|
122 |
self.excess_buffer += self.in_buffer |
|
123 |
self.in_buffer = '' |
|
124 |
return
|
|
125 |
if self._body_decoder is None: |
|
126 |
self._body_decoder = LengthPrefixedBodyDecoder() |
|
127 |
self._body_decoder.accept_bytes(self.in_buffer) |
|
128 |
self.in_buffer = self._body_decoder.unused_data |
|
129 |
body_data = self._body_decoder.read_pending_data() |
|
130 |
self.request.accept_body(body_data) |
|
131 |
if self._body_decoder.finished_reading: |
|
132 |
self.request.end_of_body() |
|
133 |
assert self.request.finished_reading, \ |
|
134 |
"no more body, request not finished"
|
|
135 |
if self.request.response is not None: |
|
2432.4.3
by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body. |
136 |
self._send_response(self.request.response) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
137 |
self.excess_buffer = self.in_buffer |
138 |
self.in_buffer = '' |
|
139 |
else: |
|
140 |
assert not self.request.finished_reading, \ |
|
141 |
"no response and we have finished reading."
|
|
142 |
||
2432.4.3
by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body. |
143 |
def _send_response(self, response): |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
144 |
"""Send a smart server response down the output stream."""
|
145 |
assert not self._finished, 'response already sent' |
|
2432.4.3
by Robert Collins
Refactor the HPSS Response code to take SmartServerResponse rather than args and body. |
146 |
args = response.args |
147 |
body = response.body |
|
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
148 |
self._finished = True |
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
149 |
self._write_protocol_version() |
2432.4.6
by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future. |
150 |
self._write_success_or_failure_prefix(response) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
151 |
self._write_func(_encode_tuple(args)) |
152 |
if body is not None: |
|
153 |
assert isinstance(body, str), 'body must be a str' |
|
154 |
bytes = self._encode_bulk_data(body) |
|
155 |
self._write_func(bytes) |
|
156 |
||
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
157 |
def _write_protocol_version(self): |
158 |
"""Write any prefixes this protocol requires.
|
|
159 |
|
|
160 |
Version one doesn't send protocol versions.
|
|
161 |
"""
|
|
162 |
||
2432.4.6
by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future. |
163 |
def _write_success_or_failure_prefix(self, response): |
164 |
"""Write the protocol specific success/failure prefix.
|
|
165 |
||
166 |
For SmartServerRequestProtocolOne this is omitted but we
|
|
167 |
call is_successful to ensure that the response is valid.
|
|
168 |
"""
|
|
169 |
response.is_successful() |
|
170 |
||
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
171 |
def next_read_size(self): |
172 |
if self._finished: |
|
173 |
return 0 |
|
174 |
if self._body_decoder is None: |
|
175 |
return 1 |
|
176 |
else: |
|
177 |
return self._body_decoder.next_read_size() |
|
178 |
||
179 |
||
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
180 |
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne): |
181 |
r"""Version two of the server side of the smart protocol. |
|
182 |
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
183 |
This prefixes responses with the value of RESPONSE_VERSION_TWO.
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
184 |
"""
|
185 |
||
2432.4.6
by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future. |
186 |
def _write_success_or_failure_prefix(self, response): |
187 |
"""Write the protocol specific success/failure prefix."""
|
|
188 |
if response.is_successful(): |
|
189 |
self._write_func('success\n') |
|
190 |
else: |
|
191 |
self._write_func('failed\n') |
|
192 |
||
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
193 |
def _write_protocol_version(self): |
194 |
r"""Write any prefixes this protocol requires. |
|
195 |
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
196 |
Version two sends the value of RESPONSE_VERSION_TWO.
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
197 |
"""
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
198 |
self._write_func(RESPONSE_VERSION_TWO) |
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
199 |
|
200 |
||
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
201 |
class LengthPrefixedBodyDecoder(object): |
202 |
"""Decodes the length-prefixed bulk data."""
|
|
203 |
||
204 |
def __init__(self): |
|
205 |
self.bytes_left = None |
|
206 |
self.finished_reading = False |
|
207 |
self.unused_data = '' |
|
208 |
self.state_accept = self._state_accept_expecting_length |
|
209 |
self.state_read = self._state_read_no_data |
|
210 |
self._in_buffer = '' |
|
211 |
self._trailer_buffer = '' |
|
212 |
||
213 |
def accept_bytes(self, bytes): |
|
214 |
"""Decode as much of bytes as possible.
|
|
215 |
||
216 |
If 'bytes' contains too much data it will be appended to
|
|
217 |
self.unused_data.
|
|
218 |
||
219 |
finished_reading will be set when no more data is required. Further
|
|
220 |
data will be appended to self.unused_data.
|
|
221 |
"""
|
|
222 |
# accept_bytes is allowed to change the state
|
|
223 |
current_state = self.state_accept |
|
224 |
self.state_accept(bytes) |
|
225 |
while current_state != self.state_accept: |
|
226 |
current_state = self.state_accept |
|
227 |
self.state_accept('') |
|
228 |
||
229 |
def next_read_size(self): |
|
230 |
if self.bytes_left is not None: |
|
231 |
# Ideally we want to read all the remainder of the body and the
|
|
232 |
# trailer in one go.
|
|
233 |
return self.bytes_left + 5 |
|
234 |
elif self.state_accept == self._state_accept_reading_trailer: |
|
235 |
# Just the trailer left
|
|
236 |
return 5 - len(self._trailer_buffer) |
|
237 |
elif self.state_accept == self._state_accept_expecting_length: |
|
238 |
# There's still at least 6 bytes left ('\n' to end the length, plus
|
|
239 |
# 'done\n').
|
|
240 |
return 6 |
|
241 |
else: |
|
242 |
# Reading excess data. Either way, 1 byte at a time is fine.
|
|
243 |
return 1 |
|
244 |
||
245 |
def read_pending_data(self): |
|
246 |
"""Return any pending data that has been decoded."""
|
|
247 |
return self.state_read() |
|
248 |
||
249 |
def _state_accept_expecting_length(self, bytes): |
|
250 |
self._in_buffer += bytes |
|
251 |
pos = self._in_buffer.find('\n') |
|
252 |
if pos == -1: |
|
253 |
return
|
|
254 |
self.bytes_left = int(self._in_buffer[:pos]) |
|
255 |
self._in_buffer = self._in_buffer[pos+1:] |
|
256 |
self.bytes_left -= len(self._in_buffer) |
|
257 |
self.state_accept = self._state_accept_reading_body |
|
258 |
self.state_read = self._state_read_in_buffer |
|
259 |
||
260 |
def _state_accept_reading_body(self, bytes): |
|
261 |
self._in_buffer += bytes |
|
262 |
self.bytes_left -= len(bytes) |
|
263 |
if self.bytes_left <= 0: |
|
264 |
# Finished with body
|
|
265 |
if self.bytes_left != 0: |
|
266 |
self._trailer_buffer = self._in_buffer[self.bytes_left:] |
|
267 |
self._in_buffer = self._in_buffer[:self.bytes_left] |
|
268 |
self.bytes_left = None |
|
269 |
self.state_accept = self._state_accept_reading_trailer |
|
270 |
||
271 |
def _state_accept_reading_trailer(self, bytes): |
|
272 |
self._trailer_buffer += bytes |
|
273 |
# TODO: what if the trailer does not match "done\n"? Should this raise
|
|
274 |
# a ProtocolViolation exception?
|
|
275 |
if self._trailer_buffer.startswith('done\n'): |
|
276 |
self.unused_data = self._trailer_buffer[len('done\n'):] |
|
277 |
self.state_accept = self._state_accept_reading_unused |
|
278 |
self.finished_reading = True |
|
279 |
||
280 |
def _state_accept_reading_unused(self, bytes): |
|
281 |
self.unused_data += bytes |
|
282 |
||
283 |
def _state_read_no_data(self): |
|
284 |
return '' |
|
285 |
||
286 |
def _state_read_in_buffer(self): |
|
287 |
result = self._in_buffer |
|
288 |
self._in_buffer = '' |
|
289 |
return result |
|
290 |
||
291 |
||
292 |
class SmartClientRequestProtocolOne(SmartProtocolBase): |
|
293 |
"""The client-side protocol for smart version 1."""
|
|
294 |
||
295 |
def __init__(self, request): |
|
296 |
"""Construct a SmartClientRequestProtocolOne.
|
|
297 |
||
298 |
:param request: A SmartClientMediumRequest to serialise onto and
|
|
299 |
deserialise from.
|
|
300 |
"""
|
|
301 |
self._request = request |
|
302 |
self._body_buffer = None |
|
2664.4.1
by John Arbash Meinel
Add timing information for call/response groups for hpss |
303 |
self._request_start_time = None |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
304 |
|
305 |
def call(self, *args): |
|
2593.3.1
by Andrew Bennetts
Add a -Dhpss debug flag. |
306 |
if 'hpss' in debug.debug_flags: |
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
307 |
mutter('hpss call: %s', repr(args)[1:-1]) |
2664.4.1
by John Arbash Meinel
Add timing information for call/response groups for hpss |
308 |
self._request_start_time = time.time() |
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
309 |
self._write_args(args) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
310 |
self._request.finished_writing() |
311 |
||
312 |
def call_with_body_bytes(self, args, body): |
|
313 |
"""Make a remote call of args with body bytes 'body'.
|
|
314 |
||
315 |
After calling this, call read_response_tuple to find the result out.
|
|
316 |
"""
|
|
2593.3.1
by Andrew Bennetts
Add a -Dhpss debug flag. |
317 |
if 'hpss' in debug.debug_flags: |
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
318 |
mutter('hpss call w/body: %s (%r...)', repr(args)[1:-1], body[:20]) |
2664.4.4
by John Arbash Meinel
Switch around what bytes get logged. |
319 |
mutter(' %d bytes', len(body)) |
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
320 |
self._request_start_time = time.time() |
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
321 |
self._write_args(args) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
322 |
bytes = self._encode_bulk_data(body) |
323 |
self._request.accept_bytes(bytes) |
|
324 |
self._request.finished_writing() |
|
325 |
||
326 |
def call_with_body_readv_array(self, args, body): |
|
327 |
"""Make a remote call with a readv array.
|
|
328 |
||
329 |
The body is encoded with one line per readv offset pair. The numbers in
|
|
330 |
each pair are separated by a comma, and no trailing \n is emitted.
|
|
331 |
"""
|
|
2593.3.1
by Andrew Bennetts
Add a -Dhpss debug flag. |
332 |
if 'hpss' in debug.debug_flags: |
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
333 |
mutter('hpss call w/readv: %s', repr(args)[1:-1]) |
334 |
self._request_start_time = time.time() |
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
335 |
self._write_args(args) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
336 |
readv_bytes = self._serialise_offsets(body) |
337 |
bytes = self._encode_bulk_data(readv_bytes) |
|
338 |
self._request.accept_bytes(bytes) |
|
339 |
self._request.finished_writing() |
|
2664.4.2
by John Arbash Meinel
Add debug timings for operations that have to send data |
340 |
if 'hpss' in debug.debug_flags: |
2664.4.4
by John Arbash Meinel
Switch around what bytes get logged. |
341 |
mutter(' %d bytes in readv request', len(readv_bytes)) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
342 |
|
343 |
def cancel_read_body(self): |
|
344 |
"""After expecting a body, a response code may indicate one otherwise.
|
|
345 |
||
346 |
This method lets the domain client inform the protocol that no body
|
|
347 |
will be transmitted. This is a terminal method: after calling it the
|
|
348 |
protocol is not able to be used further.
|
|
349 |
"""
|
|
350 |
self._request.finished_reading() |
|
351 |
||
352 |
def read_response_tuple(self, expect_body=False): |
|
353 |
"""Read a response tuple from the wire.
|
|
354 |
||
355 |
This should only be called once.
|
|
356 |
"""
|
|
357 |
result = self._recv_tuple() |
|
2593.3.1
by Andrew Bennetts
Add a -Dhpss debug flag. |
358 |
if 'hpss' in debug.debug_flags: |
2664.4.1
by John Arbash Meinel
Add timing information for call/response groups for hpss |
359 |
if self._request_start_time is not None: |
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
360 |
mutter(' result: %6.3fs %s', |
2664.4.1
by John Arbash Meinel
Add timing information for call/response groups for hpss |
361 |
time.time() - self._request_start_time, |
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
362 |
repr(result)[1:-1]) |
2664.4.1
by John Arbash Meinel
Add timing information for call/response groups for hpss |
363 |
self._request_start_time = None |
364 |
else: |
|
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
365 |
mutter(' result: %s', repr(result)[1:-1]) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
366 |
if not expect_body: |
367 |
self._request.finished_reading() |
|
368 |
return result |
|
369 |
||
370 |
def read_body_bytes(self, count=-1): |
|
371 |
"""Read bytes from the body, decoding into a byte stream.
|
|
372 |
|
|
373 |
We read all bytes at once to ensure we've checked the trailer for
|
|
374 |
errors, and then feed the buffer back as read_body_bytes is called.
|
|
375 |
"""
|
|
376 |
if self._body_buffer is not None: |
|
377 |
return self._body_buffer.read(count) |
|
378 |
_body_decoder = LengthPrefixedBodyDecoder() |
|
379 |
||
380 |
while not _body_decoder.finished_reading: |
|
381 |
bytes_wanted = _body_decoder.next_read_size() |
|
382 |
bytes = self._request.read_bytes(bytes_wanted) |
|
383 |
_body_decoder.accept_bytes(bytes) |
|
384 |
self._request.finished_reading() |
|
385 |
self._body_buffer = StringIO(_body_decoder.read_pending_data()) |
|
386 |
# XXX: TODO check the trailer result.
|
|
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
387 |
if 'hpss' in debug.debug_flags: |
2664.4.4
by John Arbash Meinel
Switch around what bytes get logged. |
388 |
mutter(' %d body bytes read', |
2664.4.3
by John Arbash Meinel
Update to include a bit better formatting |
389 |
len(self._body_buffer.getvalue())) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
390 |
return self._body_buffer.read(count) |
391 |
||
392 |
def _recv_tuple(self): |
|
393 |
"""Receive a tuple from the medium request."""
|
|
2432.4.6
by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future. |
394 |
return _decode_tuple(self._recv_line()) |
395 |
||
396 |
def _recv_line(self): |
|
397 |
"""Read an entire line from the medium request."""
|
|
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
398 |
line = '' |
399 |
while not line or line[-1] != '\n': |
|
400 |
# TODO: this is inefficient - but tuples are short.
|
|
401 |
new_char = self._request.read_bytes(1) |
|
402 |
line += new_char |
|
403 |
assert new_char != '', "end of file reading from server." |
|
2432.4.6
by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future. |
404 |
return line |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
405 |
|
406 |
def query_version(self): |
|
407 |
"""Return protocol version number of the server."""
|
|
408 |
self.call('hello') |
|
409 |
resp = self.read_response_tuple() |
|
410 |
if resp == ('ok', '1'): |
|
411 |
return 1 |
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
412 |
elif resp == ('ok', '2'): |
413 |
return 2 |
|
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
414 |
else: |
415 |
raise errors.SmartProtocolError("bad response %r" % (resp,)) |
|
416 |
||
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
417 |
def _write_args(self, args): |
418 |
self._write_protocol_version() |
|
419 |
bytes = _encode_tuple(args) |
|
420 |
self._request.accept_bytes(bytes) |
|
421 |
||
422 |
def _write_protocol_version(self): |
|
423 |
"""Write any prefixes this protocol requires.
|
|
424 |
|
|
425 |
Version one doesn't send protocol versions.
|
|
426 |
"""
|
|
427 |
||
428 |
||
429 |
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne): |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
430 |
"""Version two of the client side of the smart protocol.
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
431 |
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
432 |
This prefixes the request with the value of REQUEST_VERSION_TWO.
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
433 |
"""
|
434 |
||
435 |
def read_response_tuple(self, expect_body=False): |
|
436 |
"""Read a response tuple from the wire.
|
|
437 |
||
438 |
This should only be called once.
|
|
439 |
"""
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
440 |
version = self._request.read_line() |
441 |
if version != RESPONSE_VERSION_TWO: |
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
442 |
raise errors.SmartProtocolError('bad protocol marker %r' % version) |
2432.4.6
by Robert Collins
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future. |
443 |
response_status = self._recv_line() |
444 |
if response_status not in ('success\n', 'failed\n'): |
|
445 |
raise errors.SmartProtocolError( |
|
446 |
'bad protocol status %r' % response_status) |
|
447 |
self.response_status = response_status == 'success\n' |
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
448 |
return SmartClientRequestProtocolOne.read_response_tuple(self, expect_body) |
449 |
||
450 |
def _write_protocol_version(self): |
|
451 |
r"""Write any prefixes this protocol requires. |
|
452 |
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
453 |
Version two sends the value of REQUEST_VERSION_TWO.
|
2432.2.1
by Andrew Bennetts
Add Smart{Client,Server}RequestProtocolTwo, that prefix args tuples with a version marker. |
454 |
"""
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
455 |
self._request.accept_bytes(REQUEST_VERSION_TWO) |
2018.5.3
by Andrew Bennetts
Split up more smart server code, this time into bzrlib/transport/smart/protocol.py |
456 |