607
601
# which is newer than a previously supplied older-than version.
608
602
# This indicates that some smart verb call is not guarded
609
603
# appropriately (it should simply not have been tried).
604
raise AssertionError(
611
605
"_remember_remote_is_before(%r) called, but "
612
606
"_remember_remote_is_before(%r) was called previously."
613
, version_tuple, self._remote_version_is_before)
614
if 'hpss' in debug.debug_flags:
615
ui.ui_factory.show_warning(
616
"_remember_remote_is_before(%r) called, but "
617
"_remember_remote_is_before(%r) was called previously."
618
% (version_tuple, self._remote_version_is_before))
607
% (version_tuple, self._remote_version_is_before))
620
608
self._remote_version_is_before = version_tuple
622
610
def protocol_version(self):
734
722
def _read_bytes(self, count):
735
723
"""See SmartClientStreamMedium._read_bytes."""
736
bytes_to_read = min(count, _MAX_READ_SIZE)
737
bytes = self._readable_pipe.read(bytes_to_read)
724
bytes = self._readable_pipe.read(count)
738
725
self._report_activity(len(bytes), 'read')
742
class SSHParams(object):
743
"""A set of parameters for starting a remote bzr via SSH."""
729
class SmartSSHClientMedium(SmartClientStreamMedium):
730
"""A client medium using SSH."""
745
732
def __init__(self, host, port=None, username=None, password=None,
746
bzr_remote_path='bzr'):
749
self.username = username
750
self.password = password
751
self.bzr_remote_path = bzr_remote_path
754
class SmartSSHClientMedium(SmartClientStreamMedium):
755
"""A client medium using SSH.
757
It delegates IO to a SmartClientSocketMedium or
758
SmartClientAlreadyConnectedSocketMedium (depending on platform).
761
def __init__(self, base, ssh_params, vendor=None):
733
base=None, vendor=None, bzr_remote_path=None):
762
734
"""Creates a client that will connect on the first use.
764
:param ssh_params: A SSHParams instance.
765
736
:param vendor: An optional override for the ssh vendor to use. See
766
737
bzrlib.transport.ssh for details on ssh vendors.
768
self._real_medium = None
769
self._ssh_params = ssh_params
770
# for the benefit of progress making a short description of this
772
self._scheme = 'bzr+ssh'
739
self._connected = False
741
self._password = password
743
self._username = username
773
744
# SmartClientStreamMedium stores the repr of this object in its
774
745
# _DebugCounter so we have to store all the values used in our repr
775
746
# method before calling the super init.
776
747
SmartClientStreamMedium.__init__(self, base)
748
self._read_from = None
749
self._ssh_connection = None
777
750
self._vendor = vendor
778
self._ssh_connection = None
751
self._write_to = None
752
self._bzr_remote_path = bzr_remote_path
753
# for the benefit of progress making a short description of this
755
self._scheme = 'bzr+ssh'
780
757
def __repr__(self):
781
if self._ssh_params.port is None:
784
maybe_port = ':%s' % self._ssh_params.port
785
return "%s(%s://%s@%s%s/)" % (
758
return "%s(connected=%r, username=%r, host=%r, port=%r)" % (
786
759
self.__class__.__name__,
788
self._ssh_params.username,
789
self._ssh_params.host,
792
765
def _accept_bytes(self, bytes):
793
766
"""See SmartClientStreamMedium.accept_bytes."""
794
767
self._ensure_connection()
795
self._real_medium.accept_bytes(bytes)
768
self._write_to.write(bytes)
769
self._report_activity(len(bytes), 'write')
797
771
def disconnect(self):
798
772
"""See SmartClientMedium.disconnect()."""
799
if self._real_medium is not None:
800
self._real_medium.disconnect()
801
self._real_medium = None
802
if self._ssh_connection is not None:
803
self._ssh_connection.close()
804
self._ssh_connection = None
773
if not self._connected:
775
self._read_from.close()
776
self._write_to.close()
777
self._ssh_connection.close()
778
self._connected = False
806
780
def _ensure_connection(self):
807
781
"""Connect this medium if not already connected."""
808
if self._real_medium is not None:
810
784
if self._vendor is None:
811
785
vendor = ssh._get_ssh_vendor()
813
787
vendor = self._vendor
814
self._ssh_connection = vendor.connect_ssh(self._ssh_params.username,
815
self._ssh_params.password, self._ssh_params.host,
816
self._ssh_params.port,
817
command=[self._ssh_params.bzr_remote_path, 'serve', '--inet',
788
self._ssh_connection = vendor.connect_ssh(self._username,
789
self._password, self._host, self._port,
790
command=[self._bzr_remote_path, 'serve', '--inet',
818
791
'--directory=/', '--allow-writes'])
819
io_kind, io_object = self._ssh_connection.get_sock_or_pipes()
820
if io_kind == 'socket':
821
self._real_medium = SmartClientAlreadyConnectedSocketMedium(
822
self.base, io_object)
823
elif io_kind == 'pipes':
824
read_from, write_to = io_object
825
self._real_medium = SmartSimplePipesClientMedium(
826
read_from, write_to, self.base)
828
raise AssertionError(
829
"Unexpected io_kind %r from %r"
830
% (io_kind, self._ssh_connection))
792
self._read_from, self._write_to = \
793
self._ssh_connection.get_filelike_channels()
794
self._connected = True
832
796
def _flush(self):
833
797
"""See SmartClientStreamMedium._flush()."""
834
self._real_medium._flush()
798
self._write_to.flush()
836
800
def _read_bytes(self, count):
837
801
"""See SmartClientStreamMedium.read_bytes."""
838
if self._real_medium is None:
802
if not self._connected:
839
803
raise errors.MediumNotConnected(self)
840
return self._real_medium.read_bytes(count)
804
bytes_to_read = min(count, _MAX_READ_SIZE)
805
bytes = self._read_from.read(bytes_to_read)
806
self._report_activity(len(bytes), 'read')
843
810
# Port 4155 is the default port for bzr://, registered with IANA.
845
812
BZR_DEFAULT_PORT = 4155
848
class SmartClientSocketMedium(SmartClientStreamMedium):
849
"""A client medium using a socket.
851
This class isn't usable directly. Use one of its subclasses instead.
815
class SmartTCPClientMedium(SmartClientStreamMedium):
816
"""A client medium using TCP."""
854
def __init__(self, base):
818
def __init__(self, host, port, base):
819
"""Creates a client that will connect on the first use."""
855
820
SmartClientStreamMedium.__init__(self, base)
821
self._connected = False
856
824
self._socket = None
857
self._connected = False
859
826
def _accept_bytes(self, bytes):
860
827
"""See SmartClientMedium.accept_bytes."""
861
828
self._ensure_connection()
862
829
osutils.send_all(self._socket, bytes, self._report_activity)
864
def _ensure_connection(self):
865
"""Connect this medium if not already connected."""
866
raise NotImplementedError(self._ensure_connection)
869
"""See SmartClientStreamMedium._flush().
871
For sockets we do no flushing. For TCP sockets we may want to turn off
872
TCP_NODELAY and add a means to do a flush, but that can be done in the
876
def _read_bytes(self, count):
877
"""See SmartClientMedium.read_bytes."""
878
if not self._connected:
879
raise errors.MediumNotConnected(self)
880
return osutils.read_bytes_from_socket(
881
self._socket, self._report_activity)
883
831
def disconnect(self):
884
832
"""See SmartClientMedium.disconnect()."""
885
833
if not self._connected:
937
875
(self._host, port, err_msg))
938
876
self._connected = True
941
class SmartClientAlreadyConnectedSocketMedium(SmartClientSocketMedium):
942
"""A client medium for an already connected socket.
944
Note that this class will assume it "owns" the socket, so it will close it
945
when its disconnect method is called.
948
def __init__(self, base, sock):
949
SmartClientSocketMedium.__init__(self, base)
951
self._connected = True
953
def _ensure_connection(self):
954
# Already connected, by definition! So nothing to do.
879
"""See SmartClientStreamMedium._flush().
881
For TCP we do no flushing. We may want to turn off TCP_NODELAY and
882
add a means to do a flush, but that can be done in the future.
885
def _read_bytes(self, count):
886
"""See SmartClientMedium.read_bytes."""
887
if not self._connected:
888
raise errors.MediumNotConnected(self)
889
return _read_bytes_from_socket(
890
self._socket.recv, count, self._report_activity)
958
893
class SmartClientStreamMediumRequest(SmartClientMediumRequest):