~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
# Copyright (C) 2006-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""The 'medium' layer for the smart servers and clients.

"Medium" here is the noun meaning "a means of transmission", not the adjective
for "the quality between big and small."

Media carry the bytes of the requests somehow (e.g. via TCP, wrapped in HTTP, or
over SSH), and pass them to and from the protocol logic.  See the overview in
bzrlib/transport/smart/__init__.py.
"""

import errno
import os
import sys
import time
import urllib

import bzrlib
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import select
import socket
import thread
import weakref

from bzrlib import (
    debug,
    errors,
    trace,
    ui,
    urlutils,
    )
from bzrlib.i18n import gettext
from bzrlib.smart import client, protocol, request, signals, vfs
from bzrlib.transport import ssh
""")
from bzrlib import osutils

# Throughout this module buffer size parameters are either limited to be at
# most _MAX_READ_SIZE, or are ignored and _MAX_READ_SIZE is used instead.
# For this module's purposes, MAX_SOCKET_CHUNK is a reasonable size for reads
# from non-sockets as well.
_MAX_READ_SIZE = osutils.MAX_SOCKET_CHUNK

def _get_protocol_factory_for_bytes(bytes):
    """Determine the right protocol factory for 'bytes'.

    This will return an appropriate protocol factory depending on the version
    of the protocol being used, as determined by inspecting the given bytes.
    The bytes should have at least one newline byte (i.e. be a whole line),
    otherwise it's possible that a request will be incorrectly identified as
    version 1.

    Typical use would be::

         factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
         server_protocol = factory(transport, write_func, root_client_path)
         server_protocol.accept_bytes(unused_bytes)

    :param bytes: a str of bytes of the start of the request.
    :returns: 2-tuple of (protocol_factory, unused_bytes).  protocol_factory is
        a callable that takes three args: transport, write_func,
        root_client_path.  unused_bytes are any bytes that were not part of a
        protocol version marker.
    """
    if bytes.startswith(protocol.MESSAGE_VERSION_THREE):
        protocol_factory = protocol.build_server_protocol_three
        bytes = bytes[len(protocol.MESSAGE_VERSION_THREE):]
    elif bytes.startswith(protocol.REQUEST_VERSION_TWO):
        protocol_factory = protocol.SmartServerRequestProtocolTwo
        bytes = bytes[len(protocol.REQUEST_VERSION_TWO):]
    else:
        protocol_factory = protocol.SmartServerRequestProtocolOne
    return protocol_factory, bytes


def _get_line(read_bytes_func):
    """Read bytes using read_bytes_func until a newline byte.

    This isn't particularly efficient, so should only be used when the
    expected size of the line is quite short.

    :returns: a tuple of two strs: (line, excess)
    """
    newline_pos = -1
    bytes = ''
    while newline_pos == -1:
        new_bytes = read_bytes_func(1)
        bytes += new_bytes
        if new_bytes == '':
            # Ran out of bytes before receiving a complete line.
            return bytes, ''
        newline_pos = bytes.find('\n')
    line = bytes[:newline_pos+1]
    excess = bytes[newline_pos+1:]
    return line, excess


class SmartMedium(object):
    """Base class for smart protocol media, both client- and server-side."""

    def __init__(self):
        self._push_back_buffer = None

    def _push_back(self, bytes):
        """Return unused bytes to the medium, because they belong to the next
        request(s).

        This sets the _push_back_buffer to the given bytes.
        """
        if self._push_back_buffer is not None:
            raise AssertionError(
                "_push_back called when self._push_back_buffer is %r"
                % (self._push_back_buffer,))
        if bytes == '':
            return
        self._push_back_buffer = bytes

    def _get_push_back_buffer(self):
        if self._push_back_buffer == '':
            raise AssertionError(
                '%s._push_back_buffer should never be the empty string, '
                'which can be confused with EOF' % (self,))
        bytes = self._push_back_buffer
        self._push_back_buffer = None
        return bytes

    def read_bytes(self, desired_count):
        """Read some bytes from this medium.

        :returns: some bytes, possibly more or less than the number requested
            in 'desired_count' depending on the medium.
        """
        if self._push_back_buffer is not None:
            return self._get_push_back_buffer()
        bytes_to_read = min(desired_count, _MAX_READ_SIZE)
        return self._read_bytes(bytes_to_read)

    def _read_bytes(self, count):
        raise NotImplementedError(self._read_bytes)

    def _get_line(self):
        """Read bytes from this request's response until a newline byte.

        This isn't particularly efficient, so should only be used when the
        expected size of the line is quite short.

        :returns: a string of bytes ending in a newline (byte 0x0A).
        """
        line, excess = _get_line(self.read_bytes)
        self._push_back(excess)
        return line

    def _report_activity(self, bytes, direction):
        """Notify that this medium has activity.

        Implementations should call this from all methods that actually do IO.
        Be careful that it's not called twice, if one method is implemented on
        top of another.

        :param bytes: Number of bytes read or written.
        :param direction: 'read' or 'write' or None.
        """
        ui.ui_factory.report_transport_activity(self, bytes, direction)


