~bzr-pqm/bzr/bzr.dev

2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
1
# Copyright (C) 2006 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
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
16
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
17
"""The 'medium' layer for the smart servers and clients.
18
19
"Medium" here is the noun meaning "a means of transmission", not the adjective
20
for "the quality between big and small."
21
22
Media carry the bytes of the requests somehow (e.g. via TCP, wrapped in HTTP, or
23
over SSH), and pass them to and from the protocol logic.  See the overview in
24
bzrlib/transport/smart/__init__.py.
25
"""
26
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
27
import os
28
import socket
2018.5.162 by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import.
29
import sys
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
30
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
31
from bzrlib import (
32
    errors,
33
    symbol_versioning,
34
    )
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
35
from bzrlib.smart.protocol import (
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
36
    REQUEST_VERSION_TWO,
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
37
    SmartServerRequestProtocolOne,
38
    SmartServerRequestProtocolTwo,
39
    )
3066.2.1 by John Arbash Meinel
We don't require paramiko for bzr+ssh.
40
from bzrlib.transport import ssh
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
41
2018.5.17 by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler.
42
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
43
class SmartServerStreamMedium(object):
44
    """Handles smart commands coming over a stream.
45
46
    The stream may be a pipe connected to sshd, or a tcp socket, or an
47
    in-process fifo for testing.
48
49
    One instance is created for each connected client; it can serve multiple
50
    requests in the lifetime of the connection.
51
52
    The server passes requests through to an underlying backing transport, 
53
    which will typically be a LocalTransport looking at the server's filesystem.
54
    """
55
56
    def __init__(self, backing_transport):
57
        """Construct new server.
58
59
        :param backing_transport: Transport for the directory served.
60
        """
61
        # backing_transport could be passed to serve instead of __init__
62
        self.backing_transport = backing_transport
63
        self.finished = False
64
65
    def serve(self):
66
        """Serve requests until the client disconnects."""
67
        # Keep a reference to stderr because the sys module's globals get set to
68
        # None during interpreter shutdown.
69
        from sys import stderr
70
        try:
71
            while not self.finished:
2432.2.3 by Andrew Bennetts
Merge from bzr.dev.
72
                server_protocol = self._build_protocol()
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
73
                self._serve_one_request(server_protocol)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
74
        except Exception, e:
75
            stderr.write("%s terminating on exception %s\n" % (self, e))
76
            raise
77
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
78
    def _build_protocol(self):
2432.2.8 by Andrew Bennetts
NEWS entry, greatly improved docstring in bzrlib.smart.
79
        """Identifies the version of the incoming request, and returns an
80
        a protocol object that can interpret it.
81
82
        If more bytes than the version prefix of the request are read, they will
83
        be fed into the protocol before it is returned.
84
85
        :returns: a SmartServerRequestProtocol.
86
        """
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
87
        # Identify the protocol version.
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
88
        bytes = self._get_line()
89
        if bytes.startswith(REQUEST_VERSION_TWO):
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
90
            protocol_class = SmartServerRequestProtocolTwo
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
91
            bytes = bytes[len(REQUEST_VERSION_TWO):]
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
92
        else:
93
            protocol_class = SmartServerRequestProtocolOne
94
        protocol = protocol_class(self.backing_transport, self._write_out)
95
        protocol.accept_bytes(bytes)
96
        return protocol
97
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
98
    def _serve_one_request(self, protocol):
99
        """Read one request from input, process, send back a response.
100
        
101
        :param protocol: a SmartServerRequestProtocol.
102
        """
103
        try:
104
            self._serve_one_request_unguarded(protocol)
105
        except KeyboardInterrupt:
106
            raise
107
        except Exception, e:
108
            self.terminate_due_to_error()
109
110
    def terminate_due_to_error(self):
111
        """Called when an unhandled exception from the protocol occurs."""
112
        raise NotImplementedError(self.terminate_due_to_error)
113
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
114
    def _get_bytes(self, desired_count):
115
        """Get some bytes from the medium.
116
117
        :param desired_count: number of bytes we want to read.
118
        """
119
        raise NotImplementedError(self._get_bytes)
120
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
121
    def _get_line(self):
122
        """Read bytes from this request's response until a newline byte.
123
        
124
        This isn't particularly efficient, so should only be used when the
125
        expected size of the line is quite short.
126
127
        :returns: a string of bytes ending in a newline (byte 0x0A).
128
        """
