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
18
from cStringIO import StringIO
25
from ...sixish import (
28
from ...trace import mutter
24
from bzrlib.trace import mutter
31
27
class MessageHandler(object):
113
109
self.responder.send_error(exception)
115
111
def byte_part_received(self, byte):
116
if not isinstance(byte, bytes):
117
raise TypeError(byte)
118
112
if self.expecting == 'body':
120
114
# Success. Nothing more to come except the end of message.
121
115
self.expecting = 'end'
123
117
# Error. Expect an error structure.
124
118
self.expecting = 'error'
230
224
self._medium_request = medium_request
232
226
def byte_part_received(self, byte):
233
if not isinstance(byte, bytes):
234
raise TypeError(byte)
235
if byte not in [b'E', b'S']:
227
if byte not in ['E', 'S']:
236
228
raise errors.SmartProtocolError(
237
229
'Unknown response status: %r' % (byte,))
238
230
if self._body_started:
251
243
self._bytes_parts.append(bytes)
253
245
def structure_part_received(self, structure):
254
if not isinstance(structure, tuple):
246
if type(structure) is not tuple:
255
247
raise errors.SmartProtocolError(
256
248
'Args structure is not a sequence: %r' % (structure,))
257
249
if not self._body_started:
261
253
% (structure, self.args))
262
254
self.args = structure
264
if self._body_stream_status != b'E':
256
if self._body_stream_status != 'E':
265
257
raise errors.SmartProtocolError(
266
258
'Unexpected structure received after body: %r'
282
274
self.finished_reading = True
283
275
self._medium_request.finished_reading()
285
data = self._medium_request.read_bytes(next_read_size)
277
bytes = self._medium_request.read_bytes(next_read_size)
287
279
# end of file encountered reading from server
288
280
if 'hpss' in debug.debug_flags:
294
286
"Unexpected end of message. "
295
287
"Please check connectivity and permissions, and report a bug "
296
288
"if problems persist.")
297
self._protocol_decoder.accept_bytes(data)
289
self._protocol_decoder.accept_bytes(bytes)
299
291
def protocol_error(self, exception):
300
292
# Whatever the error is, we're done with this request.
309
301
self._wait_for_response_end()
310
302
if 'hpss' in debug.debug_flags:
311
303
mutter(' result: %r', self.args)
312
if self.status == b'E':
304
if self.status == 'E':
313
305
self._wait_for_response_end()
314
_raise_smart_server_error(self.args)
306
_translate_error(self.args)
315
307
return tuple(self.args)
317
309
def read_body_bytes(self, count=-1):
327
319
# != -1. (2008/04/30, Andrew Bennetts)
328
320
if self._body is None:
329
321
self._wait_for_response_end()
330
body_bytes = b''.join(self._bytes_parts)
322
body_bytes = ''.join(self._bytes_parts)
331
323
if 'hpss' in debug.debug_flags:
332
324
mutter(' %d body bytes read', len(body_bytes))
333
self._body = BytesIO(body_bytes)
325
self._body = StringIO(body_bytes)
334
326
self._bytes_parts = None
335
327
return self._body.read(count)
342
334
mutter(' %d byte part read', len(bytes_part))
344
336
self._read_more()
345
if self._body_stream_status == b'E':
346
_raise_smart_server_error(self._body_error_args)
337
if self._body_stream_status == 'E':
338
_translate_error(self._body_error_args)
348
340
def cancel_read_body(self):
349
341
self._wait_for_response_end()
352
def _raise_smart_server_error(error_tuple):
353
"""Raise exception based on tuple received from smart server
355
Specific error translation is handled by breezy.bzr.remote._translate_error
357
if error_tuple[0] == b'UnknownMethod':
358
raise errors.UnknownSmartMethod(error_tuple[1])
359
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)