1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# Copyright (C) 2006-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import bzrlib
from bzrlib.smart import message, protocol
from bzrlib.trace import warning
from bzrlib import (
errors,
hooks,
)
class _SmartClient(object):
def __init__(self, medium, headers=None):
"""Constructor.
:param medium: a SmartClientMedium
"""
self._medium = medium
if headers is None:
self._headers = {'Software version': bzrlib.__version__}
else:
self._headers = dict(headers)
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self._medium)
def _send_request(self, protocol_version, method, args, body=None,
readv_body=None, body_stream=None):
encoder, response_handler = self._construct_protocol(
protocol_version)
encoder.set_headers(self._headers)
if body is not None:
if readv_body is not None:
raise AssertionError(
"body and readv_body are mutually exclusive.")
if body_stream is not None:
raise AssertionError(
"body and body_stream are mutually exclusive.")
encoder.call_with_body_bytes((method, ) + args, body)
elif readv_body is not None:
if body_stream is not None:
raise AssertionError(
"readv_body and body_stream are mutually exclusive.")
encoder.call_with_body_readv_array((method, ) + args, readv_body)
elif body_stream is not None:
encoder.call_with_body_stream((method, ) + args, body_stream)
else:
encoder.call(method, *args)
return response_handler
def _run_call_hooks(self, method, args, body, readv_body):
if not _SmartClient.hooks['call']:
return
params = CallHookParams(method, args, body, readv_body, self._medium)
for hook in _SmartClient.hooks['call']:
hook(params)
def _call_and_read_response(self, method, args, body=None, readv_body=None,
body_stream=None, expect_response_body=True):
self._run_call_hooks(method, args, body, readv_body)
if self._medium._protocol_version is not None:
response_handler = self._send_request(
self._medium._protocol_version, method, args, body=body,
readv_body=readv_body, body_stream=body_stream)
return (response_handler.read_response_tuple(
expect_body=expect_response_body),
response_handler)
else:
for protocol_version in [3, 2]:
if protocol_version == 2:
# If v3 doesn't work, the remote side is older than 1.6.
self._medium._remember_remote_is_before((1, 6))
response_handler = self._send_request(
protocol_version, method, args, body=body,
readv_body=readv_body, body_stream=body_stream)
try:
response_tuple = response_handler.read_response_tuple(
expect_body=expect_response_body)
except errors.UnexpectedProtocolVersionMarker, err:
# TODO: We could recover from this without disconnecting if
# we recognise the protocol version.
warning(
'Server does not understand Bazaar network protocol %d,'
' reconnecting. (Upgrade the server to avoid this.)'
% (protocol_version,))
self._medium.disconnect()
continue
except errors.ErrorFromSmartServer:
# If we received an error reply from the server, then it
# must be ok with this protocol version.
self._medium._protocol_version = protocol_version
raise
else:
self._medium._protocol_version = protocol_version
return response_tuple, response_handler
raise errors.SmartProtocolError(
'Server is not a Bazaar server: ' + str(err))
def _construct_protocol(self, version):
request = self._medium.get_request()
if version == 3:
request_encoder = protocol.ProtocolThreeRequester(request)
response_handler = message.ConventionalResponseHandler()
response_proto = protocol.ProtocolThreeDecoder(
response_handler, expect_version_marker=True)
response_handler.setProtoAndMediumRequest(response_proto, request)
elif version == 2:
request_encoder = protocol.SmartClientRequestProtocolTwo(request)
response_handler = request_encoder
else:
request_encoder = protocol.SmartClientRequestProtocolOne(request)
response_handler = request_encoder
return request_encoder, response_handler
def call(self, method, *args):
"""Call a method on the remote server."""
result, protocol = self.call_expecting_body(method, *args)
protocol.cancel_read_body()
return result
def call_expecting_body(self, method, *args):
"""Call a method and return the result and the protocol object.
The body can be read like so::
result, smart_protocol = smart_client.call_expecting_body(...)
body = smart_protocol.read_body_bytes()
"""
return self._call_and_read_response(
method, args, expect_response_body=True)
def call_with_body_bytes(self, method, args, body):
"""Call a method on the remote server with body bytes."""
if type(method) is not str:
raise TypeError('method must be a byte string, not %r' % (method,))
for arg in args:
if type(arg) is not str:
raise TypeError('args must be byte strings, not %r' % (args,))
if type(body) is not str:
raise TypeError('body must be byte string, not %r' % (body,))
response, response_handler = self._call_and_read_response(
method, args, body=body, expect_response_body=False)
return response
def call_with_body_bytes_expecting_body(self, method, args, body):
"""Call a method on the remote server with body bytes."""
if type(method) is not str:
raise TypeError('method must be a byte string, not %r' % (method,))
for arg in args:
if type(arg) is not str:
raise TypeError('args must be byte strings, not %r' % (args,))
if type(body) is not str:
raise TypeError('body must be byte string, not %r' % (body,))
response, response_handler = self._call_and_read_response(
method, args, body=body, expect_response_body=True)
return (response, response_handler)
def call_with_body_readv_array(self, args, body):
response, response_handler = self._call_and_read_response(
args[0], args[1:], readv_body=body, expect_response_body=True)
return (response, response_handler)
def call_with_body_stream(self, args, stream):
response, response_handler = self._call_and_read_response(
args[0], args[1:], body_stream=stream,
expect_response_body=False)
return (response, response_handler)
def remote_path_from_transport(self, transport):
"""Convert transport into a path suitable for using in a request.
Note that the resulting remote path doesn't encode the host name or
anything but path, so it is only safe to use it in requests sent over
the medium from the matching transport.
"""
return self._medium.remote_path_from_transport(transport)
class SmartClientHooks(hooks.Hooks):
def __init__(self):
hooks.Hooks.__init__(self, "bzrlib.smart.client", "_SmartClient.hooks")
self.add_hook('call',
"Called when the smart client is submitting a request to the "
"smart server. Called with a bzrlib.smart.client.CallHookParams "
"object. Streaming request bodies, and responses, are not "
"accessible.", None)
_SmartClient.hooks = SmartClientHooks()
class CallHookParams(object):
def __init__(self, method, args, body, readv_body, medium):
self.method = method
self.args = args
self.body = body
self.readv_body = readv_body
self.medium = medium
def __repr__(self):
attrs = dict((k, v) for (k, v) in self.__dict__.iteritems()
if v is not None)
return '<%s %r>' % (self.__class__.__name__, attrs)
def __eq__(self, other):
if type(other) is not type(self):
return NotImplemented
return self.__dict__ == other.__dict__
def __ne__(self, other):
return not self == other
|