2018-10-27 06:06:56 +08:00
|
|
|
import asyncio
|
|
|
|
from .transport_interface import ITransport
|
|
|
|
from .listener_interface import IListener
|
|
|
|
|
|
|
|
class TCP(ITransport):
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-11-11 22:56:44 +08:00
|
|
|
self.listener = self.Listener()
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
class Listener(IListener):
|
|
|
|
|
2018-11-11 22:56:44 +08:00
|
|
|
def __init__(self, handler_function=None):
|
|
|
|
self.multiaddrs = []
|
|
|
|
self.server = None
|
|
|
|
self.handler = staticmethod(handler_function)
|
|
|
|
|
2018-10-27 06:06:56 +08:00
|
|
|
def listen(self, multiaddr):
|
|
|
|
"""
|
|
|
|
put listener in listening mode and wait for incoming connections
|
|
|
|
:param multiaddr: multiaddr of peer
|
|
|
|
:return: return True if successful
|
|
|
|
"""
|
2018-11-11 22:56:44 +08:00
|
|
|
# TODO check for exceptions
|
|
|
|
_multiaddr = multiaddr
|
|
|
|
if "ipfs" in multiaddr.get_protocols():
|
|
|
|
# ipfs_id = multiaddr.get_ipfs_id()
|
|
|
|
_multiaddr = multiaddr.remove_protocol("ipfs")
|
|
|
|
|
|
|
|
self.multiaddrs.append(_multiaddr)
|
|
|
|
_multiaddr_dict = _multiaddr.to_dict()
|
|
|
|
_loop = asyncio.get_event_loop()
|
|
|
|
_coroutine = asyncio.start_server(self.handler, _multiaddr_dict.host,\
|
|
|
|
_multiaddr_dict.port, loop=_loop)
|
|
|
|
self.server = _loop.run_until_complete(_coroutine)
|
|
|
|
return True
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
def get_addrs(self):
|
|
|
|
"""
|
|
|
|
retrieve list of addresses the listener is listening on
|
|
|
|
:return: return list of addrs
|
|
|
|
"""
|
2018-11-11 22:56:44 +08:00
|
|
|
# TODO check if server is listening
|
|
|
|
return self.multiaddrs
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
def close(self, options=None):
|
|
|
|
"""
|
|
|
|
close the listener such that no more connections
|
|
|
|
can be open on this transport instance
|
|
|
|
:param options: optional object potential with timeout
|
|
|
|
a timeout value in ms that fires and destroy all connections
|
|
|
|
:return: return True if successful
|
|
|
|
"""
|
2018-11-11 22:56:44 +08:00
|
|
|
if self.server is None:
|
|
|
|
return False
|
|
|
|
self.server.close()
|
|
|
|
_loop = asyncio.get_event_loop()
|
|
|
|
_loop.run_until_complete(self.server.wait_closed())
|
|
|
|
_loop.close()
|
|
|
|
self.server = None
|
|
|
|
return True
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
def dial(self, multiaddr, options=None):
|
|
|
|
"""
|
|
|
|
dial a transport to peer listening on multiaddr
|
|
|
|
:param multiaddr: multiaddr of peer
|
|
|
|
:param options: optional object
|
2018-11-11 22:56:44 +08:00
|
|
|
:return: True if successful
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
2018-11-11 22:56:44 +08:00
|
|
|
_multiaddr_dict = multiaddr.to_dict()
|
|
|
|
reader, writer = await asyncio.open_connection(_multiaddr_dict.host,\
|
|
|
|
_multiaddr_dict.port)
|
|
|
|
return False
|
|
|
|
# TODO dial behavior not fully understood
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
def create_listener(self, handler_function, options=None):
|
|
|
|
"""
|
|
|
|
create listener on transport
|
|
|
|
:param options: optional object with properties the listener must have
|
|
|
|
:param handler_function: a function called when a new conntion is received
|
|
|
|
that takes a connection as argument which implements interface-connection
|
|
|
|
:return: a listener object that implements listener_interface.py
|
|
|
|
"""
|
2018-11-11 22:56:44 +08:00
|
|
|
return self.Listener(handler_function)
|