~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/protocol.py

  • Committer: Robert Collins
  • Date: 2007-04-24 12:50:50 UTC
  • mto: (2432.3.5 hpss-vfs-fallback)
  • mto: This revision was merged to the branch mainline in revision 2463.
  • Revision ID: robertc@lifelesswks.robertcollins.net-20070424125050-8b4f1cf3f5b017bb
Include success/failure feedback in SmartProtocolTwo responses to allow robust handling in the future.

Show diffs side-by-side

added added

removed removed

Lines of Context:
133
133
    def _send_response(self, response):
134
134
        """Send a smart server response down the output stream."""
135
135
        assert not self._finished, 'response already sent'
136
 
        response.is_successful()
137
136
        args = response.args
138
137
        body = response.body
139
138
        self._finished = True
140
139
        self._write_protocol_version()
 
140
        self._write_success_or_failure_prefix(response)
141
141
        self._write_func(_encode_tuple(args))
142
142
        if body is not None:
143
143
            assert isinstance(body, str), 'body must be a str'
150
150
        Version one doesn't send protocol versions.
151
151
        """
152
152
 
 
153
    def _write_success_or_failure_prefix(self, response):
 
154
        """Write the protocol specific success/failure prefix.
 
155
 
 
156
        For SmartServerRequestProtocolOne this is omitted but we
 
157
        call is_successful to ensure that the response is valid.
 
158
        """
 
159
        response.is_successful()
 
160
 
153
161
    def next_read_size(self):
154
162
        if self._finished:
155
163
            return 0
165
173
    This prefixes responses with the protocol version: "2\n".
166
174
    """
167
175
 
 
176
    def _write_success_or_failure_prefix(self, response):
 
177
        """Write the protocol specific success/failure prefix."""
 
178
        if response.is_successful():
 
179
            self._write_func('success\n')
 
180
        else:
 
181
            self._write_func('failed\n')
 
182
 
168
183
    def _write_protocol_version(self):
169
184
        r"""Write any prefixes this protocol requires.
170
185
        
342
357
 
343
358
    def _recv_tuple(self):
344
359
        """Receive a tuple from the medium request."""
 
360
        return _decode_tuple(self._recv_line())
 
361
 
 
362
    def _recv_line(self):
 
363
        """Read an entire line from the medium request."""
345
364
        line = ''
346
365
        while not line or line[-1] != '\n':
347
366
            # TODO: this is inefficient - but tuples are short.
348
367
            new_char = self._request.read_bytes(1)
349
368
            line += new_char
350
369
            assert new_char != '', "end of file reading from server."
351
 
        return _decode_tuple(line)
 
370
        return line
352
371
 
353
372
    def query_version(self):
354
373
        """Return protocol version number of the server."""
389
408
        version = self._request.read_bytes(2)
390
409
        if version != SmartClientRequestProtocolTwo._version_string:
391
410
            raise errors.SmartProtocolError('bad protocol marker %r' % version)
 
411
        response_status = self._recv_line()
 
412
        if response_status not in ('success\n', 'failed\n'):
 
413
            raise errors.SmartProtocolError(
 
414
                'bad protocol status %r' % response_status)
 
415
        self.response_status = response_status == 'success\n'
392
416
        return SmartClientRequestProtocolOne.read_response_tuple(self, expect_body)
393
417
 
394
418
    def _write_protocol_version(self):