129
        # XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
130
        line = ''
131
        while not line or line[-1] != '\n':
132
            new_char = self._get_bytes(1)
133
            line += new_char
134
            if new_char == '':
135
                # Ran out of bytes before receiving a complete line.
136
                break
137
        return line
138
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
139
140
class SmartServerSocketStreamMedium(SmartServerStreamMedium):
141
142
    def __init__(self, sock, backing_transport):
143
        """Constructor.
144
145
        :param sock: the socket the server will read from.  It will be put
146
            into blocking mode.
147
        """
148
        SmartServerStreamMedium.__init__(self, backing_transport)
149
        self.push_back = ''
150
        sock.setblocking(True)
151
        self.socket = sock
152
153
    def _serve_one_request_unguarded(self, protocol):
154
        while protocol.next_read_size():
155
            if self.push_back:
156
                protocol.accept_bytes(self.push_back)
157
                self.push_back = ''
158
            else:
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
159
                bytes = self._get_bytes(4096)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
160
                if bytes == '':
161
                    self.finished = True
162
                    return
163
                protocol.accept_bytes(bytes)
164
        
165
        self.push_back = protocol.excess_buffer
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
166
167
    def _get_bytes(self, desired_count):
168
        # We ignore the desired_count because on sockets it's more efficient to
169
        # read 4k at a time.
170
        return self.socket.recv(4096)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
171
    
172
    def terminate_due_to_error(self):
173
        """Called when an unhandled exception from the protocol occurs."""
174
        # TODO: This should log to a server log file, but no such thing
175
        # exists yet.  Andrew Bennetts 2006-09-29.
176
        self.socket.close()
177
        self.finished = True
178
179
    def _write_out(self, bytes):
180
        self.socket.sendall(bytes)
181
182
183
class SmartServerPipeStreamMedium(SmartServerStreamMedium):
184
185
    def __init__(self, in_file, out_file, backing_transport):
186
        """Construct new server.
187
188
        :param in_file: Python file from which requests can be read.
189
        :param out_file: Python file to write responses.
190
        :param backing_transport: Transport for the directory served.
191
        """
192
        SmartServerStreamMedium.__init__(self, backing_transport)
2018.5.161 by Andrew Bennetts
Reinstate forcing binary mode on windows in SmartServerStreamMedium.
193
        if sys.platform == 'win32':
194
            # force binary mode for files
195
            import msvcrt
196
            for f in (in_file, out_file):
197
                fileno = getattr(f, 'fileno', None)
198
                if fileno:
199
                    msvcrt.setmode(fileno(), os.O_BINARY)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
200
        self._in = in_file
201
        self._out = out_file
202
203
    def _serve_one_request_unguarded(self, protocol):
204
        while True:
205
            bytes_to_read = protocol.next_read_size()
206
            if bytes_to_read == 0:
207
                # Finished serving this request.
208
                self._out.flush()
209
                return
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
210
            bytes = self._get_bytes(bytes_to_read)
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
211
            if bytes == '':
212
                # Connection has been closed.
213
                self.finished = True
214
                self._out.flush()
215
                return
216
            protocol.accept_bytes(bytes)
217
2432.2.2 by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly.
218
    def _get_bytes(self, desired_count):
219
        return self._in.read(desired_count)
220
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
221
    def terminate_due_to_error(self):
222
        # TODO: This should log to a server log file, but no such thing
223
        # exists yet.  Andrew Bennetts 2006-09-29.
224
        self._out.close()
225
        self.finished = True
226
227
    def _write_out(self, bytes):
228
        self._out.write(bytes)
229
230
231
class SmartClientMediumRequest(object):
232
    """A request on a SmartClientMedium.
233
234
    Each request allows bytes to be provided to it via accept_bytes, and then
235
    the response bytes to be read via read_bytes.
236
237
    For instance:
238
    request.accept_bytes('123')
239
    request.finished_writing()
240
    result = request.read_bytes(3)
241
    request.finished_reading()
242
243
    It is up to the individual SmartClientMedium whether multiple concurrent
244
    requests can exist. See SmartClientMedium.get_request to obtain instances 
245
    of SmartClientMediumRequest, and the concrete Medium you are using for 
246
    details on concurrency and pipelining.
247
    """
