From a785876c168d5003b6246073c826d6b9207d9c3e Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sun, 11 Nov 2018 12:17:12 -0500 Subject: [PATCH] restructured raw connection --- transport/connection/raw_connection.py | 22 ++++++++++++------- .../connection/raw_connection_interface.py | 14 ++++++------ transport/tcp/tcp.py | 16 +++++++++----- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/transport/connection/raw_connection.py b/transport/connection/raw_connection.py index e93e19e..efc3706 100644 --- a/transport/connection/raw_connection.py +++ b/transport/connection/raw_connection.py @@ -3,14 +3,20 @@ from .raw_connection_interface import IRawConnection class RawConnection(IRawConnection): - def __init__(self, ip, port): + def __init__(self, ip, port, reader, writer): self.conn_ip = ip self.conn_port = port - self.reader, self.writer = self.open_connection() + self.reader = reader + self.writer = writer - async def open_connection(self): - """ - opens a connection on self.ip and self.port - :return: a raw connection - """ - return await asyncio.open_connection(self.conn_ip, self.conn_port) + # def __init__(self, ip, port): + # self.conn_ip = ip + # self.conn_port = port + # self.reader, self.writer = self.open_connection() + + # async def open_connection(self): + # """ + # opens a connection on self.ip and self.port + # :return: a raw connection + # """ + # return await asyncio.open_connection(self.conn_ip, self.conn_port) diff --git a/transport/connection/raw_connection_interface.py b/transport/connection/raw_connection_interface.py index 8c07a95..2684355 100644 --- a/transport/connection/raw_connection_interface.py +++ b/transport/connection/raw_connection_interface.py @@ -6,10 +6,10 @@ class IRawConnection(ABC): open_connection should return such a connection """ - @abstractmethod - async def open_connection(self): - """ - opens a connection on ip and port - :return: a raw connection - """ - pass + # @abstractmethod + # async def open_connection(self): + # """ + # opens a connection on ip and port + # :return: a raw connection + # """ + # pass diff --git a/transport/tcp/tcp.py b/transport/tcp/tcp.py index 6ea962d..7981ede 100644 --- a/transport/tcp/tcp.py +++ b/transport/tcp/tcp.py @@ -1,6 +1,7 @@ import asyncio -from .transport_interface import ITransport -from .listener_interface import IListener +from transport.transport_interface import ITransport +from transport.listener_interface import IListener +from transport.connection.raw_connection import RawConnection class TCP(ITransport): @@ -67,9 +68,10 @@ class TCP(ITransport): :return: True if successful """ _multiaddr_dict = multiaddr.to_dict() - reader, writer = await asyncio.open_connection(_multiaddr_dict.host,\ - _multiaddr_dict.port) - return False + host = _multiaddr_dict.host + port = _multiaddr_dict.port + reader, writer = open_conn(host, port) + return RawConnection(host, port, reader, writer) # TODO dial behavior not fully understood def create_listener(self, handler_function, options=None): @@ -81,3 +83,7 @@ class TCP(ITransport): :return: a listener object that implements listener_interface.py """ return self.Listener(handler_function) + +async def open_conn(host, port): + reader, writer = await asyncio.open_connection(host, port) + return reader, writer