_bad_file_descriptor = (errno.EBADF,)
if sys.platform == 'win32':
    # Given on Windows if you pass a closed socket to select.select. Probably
    # also given if you pass a file handle to select.
    WSAENOTSOCK = 10038
    _bad_file_descriptor += (WSAENOTSOCK,)


class SmartServerStreamMedium(SmartMedium):
    """Handles smart commands coming over a stream.

    The stream may be a pipe connected to sshd, or a tcp socket, or an
    in-process fifo for testing.

    One instance is created for each connected client; it can serve multiple
    requests in the lifetime of the connection.

    The server passes requests through to an underlying backing transport,
    which will typically be a LocalTransport looking at the server's filesystem.

    :ivar _push_back_buffer: a str of bytes that have been read from the stream
        but not used yet, or None if there are no buffered bytes.  Subclasses
        should make sure to exhaust this buffer before reading more bytes from
        the stream.  See also the _push_back method.
    """

    _timer = time.time

    def __init__(self, backing_transport, root_client_path='/', timeout=None):
        """Construct new server.

        :param backing_transport: Transport for the directory served.
        """
        # backing_transport could be passed to serve instead of __init__
        self.backing_transport = backing_transport
        self.root_client_path = root_client_path
        self.finished = False
        if timeout is None:
            raise AssertionError('You must supply a timeout.')
        self._client_timeout = timeout
        self._client_poll_timeout = min(timeout / 10.0, 1.0)
        SmartMedium.__init__(self)

    def serve(self):
        """Serve requests until the client disconnects."""
        # Keep a reference to stderr because the sys module's globals get set to
        # None during interpreter shutdown.
        from sys import stderr
        try:
            while not self.finished:
                server_protocol = self._build_protocol()
                # TODO: This seems inelegant:
                if server_protocol is None:
                    # We could 'continue' only to notice that self.finished is
                    # True...
                    break
                self._serve_one_request(server_protocol)
        except errors.ConnectionTimeout, e:
            trace.note('%s' % (e,))
            trace.log_exception_quietly()
            self._disconnect_client()
            # We reported it, no reason to make a big fuss.
            return
        except Exception, e:
            stderr.write("%s terminating on exception %s\n" % (self, e))
            raise
        self._disconnect_client()

    def _stop_gracefully(self):
        """When we finish this message, stop looking for more."""
        trace.mutter('Stopping %s' % (self,))
        self.finished = True

    def _disconnect_client(self):
        """Close the current connection. We stopped due to a timeout/etc."""
        # The default implementation is a no-op, because that is all we used to
        # do when disconnecting from a client. I suppose we never had the
        # *server* initiate a disconnect, before

    def _wait_for_bytes_with_timeout(self, timeout_seconds):
        """Wait for more bytes to be read, but timeout if none available.

        This allows us to detect idle connections, and stop trying to read from
        them, without setting the socket itself to non-blocking. This also
        allows us to specify when we watch for idle timeouts.

        :return: Did we timeout? (True if we timed out, False if there is data
            to be read)
        """
        raise NotImplementedError(self._wait_for_bytes_with_timeout)

    def _build_protocol(self):
        """Identifies the version of the incoming request, and returns an
        a protocol object that can interpret it.

        If more bytes than the version prefix of the request are read, they will
        be fed into the protocol before it is returned.

        :returns: a SmartServerRequestProtocol.
        """
        self._wait_for_bytes_with_timeout(self._client_timeout)
        if self.finished:
            # We're stopping, so don't try to do any more work
            return None
        bytes = self._get_line()
        protocol_factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
        protocol = protocol_factory(
            self.backing_transport, self._write_out, self.root_client_path)
        protocol.accept_bytes(unused_bytes)
        return protocol

    def _wait_on_descriptor(self, fd, timeout_seconds):
        """select() on a file descriptor, waiting for nonblocking read()

        This will raise a ConnectionTimeout exception if we do not get a
        readable handle before timeout_seconds.
        :return: None
        """
        t_end = self._timer() + timeout_seconds
        poll_timeout = min(timeout_seconds, self._client_poll_timeout)
        rs = xs = None
        while not rs and not xs and self._timer() < t_end:
            if self.finished:
                return
            try:
                rs, _, xs = select.select([fd], [], [fd], poll_timeout)
            except (select.error, socket.error) as e:
                err = getattr(e, 'errno', None)
                if err is None and getattr(e, 'args', None) is not None:
                    # select.error doesn't have 'errno', it just has args[0]
                    err = e.args[0]
                if err in _bad_file_descriptor:
                    return # Not a socket indicates read() will fail
                elif err == errno.EINTR:
                    # Interrupted, keep looping.
                    continue
                raise
        if rs or xs:
            return
        raise errors.ConnectionTimeout('disconnecting client after %.1f seconds'
                                       % (timeout_seconds,))

    def _serve_one_request(self, protocol):
        """Read one request from input, process, send back a response.

        :param protocol: a SmartServerRequestProtocol.
        """
        try:
            self._serve_one_request_unguarded(protocol)
        except KeyboardInterrupt:
            raise
        except Exception, e:
            self.terminate_due_to_error()

    def terminate_due_to_error(self):
        """Called when an unhandled exception from the protocol occurs."""
        raise NotImplementedError(self.terminate_due_to_error)

    def _read_bytes(self, desired_count):
        """Get some bytes from the medium.

        :param desired_count: number of bytes we want to read.
        """
        raise NotImplementedError(self._read_bytes)