248
249
    def __init__(self, medium):
250
        """Construct a SmartClientMediumRequest for the medium medium."""
251
        self._medium = medium
252
        # we track state by constants - we may want to use the same
253
        # pattern as BodyReader if it gets more complex.
254
        # valid states are: "writing", "reading", "done"
255
        self._state = "writing"
256
257
    def accept_bytes(self, bytes):
258
        """Accept bytes for inclusion in this request.
259
260
        This method may not be be called after finished_writing() has been
261
        called.  It depends upon the Medium whether or not the bytes will be
262
        immediately transmitted. Message based Mediums will tend to buffer the
263
        bytes until finished_writing() is called.
264
265
        :param bytes: A bytestring.
266
        """
267
        if self._state != "writing":
268
            raise errors.WritingCompleted(self)
269
        self._accept_bytes(bytes)
270
271
    def _accept_bytes(self, bytes):
272
        """Helper for accept_bytes.
273
274
        Accept_bytes checks the state of the request to determing if bytes
275
        should be accepted. After that it hands off to _accept_bytes to do the
276
        actual acceptance.
277
        """
278
        raise NotImplementedError(self._accept_bytes)
279
280
    def finished_reading(self):
281
        """Inform the request that all desired data has been read.
282
283
        This will remove the request from the pipeline for its medium (if the
284
        medium supports pipelining) and any further calls to methods on the
285
        request will raise ReadingCompleted.
286
        """
287
        if self._state == "writing":
288
            raise errors.WritingNotComplete(self)
289
        if self._state != "reading":
290
            raise errors.ReadingCompleted(self)
291
        self._state = "done"
292
        self._finished_reading()
293
294
    def _finished_reading(self):
295
        """Helper for finished_reading.
296
297
        finished_reading checks the state of the request to determine if 
298
        finished_reading is allowed, and if it is hands off to _finished_reading
299
        to perform the action.
300
        """
301
        raise NotImplementedError(self._finished_reading)
302
303
    def finished_writing(self):
304
        """Finish the writing phase of this request.
305
306
        This will flush all pending data for this request along the medium.
307
        After calling finished_writing, you may not call accept_bytes anymore.
308
        """
309
        if self._state != "writing":
310
            raise errors.WritingCompleted(self)
311
        self._state = "reading"
312
        self._finished_writing()
313
314
    def _finished_writing(self):
315
        """Helper for finished_writing.
316
317
        finished_writing checks the state of the request to determine if 
318
        finished_writing is allowed, and if it is hands off to _finished_writing
319
        to perform the action.
320
        """
321
        raise NotImplementedError(self._finished_writing)
322
323
    def read_bytes(self, count):
324
        """Read bytes from this requests response.
325
326
        This method will block and wait for count bytes to be read. It may not
327
        be invoked until finished_writing() has been called - this is to ensure
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
328
        a message-based approach to requests, for compatibility with message
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
329
        based mediums like HTTP.
330
        """
331
        if self._state == "writing":
332
            raise errors.WritingNotComplete(self)
333
        if self._state != "reading":
334
            raise errors.ReadingCompleted(self)
335
        return self._read_bytes(count)
336
337
    def _read_bytes(self, count):
338
        """Helper for read_bytes.
339
340
        read_bytes checks the state of the request to determing if bytes
341
        should be read. After that it hands off to _read_bytes to do the
342
        actual read.
343
        """
344
        raise NotImplementedError(self._read_bytes)
345
2432.2.7 by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them.
346
    def read_line(self):
347
        """Read bytes from this request's response until a newline byte.
348
        
349
        This isn't particularly efficient, so should only be used when the
350
        expected size of the line is quite short.
351
352
        :returns: a string of bytes ending in a newline (byte 0x0A).
353
        """
354
        # XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
355
        line = ''
356
        while not line or line[-1] != '\n':
357
            new_char = self.read_bytes(1)
358
            line += new_char
359
            if new_char == '':
360
                raise errors.SmartProtocolError(
361
                    'unexpected end of file reading from server')
362
        return line
363
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
364
365
class SmartClientMedium(object):
366
    """Smart client is a medium for sending smart protocol requests over."""
367
368
    def disconnect(self):
369
        """If this medium maintains a persistent connection, close it.
370
        
371
        The default implementation does nothing.
372
        """
