/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/protocol.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-04-26 05:42:38 UTC
  • mfrom: (2432.2.9 hpss-protocol2)
  • Revision ID: pqm@pqm.ubuntu.com-20070426054238-v6k5ge3z766vaafk
(Andrew Bennetts, Robert Collins) Smart server protocol versioning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from bzrlib.smart import request
26
26
 
27
27
 
 
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'
 
33
 
 
34
 
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)
143
151
 
 
152
    def _write_protocol_version(self):
 
153
        """Write any prefixes this protocol requires.
 
154
        
 
155
        Version one doesn't send protocol versions.
 
156
        """
 
157
 
144
158
    def next_read_size(self):
145
159
        if self._finished:
146
160
            return 0
150
164
            return self._body_decoder.next_read_size()
151
165
 
152
166
 
 
167
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
 
168
    r"""Version two of the server side of the smart protocol.
 
169
   
 
170
    This prefixes responses with the value of RESPONSE_VERSION_TWO.
 
171
    """
 
172
 
 
173
    def _write_protocol_version(self):
 
174
        r"""Write any prefixes this protocol requires.
 
175
        
 
176
        Version two sends the value of RESPONSE_VERSION_TWO.
 
177
        """
 
178
        self._write_func(RESPONSE_VERSION_TWO)
 
179
 
 
180
 
153
181
class LengthPrefixedBodyDecoder(object):
154
182
    """Decodes the length-prefixed bulk data."""
155
183
    
254
282
        self._body_buffer = None
255
283
 
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()
260
287
 
261
288
    def call_with_body_bytes(self, args, body):
263
290
 
264
291
        After calling this, call read_response_tuple to find the result out.
265
292
        """
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.
277
303
        """
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'):
338
363
            return 1
 
364
        elif resp == ('ok', '2'):
 
365
            return 2
339
366
        else:
340
367
            raise errors.SmartProtocolError("bad response %r" % (resp,))
341
368
 
 
369
    def _write_args(self, args):
 
370
        self._write_protocol_version()
 
371
        bytes = _encode_tuple(args)
 
372
        self._request.accept_bytes(bytes)
 
373
 
 
374
    def _write_protocol_version(self):
 
375
        """Write any prefixes this protocol requires.
 
376
        
 
377
        Version one doesn't send protocol versions.
 
378
        """
 
379
 
 
380
 
 
381
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
 
382
    """Version two of the client side of the smart protocol.
 
383
    
 
384
    This prefixes the request with the value of REQUEST_VERSION_TWO.
 
385
    """
 
386
 
 
387
    def read_response_tuple(self, expect_body=False):
 
388
        """Read a response tuple from the wire.
 
389
 
 
390
        This should only be called once.
 
391
        """
 
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)
 
396
 
 
397
    def _write_protocol_version(self):
 
398
        r"""Write any prefixes this protocol requires.
 
399
        
 
400
        Version two sends the value of REQUEST_VERSION_TWO.
 
401
        """
 
402
        self._request.accept_bytes(REQUEST_VERSION_TWO)
342
403