class SmartServerSocketStreamMedium(SmartServerStreamMedium):

    def __init__(self, sock, backing_transport, root_client_path='/',
                 timeout=None):
        """Constructor.

        :param sock: the socket the server will read from.  It will be put
            into blocking mode.
        """
        SmartServerStreamMedium.__init__(
            self, backing_transport, root_client_path=root_client_path,
            timeout=timeout)
        sock.setblocking(True)
        self.socket = sock
        # Get the getpeername now, as we might be closed later when we care.
        try:
            self._client_info = sock.getpeername()
        except socket.error:
            self._client_info = '<unknown>'

    def __str__(self):
        return '%s(client=%s)' % (self.__class__.__name__, self._client_info)

    def __repr__(self):
        return '%s.%s(client=%s)' % (self.__module__, self.__class__.__name__,
            self._client_info)

    def _serve_one_request_unguarded(self, protocol):
        while protocol.next_read_size():
            # We can safely try to read large chunks.  If there is less data
            # than MAX_SOCKET_CHUNK ready, the socket will just return a
            # short read immediately rather than block.
            bytes = self.read_bytes(osutils.MAX_SOCKET_CHUNK)
            if bytes == '':
                self.finished = True
                return
            protocol.accept_bytes(bytes)

        self._push_back(protocol.unused_data)

    def _disconnect_client(self):
        """Close the current connection. We stopped due to a timeout/etc."""
        self.socket.close()

    def _wait_for_bytes_with_timeout(self, timeout_seconds):
        """Wait for more bytes to be read, but timeout if none available.

        This allows us to detect idle connections, and stop trying to read from
        them, without setting the socket itself to non-blocking. This also
        allows us to specify when we watch for idle timeouts.

        :return: None, this will raise ConnectionTimeout if we time out before
            data is available.
        """
        return self._wait_on_descriptor(self.socket, timeout_seconds)

    def _read_bytes(self, desired_count):
        return osutils.read_bytes_from_socket(
            self.socket, self._report_activity)

    def terminate_due_to_error(self):
        # TODO: This should log to a server log file, but no such thing
        # exists yet.  Andrew Bennetts 2006-09-29.
        self.socket.close()
        self.finished = True

    def _write_out(self, bytes):
        tstart = osutils.timer_func()
        osutils.send_all(self.socket, bytes, self._report_activity)
        if 'hpss' in debug.debug_flags:
            thread_id = thread.get_ident()
            trace.mutter('%12s: [%s] %d bytes to the socket in %.3fs'
                         % ('wrote', thread_id, len(bytes),
                            osutils.timer_func() - tstart))


