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

  • Committer: Benoît Pierre
  • Date: 2009-02-24 00:25:32 UTC
  • mfrom: (4035 +trunk)
  • mto: (4056.1.1 trunk2)
  • mto: This revision was merged to the branch mainline in revision 4058.
  • Revision ID: benoit.pierre@gmail.com-20090224002532-i2f64ou15pa7if2y
Merge with upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
class SmartServerRequest(object):
45
45
    """Base class for request handlers.
46
 
    
 
46
 
47
47
    To define a new request, subclass this class and override the `do` method
48
48
    (and if appropriate, `do_body` as well).  Request implementors should take
49
49
    care to call `translate_client_path` and `transport_from_client_path` as
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."""
77
78
 
78
79
    def do(self, *args):
79
80
        """Mandatory extension point for SmartServerRequest subclasses.
80
 
        
 
81
 
81
82
        Subclasses must implement this.
82
 
        
 
83
 
83
84
        This should return a SmartServerResponse if this command expects to
84
85
        receive no body.
85
86
        """
100
101
        """Called if the client sends a body with the request.
101
102
 
102
103
        The do() method is still called, and must have returned None.
103
 
        
 
104
 
104
105
        Must return a SmartServerResponse.
105
106
        """
106
 
        raise NotImplementedError(self.do_body)
 
107
        if body_bytes != '':
 
108
            raise errors.SmartProtocolError('Request does not expect a body')
107
109
 
108
110
    def do_chunk(self, chunk_bytes):
109
111
        """Called with each body chunk if the request has a streamed body.
110
112
 
111
113
        The do() method is still called, and must have returned None.
112
114
        """
113
 
        raise NotImplementedError(self.do_chunk)
 
115
        self._body_chunks.append(chunk_bytes)
114
116
 
115
117
    def do_end(self):
116
118
        """Called when the end of the request has been received."""
117
 
        pass
118
 
    
 
119
        body_bytes = ''.join(self._body_chunks)
 
120
        self._body_chunks = None
 
121
        return self.do_body(body_bytes)
 
122
 
119
123
    def translate_client_path(self, client_path):
120
124
        """Translate a path received from a network client into a local
121
125
        relpath.
154
158
 
155
159
class SmartServerResponse(object):
156
160
    """A response to a client request.
157
 
    
 
161
 
158
162
    This base class should not be used. Instead use
159
163
    SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
160
164
    """
204
208
 
205
209
class SmartServerRequestHandler(object):
206
210
    """Protocol logic for smart server.
207
 
    
 
211
 
208
212
    This doesn't handle serialization at all, it just processes requests and
209
213
    creates responses.
210
214
    """
229
233
        self._backing_transport = backing_transport
230
234
        self._root_client_path = root_client_path
231
235
        self._commands = commands
232
 
        self._body_bytes = ''
233
236
        self.response = None
234
237
        self.finished_reading = False
235
238
        self._command = None
236
239
 
237
240
    def accept_body(self, bytes):
238
241
        """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
247
 
        
 
242
        self._run_handler_code(self._command.do_chunk, (bytes,), {})
 
243
 
248
244
    def end_of_body(self):
249
245
        """No more body data will be received."""
250
 
        self._run_handler_code(self._command.do_body, (self._body_bytes,), {})
 
246
        self._run_handler_code(self._command.do_end, (), {})
251
247
        # cannot read after this.
252
248
        self.finished_reading = True
253
249
 
338
334
        self._command = command(self._backing_transport)
339
335
        self._run_handler_code(self._command.execute, args, {})
340
336
 
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
337
    def end_received(self):
351
338
        self._run_handler_code(self._command.do_end, (), {})
352
339
 
 
340
    def post_body_error_received(self, error_args):
 
341
        # Just a no-op at the moment.
 
342
        pass
 
343
 
353
344
 
354
345
class HelloRequest(SmartServerRequest):
355
346
    """Answer a version request with the highest protocol version this server
410
401
request_handlers.register_lazy(
411
402
    'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
412
403
request_handlers.register_lazy(
 
404
    'BzrDir.create_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestCreateRepository')
 
405
request_handlers.register_lazy(
413
406
    'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV1')
414
407
request_handlers.register_lazy(
415
408
    'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV2')
457
450
request_handlers.register_lazy(
458
451
    'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')
459
452
request_handlers.register_lazy(
 
453
    'Repository.insert_stream', 'bzrlib.smart.repository', 'SmartServerRepositoryInsertStream')
 
454
request_handlers.register_lazy(
460
455
    'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
461
456
request_handlers.register_lazy(
462
457
    'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
463
458
request_handlers.register_lazy(
 
459
    'Repository.set_make_working_trees', 'bzrlib.smart.repository',
 
460
    'SmartServerRepositorySetMakeWorkingTrees')
 
461
request_handlers.register_lazy(
464
462
    'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
465
463
request_handlers.register_lazy(
466
464
    'Repository.tarball', 'bzrlib.smart.repository',