/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/message.py

Implement interrupting body streams with an error.

Show diffs side-by-side

added added

removed removed

Lines of Context:
98
98
        self.status = None
99
99
        self.args = None
100
100
        self._bytes_parts = collections.deque()
 
101
        self._body_started = False
 
102
        self._body_stream_status = None
101
103
        self._body = None
 
104
        self._body_error_args = None
102
105
        self.finished_reading = False
103
106
 
104
107
    def setProtoAndMedium(self, protocol_decoder, medium):
106
109
        self._medium = medium
107
110
 
108
111
    def byte_part_received(self, byte):
109
 
        if self.status is not None:
110
 
            raise errors.SmartProtocolError(
111
 
                'Unexpected byte part received: %r' % (byte,))
112
112
        if byte not in ['E', 'S']:
113
113
            raise errors.SmartProtocolError(
114
114
                'Unknown response status: %r' % (byte,))
115
 
        self.status = byte
 
115
        if self._body_started:
 
116
            if self._body_stream_status is not None:
 
117
                raise errors.SmartProtocolError(
 
118
                    'Unexpected byte part received: %r' % (byte,))
 
119
            self._body_stream_status = byte
 
120
        else:
 
121
            if self.status is not None:
 
122
                raise errors.SmartProtocolError(
 
123
                    'Unexpected byte part received: %r' % (byte,))
 
124
            self.status = byte
116
125
 
117
126
    def bytes_part_received(self, bytes):
 
127
        self._body_started = True
118
128
        self._bytes_parts.append(bytes)
119
129
 
120
130
    def structure_part_received(self, structure):
121
 
        if self.args is not None:
 
131
        if type(structure) is not list:
122
132
            raise errors.SmartProtocolError(
123
 
                'Unexpected structure received: %r (already got %r)'
124
 
                % (structure, self.args))
125
 
        self.args = structure
 
133
                'Args structure is not a sequence: %r' % (structure,))
 
134
        structure = tuple(structure)
 
135
        if not self._body_started:
 
136
            if self.args is not None:
 
137
                raise errors.SmartProtocolError(
 
138
                    'Unexpected structure received: %r (already got %r)'
 
139
                    % (structure, self.args))
 
140
            self.args = structure
 
141
        else:
 
142
            if self._body_stream_status != 'E':
 
143
                raise errors.SmartProtocolError(
 
144
                    'Unexpected structure received after body: %r'
 
145
                    % (structure,))
 
146
            self._body_error_args = structure
126
147
 
127
148
    def _wait_for_response_args(self):
128
149
        while self.args is None and not self.finished_reading:
178
199
            while self._bytes_parts:
179
200
                yield self._bytes_parts.popleft()
180
201
            self._read_more()
 
202
        if self._body_stream_status == 'E':
 
203
            _translate_error(self._body_error_args)
181
204
 
182
205
    def cancel_read_body(self):
183
206
        self._wait_for_response_end()
 
207
 
 
208
 
 
209
def _translate_error(error_tuple):
 
210
    # XXX: Hmm!  Need state from the request.  Hmm.
 
211
    error_name = error_tuple[0]
 
212
    error_args = error_tuple[1:]
 
213
    if error_name == 'LockContention':
 
214
        raise errors.LockContention('(remote lock)')
 
215
    elif error_name == 'LockFailed':
 
216
        raise errors.LockContention(*error_args[:2])
 
217
    else:
 
218
        raise errors.ErrorFromSmartServer(error_tuple)
 
219