class SmartServerPipeStreamMedium(SmartServerStreamMedium):

    def __init__(self, in_file, out_file, backing_transport, timeout=None):
        """Construct new server.

        :param in_file: Python file from which requests can be read.
        :param out_file: Python file to write responses.
        :param backing_transport: Transport for the directory served.
        """
        SmartServerStreamMedium.__init__(self, backing_transport,
            timeout=timeout)
        if sys.platform == 'win32':
            # force binary mode for files
            import msvcrt
            for f in (in_file, out_file):
                fileno = getattr(f, 'fileno', None)
                if fileno:
                    msvcrt.setmode(fileno(), os.O_BINARY)
        self._in = in_file
        self._out = out_file

    def serve(self):
        """See SmartServerStreamMedium.serve"""
        # This is the regular serve, except it adds signal trapping for soft
        # shutdown.
        stop_gracefully = self._stop_gracefully
        signals.register_on_hangup(id(self), stop_gracefully)
        try:
            return super(SmartServerPipeStreamMedium, self).serve()
        finally:
            signals.unregister_on_hangup(id(self))

    def _serve_one_request_unguarded(self, protocol):
        while True:
            # We need to be careful not to read past the end of the current
            # request, or else the read from the pipe will block, so we use
            # protocol.next_read_size().
            bytes_to_read = protocol.next_read_size()
            if bytes_to_read == 0:
                # Finished serving this request.
                self._out.flush()
                return
            bytes = self.read_bytes(bytes_to_read)
            if bytes == '':
                # Connection has been closed.
                self.finished = True
                self._out.flush()
                return
            protocol.accept_bytes(bytes)

    def _disconnect_client(self):
        self._in.close()
        self._out.flush()
        self._out.close()

    def _wait_for_bytes_with_timeout(self, timeout_seconds):
        """Wait for more bytes to be read, but timeout if none available.

        This allows us to detect idle connections, and stop trying to read from
        them, without setting the socket itself to non-blocking. This also
        allows us to specify when we watch for idle timeouts.

        :return: None, this will raise ConnectionTimeout if we time out before
            data is available.
        """
        if (getattr(self._in, 'fileno', None) is None
            or sys.platform == 'win32'):
            # You can't select() file descriptors on Windows.
            return
        return self._wait_on_descriptor(self._in, timeout_seconds)

    def _read_bytes(self, desired_count):
        return self._in.read(desired_count)

    def terminate_due_to_error(self):
        # TODO: This should log to a server log file, but no such thing
        # exists yet.  Andrew Bennetts 2006-09-29.
        self._out.close()
        self.finished = True

    def _write_out(self, bytes):
        self._out.write(bytes)


class SmartClientMediumRequest(object):
    """A request on a SmartClientMedium.

    Each request allows bytes to be provided to it via accept_bytes, and then
    the response bytes to be read via read_bytes.

    For instance:
    request.accept_bytes('123')
    request.finished_writing()
    result = request.read_bytes(3)
    request.finished_reading()

    It is up to the individual SmartClientMedium whether multiple concurrent
    requests can exist. See SmartClientMedium.get_request to obtain instances
    of SmartClientMediumRequest, and the concrete Medium you are using for
    details on concurrency and pipelining.
    """

    def __init__(self, medium):
        """Construct a SmartClientMediumRequest for the medium medium."""
        self._medium = medium
        # we track state by constants - we may want to use the same
        # pattern as BodyReader if it gets more complex.
        # valid states are: "writing", "reading", "done"
        self._state = "writing"

    def accept_bytes(self, bytes):
        """Accept bytes for inclusion in this request.

        This method may not be called after finished_writing() has been
        called.  It depends upon the Medium whether or not the bytes will be
        immediately transmitted. Message based Mediums will tend to buffer the
        bytes until finished_writing() is called.

        :param bytes: A bytestring.
        """
        if self._state != "writing":
            raise errors.WritingCompleted(self)
        self._accept_bytes(bytes)

    def _accept_bytes(self, bytes):
        """Helper for accept_bytes.

        Accept_bytes checks the state of the request to determing if bytes
        should be accepted. After that it hands off to _accept_bytes to do the
        actual acceptance.
        """
        raise NotImplementedError(self._accept_bytes)

    def finished_reading(self):
        """Inform the request that all desired data has been read.

        This will remove the request from the pipeline for its medium (if the
        medium supports pipelining) and any further calls to methods on the
        request will raise ReadingCompleted.
        """
        if self._state == "writing":
            raise errors.WritingNotComplete(self)
        if self._state != "reading":
            raise errors.ReadingCompleted(self)
        self._state = "done"
        self._finished_reading()

    def _finished_reading(self):
        """Helper for finished_reading.

        finished_reading checks the state of the request to determine if
        finished_reading is allowed, and if it is hands off to _finished_reading
        to perform the action.
        """
        raise NotImplementedError(self._finished_reading)

    def finished_writing(self):
        """Finish the writing phase of this request.

        This will flush all pending data for this request along the medium.
        After calling finished_writing, you may not call accept_bytes anymore.
        """
        if self._state != "writing":
            raise errors.WritingCompleted(self)
        self._state = "reading"
        self._finished_writing()

    def _finished_writing(self):
        """Helper for finished_writing.

        finished_writing checks the state of the request to determine if
        finished_writing is allowed, and if it is hands off to _finished_writing
        to perform the action.
        """
        raise NotImplementedError(self._finished_writing)

    def read_bytes(self, count):
        """Read bytes from this requests response.

        This method will block and wait for count bytes to be read. It may not
        be invoked until finished_writing() has been called - this is to ensure
        a message-based approach to requests, for compatibility with message
        based mediums like HTTP.
        """
        if self._state == "writing":
            raise errors.WritingNotComplete(self)
        if self._state != "reading":
            raise errors.ReadingCompleted(self)
        return self._read_bytes(count)

    def _read_bytes(self, count):
        """Helper for SmartClientMediumRequest.read_bytes.

        read_bytes checks the state of the request to determing if bytes
        should be read. After that it hands off to _read_bytes to do the
        actual read.

        By default this forwards to self._medium.read_bytes because we are
        operating on the medium's stream.
        """
        return self._medium.read_bytes(count)

    def read_line(self):
        line = self._read_line()
        if not line.endswith('\n'):
            # end of file encountered reading from server
            raise errors.ConnectionReset(
                "Unexpected end of message. Please check connectivity "
                "and permissions, and report a bug if problems persist.")
        return line

    def _read_line(self):
        """Helper for SmartClientMediumRequest.read_line.

        By default this forwards to self._medium._get_line because we are
        operating on the medium's stream.
        """
        return self._medium._get_line()


