~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/medium.py

  • Committer: Martin Packman
  • Date: 2011-12-23 19:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 6405.
  • Revision ID: martin.packman@canonical.com-20111223193822-hesheea4o8aqwexv
Accept and document passing the medium rather than transport for smart connections

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""The 'medium' layer for the smart servers and clients.
18
18
 
24
24
bzrlib/transport/smart/__init__.py.
25
25
"""
26
26
 
 
27
import errno
27
28
import os
 
29
import sys
 
30
import time
 
31
import urllib
 
32
 
 
33
import bzrlib
 
34
from bzrlib.lazy_import import lazy_import
 
35
lazy_import(globals(), """
 
36
import select
28
37
import socket
29
 
import sys
 
38
import thread
 
39
import weakref
30
40
 
31
 
from bzrlib import errors
32
 
from bzrlib.smart.protocol import (
33
 
    REQUEST_VERSION_TWO,
34
 
    SmartServerRequestProtocolOne,
35
 
    SmartServerRequestProtocolTwo,
 
41
from bzrlib import (
 
42
    debug,
 
43
    errors,
 
44
    trace,
 
45
    transport,
 
46
    ui,
 
47
    urlutils,
36
48
    )
37
 
 
38
 
try:
39
 
    from bzrlib.transport import ssh
40
 
except errors.ParamikoNotPresent:
41
 
    # no paramiko.  SmartSSHClientMedium will break.
42
 
    pass
43
 
 
44
 
 
45
 
class SmartServerStreamMedium(object):
 
49
from bzrlib.i18n import gettext
 
50
from bzrlib.smart import client, protocol, request, signals, vfs
 
51
from bzrlib.transport import ssh
 
52
""")
 
53
from bzrlib import osutils
 
54
 
 
55
# Throughout this module buffer size parameters are either limited to be at
 
56
# most _MAX_READ_SIZE, or are ignored and _MAX_READ_SIZE is used instead.
 
57
# For this module's purposes, MAX_SOCKET_CHUNK is a reasonable size for reads
 
58
# from non-sockets as well.
 
59
_MAX_READ_SIZE = osutils.MAX_SOCKET_CHUNK
 
60
 
 
61
def _get_protocol_factory_for_bytes(bytes):
 
62
    """Determine the right protocol factory for 'bytes'.
 
63
 
 
64
    This will return an appropriate protocol factory depending on the version
 
65
    of the protocol being used, as determined by inspecting the given bytes.
 
66
    The bytes should have at least one newline byte (i.e. be a whole line),
 
67
    otherwise it's possible that a request will be incorrectly identified as
 
68
    version 1.
 
69
 
 
70
    Typical use would be::
 
71
 
 
72
         factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
 
73
         server_protocol = factory(transport, write_func, root_client_path)
 
74
         server_protocol.accept_bytes(unused_bytes)
 
75
 
 
76
    :param bytes: a str of bytes of the start of the request.
 
77
    :returns: 2-tuple of (protocol_factory, unused_bytes).  protocol_factory is
 
78
        a callable that takes three args: transport, write_func,
 
79
        root_client_path.  unused_bytes are any bytes that were not part of a
 
80
        protocol version marker.
 
81
    """
 
82
    if bytes.startswith(protocol.MESSAGE_VERSION_THREE):
 
83
        protocol_factory = protocol.build_server_protocol_three
 
84
        bytes = bytes[len(protocol.MESSAGE_VERSION_THREE):]
 
85
    elif bytes.startswith(protocol.REQUEST_VERSION_TWO):
 
86
        protocol_factory = protocol.SmartServerRequestProtocolTwo
 
87
        bytes = bytes[len(protocol.REQUEST_VERSION_TWO):]
 
88
    else:
 
89
        protocol_factory = protocol.SmartServerRequestProtocolOne
 
90
    return protocol_factory, bytes
 
91
 
 
92
 
 
93
def _get_line(read_bytes_func):
 
94
    """Read bytes using read_bytes_func until a newline byte.
 
95
 
 
96
    This isn't particularly efficient, so should only be used when the
 
97
    expected size of the line is quite short.
 
98
 
 
99
    :returns: a tuple of two strs: (line, excess)
 
100
    """
 
101
    newline_pos = -1
 
102
    bytes = ''
 
103
    while newline_pos == -1:
 
104
        new_bytes = read_bytes_func(1)
 
105
        bytes += new_bytes
 
106
        if new_bytes == '':
 
107
            # Ran out of bytes before receiving a complete line.
 
108
            return bytes, ''
 
109
        newline_pos = bytes.find('\n')
 
110
    line = bytes[:newline_pos+1]
 
111
    excess = bytes[newline_pos+1:]
 
112
    return line, excess
 
113
 
 
114
 
 
115
class SmartMedium(object):
 
116
    """Base class for smart protocol media, both client- and server-side."""
 
117
 
 
118
    def __init__(self):
 
119
        self._push_back_buffer = None
 
120
 
 
121
    def _push_back(self, bytes):
 
122
        """Return unused bytes to the medium, because they belong to the next
 
123
        request(s).
 
124
 
 
125
        This sets the _push_back_buffer to the given bytes.
 
126
        """
 
127
        if self._push_back_buffer is not None:
 
128
            raise AssertionError(
 
129
                "_push_back called when self._push_back_buffer is %r"
 
130
                % (self._push_back_buffer,))
 
131
        if bytes == '':
 
132
            return
 
133
        self._push_back_buffer = bytes
 
134
 
 
135
    def _get_push_back_buffer(self):
 
136
        if self._push_back_buffer == '':
 
137
            raise AssertionError(
 
138
                '%s._push_back_buffer should never be the empty string, '
 
139
                'which can be confused with EOF' % (self,))
 
140
        bytes = self._push_back_buffer
 
141
        self._push_back_buffer = None
 
142
        return bytes
 
143
 
 
144
    def read_bytes(self, desired_count):
 
145
        """Read some bytes from this medium.
 
146
 
 
147
        :returns: some bytes, possibly more or less than the number requested
 
148
            in 'desired_count' depending on the medium.
 
149
        """
 
150
        if self._push_back_buffer is not None:
 
151
            return self._get_push_back_buffer()
 
152
        bytes_to_read = min(desired_count, _MAX_READ_SIZE)
 
153
        return self._read_bytes(bytes_to_read)
 
154
 
 
155
    def _read_bytes(self, count):
 
156
        raise NotImplementedError(self._read_bytes)
 
157
 
 
158
    def _get_line(self):
 
159
        """Read bytes from this request's response until a newline byte.
 
160
 
 
161
        This isn't particularly efficient, so should only be used when the
 
162
        expected size of the line is quite short.
 
163
 
 
164
        :returns: a string of bytes ending in a newline (byte 0x0A).
 
165
        """
 
166
        line, excess = _get_line(self.read_bytes)
 
167
        self._push_back(excess)
 
168
        return line
 
169
 
 
170
    def _report_activity(self, bytes, direction):
 
171
        """Notify that this medium has activity.
 
172
 
 
173
        Implementations should call this from all methods that actually do IO.
 
174
        Be careful that it's not called twice, if one method is implemented on
 
175
        top of another.
 
176
 
 
177
        :param bytes: Number of bytes read or written.
 
178
        :param direction: 'read' or 'write' or None.
 
179
        """
 
180
        ui.ui_factory.report_transport_activity(self, bytes, direction)
 
181
 
 
182
 
 
183
_bad_file_descriptor = (errno.EBADF,)
 
184
if sys.platform == 'win32':
 
185
    # Given on Windows if you pass a closed socket to select.select. Probably
 
186
    # also given if you pass a file handle to select.
 
187
    WSAENOTSOCK = 10038
 
188
    _bad_file_descriptor += (WSAENOTSOCK,)
 
189
 
 
190
 
 
191
class SmartServerStreamMedium(SmartMedium):
46
192
    """Handles smart commands coming over a stream.
47
193
 
48
194
    The stream may be a pipe connected to sshd, or a tcp socket, or an
51
197
    One instance is created for each connected client; it can serve multiple
52
198
    requests in the lifetime of the connection.
53
199
 
54
 
    The server passes requests through to an underlying backing transport, 
 
200
    The server passes requests through to an underlying backing transport,
55
201
    which will typically be a LocalTransport looking at the server's filesystem.
 
202
 
 
203
    :ivar _push_back_buffer: a str of bytes that have been read from the stream
 
204
        but not used yet, or None if there are no buffered bytes.  Subclasses
 
205
        should make sure to exhaust this buffer before reading more bytes from
 
206
        the stream.  See also the _push_back method.
56
207
    """
57
208
 
58
 
    def __init__(self, backing_transport):
 
209
    _timer = time.time
 
210
 
 
211
    def __init__(self, backing_transport, root_client_path='/', timeout=None):
59
212
        """Construct new server.
60
213
 
61
214
        :param backing_transport: Transport for the directory served.
62
215
        """
63
216
        # backing_transport could be passed to serve instead of __init__
64
217
        self.backing_transport = backing_transport
 
218
        self.root_client_path = root_client_path
65
219
        self.finished = False
 
220
        if timeout is None:
 
221
            raise AssertionError('You must supply a timeout.')
 
222
        self._client_timeout = timeout
 
223
        self._client_poll_timeout = min(timeout / 10.0, 1.0)
 
224
        SmartMedium.__init__(self)
66
225
 
67
226
    def serve(self):
68
227
        """Serve requests until the client disconnects."""
73
232
            while not self.finished:
74
233
                server_protocol = self._build_protocol()
75
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.
 
240
            return
76
241
        except Exception, e:
77
242
            stderr.write("%s terminating on exception %s\n" % (self, e))
78
243
            raise
 
244
        self._disconnect_client()
 
245
 
 
246
    def _stop_gracefully(self):
 
247
        """When we finish this message, stop looking for more."""
 
248
        trace.mutter('Stopping %s' % (self,))
 
249
        self.finished = True
 
250
 
 
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
 
256
 
 
257
    def _wait_for_bytes_with_timeout(self, timeout_seconds):
 
258
        """Wait for more bytes to be read, but timeout if none available.
 
259
 
 
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.
 
263
 
 
264
        :return: Did we timeout? (True if we timed out, False if there is data
 
265
            to be read)
 
266
        """
 
267
        raise NotImplementedError(self._wait_for_bytes_with_timeout)
79
268
 
80
269
    def _build_protocol(self):
81
270
        """Identifies the version of the incoming request, and returns an
86
275
 
87
276
        :returns: a SmartServerRequestProtocol.
88
277
        """
89
 
        # Identify the protocol version.
 
278
        self._wait_for_bytes_with_timeout(self._client_timeout)
 
279
        if self.finished:
 
280
            # We're stopping, so don't try to do any more work
 
281
            return None
90
282
        bytes = self._get_line()
91
 
        if bytes.startswith(REQUEST_VERSION_TWO):
92
 
            protocol_class = SmartServerRequestProtocolTwo
93
 
            bytes = bytes[len(REQUEST_VERSION_TWO):]
94
 
        else:
95
 
            protocol_class = SmartServerRequestProtocolOne
96
 
        protocol = protocol_class(self.backing_transport, self._write_out)
97
 
        protocol.accept_bytes(bytes)
 
283
        protocol_factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
 
284
        protocol = protocol_factory(
 
285
            self.backing_transport, self._write_out, self.root_client_path)
 
286
        protocol.accept_bytes(unused_bytes)
98
287
        return protocol
99
288
 
 
289
    def _wait_on_descriptor(self, fd, timeout_seconds):
 
290
        """select() on a file descriptor, waiting for nonblocking read()
 
291
 
 
292
        This will raise a ConnectionTimeout exception if we do not get a
 
293
        readable handle before timeout_seconds.
 
294
        :return: None
 
295
        """
 
296
        t_end = self._timer() + timeout_seconds
 
297
        poll_timeout = min(timeout_seconds, self._client_poll_timeout)
 
298
        rs = xs = None
 
299
        while not rs and not xs and self._timer() < t_end:
 
300
            if self.finished:
 
301
                return
 
302
            try:
 
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]
 
308
                    err = e.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.
 
313
                    continue
 
314
                raise
 
315
        if rs or xs:
 
316
            return
 
317
        raise errors.ConnectionTimeout('disconnecting client after %.1f seconds'
 
318
                                       % (timeout_seconds,))
 
319
 
100
320
    def _serve_one_request(self, protocol):
101
321
        """Read one request from input, process, send back a response.
102
 
        
 
322
 
103
323
        :param protocol: a SmartServerRequestProtocol.
104
324
        """
 
325
        if protocol is None:
 
326
            return
105
327
        try:
106
328
            self._serve_one_request_unguarded(protocol)
107
329
        except KeyboardInterrupt:
113
335
        """Called when an unhandled exception from the protocol occurs."""
114
336
        raise NotImplementedError(self.terminate_due_to_error)
115
337
 
116
 
    def _get_bytes(self, desired_count):
 
338
    def _read_bytes(self, desired_count):
117
339
        """Get some bytes from the medium.
118
340
 
119
341
        :param desired_count: number of bytes we want to read.
120
342
        """
121
 
        raise NotImplementedError(self._get_bytes)
122
 
 
123
 
    def _get_line(self):
124
 
        """Read bytes from this request's response until a newline byte.
125
 
        
126
 
        This isn't particularly efficient, so should only be used when the
127
 
        expected size of the line is quite short.
128
 
 
129
 
        :returns: a string of bytes ending in a newline (byte 0x0A).
130
 
        """
131
 
        # XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
132
 
        line = ''
133
 
        while not line or line[-1] != '\n':
134
 
            new_char = self._get_bytes(1)
135
 
            line += new_char
136
 
            if new_char == '':
137
 
                # Ran out of bytes before receiving a complete line.
138
 
                break
139
 
        return line
 
343
        raise NotImplementedError(self._read_bytes)
140
344
 
141
345
 
142
346
class SmartServerSocketStreamMedium(SmartServerStreamMedium):
143
347
 
144
 
    def __init__(self, sock, backing_transport):
 
348
    def __init__(self, sock, backing_transport, root_client_path='/',
 
349
                 timeout=None):
145
350
        """Constructor.
146
351
 
147
352
        :param sock: the socket the server will read from.  It will be put
148
353
            into blocking mode.
149
354
        """
150
 
        SmartServerStreamMedium.__init__(self, backing_transport)
151
 
        self.push_back = ''
 
355
        SmartServerStreamMedium.__init__(
 
356
            self, backing_transport, root_client_path=root_client_path,
 
357
            timeout=timeout)
152
358
        sock.setblocking(True)
153
359
        self.socket = sock
 
360
        # Get the getpeername now, as we might be closed later when we care.
 
361
        try:
 
362
            self._client_info = sock.getpeername()
 
363
        except socket.error:
 
364
            self._client_info = '<unknown>'
 
365
 
 
366
    def __str__(self):
 
367
        return '%s(client=%s)' % (self.__class__.__name__, self._client_info)
 
368
 
 
369
    def __repr__(self):
 
370
        return '%s.%s(client=%s)' % (self.__module__, self.__class__.__name__,
 
371
            self._client_info)
154
372
 
155
373
    def _serve_one_request_unguarded(self, protocol):
156
374
        while protocol.next_read_size():
157
 
            if self.push_back:
158
 
                protocol.accept_bytes(self.push_back)
159
 
                self.push_back = ''
160
 
            else:
161
 
                bytes = self._get_bytes(4096)
162
 
                if bytes == '':
163
 
                    self.finished = True
164
 
                    return
165
 
                protocol.accept_bytes(bytes)
166
 
        
167
 
        self.push_back = protocol.excess_buffer
168
 
 
169
 
    def _get_bytes(self, desired_count):
170
 
        # We ignore the desired_count because on sockets it's more efficient to
171
 
        # read 4k at a time.
172
 
        return self.socket.recv(4096)
173
 
    
 
375
            # We can safely try to read large chunks.  If there is less data
 
376
            # than MAX_SOCKET_CHUNK ready, the socket will just return a
 
377
            # short read immediately rather than block.
 
378
            bytes = self.read_bytes(osutils.MAX_SOCKET_CHUNK)
 
379
            if bytes == '':
 
380
                self.finished = True
 
381
                return
 
382
            protocol.accept_bytes(bytes)
 
383
 
 
384
        self._push_back(protocol.unused_data)
 
385
 
 
386
    def _disconnect_client(self):
 
387
        """Close the current connection. We stopped due to a timeout/etc."""
 
388
        self.socket.close()
 
389
 
 
390
    def _wait_for_bytes_with_timeout(self, timeout_seconds):
 
391
        """Wait for more bytes to be read, but timeout if none available.
 
392
 
 
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.
 
396
 
 
397
        :return: None, this will raise ConnectionTimeout if we time out before
 
398
            data is available.
 
399
        """
 
400
        return self._wait_on_descriptor(self.socket, timeout_seconds)
 
401
 
 
402
    def _read_bytes(self, desired_count):
 
403
        return osutils.read_bytes_from_socket(
 
404
            self.socket, self._report_activity)
 
405
 
174
406
    def terminate_due_to_error(self):
175
 
        """Called when an unhandled exception from the protocol occurs."""
176
407
        # TODO: This should log to a server log file, but no such thing
177
408
        # exists yet.  Andrew Bennetts 2006-09-29.
178
409
        self.socket.close()
179
410
        self.finished = True
180
411
 
181
412
    def _write_out(self, bytes):
182
 
        self.socket.sendall(bytes)
 
413
        tstart = osutils.timer_func()
 
414
        osutils.send_all(self.socket, bytes, self._report_activity)
 
415
        if 'hpss' in debug.debug_flags:
 
416
            thread_id = thread.get_ident()
 
417
            trace.mutter('%12s: [%s] %d bytes to the socket in %.3fs'
 
418
                         % ('wrote', thread_id, len(bytes),
 
419
                            osutils.timer_func() - tstart))
183
420
 
184
421
 
185
422
class SmartServerPipeStreamMedium(SmartServerStreamMedium):
186
423
 
187
 
    def __init__(self, in_file, out_file, backing_transport):
 
424
    def __init__(self, in_file, out_file, backing_transport, timeout=None):
188
425
        """Construct new server.
189
426
 
190
427
        :param in_file: Python file from which requests can be read.
191
428
        :param out_file: Python file to write responses.
192
429
        :param backing_transport: Transport for the directory served.
193
430
        """
194
 
        SmartServerStreamMedium.__init__(self, backing_transport)
 
431
        SmartServerStreamMedium.__init__(self, backing_transport,
 
432
            timeout=timeout)
195
433
        if sys.platform == 'win32':
196
434
            # force binary mode for files
197
435
            import msvcrt
202
440
        self._in = in_file
203
441
        self._out = out_file
204
442
 
 
443
    def serve(self):
 
444
        """See SmartServerStreamMedium.serve"""
 
445
        # This is the regular serve, except it adds signal trapping for soft
 
446
        # shutdown.
 
447
        stop_gracefully = self._stop_gracefully
 
448
        signals.register_on_hangup(id(self), stop_gracefully)
 
449
        try:
 
450
            return super(SmartServerPipeStreamMedium, self).serve()
 
451
        finally:
 
452
            signals.unregister_on_hangup(id(self))
 
453
 
205
454
    def _serve_one_request_unguarded(self, protocol):
206
455
        while True:
 
456
            # We need to be careful not to read past the end of the current
 
457
            # request, or else the read from the pipe will block, so we use
 
458
            # protocol.next_read_size().
207
459
            bytes_to_read = protocol.next_read_size()
208
460
            if bytes_to_read == 0:
209
461
                # Finished serving this request.
210
462
                self._out.flush()
211
463
                return
212
 
            bytes = self._get_bytes(bytes_to_read)
 
464
            bytes = self.read_bytes(bytes_to_read)
213
465
            if bytes == '':
214
466
                # Connection has been closed.
215
467
                self.finished = True
217
469
                return
218
470
            protocol.accept_bytes(bytes)
219
471
 
220
 
    def _get_bytes(self, desired_count):
 
472
    def _disconnect_client(self):
 
473
        self._in.close()
 
474
        self._out.flush()
 
475
        self._out.close()
 
476
 
 
477
    def _wait_for_bytes_with_timeout(self, timeout_seconds):
 
478
        """Wait for more bytes to be read, but timeout if none available.
 
479
 
 
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.
 
483
 
 
484
        :return: None, this will raise ConnectionTimeout if we time out before
 
485
            data is available.
 
486
        """
 
487
        if (getattr(self._in, 'fileno', None) is None
 
488
            or sys.platform == 'win32'):
 
489
            # You can't select() file descriptors on Windows.
 
490
            return
 
491
        return self._wait_on_descriptor(self._in, timeout_seconds)
 
492
 
 
493
    def _read_bytes(self, desired_count):
221
494
        return self._in.read(desired_count)
222
495
 
223
496
    def terminate_due_to_error(self):
243
516
    request.finished_reading()
244
517
 
245
518
    It is up to the individual SmartClientMedium whether multiple concurrent
246
 
    requests can exist. See SmartClientMedium.get_request to obtain instances 
247
 
    of SmartClientMediumRequest, and the concrete Medium you are using for 
 
519
    requests can exist. See SmartClientMedium.get_request to obtain instances
 
520
    of SmartClientMediumRequest, and the concrete Medium you are using for
248
521
    details on concurrency and pipelining.
249
522
    """
250
523
 
259
532
    def accept_bytes(self, bytes):
260
533
        """Accept bytes for inclusion in this request.
261
534
 
262
 
        This method may not be be called after finished_writing() has been
 
535
        This method may not be called after finished_writing() has been
263
536
        called.  It depends upon the Medium whether or not the bytes will be
264
537
        immediately transmitted. Message based Mediums will tend to buffer the
265
538
        bytes until finished_writing() is called.
296
569
    def _finished_reading(self):
297
570
        """Helper for finished_reading.
298
571
 
299
 
        finished_reading checks the state of the request to determine if 
 
572
        finished_reading checks the state of the request to determine if
300
573
        finished_reading is allowed, and if it is hands off to _finished_reading
301
574
        to perform the action.
302
575
        """
316
589
    def _finished_writing(self):
317
590
        """Helper for finished_writing.
318
591
 
319
 
        finished_writing checks the state of the request to determine if 
 
592
        finished_writing checks the state of the request to determine if
320
593
        finished_writing is allowed, and if it is hands off to _finished_writing
321
594
        to perform the action.
322
595
        """
337
610
        return self._read_bytes(count)
338
611
 
339
612
    def _read_bytes(self, count):
340
 
        """Helper for read_bytes.
 
613
        """Helper for SmartClientMediumRequest.read_bytes.
341
614
 
342
615
        read_bytes checks the state of the request to determing if bytes
343
616
        should be read. After that it hands off to _read_bytes to do the
344
617
        actual read.
 
618
 
 
619
        By default this forwards to self._medium.read_bytes because we are
 
620
        operating on the medium's stream.
345
621
        """
346
 
        raise NotImplementedError(self._read_bytes)
 
622
        return self._medium.read_bytes(count)
347
623
 
348
624
    def read_line(self):
349
 
        """Read bytes from this request's response until a newline byte.
350
 
        
351
 
        This isn't particularly efficient, so should only be used when the
352
 
        expected size of the line is quite short.
353
 
 
354
 
        :returns: a string of bytes ending in a newline (byte 0x0A).
355
 
        """
356
 
        # XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
357
 
        line = ''
358
 
        while not line or line[-1] != '\n':
359
 
            new_char = self.read_bytes(1)
360
 
            line += new_char
361
 
            if new_char == '':
362
 
                raise errors.SmartProtocolError(
363
 
                    'unexpected end of file reading from server')
 
625
        line = self._read_line()
 
626
        if not line.endswith('\n'):
 
627
            # end of file encountered reading from server
 
628
            raise errors.ConnectionReset(
 
629
                "Unexpected end of message. Please check connectivity "
 
630
                "and permissions, and report a bug if problems persist.")
364
631
        return line
365
632
 
366
 
 
367
 
class SmartClientMedium(object):
 
633
    def _read_line(self):
 
634
        """Helper for SmartClientMediumRequest.read_line.
 
635
 
 
636
        By default this forwards to self._medium._get_line because we are
 
637
        operating on the medium's stream.
 
638
        """
 
639
        return self._medium._get_line()
 
640
 
 
641
 
 
642
class _VfsRefuser(object):
 
643
    """An object that refuses all VFS requests.
 
644
 
 
645
    """
 
646
 
 
647
    def __init__(self):
 
648
        client._SmartClient.hooks.install_named_hook(
 
649
            'call', self.check_vfs, 'vfs refuser')
 
650
 
 
651
    def check_vfs(self, params):
 
652
        try:
 
653
            request_method = request.request_handlers.get(params.method)
 
654
        except KeyError:
 
655
            # A method we don't know about doesn't count as a VFS method.
 
656
            return
 
657
        if issubclass(request_method, vfs.VfsRequest):
 
658
            raise errors.HpssVfsRequestNotAllowed(params.method, params.args)
 
659
 
 
660
 
 
661
class _DebugCounter(object):
 
662
    """An object that counts the HPSS calls made to each client medium.
 
663
 
 
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.
 
667
    """
 
668
 
 
669
    def __init__(self):
 
670
        self.counts = weakref.WeakKeyDictionary()
 
671
        client._SmartClient.hooks.install_named_hook(
 
672
            'call', self.increment_call_count, 'hpss call counter')
 
673
        bzrlib.global_state.cleanups.add_cleanup(self.flush_all)
 
674
 
 
675
    def track(self, medium):
 
676
        """Start tracking calls made to a medium.
 
677
 
 
678
        This only keeps a weakref to the medium, so shouldn't affect the
 
679
        medium's lifetime.
 
680
        """
 
681
        medium_repr = repr(medium)
 
682
        # Add this medium to the WeakKeyDictionary
 
683
        self.counts[medium] = dict(count=0, vfs_count=0,
 
684
                                   medium_repr=medium_repr)
 
685
        # Weakref callbacks are fired in reverse order of their association
 
686
        # with the referenced object.  So we add a weakref *after* adding to
 
687
        # the WeakKeyDict so that we can report the value from it before the
 
688
        # entry is removed by the WeakKeyDict's own callback.
 
689
        ref = weakref.ref(medium, self.done)
 
690
 
 
691
    def increment_call_count(self, params):
 
692
        # Increment the count in the WeakKeyDictionary
 
693
        value = self.counts[params.medium]
 
694
        value['count'] += 1
 
695
        try:
 
696
            request_method = request.request_handlers.get(params.method)
 
697
        except KeyError:
 
698
            # A method we don't know about doesn't count as a VFS method.
 
699
            return
 
700
        if issubclass(request_method, vfs.VfsRequest):
 
701
            value['vfs_count'] += 1
 
702
 
 
703
    def done(self, ref):
 
704
        value = self.counts[ref]
 
705
        count, vfs_count, medium_repr = (
 
706
            value['count'], value['vfs_count'], value['medium_repr'])
 
707
        # In case this callback is invoked for the same ref twice (by the
 
708
        # weakref callback and by the atexit function), set the call count back
 
709
        # to 0 so this item won't be reported twice.
 
710
        value['count'] = 0
 
711
        value['vfs_count'] = 0
 
712
        if count != 0:
 
713
            trace.note(gettext('HPSS calls: {0} ({1} vfs) {2}').format(
 
714
                       count, vfs_count, medium_repr))
 
715
 
 
716
    def flush_all(self):
 
717
        for ref in list(self.counts.keys()):
 
718
            self.done(ref)
 
719
 
 
720
_debug_counter = None
 
721
_vfs_refuser = None
 
722
 
 
723
 
 
724
class SmartClientMedium(SmartMedium):
368
725
    """Smart client is a medium for sending smart protocol requests over."""
369
726
 
 
727
    def __init__(self, base):
 
728
        super(SmartClientMedium, self).__init__()
 
729
        self.base = base
 
730
        self._protocol_version_error = None
 
731
        self._protocol_version = None
 
732
        self._done_hello = False
 
733
        # Be optimistic: we assume the remote end can accept new remote
 
734
        # requests until we get an error saying otherwise.
 
735
        # _remote_version_is_before tracks the bzr version the remote side
 
736
        # can be based on what we've seen so far.
 
737
        self._remote_version_is_before = None
 
738
        # Install debug hook function if debug flag is set.
 
739
        if 'hpss' in debug.debug_flags:
 
740
            global _debug_counter
 
741
            if _debug_counter is None:
 
742
                _debug_counter = _DebugCounter()
 
743
            _debug_counter.track(self)
 
744
        if 'hpss_client_no_vfs' in debug.debug_flags:
 
745
            global _vfs_refuser
 
746
            if _vfs_refuser is None:
 
747
                _vfs_refuser = _VfsRefuser()
 
748
 
 
749
    def _is_remote_before(self, version_tuple):
 
750
        """Is it possible the remote side supports RPCs for a given version?
 
751
 
 
752
        Typical use::
 
753
 
 
754
            needed_version = (1, 2)
 
755
            if medium._is_remote_before(needed_version):
 
756
                fallback_to_pre_1_2_rpc()
 
757
            else:
 
758
                try:
 
759
                    do_1_2_rpc()
 
760
                except UnknownSmartMethod:
 
761
                    medium._remember_remote_is_before(needed_version)
 
762
                    fallback_to_pre_1_2_rpc()
 
763
 
 
764
        :seealso: _remember_remote_is_before
 
765
        """
 
766
        if self._remote_version_is_before is None:
 
767
            # So far, the remote side seems to support everything
 
768
            return False
 
769
        return version_tuple >= self._remote_version_is_before
 
770
 
 
771
    def _remember_remote_is_before(self, version_tuple):
 
772
        """Tell this medium that the remote side is older the given version.
 
773
 
 
774
        :seealso: _is_remote_before
 
775
        """
 
776
        if (self._remote_version_is_before is not None and
 
777
            version_tuple > self._remote_version_is_before):
 
778
            # We have been told that the remote side is older than some version
 
779
            # which is newer than a previously supplied older-than version.
 
780
            # This indicates that some smart verb call is not guarded
 
781
            # appropriately (it should simply not have been tried).
 
782
            trace.mutter(
 
783
                "_remember_remote_is_before(%r) called, but "
 
784
                "_remember_remote_is_before(%r) was called previously."
 
785
                , version_tuple, self._remote_version_is_before)
 
786
            if 'hpss' in debug.debug_flags:
 
787
                ui.ui_factory.show_warning(
 
788
                    "_remember_remote_is_before(%r) called, but "
 
789
                    "_remember_remote_is_before(%r) was called previously."
 
790
                    % (version_tuple, self._remote_version_is_before))
 
791
            return
 
792
        self._remote_version_is_before = version_tuple
 
793
 
 
794
    def protocol_version(self):
 
795
        """Find out if 'hello' smart request works."""
 
796
        if self._protocol_version_error is not None:
 
797
            raise self._protocol_version_error
 
798
        if not self._done_hello:
 
799
            try:
 
800
                medium_request = self.get_request()
 
801
                # Send a 'hello' request in protocol version one, for maximum
 
802
                # backwards compatibility.
 
803
                client_protocol = protocol.SmartClientRequestProtocolOne(medium_request)
 
804
                client_protocol.query_version()
 
805
                self._done_hello = True
 
806
            except errors.SmartProtocolError, e:
 
807
                # Cache the error, just like we would cache a successful
 
808
                # result.
 
809
                self._protocol_version_error = e
 
810
                raise
 
811
        return '2'
 
812
 
 
813
    def should_probe(self):
 
814
        """Should RemoteBzrDirFormat.probe_transport send a smart request on
 
815
        this medium?
 
816
 
 
817
        Some transports are unambiguously smart-only; there's no need to check
 
818
        if the transport is able to carry smart requests, because that's all
 
819
        it is for.  In those cases, this method should return False.
 
820
 
 
821
        But some HTTP transports can sometimes fail to carry smart requests,
 
822
        but still be usuable for accessing remote bzrdirs via plain file
 
823
        accesses.  So for those transports, their media should return True here
 
824
        so that RemoteBzrDirFormat can determine if it is appropriate for that
 
825
        transport.
 
826
        """
 
827
        return False
 
828
 
370
829
    def disconnect(self):
371
830
        """If this medium maintains a persistent connection, close it.
372
 
        
 
831
 
373
832
        The default implementation does nothing.
374
833
        """
375
 
        
 
834
 
 
835
    def remote_path_from_transport(self, transport):
 
836
        """Convert transport into a path suitable for using in a request.
 
837
 
 
838
        Note that the resulting remote path doesn't encode the host name or
 
839
        anything but path, so it is only safe to use it in requests sent over
 
840
        the medium from the matching transport.
 
841
        """
 
842
        medium_base = urlutils.join(self.base, '/')
 
843
        rel_url = urlutils.relative_url(medium_base, transport.base)
 
844
        return urllib.unquote(rel_url)
 
845
 
376
846
 
377
847
class SmartClientStreamMedium(SmartClientMedium):
378
848
    """Stream based medium common class.
383
853
    receive bytes.
384
854
    """
385
855
 
386
 
    def __init__(self):
 
856
    def __init__(self, base):
 
857
        SmartClientMedium.__init__(self, base)
387
858
        self._current_request = None
388
859
 
389
860
    def accept_bytes(self, bytes):
397
868
 
398
869
    def _flush(self):
399
870
        """Flush the output stream.
400
 
        
 
871
 
401
872
        This method is used by the SmartClientStreamMediumRequest to ensure that
402
873
        all data for a request is sent, to avoid long timeouts or deadlocks.
403
874
        """
411
882
        """
412
883
        return SmartClientStreamMediumRequest(self)
413
884
 
414
 
    def read_bytes(self, count):
415
 
        return self._read_bytes(count)
 
885
    def reset(self):
 
886
        """We have been disconnected, reset current state.
 
887
 
 
888
        This resets things like _current_request and connected state.
 
889
        """
 
890
        self.disconnect()
 
891
        self._current_request = None
416
892
 
417
893
 
418
894
class SmartSimplePipesClientMedium(SmartClientStreamMedium):
419
895
    """A client medium using simple pipes.
420
 
    
 
896
 
421
897
    This client does not manage the pipes: it assumes they will always be open.
422
898
    """
423
899
 
424
 
    def __init__(self, readable_pipe, writeable_pipe):
425
 
        SmartClientStreamMedium.__init__(self)
 
900
    def __init__(self, readable_pipe, writeable_pipe, base):
 
901
        SmartClientStreamMedium.__init__(self, base)
426
902
        self._readable_pipe = readable_pipe
427
903
        self._writeable_pipe = writeable_pipe
428
904
 
429
905
    def _accept_bytes(self, bytes):
430
906
        """See SmartClientStreamMedium.accept_bytes."""
431
 
        self._writeable_pipe.write(bytes)
 
907
        try:
 
908
            self._writeable_pipe.write(bytes)
 
909
        except IOError, e:
 
910
            if e.errno in (errno.EINVAL, errno.EPIPE):
 
911
                raise errors.ConnectionReset(
 
912
                    "Error trying to write to subprocess:\n%s" % (e,))
 
913
            raise
 
914
        self._report_activity(len(bytes), 'write')
432
915
 
433
916
    def _flush(self):
434
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.
435
921
        self._writeable_pipe.flush()
436
922
 
437
923
    def _read_bytes(self, count):
438
924
        """See SmartClientStreamMedium._read_bytes."""
439
 
        return self._readable_pipe.read(count)
 
925
        bytes_to_read = min(count, _MAX_READ_SIZE)
 
926
        bytes = self._readable_pipe.read(bytes_to_read)
 
927
        self._report_activity(len(bytes), 'read')
 
928
        return bytes
 
929
 
 
930
 
 
931
class SSHParams(object):
 
932
    """A set of parameters for starting a remote bzr via SSH."""
 
933
 
 
934
    def __init__(self, host, port=None, username=None, password=None,
 
935
            bzr_remote_path='bzr'):
 
936
        self.host = host
 
937
        self.port = port
 
938
        self.username = username
 
939
        self.password = password
 
940
        self.bzr_remote_path = bzr_remote_path
440
941
 
441
942
 
442
943
class SmartSSHClientMedium(SmartClientStreamMedium):
443
 
    """A client medium using SSH."""
444
 
    
445
 
    def __init__(self, host, port=None, username=None, password=None,
446
 
            vendor=None):
 
944
    """A client medium using SSH.
 
945
 
 
946
    It delegates IO to a SmartSimplePipesClientMedium or
 
947
    SmartClientAlreadyConnectedSocketMedium (depending on platform).
 
948
    """
 
949
 
 
950
    def __init__(self, base, ssh_params, vendor=None):
447
951
        """Creates a client that will connect on the first use.
448
 
        
 
952
 
 
953
        :param ssh_params: A SSHParams instance.
449
954
        :param vendor: An optional override for the ssh vendor to use. See
450
955
            bzrlib.transport.ssh for details on ssh vendors.
451
956
        """
452
 
        SmartClientStreamMedium.__init__(self)
453
 
        self._connected = False
454
 
        self._host = host
455
 
        self._password = password
456
 
        self._port = port
457
 
        self._username = username
458
 
        self._read_from = None
 
957
        self._real_medium = None
 
958
        self._ssh_params = ssh_params
 
959
        # for the benefit of progress making a short description of this
 
960
        # transport
 
961
        self._scheme = 'bzr+ssh'
 
962
        # SmartClientStreamMedium stores the repr of this object in its
 
963
        # _DebugCounter so we have to store all the values used in our repr
 
964
        # method before calling the super init.
 
965
        SmartClientStreamMedium.__init__(self, base)
 
966
        self._vendor = vendor
459
967
        self._ssh_connection = None
460
 
        self._vendor = vendor
461
 
        self._write_to = None
 
968
 
 
969
    def __repr__(self):
 
970
        if self._ssh_params.port is None:
 
971
            maybe_port = ''
 
972
        else:
 
973
            maybe_port = ':%s' % self._ssh_params.port
 
974
        if self._ssh_params.username is None:
 
975
            maybe_user = ''
 
976
        else:
 
977
            maybe_user = '%s@' % self._ssh_params.username
 
978
        return "%s(%s://%s%s%s/)" % (
 
979
            self.__class__.__name__,
 
980
            self._scheme,
 
981
            maybe_user,
 
982
            self._ssh_params.host,
 
983
            maybe_port)
462
984
 
463
985
    def _accept_bytes(self, bytes):
464
986
        """See SmartClientStreamMedium.accept_bytes."""
465
987
        self._ensure_connection()
466
 
        self._write_to.write(bytes)
 
988
        self._real_medium.accept_bytes(bytes)
467
989
 
468
990
    def disconnect(self):
469
991
        """See SmartClientMedium.disconnect()."""
470
 
        if not self._connected:
471
 
            return
472
 
        self._read_from.close()
473
 
        self._write_to.close()
474
 
        self._ssh_connection.close()
475
 
        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
476
998
 
477
999
    def _ensure_connection(self):
478
1000
        """Connect this medium if not already connected."""
479
 
        if self._connected:
 
1001
        if self._real_medium is not None:
480
1002
            return
481
 
        executable = os.environ.get('BZR_REMOTE_PATH', 'bzr')
482
1003
        if self._vendor is None:
483
1004
            vendor = ssh._get_ssh_vendor()
484
1005
        else:
485
1006
            vendor = self._vendor
486
 
        self._ssh_connection = vendor.connect_ssh(self._username,
487
 
                self._password, self._host, self._port,
488
 
                command=[executable, 'serve', '--inet', '--directory=/',
489
 
                         '--allow-writes'])
490
 
        self._read_from, self._write_to = \
491
 
            self._ssh_connection.get_filelike_channels()
492
 
        self._connected = True
 
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',
 
1011
                         '--directory=/', '--allow-writes'])
 
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)
 
1020
        else:
 
1021
            raise AssertionError(
 
1022
                "Unexpected io_kind %r from %r"
 
1023
                % (io_kind, self._ssh_connection))
 
1024
        for hook in transport.Transport.hooks["post_connect"]:
 
1025
            hook(self)
493
1026
 
494
1027
    def _flush(self):
495
1028
        """See SmartClientStreamMedium._flush()."""
496
 
        self._write_to.flush()
 
1029
        self._real_medium._flush()
497
1030
 
498
1031
    def _read_bytes(self, count):
499
1032
        """See SmartClientStreamMedium.read_bytes."""
500
 
        if not self._connected:
 
1033
        if self._real_medium is None:
501
1034
            raise errors.MediumNotConnected(self)
502
 
        return self._read_from.read(count)
503
 
 
504
 
 
505
 
class SmartTCPClientMedium(SmartClientStreamMedium):
506
 
    """A client medium using TCP."""
 
1035
        return self._real_medium.read_bytes(count)
 
1036
 
 
1037
 
 
1038
# Port 4155 is the default port for bzr://, registered with IANA.
 
1039
BZR_DEFAULT_INTERFACE = None
 
1040
BZR_DEFAULT_PORT = 4155
 
1041
 
 
1042
 
 
1043
class SmartClientSocketMedium(SmartClientStreamMedium):
 
1044
    """A client medium using a socket.
507
1045
    
508
 
    def __init__(self, host, port):
509
 
        """Creates a client that will connect on the first use."""
510
 
        SmartClientStreamMedium.__init__(self)
 
1046
    This class isn't usable directly.  Use one of its subclasses instead.
 
1047
    """
 
1048
 
 
1049
    def __init__(self, base):
 
1050
        SmartClientStreamMedium.__init__(self, base)
 
1051
        self._socket = None
511
1052
        self._connected = False
512
 
        self._host = host
513
 
        self._port = port
514
 
        self._socket = None
515
1053
 
516
1054
    def _accept_bytes(self, bytes):
517
1055
        """See SmartClientMedium.accept_bytes."""
518
1056
        self._ensure_connection()
519
 
        self._socket.sendall(bytes)
 
1057
        osutils.send_all(self._socket, bytes, self._report_activity)
 
1058
 
 
1059
    def _ensure_connection(self):
 
1060
        """Connect this medium if not already connected."""
 
1061
        raise NotImplementedError(self._ensure_connection)
 
1062
 
 
1063
    def _flush(self):
 
1064
        """See SmartClientStreamMedium._flush().
 
1065
 
 
1066
        For sockets we do no flushing. For TCP sockets we may want to turn off
 
1067
        TCP_NODELAY and add a means to do a flush, but that can be done in the
 
1068
        future.
 
1069
        """
 
1070
 
 
1071
    def _read_bytes(self, count):
 
1072
        """See SmartClientMedium.read_bytes."""
 
1073
        if not self._connected:
 
1074
            raise errors.MediumNotConnected(self)
 
1075
        return osutils.read_bytes_from_socket(
 
1076
            self._socket, self._report_activity)
520
1077
 
521
1078
    def disconnect(self):
522
1079
        """See SmartClientMedium.disconnect()."""
526
1083
        self._socket = None
527
1084
        self._connected = False
528
1085
 
 
1086
 
 
1087
class SmartTCPClientMedium(SmartClientSocketMedium):
 
1088
    """A client medium that creates a TCP connection."""
 
1089
 
 
1090
    def __init__(self, host, port, base):
 
1091
        """Creates a client that will connect on the first use."""
 
1092
        SmartClientSocketMedium.__init__(self, base)
 
1093
        self._host = host
 
1094
        self._port = port
 
1095
 
529
1096
    def _ensure_connection(self):
530
1097
        """Connect this medium if not already connected."""
531
1098
        if self._connected:
532
1099
            return
533
 
        self._socket = socket.socket()
534
 
        self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
535
 
        result = self._socket.connect_ex((self._host, int(self._port)))
536
 
        if result:
 
1100
        if self._port is None:
 
1101
            port = BZR_DEFAULT_PORT
 
1102
        else:
 
1103
            port = int(self._port)
 
1104
        try:
 
1105
            sockaddrs = socket.getaddrinfo(self._host, port, socket.AF_UNSPEC,
 
1106
                socket.SOCK_STREAM, 0, 0)
 
1107
        except socket.gaierror, (err_num, err_msg):
 
1108
            raise errors.ConnectionError("failed to lookup %s:%d: %s" %
 
1109
                    (self._host, port, err_msg))
 
1110
        # Initialize err in case there are no addresses returned:
 
1111
        err = socket.error("no address found for %s" % self._host)
 
1112
        for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
 
1113
            try:
 
1114
                self._socket = socket.socket(family, socktype, proto)
 
1115
                self._socket.setsockopt(socket.IPPROTO_TCP,
 
1116
                                        socket.TCP_NODELAY, 1)
 
1117
                self._socket.connect(sockaddr)
 
1118
            except socket.error, err:
 
1119
                if self._socket is not None:
 
1120
                    self._socket.close()
 
1121
                self._socket = None
 
1122
                continue
 
1123
            break
 
1124
        if self._socket is None:
 
1125
            # socket errors either have a (string) or (errno, string) as their
 
1126
            # args.
 
1127
            if type(err.args) is str:
 
1128
                err_msg = err.args
 
1129
            else:
 
1130
                err_msg = err.args[1]
537
1131
            raise errors.ConnectionError("failed to connect to %s:%d: %s" %
538
 
                    (self._host, self._port, os.strerror(result)))
539
 
        self._connected = True
540
 
 
541
 
    def _flush(self):
542
 
        """See SmartClientStreamMedium._flush().
543
 
        
544
 
        For TCP we do no flushing. We may want to turn off TCP_NODELAY and 
545
 
        add a means to do a flush, but that can be done in the future.
546
 
        """
547
 
 
548
 
    def _read_bytes(self, count):
549
 
        """See SmartClientMedium.read_bytes."""
550
 
        if not self._connected:
551
 
            raise errors.MediumNotConnected(self)
552
 
        return self._socket.recv(count)
 
1132
                    (self._host, port, err_msg))
 
1133
        self._connected = True
 
1134
        for hook in transport.Transport.hooks["post_connect"]:
 
1135
            hook(self)
 
1136
 
 
1137
 
 
1138
class SmartClientAlreadyConnectedSocketMedium(SmartClientSocketMedium):
 
1139
    """A client medium for an already connected socket.
 
1140
    
 
1141
    Note that this class will assume it "owns" the socket, so it will close it
 
1142
    when its disconnect method is called.
 
1143
    """
 
1144
 
 
1145
    def __init__(self, base, sock):
 
1146
        SmartClientSocketMedium.__init__(self, base)
 
1147
        self._socket = sock
 
1148
        self._connected = True
 
1149
 
 
1150
    def _ensure_connection(self):
 
1151
        # Already connected, by definition!  So nothing to do.
 
1152
        pass
553
1153
 
554
1154
 
555
1155
class SmartClientStreamMediumRequest(SmartClientMediumRequest):
568
1168
 
569
1169
    def _accept_bytes(self, bytes):
570
1170
        """See SmartClientMediumRequest._accept_bytes.
571
 
        
 
1171
 
572
1172
        This forwards to self._medium._accept_bytes because we are operating
573
1173
        on the mediums stream.
574
1174
        """
577
1177
    def _finished_reading(self):
578
1178
        """See SmartClientMediumRequest._finished_reading.
579
1179
 
580
 
        This clears the _current_request on self._medium to allow a new 
 
1180
        This clears the _current_request on self._medium to allow a new
581
1181
        request to be created.
582
1182
        """
583
 
        assert self._medium._current_request is self
 
1183
        if self._medium._current_request is not self:
 
1184
            raise AssertionError()
584
1185
        self._medium._current_request = None
585
 
        
 
1186
 
586
1187
    def _finished_writing(self):
587
1188
        """See SmartClientMediumRequest._finished_writing.
588
1189
 
589
1190
        This invokes self._medium._flush to ensure all bytes are transmitted.
590
1191
        """
591
1192
        self._medium._flush()
592
 
 
593
 
    def _read_bytes(self, count):
594
 
        """See SmartClientMediumRequest._read_bytes.
595
 
        
596
 
        This forwards to self._medium._read_bytes because we are operating
597
 
        on the mediums stream.
598
 
        """
599
 
        return self._medium._read_bytes(count)
600