25
25
from bzrlib.smart import request
28
# Protocol version strings. These are sent as prefixes of bzr requests and
29
# responses to identify the protocol version being used. (There are no version
30
# one strings because that version doesn't send any).
31
REQUEST_VERSION_TWO = 'bzr request 2\n'
32
RESPONSE_VERSION_TWO = 'bzr response 2\n'
28
35
def _recv_tuple(from_file):
29
36
req_line = from_file.readline()
30
37
return _decode_tuple(req_line)
135
142
"""Send a smart server response down the output stream."""
136
143
assert not self._finished, 'response already sent'
137
144
self._finished = True
145
self._write_protocol_version()
138
146
self._write_func(_encode_tuple(args))
139
147
if body is not None:
140
148
assert isinstance(body, str), 'body must be a str'
141
149
bytes = self._encode_bulk_data(body)
142
150
self._write_func(bytes)
152
def _write_protocol_version(self):
153
"""Write any prefixes this protocol requires.
155
Version one doesn't send protocol versions.
144
158
def next_read_size(self):
145
159
if self._finished:
150
164
return self._body_decoder.next_read_size()
167
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
168
r"""Version two of the server side of the smart protocol.
170
This prefixes responses with the value of RESPONSE_VERSION_TWO.
173
def _write_protocol_version(self):
174
r"""Write any prefixes this protocol requires.
176
Version two sends the value of RESPONSE_VERSION_TWO.
178
self._write_func(RESPONSE_VERSION_TWO)
153
181
class LengthPrefixedBodyDecoder(object):
154
182
"""Decodes the length-prefixed bulk data."""
254
282
self._body_buffer = None
256
284
def call(self, *args):
257
bytes = _encode_tuple(args)
258
self._request.accept_bytes(bytes)
285
self._write_args(args)
259
286
self._request.finished_writing()
261
288
def call_with_body_bytes(self, args, body):
264
291
After calling this, call read_response_tuple to find the result out.
266
bytes = _encode_tuple(args)
267
self._request.accept_bytes(bytes)
293
self._write_args(args)
268
294
bytes = self._encode_bulk_data(body)
269
295
self._request.accept_bytes(bytes)
270
296
self._request.finished_writing()
275
301
The body is encoded with one line per readv offset pair. The numbers in
276
302
each pair are separated by a comma, and no trailing \n is emitted.
278
bytes = _encode_tuple(args)
279
self._request.accept_bytes(bytes)
304
self._write_args(args)
280
305
readv_bytes = self._serialise_offsets(body)
281
306
bytes = self._encode_bulk_data(readv_bytes)
282
307
self._request.accept_bytes(bytes)
336
361
resp = self.read_response_tuple()
337
362
if resp == ('ok', '1'):
364
elif resp == ('ok', '2'):
340
367
raise errors.SmartProtocolError("bad response %r" % (resp,))
369
def _write_args(self, args):
370
self._write_protocol_version()
371
bytes = _encode_tuple(args)
372
self._request.accept_bytes(bytes)
374
def _write_protocol_version(self):
375
"""Write any prefixes this protocol requires.
377
Version one doesn't send protocol versions.
381
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
382
"""Version two of the client side of the smart protocol.
384
This prefixes the request with the value of REQUEST_VERSION_TWO.
387
def read_response_tuple(self, expect_body=False):
388
"""Read a response tuple from the wire.
390
This should only be called once.
392
version = self._request.read_line()
393
if version != RESPONSE_VERSION_TWO:
394
raise errors.SmartProtocolError('bad protocol marker %r' % version)
395
return SmartClientRequestProtocolOne.read_response_tuple(self, expect_body)
397
def _write_protocol_version(self):
398
r"""Write any prefixes this protocol requires.
400
Version two sends the value of REQUEST_VERSION_TWO.
402
self._request.accept_bytes(REQUEST_VERSION_TWO)