~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-02-04 12:56:11 UTC
  • mfrom: (3842.3.22 call_with_body_stream)
  • Revision ID: pqm@pqm.ubuntu.com-20090204125611-m7kqmwruvndk7yrv
Add client and server APIs for streamed request bodies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
            if not root_client_path.endswith('/'):
71
71
                root_client_path += '/'
72
72
        self._root_client_path = root_client_path
 
73
        self._body_chunks = []
73
74
 
74
75
    def _check_enabled(self):
75
76
        """Raises DisabledMethod if this method is disabled."""
110
111
 
111
112
        The do() method is still called, and must have returned None.
112
113
        """
113
 
        raise NotImplementedError(self.do_chunk)
 
114
        self._body_chunks.append(chunk_bytes)
114
115
 
115
116
    def do_end(self):
116
117
        """Called when the end of the request has been received."""
117
 
        pass
 
118
        body_bytes = ''.join(self._body_chunks)
 
119
        self._body_chunks = None
 
120
        return self.do_body(body_bytes)
118
121
    
119
122
    def translate_client_path(self, client_path):
120
123
        """Translate a path received from a network client into a local
229
232
        self._backing_transport = backing_transport
230
233
        self._root_client_path = root_client_path
231
234
        self._commands = commands
232
 
        self._body_bytes = ''
233
235
        self.response = None
234
236
        self.finished_reading = False
235
237
        self._command = None
236
238
 
237
239
    def accept_body(self, bytes):
238
240
        """Accept body data."""
239
 
 
240
 
        # TODO: This should be overriden for each command that desired body data
241
 
        # to handle the right format of that data, i.e. plain bytes, a bundle,
242
 
        # etc.  The deserialisation into that format should be done in the
243
 
        # Protocol object.
244
 
 
245
 
        # default fallback is to accumulate bytes.
246
 
        self._body_bytes += bytes
 
241
        self._run_handler_code(self._command.do_chunk, (bytes,), {})
247
242
        
248
243
    def end_of_body(self):
249
244
        """No more body data will be received."""
250
 
        self._run_handler_code(self._command.do_body, (self._body_bytes,), {})
 
245
        self._run_handler_code(self._command.do_end, (), {})
251
246
        # cannot read after this.
252
247
        self.finished_reading = True
253
248
 
338
333
        self._command = command(self._backing_transport)
339
334
        self._run_handler_code(self._command.execute, args, {})
340
335
 
341
 
    def prefixed_body_received(self, body_bytes):
342
 
        """No more body data will be received."""
343
 
        self._run_handler_code(self._command.do_body, (body_bytes,), {})
344
 
        # cannot read after this.
345
 
        self.finished_reading = True
346
 
 
347
 
    def body_chunk_received(self, chunk_bytes):
348
 
        self._run_handler_code(self._command.do_chunk, (chunk_bytes,), {})
349
 
 
350
336
    def end_received(self):
351
337
        self._run_handler_code(self._command.do_end, (), {})
352
338
 
 
339
    def post_body_error_received(self, error_args):
 
340
        # Just a no-op at the moment.
 
341
        pass
 
342
 
353
343
 
354
344
class HelloRequest(SmartServerRequest):
355
345
    """Answer a version request with the highest protocol version this server