1
# Copyright (C) 2006 Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
17
"""The 'medium' layer for the smart servers and clients.
 
 
19
"Medium" here is the noun meaning "a means of transmission", not the adjective
 
 
20
for "the quality between big and small."
 
 
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.
 
 
31
from bzrlib import errors
 
 
32
from bzrlib.smart.protocol import (
 
 
34
    SmartServerRequestProtocolOne,
 
 
35
    SmartServerRequestProtocolTwo,
 
 
39
    from bzrlib.transport import ssh
 
 
40
except errors.ParamikoNotPresent:
 
 
41
    # no paramiko.  SmartSSHClientMedium will break.
 
 
45
class SmartServerStreamMedium(object):
 
 
46
    """Handles smart commands coming over a stream.
 
 
48
    The stream may be a pipe connected to sshd, or a tcp socket, or an
 
 
49
    in-process fifo for testing.
 
 
51
    One instance is created for each connected client; it can serve multiple
 
 
52
    requests in the lifetime of the connection.
 
 
54
    The server passes requests through to an underlying backing transport, 
 
 
55
    which will typically be a LocalTransport looking at the server's filesystem.
 
 
58
    def __init__(self, backing_transport):
 
 
59
        """Construct new server.
 
 
61
        :param backing_transport: Transport for the directory served.
 
 
63
        # backing_transport could be passed to serve instead of __init__
 
 
64
        self.backing_transport = backing_transport
 
 
68
        """Serve requests until the client disconnects."""
 
 
69
        # Keep a reference to stderr because the sys module's globals get set to
 
 
70
        # None during interpreter shutdown.
 
 
71
        from sys import stderr
 
 
73
            while not self.finished:
 
 
74
                server_protocol = self._build_protocol()
 
 
75
                self._serve_one_request(server_protocol)
 
 
77
            stderr.write("%s terminating on exception %s\n" % (self, e))
 
 
80
    def _build_protocol(self):
 
 
81
        """Identifies the version of the incoming request, and returns an
 
 
82
        a protocol object that can interpret it.
 
 
84
        If more bytes than the version prefix of the request are read, they will
 
 
85
        be fed into the protocol before it is returned.
 
 
87
        :returns: a SmartServerRequestProtocol.
 
 
89
        # Identify the protocol version.
 
 
90
        bytes = self._get_line()
 
 
91
        if bytes.startswith(REQUEST_VERSION_TWO):
 
 
92
            protocol_class = SmartServerRequestProtocolTwo
 
 
93
            bytes = bytes[len(REQUEST_VERSION_TWO):]
 
 
95
            protocol_class = SmartServerRequestProtocolOne
 
 
96
        protocol = protocol_class(self.backing_transport, self._write_out)
 
 
97
        protocol.accept_bytes(bytes)
 
 
100
    def _serve_one_request(self, protocol):
 
 
101
        """Read one request from input, process, send back a response.
 
 
103
        :param protocol: a SmartServerRequestProtocol.
 
 
106
            self._serve_one_request_unguarded(protocol)
 
 
107
        except KeyboardInterrupt:
 
 
110
            self.terminate_due_to_error()
 
 
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)
 
 
116
    def _get_bytes(self, desired_count):
 
 
117
        """Get some bytes from the medium.
 
 
119
        :param desired_count: number of bytes we want to read.
 
 
121
        raise NotImplementedError(self._get_bytes)
 
 
124
        """Read bytes from this request's response until a newline byte.
 
 
126
        This isn't particularly efficient, so should only be used when the
 
 
127
        expected size of the line is quite short.
 
 
129
        :returns: a string of bytes ending in a newline (byte 0x0A).
 
 
131
        # XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
 
 
133
        while not line or line[-1] != '\n':
 
 
134
            new_char = self._get_bytes(1)
 
 
137
                # Ran out of bytes before receiving a complete line.
 
 
142
class SmartServerSocketStreamMedium(SmartServerStreamMedium):
 
 
144
    def __init__(self, sock, backing_transport):
 
 
147
        :param sock: the socket the server will read from.  It will be put
 
 
150
        SmartServerStreamMedium.__init__(self, backing_transport)
 
 
152
        sock.setblocking(True)
 
 
155
    def _serve_one_request_unguarded(self, protocol):
 
 
156
        while protocol.next_read_size():
 
 
158
                protocol.accept_bytes(self.push_back)
 
 
161
                bytes = self._get_bytes(4096)
 
 
165
                protocol.accept_bytes(bytes)
 
 
167
        self.push_back = protocol.excess_buffer
 
 
169
    def _get_bytes(self, desired_count):
 
 
170
        # We ignore the desired_count because on sockets it's more efficient to
 
 
172
        return self.socket.recv(4096)
 
 
174
    def terminate_due_to_error(self):
 
 
175
        """Called when an unhandled exception from the protocol occurs."""
 
 
176
        # TODO: This should log to a server log file, but no such thing
 
 
177
        # exists yet.  Andrew Bennetts 2006-09-29.
 
 
181
    def _write_out(self, bytes):
 
 
182
        self.socket.sendall(bytes)
 
 
185
class SmartServerPipeStreamMedium(SmartServerStreamMedium):
 
 
187
    def __init__(self, in_file, out_file, backing_transport):
 
 
188
        """Construct new server.
 
 
