Merge pull request #39 from zixuanzh/fix-dial

restructured raw connection
This commit is contained in:
ZX 2018-11-11 12:28:00 -05:00 committed by GitHub
commit 5ccf6323e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 20 deletions

View File

@ -3,14 +3,20 @@ from .raw_connection_interface import IRawConnection
class RawConnection(IRawConnection): class RawConnection(IRawConnection):
def __init__(self, ip, port): def __init__(self, ip, port, reader, writer):
self.conn_ip = ip self.conn_ip = ip
self.conn_port = port self.conn_port = port
self.reader, self.writer = self.open_connection() self.reader = reader
self.writer = writer
async def open_connection(self): # def __init__(self, ip, port):
""" # self.conn_ip = ip
opens a connection on self.ip and self.port # self.conn_port = port
:return: a raw connection # self.reader, self.writer = self.open_connection()
"""
return await asyncio.open_connection(self.conn_ip, self.conn_port) # 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)

View File

@ -6,10 +6,10 @@ class IRawConnection(ABC):
open_connection should return such a connection open_connection should return such a connection
""" """
@abstractmethod # @abstractmethod
async def open_connection(self): # async def open_connection(self):
""" # """
opens a connection on ip and port # opens a connection on ip and port
:return: a raw connection # :return: a raw connection
""" # """
pass # pass

View File

@ -1,6 +1,7 @@
import asyncio import asyncio
from .transport_interface import ITransport from transport.transport_interface import ITransport
from .listener_interface import IListener from transport.listener_interface import IListener
from transport.connection.raw_connection import RawConnection
class TCP(ITransport): class TCP(ITransport):
@ -67,9 +68,10 @@ class TCP(ITransport):
:return: True if successful :return: True if successful
""" """
_multiaddr_dict = multiaddr.to_dict() _multiaddr_dict = multiaddr.to_dict()
reader, writer = await asyncio.open_connection(_multiaddr_dict.host,\ host = _multiaddr_dict.host
_multiaddr_dict.port) port = _multiaddr_dict.port
return False reader, writer = open_conn(host, port)
return RawConnection(host, port, reader, writer)
# TODO dial behavior not fully understood # TODO dial behavior not fully understood
def create_listener(self, handler_function, options=None): 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: a listener object that implements listener_interface.py
""" """
return self.Listener(handler_function) return self.Listener(handler_function)
async def open_conn(host, port):
reader, writer = await asyncio.open_connection(host, port)
return reader, writer