214
231
while not self.finished:
215
232
server_protocol = self._build_protocol()
216
233
self._serve_one_request(server_protocol)
234
except errors.ConnectionTimeout, e:
235
trace.note('%s' % (e,))
236
trace.log_exception_quietly()
237
self._disconnect_client()
238
# We reported it, no reason to make a big fuss.
217
240
except Exception, e:
218
241
stderr.write("%s terminating on exception %s\n" % (self, e))
243
self._disconnect_client()
245
def _stop_gracefully(self):
246
"""When we finish this message, stop looking for more."""
247
trace.mutter('Stopping %s' % (self,))
250
def _disconnect_client(self):
251
"""Close the current connection. We stopped due to a timeout/etc."""
252
# The default implementation is a no-op, because that is all we used to
253
# do when disconnecting from a client. I suppose we never had the
254
# *server* initiate a disconnect, before
256
def _wait_for_bytes_with_timeout(self, timeout_seconds):
257
"""Wait for more bytes to be read, but timeout if none available.
259
This allows us to detect idle connections, and stop trying to read from
260
them, without setting the socket itself to non-blocking. This also
261
allows us to specify when we watch for idle timeouts.
263
:return: Did we timeout? (True if we timed out, False if there is data
266
raise NotImplementedError(self._wait_for_bytes_with_timeout)
221
268
def _build_protocol(self):
222
269
"""Identifies the version of the incoming request, and returns an
234
285
protocol.accept_bytes(unused_bytes)
288
def _wait_on_descriptor(self, fd, timeout_seconds):
289
"""select() on a file descriptor, waiting for nonblocking read()
291
This will raise a ConnectionTimeout exception if we do not get a
292
readable handle before timeout_seconds.
295
t_end = self._timer() + timeout_seconds
296
poll_timeout = min(timeout_seconds, self._client_poll_timeout)
298
while not rs and not xs and self._timer() < t_end:
302
rs, _, xs = select.select([fd], [], [fd], poll_timeout)
303
except (select.error, socket.error) as e:
304
err = getattr(e, 'errno', None)
305
if err is None and getattr(e, 'args', None) is not None:
306
# select.error doesn't have 'errno', it just has args[0]
308
if err in _bad_file_descriptor:
309
return # Not a socket indicates read() will fail
310
elif err == errno.EINTR:
311
# Interrupted, keep looping.
316
raise errors.ConnectionTimeout('disconnecting client after %.1f seconds'
317
% (timeout_seconds,))
237
319
def _serve_one_request(self, protocol):
238
320
"""Read one request from input, process, send back a response.
240
322
:param protocol: a SmartServerRequestProtocol.
243
327
self._serve_one_request_unguarded(protocol)
244
328
except KeyboardInterrupt:
261
345
class SmartServerSocketStreamMedium(SmartServerStreamMedium):
263
def __init__(self, sock, backing_transport, root_client_path='/'):
347
def __init__(self, sock, backing_transport, root_client_path='/',
266
351
:param sock: the socket the server will read from. It will be put
267
352
into blocking mode.
269
354
SmartServerStreamMedium.__init__(
270
self, backing_transport, root_client_path=root_client_path)
355
self, backing_transport, root_client_path=root_client_path,
271
357
sock.setblocking(True)
272
358
self.socket = sock
359
# Get the getpeername now, as we might be closed later when we care.
361
self._client_info = sock.getpeername()
363
self._client_info = '<unknown>'
366
return '%s(client=%s)' % (self.__class__.__name__, self._client_info)
369
return '%s.%s(client=%s)' % (self.__module__, self.__class__.__name__,
274
372
def _serve_one_request_unguarded(self, protocol):
275
373
while protocol.next_read_size():
285
383
self._push_back(protocol.unused_data)
385
def _disconnect_client(self):
386
"""Close the current connection. We stopped due to a timeout/etc."""
389
def _wait_for_bytes_with_timeout(self, timeout_seconds):
390
"""Wait for more bytes to be read, but timeout if none available.
392
This allows us to detect idle connections, and stop trying to read from
393
them, without setting the socket itself to non-blocking. This also
394
allows us to specify when we watch for idle timeouts.
396
:return: None, this will raise ConnectionTimeout if we time out before
399
return self._wait_on_descriptor(self.socket, timeout_seconds)
287
401
def _read_bytes(self, desired_count):
288
402
return osutils.read_bytes_from_socket(
289
403
self.socket, self._report_activity)
307
421
class SmartServerPipeStreamMedium(SmartServerStreamMedium):
309
def __init__(self, in_file, out_file, backing_transport):
423
def __init__(self, in_file, out_file, backing_transport, timeout=None):
310
424
"""Construct new server.
312
426
:param in_file: Python file from which requests can be read.
313
427
:param out_file: Python file to write responses.
314
428
:param backing_transport: Transport for the directory served.
316
SmartServerStreamMedium.__init__(self, backing_transport)
430
SmartServerStreamMedium.__init__(self, backing_transport,
317
432
if sys.platform == 'win32':
318
433
# force binary mode for files
343
469
protocol.accept_bytes(bytes)
471
def _disconnect_client(self):
476
def _wait_for_bytes_with_timeout(self, timeout_seconds):
477
"""Wait for more bytes to be read, but timeout if none available.
479
This allows us to detect idle connections, and stop trying to read from
480
them, without setting the socket itself to non-blocking. This also
481
allows us to specify when we watch for idle timeouts.
483
:return: None, this will raise ConnectionTimeout if we time out before
486
if (getattr(self._in, 'fileno', None) is None
487
or sys.platform == 'win32'):
488
# You can't select() file descriptors on Windows.
490
return self._wait_on_descriptor(self._in, timeout_seconds)
345
492
def _read_bytes(self, desired_count):
346
493
return self._in.read(desired_count)
491
638
return self._medium._get_line()
641
class _VfsRefuser(object):
642
"""An object that refuses all VFS requests.
647
client._SmartClient.hooks.install_named_hook(
648
'call', self.check_vfs, 'vfs refuser')
650
def check_vfs(self, params):
652
request_method = request.request_handlers.get(params.method)
654
# A method we don't know about doesn't count as a VFS method.
656
if issubclass(request_method, vfs.VfsRequest):
657
raise errors.HpssVfsRequestNotAllowed(params.method, params.args)
494
660
class _DebugCounter(object):
495
661
"""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
663
When a medium is garbage-collected, or failing that when
664
bzrlib.global_state exits, the total number of calls made on that medium
665
are reported via trace.note.
502
668
def __init__(self):
503
669
self.counts = weakref.WeakKeyDictionary()
504
670
client._SmartClient.hooks.install_named_hook(
505
671
'call', self.increment_call_count, 'hpss call counter')
506
atexit.register(self.flush_all)
672
bzrlib.global_state.cleanups.add_cleanup(self.flush_all)
508
674
def track(self, medium):
509
675
"""Start tracking calls made to a medium.
711
882
return SmartClientStreamMediumRequest(self)
885
"""We have been disconnected, reset current state.
887
This resets things like _current_request and connected state.
890
self._current_request = None
714
893
class SmartSimplePipesClientMedium(SmartClientStreamMedium):
715
894
"""A client medium using simple pipes.
717
896
This client does not manage the pipes: it assumes they will always be open.
719
Note that if readable_pipe.read might raise IOError or OSError with errno
720
of EINTR, it must be safe to retry the read. Plain CPython fileobjects
721
(such as used for sys.stdin) are safe.
724
899
def __init__(self, readable_pipe, writeable_pipe, base):
729
904
def _accept_bytes(self, bytes):
730
905
"""See SmartClientStreamMedium.accept_bytes."""
731
self._writeable_pipe.write(bytes)
907
self._writeable_pipe.write(bytes)
909
if e.errno in (errno.EINVAL, errno.EPIPE):
910
raise errors.ConnectionReset(
911
"Error trying to write to subprocess:\n%s" % (e,))
732
913
self._report_activity(len(bytes), 'write')
734
915
def _flush(self):
735
916
"""See SmartClientStreamMedium._flush()."""
917
# Note: If flush were to fail, we'd like to raise ConnectionReset, etc.
918
# However, testing shows that even when the child process is
919
# gone, this doesn't error.
736
920
self._writeable_pipe.flush()
738
922
def _read_bytes(self, count):
739
923
"""See SmartClientStreamMedium._read_bytes."""
740
bytes = osutils.until_no_eintr(self._readable_pipe.read, count)
924
bytes_to_read = min(count, _MAX_READ_SIZE)
925
bytes = self._readable_pipe.read(bytes_to_read)
741
926
self._report_activity(len(bytes), 'read')
930
class SSHParams(object):
931
"""A set of parameters for starting a remote bzr via SSH."""
933
def __init__(self, host, port=None, username=None, password=None,
934
bzr_remote_path='bzr'):
937
self.username = username
938
self.password = password
939
self.bzr_remote_path = bzr_remote_path
745
942
class SmartSSHClientMedium(SmartClientStreamMedium):
746
"""A client medium using SSH."""
748
def __init__(self, host, port=None, username=None, password=None,
749
base=None, vendor=None, bzr_remote_path=None):
943
"""A client medium using SSH.
945
It delegates IO to a SmartSimplePipesClientMedium or
946
SmartClientAlreadyConnectedSocketMedium (depending on platform).
949
def __init__(self, base, ssh_params, vendor=None):
750
950
"""Creates a client that will connect on the first use.
952
:param ssh_params: A SSHParams instance.
752
953
:param vendor: An optional override for the ssh vendor to use. See
753
954
bzrlib.transport.ssh for details on ssh vendors.
755
self._connected = False
757
self._password = password
759
self._username = username
956
self._real_medium = None
957
self._ssh_params = ssh_params
760
958
# for the benefit of progress making a short description of this
762
960
self._scheme = 'bzr+ssh'
764
962
# _DebugCounter so we have to store all the values used in our repr
765
963
# method before calling the super init.
766
964
SmartClientStreamMedium.__init__(self, base)
767
self._read_from = None
965
self._vendor = vendor
768
966
self._ssh_connection = None
769
self._vendor = vendor
770
self._write_to = None
771
self._bzr_remote_path = bzr_remote_path
773
968
def __repr__(self):
774
if self._port is None:
969
if self._ssh_params.port is None:
777
maybe_port = ':%s' % self._port
778
return "%s(%s://%s@%s%s/)" % (
972
maybe_port = ':%s' % self._ssh_params.port
973
if self._ssh_params.username is None:
976
maybe_user = '%s@' % self._ssh_params.username
977
return "%s(%s://%s%s%s/)" % (
779
978
self.__class__.__name__,
981
self._ssh_params.host,
785
984
def _accept_bytes(self, bytes):
786
985
"""See SmartClientStreamMedium.accept_bytes."""
787
986
self._ensure_connection()
788
self._write_to.write(bytes)
789
self._report_activity(len(bytes), 'write')
987
self._real_medium.accept_bytes(bytes)
791
989
def disconnect(self):
792
990
"""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
991
if self._real_medium is not None:
992
self._real_medium.disconnect()
993
self._real_medium = None
994
if self._ssh_connection is not None:
995
self._ssh_connection.close()
996
self._ssh_connection = None
800
998
def _ensure_connection(self):
801
999
"""Connect this medium if not already connected."""
1000
if self._real_medium is not None:
804
1002
if self._vendor is None:
805
1003
vendor = ssh._get_ssh_vendor()
807
1005
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',
1006
self._ssh_connection = vendor.connect_ssh(self._ssh_params.username,
1007
self._ssh_params.password, self._ssh_params.host,
1008
self._ssh_params.port,
1009
command=[self._ssh_params.bzr_remote_path, 'serve', '--inet',
811
1010
'--directory=/', '--allow-writes'])
812
self._read_from, self._write_to = \
813
self._ssh_connection.get_filelike_channels()
814
self._connected = True
1011
io_kind, io_object = self._ssh_connection.get_sock_or_pipes()
1012
if io_kind == 'socket':
1013
self._real_medium = SmartClientAlreadyConnectedSocketMedium(
1014
self.base, io_object)
1015
elif io_kind == 'pipes':
1016
read_from, write_to = io_object
1017
self._real_medium = SmartSimplePipesClientMedium(
1018
read_from, write_to, self.base)
1020
raise AssertionError(
1021
"Unexpected io_kind %r from %r"
1022
% (io_kind, self._ssh_connection))
816
1024
def _flush(self):
817
1025
"""See SmartClientStreamMedium._flush()."""
818
self._write_to.flush()
1026
self._real_medium._flush()
820
1028
def _read_bytes(self, count):
821
1029
"""See SmartClientStreamMedium.read_bytes."""
822
if not self._connected:
1030
if self._real_medium is None:
823
1031
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')
1032
return self._real_medium.read_bytes(count)
830
1035
# Port 4155 is the default port for bzr://, registered with IANA.
832
1037
BZR_DEFAULT_PORT = 4155
835
class SmartTCPClientMedium(SmartClientStreamMedium):
836
"""A client medium using TCP."""
1040
class SmartClientSocketMedium(SmartClientStreamMedium):
1041
"""A client medium using a socket.
1043
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."""
1046
def __init__(self, base):
840
1047
SmartClientStreamMedium.__init__(self, base)
841
1049
self._connected = False
846
1051
def _accept_bytes(self, bytes):
847
1052
"""See SmartClientMedium.accept_bytes."""
848
1053
self._ensure_connection()
849
1054
osutils.send_all(self._socket, bytes, self._report_activity)
1056
def _ensure_connection(self):
1057
"""Connect this medium if not already connected."""
1058
raise NotImplementedError(self._ensure_connection)
1061
"""See SmartClientStreamMedium._flush().
1063
For sockets we do no flushing. For TCP sockets we may want to turn off
1064
TCP_NODELAY and add a means to do a flush, but that can be done in the
1068
def _read_bytes(self, count):
1069
"""See SmartClientMedium.read_bytes."""
1070
if not self._connected:
1071
raise errors.MediumNotConnected(self)
1072
return osutils.read_bytes_from_socket(
1073
self._socket, self._report_activity)
851
1075
def disconnect(self):
852
1076
"""See SmartClientMedium.disconnect()."""
853
1077
if not self._connected:
895
1129
(self._host, port, err_msg))
896
1130
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)
1133
class SmartClientAlreadyConnectedSocketMedium(SmartClientSocketMedium):
1134
"""A client medium for an already connected socket.
1136
Note that this class will assume it "owns" the socket, so it will close it
1137
when its disconnect method is called.
1140
def __init__(self, base, sock):
1141
SmartClientSocketMedium.__init__(self, base)
1143
self._connected = True
1145
def _ensure_connection(self):
1146
# Already connected, by definition! So nothing to do.
913
1150
class SmartClientStreamMediumRequest(SmartClientMediumRequest):