190
        :param in_file: Python file from which requests can be read.
 
 
191
        :param out_file: Python file to write responses.
 
 
192
        :param backing_transport: Transport for the directory served.
 
 
194
        SmartServerStreamMedium.__init__(self, backing_transport)
 
 
195
        if sys.platform == 'win32':
 
 
196
            # force binary mode for files
 
 
198
            for f in (in_file, out_file):
 
 
199
                fileno = getattr(f, 'fileno', None)
 
 
201
                    msvcrt.setmode(fileno(), os.O_BINARY)
 
 
205
    def _serve_one_request_unguarded(self, protocol):
 
 
207
            bytes_to_read = protocol.next_read_size()
 
 
208
            if bytes_to_read == 0:
 
 
209
                # Finished serving this request.
 
 
212
            bytes = self._get_bytes(bytes_to_read)
 
 
214
                # Connection has been closed.
 
 
218
            protocol.accept_bytes(bytes)
 
 
220
    def _get_bytes(self, desired_count):
 
 
221
        return self._in.read(desired_count)
 
 
223
    def terminate_due_to_error(self):
 
 
224
        # TODO: This should log to a server log file, but no such thing
 
 
225
        # exists yet.  Andrew Bennetts 2006-09-29.
 
 
229
    def _write_out(self, bytes):
 
 
230
        self._out.write(bytes)
 
 
233
class SmartClientMediumRequest(object):
 
 
234
    """A request on a SmartClientMedium.
 
 
236
    Each request allows bytes to be provided to it via accept_bytes, and then
 
 
237
    the response bytes to be read via read_bytes.
 
 
240
    request.accept_bytes('123')
 
 
241
    request.finished_writing()
 
 
242
    result = request.read_bytes(3)
 
 
243
    request.finished_reading()
 
 
245
    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 
 
 
248
    details on concurrency and pipelining.
 
 
251
    def __init__(self, medium):
 
 
252
        """Construct a SmartClientMediumRequest for the medium medium."""
 
 
253
        self._medium = medium
 
 
254
        # we track state by constants - we may want to use the same
 
 
255
        # pattern as BodyReader if it gets more complex.
 
 
256
        # valid states are: "writing", "reading", "done"
 
 
257
        self._state = "writing"
 
 
259
    def accept_bytes(self, bytes):
 
 
260
        """Accept bytes for inclusion in this request.
 
 
262
        This method may not be be called after finished_writing() has been
 
 
263
        called.  It depends upon the Medium whether or not the bytes will be
 
 
264
        immediately transmitted. Message based Mediums will tend to buffer the
 
 
265
        bytes until finished_writing() is called.
 
 
267
        :param bytes: A bytestring.
 
 
269
        if self._state != "writing":
 
 
270
            raise errors.WritingCompleted(self)
 
 
271
        self._accept_bytes(bytes)
 
 
273
    def _accept_bytes(self, bytes):
 
 
274
        """Helper for accept_bytes.
 
 
276
        Accept_bytes checks the state of the request to determing if bytes
 
 
277
        should be accepted. After that it hands off to _accept_bytes to do the
 
 
280
        raise NotImplementedError(self._accept_bytes)
 
 
282
    def finished_reading(self):
 
 
283
        """Inform the request that all desired data has been read.
 
 
285
        This will remove the request from the pipeline for its medium (if the
 
 
286
        medium supports pipelining) and any further calls to methods on the
 
 
287
        request will raise ReadingCompleted.
 
 
289
        if self._state == "writing":
 
 
290
            raise errors.WritingNotComplete(self)
 
 
291
        if self._state != "reading":
 
 
292
            raise errors.ReadingCompleted(self)
 
 
294
        self._finished_reading()
 
 
296
    def _finished_reading(self):
 
 
297
        """Helper for finished_reading.
 
 
299
        finished_reading checks the state of the request to determine if 
 
 
300
        finished_reading is allowed, and if it is hands off to _finished_reading
 
 
301
        to perform the action.
 
 
303
        raise NotImplementedError(self._finished_reading)
 
 
305
    def finished_writing(self):
 
 
306
        """Finish the writing phase of this request.
 
 
308
        This will flush all pending data for this request along the medium.
 
 
309
        After calling finished_writing, you may not call accept_bytes anymore.
 
 
