1
# Copyright (C) 2006-2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.smart import message, protocol
19
from bzrlib.trace import warning
26
class _SmartClient(object):
28
def __init__(self, medium, headers=None):
31
:param medium: a SmartClientMedium
35
self._headers = {'Software version': bzrlib.__version__}
37
self._headers = dict(headers)
39
def _send_request(self, protocol_version, method, args, body=None,
41
encoder, response_handler = self._construct_protocol(
43
encoder.set_headers(self._headers)
45
if readv_body is not None:
47
"body and readv_body are mutually exclusive.")
48
encoder.call_with_body_bytes((method, ) + args, body)
49
elif readv_body is not None:
50
encoder.call_with_body_readv_array((method, ) + args,
53
encoder.call(method, *args)
54
return response_handler
56
def _run_call_hooks(self, method, args, body, readv_body):
57
if not _SmartClient.hooks['call']:
59
params = CallHookParams(method, args, body, readv_body, self._medium)
60
for hook in _SmartClient.hooks['call']:
63
def _call_and_read_response(self, method, args, body=None, readv_body=None,
64
expect_response_body=True):
65
self._run_call_hooks(method, args, body, readv_body)
66
if self._medium._protocol_version is not None:
67
response_handler = self._send_request(
68
self._medium._protocol_version, method, args, body=body,
69
readv_body=readv_body)
70
return (response_handler.read_response_tuple(
71
expect_body=expect_response_body),
74
for protocol_version in [3, 2]:
75
if protocol_version == 2:
76
# If v3 doesn't work, the remote side is older than 1.6.
77
self._medium._remember_remote_is_before((1, 6))
78
response_handler = self._send_request(
79
protocol_version, method, args, body=body,
80
readv_body=readv_body)
82
response_tuple = response_handler.read_response_tuple(
83
expect_body=expect_response_body)
84
except errors.UnexpectedProtocolVersionMarker, err:
85
# TODO: We could recover from this without disconnecting if
86
# we recognise the protocol version.
88
'Server does not understand Bazaar network protocol %d,'
89
' reconnecting. (Upgrade the server to avoid this.)'
90
% (protocol_version,))
91
self._medium.disconnect()
93
except errors.ErrorFromSmartServer:
94
# If we received an error reply from the server, then it
95
# must be ok with this protocol version.
96
self._medium._protocol_version = protocol_version
99
self._medium._protocol_version = protocol_version
100
return response_tuple, response_handler
101
raise errors.SmartProtocolError(
102
'Server is not a Bazaar server: ' + str(err))
104
def _construct_protocol(self, version):
105
request = self._medium.get_request()
107
request_encoder = protocol.ProtocolThreeRequester(request)
108
response_handler = message.ConventionalResponseHandler()
109
response_proto = protocol.ProtocolThreeDecoder(
110
response_handler, expect_version_marker=True)
111
response_handler.setProtoAndMediumRequest(response_proto, request)
113
request_encoder = protocol.SmartClientRequestProtocolTwo(request)
114
response_handler = request_encoder
116
request_encoder = protocol.SmartClientRequestProtocolOne(request)
117
response_handler = request_encoder
118
return request_encoder, response_handler
120
def call(self, method, *args):
121
"""Call a method on the remote server."""
122
result, protocol = self.call_expecting_body(method, *args)
123
protocol.cancel_read_body()
126
def call_expecting_body(self, method, *args):
127
"""Call a method and return the result and the protocol object.
129
The body can be read like so::
131
result, smart_protocol = smart_client.call_expecting_body(...)
132
body = smart_protocol.read_body_bytes()
134
return self._call_and_read_response(
135
method, args, expect_response_body=True)
137
def call_with_body_bytes(self, method, args, body):
138
"""Call a method on the remote server with body bytes."""
139
if type(method) is not str:
140
raise TypeError('method must be a byte string, not %r' % (method,))
142
if type(arg) is not str:
143
raise TypeError('args must be byte strings, not %r' % (args,))
144
if type(body) is not str:
145
raise TypeError('body must be byte string, not %r' % (body,))
146
response, response_handler = self._call_and_read_response(
147
method, args, body=body, expect_response_body=False)
150
def call_with_body_bytes_expecting_body(self, method, args, body):
151
"""Call a method on the remote server with body bytes."""
152
if type(method) is not str:
153
raise TypeError('method must be a byte string, not %r' % (method,))
155
if type(arg) is not str:
156
raise TypeError('args must be byte strings, not %r' % (args,))
157
if type(body) is not str:
158
raise TypeError('body must be byte string, not %r' % (body,))
159
response, response_handler = self._call_and_read_response(
160
method, args, body=body, expect_response_body=True)
161
return (response, response_handler)
163
def call_with_body_readv_array(self, args, body):
164
response, response_handler = self._call_and_read_response(
165
args[0], args[1:], readv_body=body, expect_response_body=True)
166
return (response, response_handler)
168
def remote_path_from_transport(self, transport):
169
"""Convert transport into a path suitable for using in a request.
171
Note that the resulting remote path doesn't encode the host name or
172
anything but path, so it is only safe to use it in requests sent over
173
the medium from the matching transport.
175
return self._medium.remote_path_from_transport(transport)
178
class SmartClientHooks(hooks.Hooks):
181
hooks.Hooks.__init__(self)
185
_SmartClient.hooks = SmartClientHooks()
188
class CallHookParams(object):
190
def __init__(self, method, args, body, readv_body, medium):
194
self.readv_body = readv_body
198
attrs = dict((k, v) for (k, v) in self.__dict__.iteritems()
200
return '<%s %r>' % (self.__class__.__name__, attrs)
202
def __eq__(self, other):
203
if type(other) is not type(self):
204
return NotImplemented
205
return self.__dict__ == other.__dict__
207
def __ne__(self, other):
208
return not self == other