/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: Marius Kruger
  • Date: 2007-08-12 08:15:15 UTC
  • mfrom: (2695 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2979.
  • Revision ID: amanic@gmail.com-20070812081515-vgekipfhohcuj6rn
merge with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
client and server.
19
19
"""
20
20
 
21
 
 
22
21
from cStringIO import StringIO
 
22
import time
23
23
 
 
24
from bzrlib import debug
24
25
from bzrlib import errors
25
26
from bzrlib.smart import request
 
27
from bzrlib.trace import log_exception_quietly, mutter
 
28
 
 
29
 
 
30
# Protocol version strings.  These are sent as prefixes of bzr requests and
 
31
# responses to identify the protocol version being used. (There are no version
 
32
# one strings because that version doesn't send any).
 
33
REQUEST_VERSION_TWO = 'bzr request 2\n'
 
34
RESPONSE_VERSION_TWO = 'bzr response 2\n'
26
35
 
27
36
 
28
37
def _recv_tuple(from_file):
96
105
                    # trivial request
97
106
                    self.excess_buffer = self.in_buffer
98
107
                    self.in_buffer = ''
99
 
                    self._send_response(self.request.response.args,
100
 
                        self.request.response.body)
 
108
                    self._send_response(self.request.response)
101
109
            except KeyboardInterrupt:
102
110
                raise
103
111
            except Exception, exception:
104
112
                # everything else: pass to client, flush, and quit
105
 
                self._send_response(('error', str(exception)))
 
113
                log_exception_quietly()
 
114
                self._send_response(request.FailedSmartServerResponse(
 
115
                    ('error', str(exception))))
106
116
                return
107
117
 
108
118
        if self.has_dispatched:
123
133
                assert self.request.finished_reading, \
124
134
                    "no more body, request not finished"
125
135
            if self.request.response is not None:
126
 
                self._send_response(self.request.response.args,
127
 
                    self.request.response.body)
 
136
                self._send_response(self.request.response)
128
137
                self.excess_buffer = self.in_buffer
129
138
                self.in_buffer = ''
130
139
            else:
131
140
                assert not self.request.finished_reading, \
132
141
                    "no response and we have finished reading."
133
142
 
134
 
    def _send_response(self, args, body=None):
 
143
    def _send_response(self, response):
135
144
        """Send a smart server response down the output stream."""
136
145
        assert not self._finished, 'response already sent'
 
146
        args = response.args
 
147
        body = response.body
137
148
        self._finished = True
 
149
        self._write_protocol_version()
 
150
        self._write_success_or_failure_prefix(response)
138
151
        self._write_func(_encode_tuple(args))
139
152
        if body is not None:
140
153
            assert isinstance(body, str), 'body must be a str'
141
154
            bytes = self._encode_bulk_data(body)
142
155
            self._write_func(bytes)
143
156
 
 
157
    def _write_protocol_version(self):
 
158
        """Write any prefixes this protocol requires.
 
159
        
 
160
        Version one doesn't send protocol versions.
 
161
        """
 
162
 
 
163
    def _write_success_or_failure_prefix(self, response):
 
164
        """Write the protocol specific success/failure prefix.
 
165
 
 
166
        For SmartServerRequestProtocolOne this is omitted but we
 
167
        call is_successful to ensure that the response is valid.
 
168
        """
 
169
        response.is_successful()
 
170
 
144
171
    def next_read_size(self):
145
172
        if self._finished:
146
173
            return 0
150
177
            return self._body_decoder.next_read_size()
151
178
 
152
179
 
 
180
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
 
181
    r"""Version two of the server side of the smart protocol.
 
182
   
 
183
    This prefixes responses with the value of RESPONSE_VERSION_TWO.
 
184
    """
 
185
 
 
186
    def _write_success_or_failure_prefix(self, response):
 
187
        """Write the protocol specific success/failure prefix."""
 
188
        if response.is_successful():
 
189
            self._write_func('success\n')
 
190
        else:
 
191
            self._write_func('failed\n')
 
192
 
 
193
    def _write_protocol_version(self):
 
194
        r"""Write any prefixes this protocol requires.
 
195
        
 
196
        Version two sends the value of RESPONSE_VERSION_TWO.
 
197
        """
 
198
        self._write_func(RESPONSE_VERSION_TWO)
 
199
 
 
200
 
153
201
class LengthPrefixedBodyDecoder(object):
154
202
    """Decodes the length-prefixed bulk data."""
155
203
    
252
300
        """
253
301
        self._request = request
254
302
        self._body_buffer = None
 
303
        self._request_start_time = None
255
304
 
256
305
    def call(self, *args):
257
 
        bytes = _encode_tuple(args)
258
 
        self._request.accept_bytes(bytes)
 
306
        if 'hpss' in debug.debug_flags:
 
307
            mutter('hpss call:   %s', repr(args)[1:-1])
 
308
            self._request_start_time = time.time()
 
309
        self._write_args(args)
259
310
        self._request.finished_writing()
260
311
 
261
312
    def call_with_body_bytes(self, args, body):
263
314
 
264
315
        After calling this, call read_response_tuple to find the result out.
265
316
        """
266
 
        bytes = _encode_tuple(args)
267
 
        self._request.accept_bytes(bytes)
 
317
        if 'hpss' in debug.debug_flags:
 
318
            mutter('hpss call w/body: %s (%r...)', repr(args)[1:-1], body[:20])
 
319
            mutter('              %d bytes', len(body))
 
320
            self._request_start_time = time.time()
 
321
        self._write_args(args)
268
322
        bytes = self._encode_bulk_data(body)
269
323
        self._request.accept_bytes(bytes)
270
324
        self._request.finished_writing()
275
329
        The body is encoded with one line per readv offset pair. The numbers in
276
330
        each pair are separated by a comma, and no trailing \n is emitted.
277
331
        """
278
 
        bytes = _encode_tuple(args)
279
 
        self._request.accept_bytes(bytes)
 
332
        if 'hpss' in debug.debug_flags:
 
333
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
 
334
            self._request_start_time = time.time()
 
335
        self._write_args(args)
280
336
        readv_bytes = self._serialise_offsets(body)
281
337
        bytes = self._encode_bulk_data(readv_bytes)
282
338
        self._request.accept_bytes(bytes)
283
339
        self._request.finished_writing()
 
340
        if 'hpss' in debug.debug_flags:
 
341
            mutter('              %d bytes in readv request', len(readv_bytes))
284
342
 
285
343
    def cancel_read_body(self):
286
344
        """After expecting a body, a response code may indicate one otherwise.
297
355
        This should only be called once.
298
356
        """
299
357
        result = self._recv_tuple()
 
358
        if 'hpss' in debug.debug_flags:
 
359
            if self._request_start_time is not None:
 
360
                mutter('   result:   %6.3fs  %s',
 
361
                       time.time() - self._request_start_time,
 
362
                       repr(result)[1:-1])
 
363
                self._request_start_time = None
 
364
            else:
 
365
                mutter('   result:   %s', repr(result)[1:-1])
300
366
        if not expect_body:
301
367
            self._request.finished_reading()
302
368
        return result
318
384
        self._request.finished_reading()
319
385
        self._body_buffer = StringIO(_body_decoder.read_pending_data())
320
386
        # XXX: TODO check the trailer result.
 
387
        if 'hpss' in debug.debug_flags:
 
388
            mutter('              %d body bytes read',
 
389
                   len(self._body_buffer.getvalue()))
321
390
        return self._body_buffer.read(count)
322
391
 
323
392
    def _recv_tuple(self):
324
393
        """Receive a tuple from the medium request."""
 
394
        return _decode_tuple(self._recv_line())
 
395
 
 
396
    def _recv_line(self):
 
397
        """Read an entire line from the medium request."""
325
398
        line = ''
326
399
        while not line or line[-1] != '\n':
327
400
            # TODO: this is inefficient - but tuples are short.
328
401
            new_char = self._request.read_bytes(1)
329
402
            line += new_char
330
403
            assert new_char != '', "end of file reading from server."
331
 
        return _decode_tuple(line)
 
404
        return line
332
405
 
333
406
    def query_version(self):
334
407
        """Return protocol version number of the server."""
336
409
        resp = self.read_response_tuple()
337
410
        if resp == ('ok', '1'):
338
411
            return 1
 
412
        elif resp == ('ok', '2'):
 
413
            return 2
339
414
        else:
340
415
            raise errors.SmartProtocolError("bad response %r" % (resp,))
341
416
 
 
417
    def _write_args(self, args):
 
418
        self._write_protocol_version()
 
419
        bytes = _encode_tuple(args)
 
420
        self._request.accept_bytes(bytes)
 
421
 
 
422
    def _write_protocol_version(self):
 
423
        """Write any prefixes this protocol requires.
 
424
        
 
425
        Version one doesn't send protocol versions.
 
426
        """
 
427
 
 
428
 
 
429
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
 
430
    """Version two of the client side of the smart protocol.
 
431
    
 
432
    This prefixes the request with the value of REQUEST_VERSION_TWO.
 
433
    """
 
434
 
 
435
    def read_response_tuple(self, expect_body=False):
 
436
        """Read a response tuple from the wire.
 
437
 
 
438
        This should only be called once.
 
439
        """
 
440
        version = self._request.read_line()
 
441
        if version != RESPONSE_VERSION_TWO:
 
442
            raise errors.SmartProtocolError('bad protocol marker %r' % version)
 
443
        response_status = self._recv_line()
 
444
        if response_status not in ('success\n', 'failed\n'):
 
445
            raise errors.SmartProtocolError(
 
446
                'bad protocol status %r' % response_status)
 
447
        self.response_status = response_status == 'success\n'
 
448
        return SmartClientRequestProtocolOne.read_response_tuple(self, expect_body)
 
449
 
 
450
    def _write_protocol_version(self):
 
451
        r"""Write any prefixes this protocol requires.
 
452
        
 
453
        Version two sends the value of REQUEST_VERSION_TWO.
 
454
        """
 
455
        self._request.accept_bytes(REQUEST_VERSION_TWO)
342
456