class _VfsRefuser(object):
    """An object that refuses all VFS requests.

    """

    def __init__(self):
        client._SmartClient.hooks.install_named_hook(
            'call', self.check_vfs, 'vfs refuser')

    def check_vfs(self, params):
        try:
            request_method = request.request_handlers.get(params.method)
        except KeyError:
            # A method we don't know about doesn't count as a VFS method.
            return
        if issubclass(request_method, vfs.VfsRequest):
            raise errors.HpssVfsRequestNotAllowed(params.method, params.args)


class _DebugCounter(object):
    """An object that counts the HPSS calls made to each client medium.

    When a medium is garbage-collected, or failing that when
    bzrlib.global_state exits, the total number of calls made on that medium
    are reported via trace.note.
    """

    def __init__(self):
        self.counts = weakref.WeakKeyDictionary()
        client._SmartClient.hooks.install_named_hook(
            'call', self.increment_call_count, 'hpss call counter')
        bzrlib.global_state.cleanups.add_cleanup(self.flush_all)

    def track(self, medium):
        """Start tracking calls made to a medium.

        This only keeps a weakref to the medium, so shouldn't affect the
        medium's lifetime.
        """
        medium_repr = repr(medium)
        # Add this medium to the WeakKeyDictionary
        self.counts[medium] = dict(count=0, vfs_count=0,
                                   medium_repr=medium_repr)
        # Weakref callbacks are fired in reverse order of their association
        # with the referenced object.  So we add a weakref *after* adding to
        # the WeakKeyDict so that we can report the value from it before the
        # entry is removed by the WeakKeyDict's own callback.
        ref = weakref.ref(medium, self.done)

    def increment_call_count(self, params):
        # Increment the count in the WeakKeyDictionary
        value = self.counts[params.medium]
        value['count'] += 1
        try:
            request_method = request.request_handlers.get(params.method)
        except KeyError:
            # A method we don't know about doesn't count as a VFS method.
            return
        if issubclass(request_method, vfs.VfsRequest):
            value['vfs_count'] += 1

    def done(self, ref):
        value = self.counts[ref]
        count, vfs_count, medium_repr = (
            value['count'], value['vfs_count'], value['medium_repr'])
        # In case this callback is invoked for the same ref twice (by the
        # weakref callback and by the atexit function), set the call count back
        # to 0 so this item won't be reported twice.
        value['count'] = 0
        value['vfs_count'] = 0
        if count != 0:
            trace.note(gettext('HPSS calls: {0} ({1} vfs) {2}').format(
                       count, vfs_count, medium_repr))

    def flush_all(self):
        for ref in list(self.counts.keys()):
            self.done(ref)

_debug_counter = None
_vfs_refuser = None