311
        if self._state != "writing":
 
 
312
            raise errors.WritingCompleted(self)
 
 
313
        self._state = "reading"
 
 
314
        self._finished_writing()
 
 
316
    def _finished_writing(self):
 
 
317
        """Helper for finished_writing.
 
 
319
        finished_writing checks the state of the request to determine if 
 
 
320
        finished_writing is allowed, and if it is hands off to _finished_writing
 
 
321
        to perform the action.
 
 
323
        raise NotImplementedError(self._finished_writing)
 
 
325
    def read_bytes(self, count):
 
 
326
        """Read bytes from this requests response.
 
 
328
        This method will block and wait for count bytes to be read. It may not
 
 
329
        be invoked until finished_writing() has been called - this is to ensure
 
 
330
        a message-based approach to requests, for compatibility with message
 
 
331
        based mediums like HTTP.
 
 
333
        if self._state == "writing":
 
 
334
            raise errors.WritingNotComplete(self)
 
 
335
        if self._state != "reading":
 
 
336
            raise errors.ReadingCompleted(self)
 
 
337
        return self._read_bytes(count)
 
 
339
    def _read_bytes(self, count):
 
 
340
        """Helper for read_bytes.
 
 
342
        read_bytes checks the state of the request to determing if bytes
 
 
343
        should be read. After that it hands off to _read_bytes to do the
 
 
346
        raise NotImplementedError(self._read_bytes)
 
 
349
        """Read bytes from this request's response until a newline byte.
 
 
351
        This isn't particularly efficient, so should only be used when the
 
 
352
        expected size of the line is quite short.
 
 
354
        :returns: a string of bytes ending in a newline (byte 0x0A).
 
 
356
        # XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
 
 
358
        while not line or line[-1] != '\n':
 
 
359
            new_char = self.read_bytes(1)
 
 
362
                raise errors.SmartProtocolError(
 
 
363
                    'unexpected end of file reading from server')
 
 
367
class SmartClientMedium(object):
 
 
368
    """Smart client is a medium for sending smart protocol requests over."""
 
 
370
    def disconnect(self):
 
 
371
        """If this medium maintains a persistent connection, close it.
 
 
373
        The default implementation does nothing.
 
 
377
class SmartClientStreamMedium(SmartClientMedium):
 
 
378
    """Stream based medium common class.
 
 
380
    SmartClientStreamMediums operate on a stream. All subclasses use a common
 
 
381
    SmartClientStreamMediumRequest for their requests, and should implement
 
 
382
    _accept_bytes and _read_bytes to allow the request objects to send and
 
 
387
        self._current_request = None
 
 
389
    def accept_bytes(self, bytes):
 
 
390
        self._accept_bytes(bytes)
 
 
393
        """The SmartClientStreamMedium knows how to close the stream when it is
 
 
399
        """Flush the output stream.
 
 
401
        This method is used by the SmartClientStreamMediumRequest to ensure that
 
 
402
        all data for a request is sent, to avoid long timeouts or deadlocks.
 
 
404
        raise NotImplementedError(self._flush)
 
 
406
    def get_request(self):
 
 
407
        """See SmartClientMedium.get_request().
 
 
409
        SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
 
 
412
        return SmartClientStreamMediumRequest(self)
 
 
414
    def read_bytes(self, count):
 
 
415
        return self._read_bytes(count)
 
 
418
class SmartSimplePipesClientMedium(SmartClientStreamMedium):
 
 
419
    """A client medium using simple pipes.
 
 
421
    This client does not manage the pipes: it assumes they will always be open.
 
 
424
    def __init__(self, readable_pipe, writeable_pipe):
 
 
425
        SmartClientStreamMedium.__init__(self)
 
 
426
        self._readable_pipe = readable_pipe
 
 
427
        self._writeable_pipe = writeable_pipe
 
 
429
    def _accept_bytes(self, bytes):
 
 
430
        """See SmartClientStreamMedium.accept_bytes."""
 
 
431
        self._writeable_pipe.write(bytes)
 
 
434
        """See SmartClientStreamMedium._flush()."""
 
 
435
        self._writeable_pipe.flush()
 
 
437
    def _read_bytes(self, count):
 
 
438
        """See SmartClientStreamMedium._read_bytes."""
 
 
439
        return self._readable_pipe.read(count)
 
 
442
class SmartSSHClientMedium(SmartClientStreamMedium):
 
 
443
    """A client medium using SSH."""
 
 
