~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: John Arbash Meinel
  • Date: 2007-04-28 15:04:17 UTC
  • mfrom: (2466 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20070428150417-trp3pi0pzd411pu4
[merge] bzr.dev 2466

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
 
78
78
 
79
79
class SmartServerResponse(object):
80
 
    """Response generated by SmartServerRequestHandler."""
 
80
    """A response to a client request.
 
81
    
 
82
    This base class should not be used. Instead use
 
83
    SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
 
84
    """
81
85
 
82
86
    def __init__(self, args, body=None):
83
87
        self.args = args
89
93
        return other.args == self.args and other.body == self.body
90
94
 
91
95
    def __repr__(self):
92
 
        return "<SmartServerResponse args=%r body=%r>" % (self.args, self.body)
 
96
        return "<SmartServerResponse args=%r body=%r>" % (self.is_successful(), 
 
97
            self.args, self.body)
 
98
 
 
99
 
 
100
class FailedSmartServerResponse(SmartServerResponse):
 
101
    """A SmartServerResponse for a request which failed."""
 
102
 
 
103
    def is_successful(self):
 
104
        """FailedSmartServerResponse are not successful."""
 
105
        return False
 
106
 
 
107
 
 
108
class SuccessfulSmartServerResponse(SmartServerResponse):
 
109
    """A SmartServerResponse for a successfully completed request."""
 
110
 
 
111
    def is_successful(self):
 
112
        """SuccessfulSmartServerResponse are successful."""
 
113
        return True
93
114
 
94
115
 
95
116
class SmartServerRequestHandler(object):
171
192
        try:
172
193
            return callable(*args, **kwargs)
173
194
        except errors.NoSuchFile, e:
174
 
            return SmartServerResponse(('NoSuchFile', e.path))
 
195
            return FailedSmartServerResponse(('NoSuchFile', e.path))
175
196
        except errors.FileExists, e:
176
 
            return SmartServerResponse(('FileExists', e.path))
 
197
            return FailedSmartServerResponse(('FileExists', e.path))
177
198
        except errors.DirectoryNotEmpty, e:
178
 
            return SmartServerResponse(('DirectoryNotEmpty', e.path))
 
199
            return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
179
200
        except errors.ShortReadvError, e:
180
 
            return SmartServerResponse(('ShortReadvError',
 
201
            return FailedSmartServerResponse(('ShortReadvError',
181
202
                e.path, str(e.offset), str(e.length), str(e.actual)))
182
203
        except UnicodeError, e:
183
204
            # If it is a DecodeError, than most likely we are starting
190
211
            else:
191
212
                val = 's:' + str_or_unicode.encode('base64')
192
213
            # This handles UnicodeEncodeError or UnicodeDecodeError
193
 
            return SmartServerResponse((e.__class__.__name__,
 
214
            return FailedSmartServerResponse((e.__class__.__name__,
194
215
                    e.encoding, val, str(e.start), str(e.end), e.reason))
195
216
        except errors.TransportNotPossible, e:
196
217
            if e.msg == "readonly transport":
197
 
                return SmartServerResponse(('ReadOnlyError', ))
 
218
                return FailedSmartServerResponse(('ReadOnlyError', ))
198
219
            else:
199
220
                raise
200
221
 
201
222
 
202
223
class HelloRequest(SmartServerRequest):
203
 
    """Answer a version request with my version."""
 
224
    """Answer a version request with the highest protocol version this server
 
225
    supports.
 
226
    """
204
227
 
205
228
    def do(self):
206
 
        return SmartServerResponse(('ok', '1'))
 
229
        return SuccessfulSmartServerResponse(('ok', '2'))
207
230
 
208
231
 
209
232
class GetBundleRequest(SmartServerRequest):
218
241
        base_revision = revision.NULL_REVISION
219
242
        write_bundle(repo, revision_id, base_revision, tmpf)
220
243
        tmpf.seek(0)
221
 
        return SmartServerResponse((), tmpf.read())
 
244
        return SuccessfulSmartServerResponse((), tmpf.read())
222
245
 
223
246
 
224
247
class SmartServerIsReadonly(SmartServerRequest):
229
252
            answer = 'yes'
230
253
        else:
231
254
            answer = 'no'
232
 
        return SmartServerResponse((answer,))
 
255
        return SuccessfulSmartServerResponse((answer,))
233
256
 
234
257
 
235
258
request_handlers = registry.Registry()
236
259
request_handlers.register_lazy(
237
260
    'append', 'bzrlib.smart.vfs', 'AppendRequest')
238
261
request_handlers.register_lazy(
 
262
    'Branch.get_config_file', 'bzrlib.smart.branch', 'SmartServerBranchGetConfigFile')
 
263
request_handlers.register_lazy(
 
264
    'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
 
265
request_handlers.register_lazy(
 
266
    'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
 
267
request_handlers.register_lazy(
 
268
    'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
 
269
request_handlers.register_lazy(
 
270
    'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
 
271
request_handlers.register_lazy(
 
272
    'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
 
273
request_handlers.register_lazy(
 
274
    'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepository')
 
275
request_handlers.register_lazy(
 
276
    'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir', 'SmartServerRequestInitializeBzrDir')
 
277
request_handlers.register_lazy(
 
278
    'BzrDir.open_branch', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBranch')
 
279
request_handlers.register_lazy(
239
280
    'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
240
281
request_handlers.register_lazy(
241
282
    'get', 'bzrlib.smart.vfs', 'GetRequest')
261
302
    'readv', 'bzrlib.smart.vfs', 'ReadvRequest')
262
303
request_handlers.register_lazy(
263
304
    'rename', 'bzrlib.smart.vfs', 'RenameRequest')
 
305
request_handlers.register_lazy('Repository.gather_stats',
 
306
                               'bzrlib.smart.repository',
 
307
                               'SmartServerRepositoryGatherStats')
 
308
request_handlers.register_lazy(
 
309
    'Repository.get_revision_graph', 'bzrlib.smart.repository', 'SmartServerRepositoryGetRevisionGraph')
 
310
request_handlers.register_lazy(
 
311
    'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')
 
312
request_handlers.register_lazy(
 
313
    'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
 
314
request_handlers.register_lazy(
 
315
    'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
 
316
request_handlers.register_lazy(
 
317
    'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
 
318
request_handlers.register_lazy(
 
319
    'Repository.tarball', 'bzrlib.smart.repository',
 
320
    'SmartServerRepositoryTarball')
264
321
request_handlers.register_lazy(
265
322
    'rmdir', 'bzrlib.smart.vfs', 'RmdirRequest')
266
323
request_handlers.register_lazy(
267
324
    'stat', 'bzrlib.smart.vfs', 'StatRequest')
 
325
request_handlers.register_lazy(
 
326
    'Transport.is_readonly', 'bzrlib.smart.request', 'SmartServerIsReadonly')
 
327
request_handlers.register_lazy(
 
328
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')