class SmartClientMedium(SmartMedium):
    """Smart client is a medium for sending smart protocol requests over."""

    def __init__(self, base):
        super(SmartClientMedium, self).__init__()
        self.base = base
        self._protocol_version_error = None
        self._protocol_version = None
        self._done_hello = False
        # Be optimistic: we assume the remote end can accept new remote
        # requests until we get an error saying otherwise.
        # _remote_version_is_before tracks the bzr version the remote side
        # can be based on what we've seen so far.
        self._remote_version_is_before = None
        # Install debug hook function if debug flag is set.
        if 'hpss' in debug.debug_flags:
            global _debug_counter
            if _debug_counter is None:
                _debug_counter = _DebugCounter()
            _debug_counter.track(self)
        if 'hpss_client_no_vfs' in debug.debug_flags:
            global _vfs_refuser
            if _vfs_refuser is None:
                _vfs_refuser = _VfsRefuser()

    def _is_remote_before(self, version_tuple):
        """Is it possible the remote side supports RPCs for a given version?

        Typical use::

            needed_version = (1, 2)
            if medium._is_remote_before(needed_version):
                fallback_to_pre_1_2_rpc()
            else:
                try:
                    do_1_2_rpc()
                except UnknownSmartMethod:
                    medium._remember_remote_is_before(needed_version)
                    fallback_to_pre_1_2_rpc()

        :seealso: _remember_remote_is_before
        """
        if self._remote_version_is_before is None:
            # So far, the remote side seems to support everything
            return False
        return version_tuple >= self._remote_version_is_before

    def _remember_remote_is_before(self, version_tuple):
        """Tell this medium that the remote side is older the given version.

        :seealso: _is_remote_before
        """
        if (self._remote_version_is_before is not None and
            version_tuple > self._remote_version_is_before):
            # We have been told that the remote side is older than some version
            # which is newer than a previously supplied older-than version.
            # This indicates that some smart verb call is not guarded
            # appropriately (it should simply not have been tried).
            trace.mutter(
                "_remember_remote_is_before(%r) called, but "
                "_remember_remote_is_before(%r) was called previously."
                , version_tuple, self._remote_version_is_before)
            if 'hpss' in debug.debug_flags:
                ui.ui_factory.show_warning(
                    "_remember_remote_is_before(%r) called, but "
                    "_remember_remote_is_before(%r) was called previously."
                    % (version_tuple, self._remote_version_is_before))
            return
        self._remote_version_is_before = version_tuple

    def protocol_version(self):
        """Find out if 'hello' smart request works."""
        if self._protocol_version_error is not None:
            raise self._protocol_version_error
        if not self._done_hello:
            try:
                medium_request = self.get_request()
                # Send a 'hello' request in protocol version one, for maximum
                # backwards compatibility.
                client_protocol = protocol.SmartClientRequestProtocolOne(medium_request)
                client_protocol.query_version()
                self._done_hello = True
            except errors.SmartProtocolError, e:
                # Cache the error, just like we would cache a successful
                # result.
                self._protocol_version_error = e
                raise
        return '2'

    def should_probe(self):
        """Should RemoteBzrDirFormat.probe_transport send a smart request on
        this medium?

        Some transports are unambiguously smart-only; there's no need to check
        if the transport is able to carry smart requests, because that's all
        it is for.  In those cases, this method should return False.

        But some HTTP transports can sometimes fail to carry smart requests,
        but still be usuable for accessing remote bzrdirs via plain file
        accesses.  So for those transports, their media should return True here
        so that RemoteBzrDirFormat can determine if it is appropriate for that
        transport.
        """
        return False

    def disconnect(self):
        """If this medium maintains a persistent connection, close it.

        The default implementation does nothing.
        """

    def remote_path_from_transport(self, transport):
        """Convert transport into a path suitable for using in a request.

        Note that the resulting remote path doesn't encode the host name or
        anything but path, so it is only safe to use it in requests sent over
        the medium from the matching transport.
        """
        medium_base = urlutils.join(self.base, '/')
        rel_url = urlutils.relative_url(medium_base, transport.base)
        return urllib.unquote(rel_url)


class SmartClientStreamMedium(SmartClientMedium):
    """Stream based medium common class.

    SmartClientStreamMediums operate on a stream. All subclasses use a common
    SmartClientStreamMediumRequest for their requests, and should implement
    _accept_bytes and _read_bytes to allow the request objects to send and
    receive bytes.
    """

    def __init__(self, base):
        SmartClientMedium.__init__(self, base)
        self._current_request = None

    def accept_bytes(self, bytes):
        self._accept_bytes(bytes)

    def __del__(self):
        """The SmartClientStreamMedium knows how to close the stream when it is
        finished with it.
        """
        self.disconnect()

    def _flush(self):
        """Flush the output stream.

        This method is used by the SmartClientStreamMediumRequest to ensure that
        all data for a request is sent, to avoid long timeouts or deadlocks.
        """
        raise NotImplementedError(self._flush)

    def get_request(self):
        """See SmartClientMedium.get_request().

        SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
        for get_request.
        """
        return SmartClientStreamMediumRequest(self)