373
        
374
375
class SmartClientStreamMedium(SmartClientMedium):
376
    """Stream based medium common class.
377
378
    SmartClientStreamMediums operate on a stream. All subclasses use a common
379
    SmartClientStreamMediumRequest for their requests, and should implement
380
    _accept_bytes and _read_bytes to allow the request objects to send and
381
    receive bytes.
382
    """
383
384
    def __init__(self):
385
        self._current_request = None
386
387
    def accept_bytes(self, bytes):
388
        self._accept_bytes(bytes)
389
390
    def __del__(self):
391
        """The SmartClientStreamMedium knows how to close the stream when it is
392
        finished with it.
393
        """
394
        self.disconnect()
395
396
    def _flush(self):
397
        """Flush the output stream.
398
        
399
        This method is used by the SmartClientStreamMediumRequest to ensure that
400
        all data for a request is sent, to avoid long timeouts or deadlocks.
401
        """
402
        raise NotImplementedError(self._flush)
403
404
    def get_request(self):
405
        """See SmartClientMedium.get_request().
406
407
        SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
408
        for get_request.
409
        """
410
        return SmartClientStreamMediumRequest(self)
411
412
    def read_bytes(self, count):
413
        return self._read_bytes(count)
414
415
416
class SmartSimplePipesClientMedium(SmartClientStreamMedium):
417
    """A client medium using simple pipes.
418
    
419
    This client does not manage the pipes: it assumes they will always be open.
420
    """
421
422
    def __init__(self, readable_pipe, writeable_pipe):
423
        SmartClientStreamMedium.__init__(self)
424
        self._readable_pipe = readable_pipe
425
        self._writeable_pipe = writeable_pipe
426
427
    def _accept_bytes(self, bytes):
428
        """See SmartClientStreamMedium.accept_bytes."""
429
        self._writeable_pipe.write(bytes)
430
431
    def _flush(self):
432
        """See SmartClientStreamMedium._flush()."""
433
        self._writeable_pipe.flush()
434
435
    def _read_bytes(self, count):
436
        """See SmartClientStreamMedium._read_bytes."""
437
        return self._readable_pipe.read(count)
438
439
440
class SmartSSHClientMedium(SmartClientStreamMedium):
441
    """A client medium using SSH."""
442
    
443
    def __init__(self, host, port=None, username=None, password=None,
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
444
            vendor=None, bzr_remote_path=None):
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
445
        """Creates a client that will connect on the first use.
446
        
447
        :param vendor: An optional override for the ssh vendor to use. See
448
            bzrlib.transport.ssh for details on ssh vendors.
449
        """
450
        SmartClientStreamMedium.__init__(self)
451
        self._connected = False
452
        self._host = host
453
        self._password = password
454
        self._port = port
455
        self._username = username
456
        self._read_from = None
457
        self._ssh_connection = None
458
        self._vendor = vendor
459
        self._write_to = None
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
460
        self._bzr_remote_path = bzr_remote_path
461
        if self._bzr_remote_path is None:
462
            symbol_versioning.warn(
463
                'bzr_remote_path is required as of bzr 0.92',
464
                DeprecationWarning, stacklevel=2)
465
            self._bzr_remote_path = os.environ.get('BZR_REMOTE_PATH', 'bzr')
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
466
467
    def _accept_bytes(self, bytes):
468
        """See SmartClientStreamMedium.accept_bytes."""
469
        self._ensure_connection()
470
        self._write_to.write(bytes)
471
472
    def disconnect(self):
473
        """See SmartClientMedium.disconnect()."""
474
        if not self._connected:
475
            return
476
        self._read_from.close()
477
        self._write_to.close()
478
        self._ssh_connection.close()
479
        self._connected = False
480
481
    def _ensure_connection(self):
482
        """Connect this medium if not already connected."""
483
        if self._connected:
484
            return
485
        if self._vendor is None:
486
            vendor = ssh._get_ssh_vendor()
487
        else:
488
            vendor = self._vendor
489
        self._ssh_connection = vendor.connect_ssh(self._username,
490
                self._password, self._host, self._port,
1551.18.17 by Aaron Bentley
Introduce bzr_remote_path configuration variable
491
                command=[self._bzr_remote_path, 'serve', '--inet',
492
                         '--directory=/', '--allow-writes'])
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
493
        self._read_from, self._write_to = \
