37
from bzrlib.smart import client, medium, protocol
35
from bzrlib.smart import client, medium
38
36
from bzrlib.symbol_versioning import (deprecated_method, one_four)
79
77
one is being cloned from. Attributes such as the medium will
82
:param medium: The medium to use for this RemoteTransport. This must be
83
supplied if _from_transport is None.
80
:param medium: The medium to use for this RemoteTransport. If None,
81
the medium from the _from_transport is shared. If both this
82
and _from_transport are None, a new medium will be built.
83
_from_transport and medium cannot both be specified.
85
85
:param _client: Override the _SmartClient used by this transport. This
86
86
should only be used for testing purposes; normally this is
105
105
self._shared_connection = transport._SharedConnection(medium,
109
# No medium was specified, so share the medium from the
111
medium = self._shared_connection.connection
110
# No medium was specified, so share the medium from the
112
medium = self._shared_connection.connection
113
raise AssertionError(
114
"Both _from_transport (%r) and medium (%r) passed to "
115
"RemoteTransport.__init__, but these parameters are mutally "
116
"exclusive." % (_from_transport, medium))
114
118
if _client is None:
115
self._client = client._SmartClient(medium, self.base)
119
self._client = client._SmartClient(medium)
117
121
self._client = _client
157
161
return self._combine_paths(self._path, relpath)
159
163
def _call(self, method, *args):
160
resp = self._call2(method, *args)
165
resp = self._call2(method, *args)
166
except errors.ErrorFromSmartServer, err:
167
self._translate_error(err.error_tuple)
161
168
self._translate_error(resp)
163
170
def _call2(self, method, *args):
164
171
"""Call a method on the remote server."""
165
return self._client.call(method, *args)
173
return self._client.call(method, *args)
174
except errors.ErrorFromSmartServer, err:
175
self._translate_error(err.error_tuple)
167
177
def _call_with_body_bytes(self, method, args, body):
168
178
"""Call a method on the remote server with body bytes."""
169
return self._client.call_with_body_bytes(method, args, body)
180
return self._client.call_with_body_bytes(method, args, body)
181
except errors.ErrorFromSmartServer, err:
182
self._translate_error(err.error_tuple)
171
184
def has(self, relpath):
172
185
"""Indicate whether a remote file of the given name exists or not.
191
204
def get_bytes(self, relpath):
192
205
remote = self._remote_path(relpath)
193
request = self.get_smart_medium().get_request()
194
smart_protocol = protocol.SmartClientRequestProtocolOne(request)
195
smart_protocol.call('get', remote)
196
resp = smart_protocol.read_response_tuple(True)
207
resp, response_handler = self._client.call_expecting_body('get', remote)
208
except errors.ErrorFromSmartServer, err:
209
self._translate_error(err.error_tuple, relpath)
197
210
if resp != ('ok', ):
198
smart_protocol.cancel_read_body()
199
self._translate_error(resp, relpath)
200
return smart_protocol.read_body_bytes()
211
response_handler.cancel_read_body()
212
raise errors.UnexpectedSmartServerResponse(resp)
213
return response_handler.read_body_bytes()
202
215
def _serialise_optional_mode(self, mode):
306
319
limit=self._max_readv_combine,
307
320
fudge_factor=self._bytes_to_read_before_seek))
309
request = self.get_smart_medium().get_request()
310
smart_protocol = protocol.SmartClientRequestProtocolOne(request)
311
smart_protocol.call_with_body_readv_array(
312
('readv', self._remote_path(relpath)),
313
[(c.start, c.length) for c in coalesced])
314
resp = smart_protocol.read_response_tuple(True)
323
result = self._client.call_with_body_readv_array(
324
('readv', self._remote_path(relpath),),
325
[(c.start, c.length) for c in coalesced])
326
resp, response_handler = result
327
except errors.ErrorFromSmartServer, err:
328
self._translate_error(err.error_tuple)
316
330
if resp[0] != 'readv':
317
331
# This should raise an exception
318
smart_protocol.cancel_read_body()
319
self._translate_error(resp)
332
response_handler.cancel_read_body()
333
raise errors.UnexpectedSmartServerResponse(resp)
322
335
# FIXME: this should know how many bytes are needed, for clarity.
323
data = smart_protocol.read_body_bytes()
336
data = response_handler.read_body_bytes()
324
337
# Cache the results, but only until they have been fulfilled
326
339
for c_offset in coalesced:
459
472
def _build_medium(self):
460
return medium.SmartTCPClientMedium(self._host, self._port), None
473
client_medium = medium.SmartTCPClientMedium(
474
self._host, self._port, self.base)
475
return client_medium, None
463
478
class RemoteSSHTransport(RemoteTransport):
474
489
location_config = config.LocationConfig(self.base)
475
490
bzr_remote_path = location_config.get_bzr_remote_path()
476
return medium.SmartSSHClientMedium(self._host, self._port,
477
self._user, self._password, bzr_remote_path=bzr_remote_path), None
491
client_medium = medium.SmartSSHClientMedium(self._host, self._port,
492
self._user, self._password, self.base,
493
bzr_remote_path=bzr_remote_path)
494
return client_medium, None
480
497
class RemoteHTTPTransport(RemoteTransport):