class SmartSimplePipesClientMedium(SmartClientStreamMedium):
    """A client medium using simple pipes.

    This client does not manage the pipes: it assumes they will always be open.
    """

    def __init__(self, readable_pipe, writeable_pipe, base):
        SmartClientStreamMedium.__init__(self, base)
        self._readable_pipe = readable_pipe
        self._writeable_pipe = writeable_pipe

    def _accept_bytes(self, bytes):
        """See SmartClientStreamMedium.accept_bytes."""
        self._writeable_pipe.write(bytes)
        self._report_activity(len(bytes), 'write')

    def _flush(self):
        """See SmartClientStreamMedium._flush()."""
        self._writeable_pipe.flush()

    def _read_bytes(self, count):
        """See SmartClientStreamMedium._read_bytes."""
        bytes_to_read = min(count, _MAX_READ_SIZE)
        bytes = self._readable_pipe.read(bytes_to_read)
        self._report_activity(len(bytes), 'read')
        return bytes


class SSHParams(object):
    """A set of parameters for starting a remote bzr via SSH."""

    def __init__(self, host, port=None, username=None, password=None,
            bzr_remote_path='bzr'):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.bzr_remote_path = bzr_remote_path


class SmartSSHClientMedium(SmartClientStreamMedium):
    """A client medium using SSH.
    
    It delegates IO to a SmartClientSocketMedium or
    SmartClientAlreadyConnectedSocketMedium (depending on platform).
    """

    def __init__(self, base, ssh_params, vendor=None):
        """Creates a client that will connect on the first use.

        :param ssh_params: A SSHParams instance.
        :param vendor: An optional override for the ssh vendor to use. See
            bzrlib.transport.ssh for details on ssh vendors.
        """
        self._real_medium = None
        self._ssh_params = ssh_params
        # for the benefit of progress making a short description of this
        # transport
        self._scheme = 'bzr+ssh'
        # SmartClientStreamMedium stores the repr of this object in its
        # _DebugCounter so we have to store all the values used in our repr
        # method before calling the super init.
        SmartClientStreamMedium.__init__(self, base)
        self._vendor = vendor
        self._ssh_connection = None

    def __repr__(self):
        if self._ssh_params.port is None:
            maybe_port = ''
        else:
            maybe_port = ':%s' % self._ssh_params.port
        return "%s(%s://%s@%s%s/)" % (
            self.__class__.__name__,
            self._scheme,
            self._ssh_params.username,
            self._ssh_params.host,
            maybe_port)

    def _accept_bytes(self, bytes):
        """See SmartClientStreamMedium.accept_bytes."""
        self._ensure_connection()
        self._real_medium.accept_bytes(bytes)

    def disconnect(self):
        """See SmartClientMedium.disconnect()."""
        if self._real_medium is not None:
            self._real_medium.disconnect()
            self._real_medium = None
        if self._ssh_connection is not None:
            self._ssh_connection.close()
            self._ssh_connection = None

    def _ensure_connection(self):
        """Connect this medium if not already connected."""
        if self._real_medium is not None:
            return
        if self._vendor is None:
            vendor = ssh._get_ssh_vendor()
        else:
            vendor = self._vendor
        self._ssh_connection = vendor.connect_ssh(self._ssh_params.username,
                self._ssh_params.password, self._ssh_params.host,
                self._ssh_params.port,
                command=[self._ssh_params.bzr_remote_path, 'serve', '--inet',
                         '--directory=/', '--allow-writes'])
        io_kind, io_object = self._ssh_connection.get_sock_or_pipes()
        if io_kind == 'socket':
            self._real_medium = SmartClientAlreadyConnectedSocketMedium(
                self.base, io_object)
        elif io_kind == 'pipes':
            read_from, write_to = io_object
            self._real_medium = SmartSimplePipesClientMedium(
                read_from, write_to, self.base)
        else:
            raise AssertionError(
                "Unexpected io_kind %r from %r"
                % (io_kind, self._ssh_connection))

    def _flush(self):
        """See SmartClientStreamMedium._flush()."""
        self._real_medium._flush()

    def _read_bytes(self, count):
        """See SmartClientStreamMedium.read_bytes."""
        if self._real_medium is None:
            raise errors.MediumNotConnected(self)
        return self._real_medium.read_bytes(count)


# Port 4155 is the default port for bzr://, registered with IANA.
BZR_DEFAULT_INTERFACE = None
BZR_DEFAULT_PORT = 4155


