~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/client.py

  • Committer: John Arbash Meinel
  • Date: 2008-10-04 14:10:13 UTC
  • mto: This revision was merged to the branch mainline in revision 3805.
  • Revision ID: john@arbash-meinel.com-20081004141013-yskxjlwtuy2k18ue
Playing around with expanding requests for btree index nodes into neighboring nodes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import bzrlib
18
18
from bzrlib.smart import message, protocol
36
36
        else:
37
37
            self._headers = dict(headers)
38
38
 
39
 
    def __repr__(self):
40
 
        return '%s(%r)' % (self.__class__.__name__, self._medium)
41
 
 
42
39
    def _send_request(self, protocol_version, method, args, body=None,
43
 
                      readv_body=None, body_stream=None):
 
40
                      readv_body=None):
44
41
        encoder, response_handler = self._construct_protocol(
45
42
            protocol_version)
46
43
        encoder.set_headers(self._headers)
48
45
            if readv_body is not None:
49
46
                raise AssertionError(
50
47
                    "body and readv_body are mutually exclusive.")
51
 
            if body_stream is not None:
52
 
                raise AssertionError(
53
 
                    "body and body_stream are mutually exclusive.")
54
48
            encoder.call_with_body_bytes((method, ) + args, body)
55
49
        elif readv_body is not None:
56
 
            if body_stream is not None:
57
 
                raise AssertionError(
58
 
                    "readv_body and body_stream are mutually exclusive.")
59
 
            encoder.call_with_body_readv_array((method, ) + args, readv_body)
60
 
        elif body_stream is not None:
61
 
            encoder.call_with_body_stream((method, ) + args, body_stream)
 
50
            encoder.call_with_body_readv_array((method, ) + args,
 
51
                    readv_body)
62
52
        else:
63
53
            encoder.call(method, *args)
64
54
        return response_handler
69
59
        params = CallHookParams(method, args, body, readv_body, self._medium)
70
60
        for hook in _SmartClient.hooks['call']:
71
61
            hook(params)
72
 
 
 
62
            
73
63
    def _call_and_read_response(self, method, args, body=None, readv_body=None,
74
 
            body_stream=None, expect_response_body=True):
 
64
            expect_response_body=True):
75
65
        self._run_call_hooks(method, args, body, readv_body)
76
66
        if self._medium._protocol_version is not None:
77
67
            response_handler = self._send_request(
78
68
                self._medium._protocol_version, method, args, body=body,
79
 
                readv_body=readv_body, body_stream=body_stream)
 
69
                readv_body=readv_body)
80
70
            return (response_handler.read_response_tuple(
81
71
                        expect_body=expect_response_body),
82
72
                    response_handler)
87
77
                    self._medium._remember_remote_is_before((1, 6))
88
78
                response_handler = self._send_request(
89
79
                    protocol_version, method, args, body=body,
90
 
                    readv_body=readv_body, body_stream=body_stream)
 
80
                    readv_body=readv_body)
91
81
                try:
92
82
                    response_tuple = response_handler.read_response_tuple(
93
83
                        expect_body=expect_response_body)
135
125
 
136
126
    def call_expecting_body(self, method, *args):
137
127
        """Call a method and return the result and the protocol object.
138
 
 
 
128
        
139
129
        The body can be read like so::
140
130
 
141
131
            result, smart_protocol = smart_client.call_expecting_body(...)
175
165
                args[0], args[1:], readv_body=body, expect_response_body=True)
176
166
        return (response, response_handler)
177
167
 
178
 
    def call_with_body_stream(self, args, stream):
179
 
        response, response_handler = self._call_and_read_response(
180
 
                args[0], args[1:], body_stream=stream,
181
 
                expect_response_body=False)
182
 
        return (response, response_handler)
183
 
 
184
168
    def remote_path_from_transport(self, transport):
185
169
        """Convert transport into a path suitable for using in a request.
186
 
 
 
170
        
187
171
        Note that the resulting remote path doesn't encode the host name or
188
172
        anything but path, so it is only safe to use it in requests sent over
189
173
        the medium from the matching transport.
194
178
class SmartClientHooks(hooks.Hooks):
195
179
 
196
180
    def __init__(self):
197
 
        hooks.Hooks.__init__(self, "bzrlib.smart.client", "_SmartClient.hooks")
198
 
        self.add_hook('call',
199
 
            "Called when the smart client is submitting a request to the "
200
 
            "smart server. Called with a bzrlib.smart.client.CallHookParams "
201
 
            "object. Streaming request bodies, and responses, are not "
202
 
            "accessible.", None)
203
 
 
204
 
 
 
181
        hooks.Hooks.__init__(self)
 
182
        self['call'] = []
 
183
 
 
184
        
205
185
_SmartClient.hooks = SmartClientHooks()
206
186
 
207
187
 
208
188
class CallHookParams(object):
209
 
 
 
189
    
210
190
    def __init__(self, method, args, body, readv_body, medium):
211
191
        self.method = method
212
192
        self.args = args