214
232
while not self.finished:
215
233
server_protocol = self._build_protocol()
216
234
self._serve_one_request(server_protocol)
235
except errors.ConnectionTimeout, e:
236
trace.note('%s' % (e,))
237
trace.log_exception_quietly()
238
self._disconnect_client()
239
# We reported it, no reason to make a big fuss.
217
241
except Exception, e:
218
242
stderr.write("%s terminating on exception %s\n" % (self, e))
244
self._disconnect_client()
246
def _stop_gracefully(self):
247
"""When we finish this message, stop looking for more."""
248
trace.mutter('Stopping %s' % (self,))
251
def _disconnect_client(self):
252
"""Close the current connection. We stopped due to a timeout/etc."""
253
# The default implementation is a no-op, because that is all we used to
254
# do when disconnecting from a client. I suppose we never had the
255
# *server* initiate a disconnect, before
257
def _wait_for_bytes_with_timeout(self, timeout_seconds):
258
"""Wait for more bytes to be read, but timeout if none available.
260
This allows us to detect idle connections, and stop trying to read from
261
them, without setting the socket itself to non-blocking. This also
262
allows us to specify when we watch for idle timeouts.
264
:return: Did we timeout? (True if we timed out, False if there is data
267
raise NotImplementedError(self._wait_for_bytes_with_timeout)
221
269
def _build_protocol(self):
222
270
"""Identifies the version of the incoming request, and returns an
234
286
protocol.accept_bytes(unused_bytes)
289
def _wait_on_descriptor(self, fd, timeout_seconds):
290
"""select() on a file descriptor, waiting for nonblocking read()
292
This will raise a ConnectionTimeout exception if we do not get a
293
readable handle before timeout_seconds.
296
t_end = self._timer() + timeout_seconds
297
poll_timeout = min(timeout_seconds, self._client_poll_timeout)
299
while not rs and not xs and self._timer() < t_end:
303
rs, _, xs = select.select([fd], [], [fd], poll_timeout)
304
except (select.error, socket.error) as e:
305
err = getattr(e, 'errno', None)
306
if err is None and getattr(e, 'args', None) is not None:
307
# select.error doesn't have 'errno', it just has args[0]
309
if err in _bad_file_descriptor:
310
return # Not a socket indicates read() will fail
311
elif err == errno.EINTR:
312
# Interrupted, keep looping.
317
raise errors.ConnectionTimeout('disconnecting client after %.1f seconds'
318
% (timeout_seconds,))
237
320
def _serve_one_request(self, protocol):
238
321
"""Read one request from input, process, send back a response.
240
323
:param protocol: a SmartServerRequestProtocol.
243
328
self._serve_one_request_unguarded(protocol)
244
329
except KeyboardInterrupt:
261
346
class SmartServerSocketStreamMedium(SmartServerStreamMedium):
263
def __init__(self, sock, backing_transport, root_client_path='/'):
348
def __init__(self, sock, backing_transport, root_client_path='/',
266
352
:param sock: the socket the server will read from. It will be put
267
353
into blocking mode.
269
355
SmartServerStreamMedium.__init__(
270
self, backing_transport, root_client_path=root_client_path)
356
self, backing_transport, root_client_path=root_client_path,
271
358
sock.setblocking(True)
272
359
self.socket = sock
360
# Get the getpeername now, as we might be closed later when we care.
362
self._client_info = sock.getpeername()
364
self._client_info = '<unknown>'
367
return '%s(client=%s)' % (self.__class__.__name__, self._client_info)
370
return '%s.%s(client=%s)' % (self.__module__, self.__class__.__name__,
274
373
def _serve_one_request_unguarded(self, protocol):
275
374
while protocol.next_read_size():
285
384
self._push_back(protocol.unused_data)
386
def _disconnect_client(self):
387
"""Close the current connection. We stopped due to a timeout/etc."""
390
def _wait_for_bytes_with_timeout(self, timeout_seconds):
391
"""Wait for more bytes to be read, but timeout if none available.
393
This allows us to detect idle connections, and stop trying to read from
394
them, without setting the socket itself to non-blocking. This also
395
allows us to specify when we watch for idle timeouts.
397
:return: None, this will raise ConnectionTimeout if we time out before
400
return self._wait_on_descriptor(self.socket, timeout_seconds)
287
402
def _read_bytes(self, desired_count):
288
403
return osutils.read_bytes_from_socket(
289
404
self.socket, self._report_activity)
307
422
class SmartServerPipeStreamMedium(SmartServerStreamMedium):
309
def __init__(self, in_file, out_file, backing_transport):
424
def __init__(self, in_file, out_file, backing_transport, timeout=None):
310
425
"""Construct new server.
312
427
:param in_file: Python file from which requests can be read.
313
428
:param out_file: Python file to write responses.
314
429
:param backing_transport: Transport for the directory served.
316
SmartServerStreamMedium.__init__(self, backing_transport)
431
SmartServerStreamMedium.__init__(self, backing_transport,
317
433
if sys.platform == 'win32':
318
434
# force binary mode for files
343
470
protocol.accept_bytes(bytes)
472
def _disconnect_client(self):
477
def _wait_for_bytes_with_timeout(self, timeout_seconds):
478
"""Wait for more bytes to be read, but timeout if none available.
480
This allows us to detect idle connections, and stop trying to read from
481
them, without setting the socket itself to non-blocking. This also
482
allows us to specify when we watch for idle timeouts.
484
:return: None, this will raise ConnectionTimeout if we time out before
487
if (getattr(self._in, 'fileno', None) is None
488
or sys.platform == 'win32'):
489
# You can't select() file descriptors on Windows.
491
return self._wait_on_descriptor(self._in, timeout_seconds)
345
493
def _read_bytes(self, desired_count):
346
494
return self._in.read(desired_count)
491
639
return self._medium._get_line()
642
class _VfsRefuser(object):
643
"""An object that refuses all VFS requests.
648
client._SmartClient.hooks.install_named_hook(
649
'call', self.check_vfs, 'vfs refuser')
651
def check_vfs(self, params):
653
request_method = request.request_handlers.get(params.method)
655
# A method we don't know about doesn't count as a VFS method.
657
if issubclass(request_method, vfs.VfsRequest):
658
raise errors.HpssVfsRequestNotAllowed(params.method, params.args)
494
661
class _DebugCounter(object):
495
662
"""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
664
When a medium is garbage-collected, or failing that when
665
bzrlib.global_state exits, the total number of calls made on that medium
666
are reported via trace.note.
502
669
def __init__(self):
503
670
self.counts = weakref.WeakKeyDictionary()
504
671
client._SmartClient.hooks.install_named_hook(
505
672
'call', self.increment_call_count, 'hpss call counter')
506
atexit.register(self.flush_all)
673
bzrlib.global_state.cleanups.add_cleanup(self.flush_all)
508
675
def track(self, medium):
509
676
"""Start tracking calls made to a medium.
711
883
return SmartClientStreamMediumRequest(self)
886
"""We have been disconnected, reset current state.
888
This resets things like _current_request and connected state.
891
self._current_request = None
714
894
class SmartSimplePipesClientMedium(SmartClientStreamMedium):
715
895
"""A client medium using simple pipes.
717
897
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
900
def __init__(self, readable_pipe, writeable_pipe, base):
729
905
def _accept_bytes(self, bytes):
730
906
"""See SmartClientStreamMedium.accept_bytes."""
731
self._writeable_pipe.write(bytes)
908
self._writeable_pipe.write(bytes)
910
if e.errno in (errno.EINVAL, errno.EPIPE):
911
raise errors.ConnectionReset(
912
"Error trying to write to subprocess:\n%s" % (e,))
732
914
self._report_activity(len(bytes), 'write')
734
916
def _flush(self):
735
917
"""See SmartClientStreamMedium._flush()."""
918
# Note: If flush were to fail, we'd like to raise ConnectionReset, etc.
919
# However, testing shows that even when the child process is
920
# gone, this doesn't error.
736
921
self._writeable_pipe.flush()
738
923
def _read_bytes(self, count):
739
924
"""See SmartClientStreamMedium._read_bytes."""
740
bytes = osutils.until_no_eintr(self._readable_pipe.read, count)
925
bytes_to_read = min(count, _MAX_READ_SIZE)
926
bytes = self._readable_pipe.read(bytes_to_read)
741
927
self._report_activity(len(bytes), 'read')
931
class SSHParams(object):
932
"""A set of parameters for starting a remote bzr via SSH."""
934
def __init__(self, host, port=None, username=None, password=None,
935
bzr_remote_path='bzr'):
938
self.username = username
939
self.password = password
940
self.bzr_remote_path = bzr_remote_path
745
943
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):
944
"""A client medium using SSH.
946
It delegates IO to a SmartSimplePipesClientMedium or
947
SmartClientAlreadyConnectedSocketMedium (depending on platform).
950
def __init__(self, base, ssh_params, vendor=None):
750
951
"""Creates a client that will connect on the first use.
953
:param ssh_params: A SSHParams instance.
752
954
:param vendor: An optional override for the ssh vendor to use. See
753
955
bzrlib.transport.ssh for details on ssh vendors.
755
self._connected = False
757
self._password = password
759
self._username = username
957
self._real_medium = None
958
self._ssh_params = ssh_params
760
959
# for the benefit of progress making a short description of this
762
961
self._scheme = 'bzr+ssh'
764
963
# _DebugCounter so we have to store all the values used in our repr
765
964
# method before calling the super init.
766
965
SmartClientStreamMedium.__init__(self, base)
767
self._read_from = None
966
self._vendor = vendor
768
967
self._ssh_connection = None
769
self._vendor = vendor
770
self._write_to = None
771
self._bzr_remote_path = bzr_remote_path
773
969
def __repr__(self):
774
if self._port is None:
970
if self._ssh_params.port is None:
777
maybe_port = ':%s' % self._port
778
return "%s(%s://%s@%s%s/)" % (
973
maybe_port = ':%s' % self._ssh_params.port
974
if self._ssh_params.username is None:
977
maybe_user = '%s@' % self._ssh_params.username
978
return "%s(%s://%s%s%s/)" % (
779
979
self.__class__.__name__,
982
self._ssh_params.host,
785
985
def _accept_bytes(self, bytes):
786
986
"""See SmartClientStreamMedium.accept_bytes."""
787
987
self._ensure_connection()
788
self._write_to.write(bytes)
789
self._report_activity(len(bytes), 'write')
988
self._real_medium.accept_bytes(bytes)
791
990
def disconnect(self):
792
991
"""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
992
if self._real_medium is not None:
993
self._real_medium.disconnect()
994
self._real_medium = None
995
if self._ssh_connection is not None:
996
self._ssh_connection.close()
997
self._ssh_connection = None
800
999
def _ensure_connection(self):
801
1000
"""Connect this medium if not already connected."""
1001
if self._real_medium is not None:
804
1003
if self._vendor is None:
805
1004
vendor = ssh._get_ssh_vendor()
807
1006
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',
1007
self._ssh_connection = vendor.connect_ssh(self._ssh_params.username,
1008
self._ssh_params.password, self._ssh_params.host,
1009
self._ssh_params.port,
1010
command=[self._ssh_params.bzr_remote_path, 'serve', '--inet',
811
1011
'--directory=/', '--allow-writes'])
812
self._read_from, self._write_to = \
813
self._ssh_connection.get_filelike_channels()
814
self._connected = True
1012
io_kind, io_object = self._ssh_connection.get_sock_or_pipes()
1013
if io_kind == 'socket':
1014
self._real_medium = SmartClientAlreadyConnectedSocketMedium(
1015
self.base, io_object)
1016
elif io_kind == 'pipes':
1017
read_from, write_to = io_object
1018
self._real_medium = SmartSimplePipesClientMedium(
1019
read_from, write_to, self.base)
1021
raise AssertionError(
1022
"Unexpected io_kind %r from %r"
1023
% (io_kind, self._ssh_connection))
816
1025
def _flush(self):
817
1026
"""See SmartClientStreamMedium._flush()."""
818
self._write_to.flush()
1027
self._real_medium._flush()
820
1029
def _read_bytes(self, count):
821
1030
"""See SmartClientStreamMedium.read_bytes."""
822
if not self._connected:
1031
if self._real_medium is None:
823
1032
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')
1033
return self._real_medium.read_bytes(count)
830
1036
# Port 4155 is the default port for bzr://, registered with IANA.
832
1038
BZR_DEFAULT_PORT = 4155
835
class SmartTCPClientMedium(SmartClientStreamMedium):
836
"""A client medium using TCP."""
1041
class SmartClientSocketMedium(SmartClientStreamMedium):
1042
"""A client medium using a socket.
1044
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."""
1047
def __init__(self, base):
840
1048
SmartClientStreamMedium.__init__(self, base)
841
1050
self._connected = False
846
1052
def _accept_bytes(self, bytes):
847
1053
"""See SmartClientMedium.accept_bytes."""
848
1054
self._ensure_connection()
849
1055
osutils.send_all(self._socket, bytes, self._report_activity)
1057
def _ensure_connection(self):
1058
"""Connect this medium if not already connected."""
1059
raise NotImplementedError(self._ensure_connection)
1062
"""See SmartClientStreamMedium._flush().
1064
For sockets we do no flushing. For TCP sockets we may want to turn off
1065
TCP_NODELAY and add a means to do a flush, but that can be done in the
1069
def _read_bytes(self, count):
1070
"""See SmartClientMedium.read_bytes."""
1071
if not self._connected:
1072
raise errors.MediumNotConnected(self)
1073
return osutils.read_bytes_from_socket(
1074
self._socket, self._report_activity)
851
1076
def disconnect(self):
852
1077
"""See SmartClientMedium.disconnect()."""
853
1078
if not self._connected:
895
1130
(self._host, port, err_msg))
896
1131
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)
1134
class SmartClientAlreadyConnectedSocketMedium(SmartClientSocketMedium):
1135
"""A client medium for an already connected socket.
1137
Note that this class will assume it "owns" the socket, so it will close it
1138
when its disconnect method is called.
1141
def __init__(self, base, sock):
1142
SmartClientSocketMedium.__init__(self, base)
1144
self._connected = True
1146
def _ensure_connection(self):
1147
# Already connected, by definition! So nothing to do.
913
1151
class SmartClientStreamMediumRequest(SmartClientMediumRequest):