class SmartClientSocketMedium(SmartClientStreamMedium):
    """A client medium using a socket.
    
    This class isn't usable directly.  Use one of its subclasses instead.
    """

    def __init__(self, base):
        SmartClientStreamMedium.__init__(self, base)
        self._socket = None
        self._connected = False

    def _accept_bytes(self, bytes):
        """See SmartClientMedium.accept_bytes."""
        self._ensure_connection()
        osutils.send_all(self._socket, bytes, self._report_activity)

    def _ensure_connection(self):
        """Connect this medium if not already connected."""
        raise NotImplementedError(self._ensure_connection)

    def _flush(self):
        """See SmartClientStreamMedium._flush().

        For sockets we do no flushing. For TCP sockets we may want to turn off
        TCP_NODELAY and add a means to do a flush, but that can be done in the
        future.
        """

    def _read_bytes(self, count):
        """See SmartClientMedium.read_bytes."""
        if not self._connected:
            raise errors.MediumNotConnected(self)
        return osutils.read_bytes_from_socket(
            self._socket, self._report_activity)

    def disconnect(self):
        """See SmartClientMedium.disconnect()."""
        if not self._connected:
            return
        self._socket.close()
        self._socket = None
        self._connected = False


class SmartTCPClientMedium(SmartClientSocketMedium):
    """A client medium that creates a TCP connection."""

    def __init__(self, host, port, base):
        """Creates a client that will connect on the first use."""
        SmartClientSocketMedium.__init__(self, base)
        self._host = host
        self._port = port

    def _ensure_connection(self):
        """Connect this medium if not already connected."""
        if self._connected:
            return
        if self._port is None:
            port = BZR_DEFAULT_PORT
        else:
            port = int(self._port)
        try:
            sockaddrs = socket.getaddrinfo(self._host, port, socket.AF_UNSPEC,
                socket.SOCK_STREAM, 0, 0)
        except socket.gaierror, (err_num, err_msg):
            raise errors.ConnectionError("failed to lookup %s:%d: %s" %
                    (self._host, port, err_msg))
        # Initialize err in case there are no addresses returned:
        err = socket.error("no address found for %s" % self._host)
        for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
            try:
                self._socket = socket.socket(family, socktype, proto)
                self._socket.setsockopt(socket.IPPROTO_TCP,
                                        socket.TCP_NODELAY, 1)
                self._socket.connect(sockaddr)
            except socket.error, err:
                if self._socket is not None:
                    self._socket.close()
                self._socket = None
                continue
            break
        if self._socket is None:
            # socket errors either have a (string) or (errno, string) as their
            # args.
            if type(err.args) is str:
                err_msg = err.args
            else:
                err_msg = err.args[1]
            raise errors.ConnectionError("failed to connect to %s:%d: %s" %
                    (self._host, port, err_msg))
        self._connected = True


class SmartClientAlreadyConnectedSocketMedium(SmartClientSocketMedium):
    """A client medium for an already connected socket.
    
    Note that this class will assume it "owns" the socket, so it will close it
    when its disconnect method is called.
    """

    def __init__(self, base, sock):
        SmartClientSocketMedium.__init__(self, base)
        self._socket = sock
        self._connected = True

    def _ensure_connection(self):
        # Already connected, by definition!  So nothing to do.
        pass


class SmartClientStreamMediumRequest(SmartClientMediumRequest):
    """A SmartClientMediumRequest that works with an SmartClientStreamMedium."""

    def __init__(self, medium):
        SmartClientMediumRequest.__init__(self, medium)
        # check that we are safe concurrency wise. If some streams start
        # allowing concurrent requests - i.e. via multiplexing - then this
        # assert should be moved to SmartClientStreamMedium.get_request,
        # and the setting/unsetting of _current_request likewise moved into
        # that class : but its unneeded overhead for now. RBC 20060922
        if self._medium._current_request is not None:
            raise errors.TooManyConcurrentRequests(self._medium)
        self._medium._current_request = self

    def _accept_bytes(self, bytes):
        """See SmartClientMediumRequest._accept_bytes.

        This forwards to self._medium._accept_bytes because we are operating
        on the mediums stream.
        """
        self._medium._accept_bytes(bytes)

    def _finished_reading(self):
        """See SmartClientMediumRequest._finished_reading.

        This clears the _current_request on self._medium to allow a new
        request to be created.
        """
        if self._medium._current_request is not self:
            raise AssertionError()
        self._medium._current_request = None

    def _finished_writing(self):
        """See SmartClientMediumRequest._finished_writing.

        This invokes self._medium._flush to ensure all bytes are transmitted.
        """
        self._medium._flush()