491
491
return self._medium._get_line()
494
class _VfsRefuser(object):
495
"""An object that refuses all VFS requests.
500
client._SmartClient.hooks.install_named_hook(
501
'call', self.check_vfs, 'vfs refuser')
503
def check_vfs(self, params):
505
request_method = request.request_handlers.get(params.method)
507
# A method we don't know about doesn't count as a VFS method.
509
if issubclass(request_method, vfs.VfsRequest):
510
raise errors.HpssVfsRequestNotAllowed(params.method, params.args)
494
513
class _DebugCounter(object):
495
514
"""An object that counts the HPSS calls made to each client medium.
497
When a medium is garbage-collected, or failing that when atexit functions
498
are run, the total number of calls made on that medium are reported via
516
When a medium is garbage-collected, or failing that when
517
bzrlib.global_state exits, the total number of calls made on that medium
518
are reported via trace.note.
502
521
def __init__(self):
503
522
self.counts = weakref.WeakKeyDictionary()
504
523
client._SmartClient.hooks.install_named_hook(
505
524
'call', self.increment_call_count, 'hpss call counter')
506
atexit.register(self.flush_all)
525
bzrlib.global_state.cleanups.add_cleanup(self.flush_all)
508
527
def track(self, medium):
509
528
"""Start tracking calls made to a medium.
738
758
def _read_bytes(self, count):
739
759
"""See SmartClientStreamMedium._read_bytes."""
740
bytes = osutils.until_no_eintr(self._readable_pipe.read, count)
760
bytes_to_read = min(count, _MAX_READ_SIZE)
761
bytes = self._readable_pipe.read(bytes_to_read)
741
762
self._report_activity(len(bytes), 'read')
766
class SSHParams(object):
767
"""A set of parameters for starting a remote bzr via SSH."""
769
def __init__(self, host, port=None, username=None, password=None,
770
bzr_remote_path='bzr'):
773
self.username = username
774
self.password = password
775
self.bzr_remote_path = bzr_remote_path
745
778
class SmartSSHClientMedium(SmartClientStreamMedium):
746
"""A client medium using SSH."""
779
"""A client medium using SSH.
781
It delegates IO to a SmartClientSocketMedium or
782
SmartClientAlreadyConnectedSocketMedium (depending on platform).
748
def __init__(self, host, port=None, username=None, password=None,
749
base=None, vendor=None, bzr_remote_path=None):
785
def __init__(self, base, ssh_params, vendor=None):
750
786
"""Creates a client that will connect on the first use.
788
:param ssh_params: A SSHParams instance.
752
789
:param vendor: An optional override for the ssh vendor to use. See
753
790
bzrlib.transport.ssh for details on ssh vendors.
755
self._connected = False
757
self._password = password
759
self._username = username
792
self._real_medium = None
793
self._ssh_params = ssh_params
760
794
# for the benefit of progress making a short description of this
762
796
self._scheme = 'bzr+ssh'
764
798
# _DebugCounter so we have to store all the values used in our repr
765
799
# method before calling the super init.
766
800
SmartClientStreamMedium.__init__(self, base)
767
self._read_from = None
801
self._vendor = vendor
768
802
self._ssh_connection = None
769
self._vendor = vendor
770
self._write_to = None
771
self._bzr_remote_path = bzr_remote_path
773
804
def __repr__(self):
774
if self._port is None:
805
if self._ssh_params.port is None:
777
maybe_port = ':%s' % self._port
808
maybe_port = ':%s' % self._ssh_params.port
778
809
return "%s(%s://%s@%s%s/)" % (
779
810
self.__class__.__name__,
812
self._ssh_params.username,
813
self._ssh_params.host,
785
816
def _accept_bytes(self, bytes):
786
817
"""See SmartClientStreamMedium.accept_bytes."""
787
818
self._ensure_connection()
788
self._write_to.write(bytes)
789
self._report_activity(len(bytes), 'write')
819
self._real_medium.accept_bytes(bytes)
791
821
def disconnect(self):
792
822
"""See SmartClientMedium.disconnect()."""
793
if not self._connected:
795
self._read_from.close()
796
self._write_to.close()
797
self._ssh_connection.close()
798
self._connected = False
823
if self._real_medium is not None:
824
self._real_medium.disconnect()
825
self._real_medium = None
826
if self._ssh_connection is not None:
827
self._ssh_connection.close()
828
self._ssh_connection = None
800
830
def _ensure_connection(self):
801
831
"""Connect this medium if not already connected."""
832
if self._real_medium is not None:
804
834
if self._vendor is None:
805
835
vendor = ssh._get_ssh_vendor()
807
837
vendor = self._vendor
808
self._ssh_connection = vendor.connect_ssh(self._username,
809
self._password, self._host, self._port,
810
command=[self._bzr_remote_path, 'serve', '--inet',
838
self._ssh_connection = vendor.connect_ssh(self._ssh_params.username,
839
self._ssh_params.password, self._ssh_params.host,
840
self._ssh_params.port,
841
command=[self._ssh_params.bzr_remote_path, 'serve', '--inet',
811
842
'--directory=/', '--allow-writes'])
812
self._read_from, self._write_to = \
813
self._ssh_connection.get_filelike_channels()
814
self._connected = True
843
io_kind, io_object = self._ssh_connection.get_sock_or_pipes()
844
if io_kind == 'socket':
845
self._real_medium = SmartClientAlreadyConnectedSocketMedium(
846
self.base, io_object)
847
elif io_kind == 'pipes':
848
read_from, write_to = io_object
849
self._real_medium = SmartSimplePipesClientMedium(
850
read_from, write_to, self.base)
852
raise AssertionError(
853
"Unexpected io_kind %r from %r"
854
% (io_kind, self._ssh_connection))
816
856
def _flush(self):
817
857
"""See SmartClientStreamMedium._flush()."""
818
self._write_to.flush()
858
self._real_medium._flush()
820
860
def _read_bytes(self, count):
821
861
"""See SmartClientStreamMedium.read_bytes."""
822
if not self._connected:
862
if self._real_medium is None:
823
863
raise errors.MediumNotConnected(self)
824
bytes_to_read = min(count, _MAX_READ_SIZE)
825
bytes = self._read_from.read(bytes_to_read)
826
self._report_activity(len(bytes), 'read')
864
return self._real_medium.read_bytes(count)
830
867
# Port 4155 is the default port for bzr://, registered with IANA.
832
869
BZR_DEFAULT_PORT = 4155
835
class SmartTCPClientMedium(SmartClientStreamMedium):
836
"""A client medium using TCP."""
872
class SmartClientSocketMedium(SmartClientStreamMedium):
873
"""A client medium using a socket.
875
This class isn't usable directly. Use one of its subclasses instead.
838
def __init__(self, host, port, base):
839
"""Creates a client that will connect on the first use."""
878
def __init__(self, base):
840
879
SmartClientStreamMedium.__init__(self, base)
841
881
self._connected = False
846
883
def _accept_bytes(self, bytes):
847
884
"""See SmartClientMedium.accept_bytes."""
848
885
self._ensure_connection()
849
886
osutils.send_all(self._socket, bytes, self._report_activity)
888
def _ensure_connection(self):
889
"""Connect this medium if not already connected."""
890
raise NotImplementedError(self._ensure_connection)
893
"""See SmartClientStreamMedium._flush().
895
For sockets we do no flushing. For TCP sockets we may want to turn off
896
TCP_NODELAY and add a means to do a flush, but that can be done in the
900
def _read_bytes(self, count):
901
"""See SmartClientMedium.read_bytes."""
902
if not self._connected:
903
raise errors.MediumNotConnected(self)
904
return osutils.read_bytes_from_socket(
905
self._socket, self._report_activity)
851
907
def disconnect(self):
852
908
"""See SmartClientMedium.disconnect()."""
853
909
if not self._connected:
895
961
(self._host, port, err_msg))
896
962
self._connected = True
899
"""See SmartClientStreamMedium._flush().
901
For TCP we do no flushing. We may want to turn off TCP_NODELAY and
902
add a means to do a flush, but that can be done in the future.
905
def _read_bytes(self, count):
906
"""See SmartClientMedium.read_bytes."""
907
if not self._connected:
908
raise errors.MediumNotConnected(self)
909
return osutils.read_bytes_from_socket(
910
self._socket, self._report_activity)
965
class SmartClientAlreadyConnectedSocketMedium(SmartClientSocketMedium):
966
"""A client medium for an already connected socket.
968
Note that this class will assume it "owns" the socket, so it will close it
969
when its disconnect method is called.
972
def __init__(self, base, sock):
973
SmartClientSocketMedium.__init__(self, base)
975
self._connected = True
977
def _ensure_connection(self):
978
# Already connected, by definition! So nothing to do.
913
982
class SmartClientStreamMediumRequest(SmartClientMediumRequest):