445
    def __init__(self, host, port=None, username=None, password=None,
 
 
447
        """Creates a client that will connect on the first use.
 
 
449
        :param vendor: An optional override for the ssh vendor to use. See
 
 
450
            bzrlib.transport.ssh for details on ssh vendors.
 
 
452
        SmartClientStreamMedium.__init__(self)
 
 
453
        self._connected = False
 
 
455
        self._password = password
 
 
457
        self._username = username
 
 
458
        self._read_from = None
 
 
459
        self._ssh_connection = None
 
 
460
        self._vendor = vendor
 
 
461
        self._write_to = None
 
 
463
    def _accept_bytes(self, bytes):
 
 
464
        """See SmartClientStreamMedium.accept_bytes."""
 
 
465
        self._ensure_connection()
 
 
466
        self._write_to.write(bytes)
 
 
468
    def disconnect(self):
 
 
469
        """See SmartClientMedium.disconnect()."""
 
 
470
        if not self._connected:
 
 
472
        self._read_from.close()
 
 
473
        self._write_to.close()
 
 
474
        self._ssh_connection.close()
 
 
475
        self._connected = False
 
 
477
    def _ensure_connection(self):
 
 
478
        """Connect this medium if not already connected."""
 
 
481
        executable = os.environ.get('BZR_REMOTE_PATH', 'bzr')
 
 
482
        if self._vendor is None:
 
 
483
            vendor = ssh._get_ssh_vendor()
 
 
485
            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=/',
 
 
490
        self._read_from, self._write_to = \
 
 
491
            self._ssh_connection.get_filelike_channels()
 
 
492
        self._connected = True
 
 
495
        """See SmartClientStreamMedium._flush()."""
 
 
496
        self._write_to.flush()
 
 
498
    def _read_bytes(self, count):
 
 
499
        """See SmartClientStreamMedium.read_bytes."""
 
 
500
        if not self._connected:
 
 
501
            raise errors.MediumNotConnected(self)
 
 
502
        return self._read_from.read(count)
 
 
505
class SmartTCPClientMedium(SmartClientStreamMedium):
 
 
506
    """A client medium using TCP."""
 
 
508
    def __init__(self, host, port):
 
 
509
        """Creates a client that will connect on the first use."""
 
 
510
        SmartClientStreamMedium.__init__(self)
 
 
511
        self._connected = False
 
 
516
    def _accept_bytes(self, bytes):
 
 
517
        """See SmartClientMedium.accept_bytes."""
 
 
518
        self._ensure_connection()
 
 
519
        self._socket.sendall(bytes)
 
 
521
    def disconnect(self):
 
 
522
        """See SmartClientMedium.disconnect()."""
 
 
523
        if not self._connected:
 
 
527
        self._connected = False
 
 
529
    def _ensure_connection(self):
 
 
530
        """Connect this medium if not already connected."""
 
 
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)))
 
 
537
            raise errors.ConnectionError("failed to connect to %s:%d: %s" %
 
 
538
                    (self._host, self._port, os.strerror(result)))
 
 
539
        self._connected = True
 
 
542
        """See SmartClientStreamMedium._flush().
 
 
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.
 
 
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)
 
 
555
class SmartClientStreamMediumRequest(SmartClientMediumRequest):
 
 
556
    """A SmartClientMediumRequest that works with an SmartClientStreamMedium."""
 
 
558
    def __init__(self, medium):
 
 
559
        SmartClientMediumRequest.__init__(self, medium)
 
 
560
        # check that we are safe concurrency wise. If some streams start
 
 
561
        # allowing concurrent requests - i.e. via multiplexing - then this
 
 
562
        # assert should be moved to SmartClientStreamMedium.get_request,
 
 
563
        # and the setting/unsetting of _current_request likewise moved into
 
 
564
        # that class : but its unneeded overhead for now. RBC 20060922
 
 
565
        if self._medium._current_request is not None:
 
 
566
            raise errors.TooManyConcurrentRequests(self._medium)
 
 
567
        self._medium._current_request = self
 
 
569
    def _accept_bytes(self, bytes):
 
 
570
        """See SmartClientMediumRequest._accept_bytes.
 
 
572
        This forwards to self._medium._accept_bytes because we are operating
 
 
573
        on the mediums stream.
 
 
575
        self._medium._accept_bytes(bytes)
 
 
577
    def _finished_reading(self):
 
 
578
        """See SmartClientMediumRequest._finished_reading.
 
 
580
        This clears the _current_request on self._medium to allow a new 
 
 
581
        request to be created.
 
 
583
        assert self._medium._current_request is self
 
 
584
        self._medium._current_request = None
 
 
586
    def _finished_writing(self):
 
 
587
        """See SmartClientMediumRequest._finished_writing.
 
 
589
        This invokes self._medium._flush to ensure all bytes are transmitted.
 
 
591
        self._medium._flush()
 
 
593
    def _read_bytes(self, count):
 
 
594
        """See SmartClientMediumRequest._read_bytes.
 
 
596
        This forwards to self._medium._read_bytes because we are operating
 
 
597
        on the mediums stream.
 
 
599
        return self._medium._read_bytes(count)