611de28aca
* Improved peer ID construction and usage * peer id object is directly passed to the network no need to cast from a string to an ID * don't base64 encode the peer id when loading from public key * use proper multiaddr address - keep multiaddr object into peerstore instead of string - update network code to use new multiaddr lib - update tests and example * don't instanciate peerstore object in constructor This has side effect where the same peerstore is used for different instance of Libp2p * add connect method to basic_host * use zaibon's fork of sbuss/py-multiaddr * lint
83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
import asyncio
|
|
from transport.transport_interface import ITransport
|
|
from transport.listener_interface import IListener
|
|
from network.connection.raw_connection import RawConnection
|
|
|
|
class TCP(ITransport):
|
|
|
|
def __init__(self):
|
|
self.listener = self.Listener()
|
|
|
|
class Listener(IListener):
|
|
|
|
def __init__(self, handler_function=None):
|
|
self.multiaddrs = []
|
|
self.server = None
|
|
self.handler = handler_function
|
|
|
|
async def listen(self, multiaddr):
|
|
"""
|
|
put listener in listening mode and wait for incoming connections
|
|
:param multiaddr: multiaddr of peer
|
|
:return: return True if successful
|
|
"""
|
|
_multiaddr = multiaddr
|
|
|
|
_multiaddr = _multiaddr.decapsulate('/ipfs')
|
|
self.multiaddrs.append(_multiaddr)
|
|
|
|
coroutine = asyncio.start_server(self.handler,
|
|
_multiaddr.value_for_protocol('ip4'),
|
|
_multiaddr.value_for_protocol('tcp'))
|
|
self.server = await coroutine
|
|
return True
|
|
|
|
def get_addrs(self):
|
|
"""
|
|
retrieve list of addresses the listener is listening on
|
|
:return: return list of addrs
|
|
"""
|
|
# TODO check if server is listening
|
|
return self.multiaddrs
|
|
|
|
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
|
|
"""
|
|
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
|
|
|
|
async def dial(self, multiaddr, options=None):
|
|
"""
|
|
dial a transport to peer listening on multiaddr
|
|
:param multiaddr: multiaddr of peer
|
|
:param options: optional object
|
|
:return: True if successful
|
|
"""
|
|
host = multiaddr.value_for_protocol('ip4')
|
|
port = int(multiaddr.value_for_protocol('tcp'))
|
|
|
|
reader, writer = await asyncio.open_connection(host, port)
|
|
|
|
return RawConnection(host, port, reader, writer)
|
|
|
|
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 connection is received
|
|
that takes a connection as argument which implements interface-connection
|
|
:return: a listener object that implements listener_interface.py
|
|
"""
|
|
return self.Listener(handler_function)
|