494
            self._ssh_connection.get_filelike_channels()
495
        self._connected = True
496
497
    def _flush(self):
498
        """See SmartClientStreamMedium._flush()."""
499
        self._write_to.flush()
500
501
    def _read_bytes(self, count):
502
        """See SmartClientStreamMedium.read_bytes."""
503
        if not self._connected:
504
            raise errors.MediumNotConnected(self)
505
        return self._read_from.read(count)
506
507
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
508
# Port 4155 is the default port for bzr://, registered with IANA.
509
BZR_DEFAULT_INTERFACE = '0.0.0.0'
510
BZR_DEFAULT_PORT = 4155
511
512
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
513
class SmartTCPClientMedium(SmartClientStreamMedium):
514
    """A client medium using TCP."""
515
    
516
    def __init__(self, host, port):
517
        """Creates a client that will connect on the first use."""
518
        SmartClientStreamMedium.__init__(self)
519
        self._connected = False
520
        self._host = host
521
        self._port = port
522
        self._socket = None
523
524
    def _accept_bytes(self, bytes):
525
        """See SmartClientMedium.accept_bytes."""
526
        self._ensure_connection()
527
        self._socket.sendall(bytes)
528
529
    def disconnect(self):
530
        """See SmartClientMedium.disconnect()."""
531
        if not self._connected:
532
            return
533
        self._socket.close()
534
        self._socket = None
535
        self._connected = False
536
537
    def _ensure_connection(self):
538
        """Connect this medium if not already connected."""
539
        if self._connected:
540
            return
541
        self._socket = socket.socket()
542
        self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
543
        if self._port is None:
544
            port = BZR_DEFAULT_PORT
545
        else:
546
            port = int(self._port)
547
        result = self._socket.connect_ex((self._host, port))
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
548
        if result:
549
            raise errors.ConnectionError("failed to connect to %s:%d: %s" %
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
550
                    (self._host, port, os.strerror(result)))
2018.5.2 by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package.
551
        self._connected = True
552
553
    def _flush(self):
554
        """See SmartClientStreamMedium._flush().
555
        
556
        For TCP we do no flushing. We may want to turn off TCP_NODELAY and 
557
        add a means to do a flush, but that can be done in the future.
558
        """
559
560
    def _read_bytes(self, count):
561
        """See SmartClientMedium.read_bytes."""
562
        if not self._connected:
563
            raise errors.MediumNotConnected(self)
564
        return self._socket.recv(count)
565
566
567
class SmartClientStreamMediumRequest(SmartClientMediumRequest):
568
    """A SmartClientMediumRequest that works with an SmartClientStreamMedium."""
569
570
    def __init__(self, medium):
571
        SmartClientMediumRequest.__init__(self, medium)
572
        # check that we are safe concurrency wise. If some streams start
573
        # allowing concurrent requests - i.e. via multiplexing - then this
574
        # assert should be moved to SmartClientStreamMedium.get_request,
575
        # and the setting/unsetting of _current_request likewise moved into
576
        # that class : but its unneeded overhead for now. RBC 20060922
577
        if self._medium._current_request is not None:
578
            raise errors.TooManyConcurrentRequests(self._medium)
579
        self._medium._current_request = self
580
581
    def _accept_bytes(self, bytes):
582
        """See SmartClientMediumRequest._accept_bytes.
583
        
584
        This forwards to self._medium._accept_bytes because we are operating
585
        on the mediums stream.
586
        """
587
        self._medium._accept_bytes(bytes)
588
589
    def _finished_reading(self):
590
        """See SmartClientMediumRequest._finished_reading.
591
592
        This clears the _current_request on self._medium to allow a new 
593
        request to be created.
594
        """
595
        assert self._medium._current_request is self
596
        self._medium._current_request = None
597
        
598
    def _finished_writing(self):
599
        """See SmartClientMediumRequest._finished_writing.
600
601
        This invokes self._medium._flush to ensure all bytes are transmitted.
602
        """
603
        self._medium._flush()
604
605
    def _read_bytes(self, count):
606
        """See SmartClientMediumRequest._read_bytes.
607
        
608
        This forwards to self._medium._read_bytes because we are operating
609
        on the mediums stream.
610
        """
611
        return self._medium._read_bytes(count)
612