/brz/remove-bazaar

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