Example #1
0
class Connection(object):
    """Represents a connection to a server from a client."""

    def __init__(self, loop, sock, addr, protocol, client):
        """Create a client connection."""
        self.loop = loop
        self.sock = sock
        self.addr = addr
        self.protocol = protocol
        self.client = client
        logger.debug("making transport")
        self.transport = SocketTransport(self.loop, self.sock, self.protocol.data, self.closed)
        logger.debug("protocol.make_connection")
        self.protocol.make_connection(self.transport, self.addr)
        logger.debug("transport.start()")
        self.transport.start()
        logger.debug("transport started")

    def closed(self, reason):
        """Callback performed when the transport is closed."""
        self.client.remove_connection(self)
        self.protocol.connection_lost(reason)
        if not isinstance(reason, ConnectionClosed):
            logger.warn("connection closed, reason {}".format(reason))
        else:
            logger.info("connection closed")

    def close(self):
        """Close the connection."""
        self.transport.close()
Example #2
0
class Connection(object):
    """A connection to the server from a remote client."""

    def __init__(self, loop, sock, address, protocol, server):
        """Create a server connection."""
        self.loop = loop
        self.sock = sock
        self.address = address
        self.protocol = protocol
        self.server = server
        self.transport = SocketTransport(self.loop, self.sock, self.protocol.data, self.closed)

    def make_connection(self):
        self.transport.start()
        self.protocol.make_connection(self.transport, self.address)

    def closed(self, reason):
        """Callback performed when the transport is closed."""
        self.server.remove_connection(self)
        self.protocol.connection_lost(reason)
        if not isinstance(reason, ConnectionClosed):
            logger.warn("connection closed, reason: %s" % str(reason))
        else:
            logger.info("connection closed")

    def close(self):
        """Close the connection."""
        self.transport.close()
Example #3
0
 def test_write(self):
     msg = b"hello"
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     t.write(msg)
     loop.start(pyev.EVRUN_NOWAIT)
     rmsg = self.csock.recv(len(msg))
     self.assertEqual(rmsg, msg)
Example #4
0
 def test_buffered_write(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     count = 0
     msg = b"hello"
     while t.write != t.buffered_write:
         count += 1
         t.write(msg)
     t.write(msg)
     self.csock.recv(count * len(msg))
     loop.start(pyev.EVRUN_NOWAIT)
     self.assertTrue(t.write == t.unbuffered_write)
Example #5
0
 def __init__(self, loop, sock, address, protocol, server):
     """Create a server connection."""
     self.loop = loop
     self.sock = sock
     self.address = address
     self.protocol = protocol
     self.server = server
     self.transport = SocketTransport(self.loop, self.sock, self.protocol.data, self.closed)
Example #6
0
 def test_close_buffered_write(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     count = 0
     msg = b"hello"
     while t.write != t.buffered_write:
         count += 1
         t.write(msg)
     t.write(msg)
     t.close()
     self.assertRaises(ConnectionClosed, t.write, msg)
Example #7
0
 def __init__(self, loop, sock, addr, protocol, client):
     """Create a client connection."""
     self.loop = loop
     self.sock = sock
     self.addr = addr
     self.protocol = protocol
     self.client = client
     logger.debug("making transport")
     self.transport = SocketTransport(self.loop, self.sock, self.protocol.data, self.closed)
     logger.debug("protocol.make_connection")
     self.protocol.make_connection(self.transport, self.addr)
     logger.debug("transport.start()")
     self.transport.start()
     logger.debug("transport started")
Example #8
0
 def test_stop(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     t.stop()
Example #9
0
 def test_closed_stop(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     t.close()
     self.assertRaises(ConnectionClosed, t.stop)
Example #10
0
 def test_closed_write(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     t.close()
     self.assertRaises(ConnectionClosed, t.write, ("hello"))
Example #11
0
 def test_close(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     t.close()
     self.assertTrue(t.closed)
     self.assertTrue(isinstance(self.reason, ConnectionClosed))
Example #12
0
 def test_error(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     self.csock.close()
     t.write(b"hello")
     loop.start(pyev.EVRUN_NOWAIT)
     self.assertTrue(self.reason is not None)
Example #13
0
 def test_read(self):
     t = SocketTransport(loop, self.ssock, self.read, self.close)
     t.start()
     self.csock.send(b"hello")
     loop.start(pyev.EVRUN_NOWAIT)
     self.assertEqual(self.data, b"hello")