14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
20
from collections.abc import deque
21
except ImportError: # python < 3.7
22
from collections import deque
18
from cStringIO import StringIO
28
from ...sixish import (
31
from ...trace import mutter
24
from bzrlib.trace import mutter
34
27
class MessageHandler(object):
116
109
self.responder.send_error(exception)
118
111
def byte_part_received(self, byte):
119
if not isinstance(byte, bytes):
120
raise TypeError(byte)
121
112
if self.expecting == 'body':
123
114
# Success. Nothing more to come except the end of message.
124
115
self.expecting = 'end'
126
117
# Error. Expect an error structure.
127
118
self.expecting = 'error'
221
212
MessageHandler.__init__(self)
222
213
self.status = None
224
self._bytes_parts = deque()
215
self._bytes_parts = collections.deque()
225
216
self._body_started = False
226
217
self._body_stream_status = None
227
218
self._body = None
233
224
self._medium_request = medium_request
235
226
def byte_part_received(self, byte):
236
if not isinstance(byte, bytes):
237
raise TypeError(byte)
238
if byte not in [b'E', b'S']:
227
if byte not in ['E', 'S']:
239
228
raise errors.SmartProtocolError(
240
229
'Unknown response status: %r' % (byte,))
241
230
if self._body_started:
254
243
self._bytes_parts.append(bytes)
256
245
def structure_part_received(self, structure):
257
if not isinstance(structure, tuple):
246
if type(structure) is not tuple:
258
247
raise errors.SmartProtocolError(
259
248
'Args structure is not a sequence: %r' % (structure,))
260
249
if not self._body_started:
264
253
% (structure, self.args))
265
254
self.args = structure
267
if self._body_stream_status != b'E':
256
if self._body_stream_status != 'E':
268
257
raise errors.SmartProtocolError(
269
258
'Unexpected structure received after body: %r'
285
274
self.finished_reading = True
286
275
self._medium_request.finished_reading()
288
data = self._medium_request.read_bytes(next_read_size)
277
bytes = self._medium_request.read_bytes(next_read_size)
290
279
# end of file encountered reading from server
291
280
if 'hpss' in debug.debug_flags:
297
286
"Unexpected end of message. "
298
287
"Please check connectivity and permissions, and report a bug "
299
288
"if problems persist.")
300
self._protocol_decoder.accept_bytes(data)
289
self._protocol_decoder.accept_bytes(bytes)
302
291
def protocol_error(self, exception):
303
292
# Whatever the error is, we're done with this request.
312
301
self._wait_for_response_end()
313
302
if 'hpss' in debug.debug_flags:
314
303
mutter(' result: %r', self.args)
315
if self.status == b'E':
304
if self.status == 'E':
316
305
self._wait_for_response_end()
317
_raise_smart_server_error(self.args)
306
_translate_error(self.args)
318
307
return tuple(self.args)
320
309
def read_body_bytes(self, count=-1):
330
319
# != -1. (2008/04/30, Andrew Bennetts)
331
320
if self._body is None:
332
321
self._wait_for_response_end()
333
body_bytes = b''.join(self._bytes_parts)
322
body_bytes = ''.join(self._bytes_parts)
334
323
if 'hpss' in debug.debug_flags:
335
324
mutter(' %d body bytes read', len(body_bytes))
336
self._body = BytesIO(body_bytes)
325
self._body = StringIO(body_bytes)
337
326
self._bytes_parts = None
338
327
return self._body.read(count)
345
334
mutter(' %d byte part read', len(bytes_part))
347
336
self._read_more()
348
if self._body_stream_status == b'E':
349
_raise_smart_server_error(self._body_error_args)
337
if self._body_stream_status == 'E':
338
_translate_error(self._body_error_args)
351
340
def cancel_read_body(self):
352
341
self._wait_for_response_end()
355
def _raise_smart_server_error(error_tuple):
356
"""Raise exception based on tuple received from smart server
358
Specific error translation is handled by breezy.bzr.remote._translate_error
360
if error_tuple[0] == b'UnknownMethod':
361
raise errors.UnknownSmartMethod(error_tuple[1])
362
raise errors.ErrorFromSmartServer(error_tuple)
344
def _translate_error(error_tuple):
345
# Many exceptions need some state from the requestor to be properly
346
# translated (e.g. they need a branch object). So this only translates a
347
# few errors, and the rest are turned into a generic ErrorFromSmartServer.
348
error_name = error_tuple[0]
349
error_args = error_tuple[1:]
350
if error_name == 'UnknownMethod':
351
raise errors.UnknownSmartMethod(error_args[0])
352
if error_name == 'LockContention':
353
raise errors.LockContention('(remote lock)')
354
elif error_name == 'LockFailed':
355
raise errors.LockFailed(*error_args[:2])
356
elif error_name == 'FileExists':
357
raise errors.FileExists(error_args[0])
358
elif error_name == 'NoSuchFile':
359
raise errors.NoSuchFile(error_args[0])
361
raise errors.ErrorFromSmartServer(error_tuple)