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
20
from bzrlib import errors
23
class _SmartClient(object):
25
def __init__(self, medium, headers=None):
28
:param medium: a SmartClientMedium
32
self._headers = {'Software version': bzrlib.__version__}
34
self._headers = dict(headers)
36
def _send_request(self, protocol_version, method, args, body=None,
38
encoder, response_handler = self._construct_protocol(
40
encoder.set_headers(self._headers)
42
if readv_body is not None:
44
"body and readv_body are mutually exclusive.")
45
encoder.call_with_body_bytes((method, ) + args, body)
46
elif readv_body is not None:
47
encoder.call_with_body_readv_array((method, ) + args,
50
encoder.call(method, *args)
51
return response_handler
53
def _call_and_read_response(self, method, args, body=None, readv_body=None,
54
expect_response_body=True):
55
if self._medium._protocol_version is not None:
56
response_handler = self._send_request(
57
self._medium._protocol_version, method, args, body=body,
58
readv_body=readv_body)
59
return (response_handler.read_response_tuple(
60
expect_body=expect_response_body),
63
for protocol_version in [3, 2]:
64
response_handler = self._send_request(
65
protocol_version, method, args, body=body,
66
readv_body=readv_body)
68
response_tuple = response_handler.read_response_tuple(
69
expect_body=expect_response_body)
70
except errors.UnexpectedProtocolVersionMarker, err:
71
# TODO: We could recover from this without disconnecting if
72
# we recognise the protocol version.
74
'Server does not understand Bazaar network protocol %d,'
75
' reconnecting. (Upgrade the server to avoid this.)'
76
% (protocol_version,))
77
self._medium.disconnect()
80
self._medium._protocol_version = protocol_version
81
return response_tuple, response_handler
82
raise errors.SmartProtocolError(
83
'Server is not a Bazaar server: ' + str(err))
85
def _construct_protocol(self, version):
86
request = self._medium.get_request()
88
request_encoder = protocol.ProtocolThreeRequester(request)
89
response_handler = message.ConventionalResponseHandler()
90
response_proto = protocol.ProtocolThreeDecoder(
91
response_handler, expect_version_marker=True)
92
response_handler.setProtoAndMediumRequest(response_proto, request)
94
request_encoder = protocol.SmartClientRequestProtocolTwo(request)
95
response_handler = request_encoder
97
request_encoder = protocol.SmartClientRequestProtocolOne(request)
98
response_handler = request_encoder
99
return request_encoder, response_handler
101
def call(self, method, *args):
102
"""Call a method on the remote server."""
103
result, protocol = self.call_expecting_body(method, *args)
104
protocol.cancel_read_body()
107
def call_expecting_body(self, method, *args):
108
"""Call a method and return the result and the protocol object.
110
The body can be read like so::
112
result, smart_protocol = smart_client.call_expecting_body(...)
113
body = smart_protocol.read_body_bytes()
115
return self._call_and_read_response(
116
method, args, expect_response_body=True)
118
def call_with_body_bytes(self, method, args, body):
119
"""Call a method on the remote server with body bytes."""
120
if type(method) is not str:
121
raise TypeError('method must be a byte string, not %r' % (method,))
123
if type(arg) is not str:
124
raise TypeError('args must be byte strings, not %r' % (args,))
125
if type(body) is not str:
126
raise TypeError('body must be byte string, not %r' % (body,))
127
response, response_handler = self._call_and_read_response(
128
method, args, body=body, expect_response_body=False)
131
def call_with_body_bytes_expecting_body(self, method, args, body):
132
"""Call a method on the remote server with body bytes."""
133
if type(method) is not str:
134
raise TypeError('method must be a byte string, not %r' % (method,))
136
if type(arg) is not str:
137
raise TypeError('args must be byte strings, not %r' % (args,))
138
if type(body) is not str:
139
raise TypeError('body must be byte string, not %r' % (body,))
140
response, response_handler = self._call_and_read_response(
141
method, args, body=body, expect_response_body=True)
142
return (response, response_handler)
144
def call_with_body_readv_array(self, args, body):
145
response, response_handler = self._call_and_read_response(
146
args[0], args[1:], readv_body=body, expect_response_body=True)
147
return (response, response_handler)
149
def remote_path_from_transport(self, transport):
150
"""Convert transport into a path suitable for using in a request.
152
Note that the resulting remote path doesn't encode the host name or
153
anything but path, so it is only safe to use it in requests sent over
154
the medium from the matching transport.
156
return self._medium.remote_path_from_transport(transport)