2018-10-27 06:06:56 +08:00
|
|
|
import asyncio
|
2018-12-08 04:00:46 +08:00
|
|
|
|
|
|
|
import multiaddr
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
from libp2p.network.connection.raw_connection import RawConnection
|
2019-02-25 09:58:23 +08:00
|
|
|
from libp2p.peer.id import id_b58_encode
|
2019-01-10 02:38:56 +08:00
|
|
|
|
|
|
|
from ..listener_interface import IListener
|
|
|
|
from ..transport_interface import ITransport
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
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
|
2018-11-13 00:00:43 +08:00
|
|
|
self.handler = handler_function
|
2018-11-11 22:56:44 +08:00
|
|
|
|
2018-11-13 00:00:43 +08:00
|
|
|
async def listen(self, multiaddr):
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
|
|
|
put listener in listening mode and wait for incoming connections
|
|
|
|
:param multiaddr: multiaddr of peer
|
|
|
|
:return: return True if successful
|
|
|
|
"""
|
2018-11-13 00:00:43 +08:00
|
|
|
_multiaddr = multiaddr
|
2018-12-28 14:56:00 +08:00
|
|
|
_multiaddr = _multiaddr.decapsulate('/p2p')
|
2018-11-29 23:06:40 +08:00
|
|
|
|
|
|
|
coroutine = asyncio.start_server(self.handler,
|
2018-12-08 04:00:46 +08:00
|
|
|
_multiaddr.value_for_protocol('ip4'),
|
|
|
|
_multiaddr.value_for_protocol('tcp'))
|
2018-11-13 00:00:43 +08:00
|
|
|
self.server = await coroutine
|
2018-12-08 04:00:46 +08:00
|
|
|
socket = self.server.sockets[0]
|
|
|
|
self.multiaddrs.append(_multiaddr_from_socket(socket))
|
|
|
|
|
2018-11-11 22:56:44 +08:00
|
|
|
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
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
async def dial(self, multiaddr, self_id, options=None):
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
|
|
|
dial a transport to peer listening on multiaddr
|
|
|
|
:param multiaddr: multiaddr of peer
|
2019-02-25 12:11:54 +08:00
|
|
|
:param self_id: peer_id of the dialer (to send to receiver)
|
2018-10-27 06:06:56 +08:00
|
|
|
: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-29 23:06:40 +08:00
|
|
|
host = multiaddr.value_for_protocol('ip4')
|
|
|
|
port = int(multiaddr.value_for_protocol('tcp'))
|
2018-11-13 00:00:43 +08:00
|
|
|
|
|
|
|
reader, writer = await asyncio.open_connection(host, port)
|
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
# First: send our peer ID so receiver knows it
|
|
|
|
writer.write(id_b58_encode(self_id).encode())
|
2019-04-11 05:44:00 +08:00
|
|
|
print("Length of written id is " + str(len(id_b58_encode(self_id).encode())))
|
2019-02-25 09:58:23 +08:00
|
|
|
await writer.drain()
|
|
|
|
|
|
|
|
# Await ack for peer id
|
|
|
|
ack = (await reader.read(1024)).decode()
|
2019-04-11 05:44:00 +08:00
|
|
|
print(ack)
|
2019-02-25 09:58:23 +08:00
|
|
|
|
|
|
|
if ack != "received peer id":
|
|
|
|
raise Exception("Receiver did not receive peer id")
|
|
|
|
|
2018-11-30 02:42:05 +08:00
|
|
|
return RawConnection(host, port, reader, writer, True)
|
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
|
2018-11-29 23:06:40 +08:00
|
|
|
:param handler_function: a function called when a new connection is received
|
2018-10-27 06:06:56 +08:00
|
|
|
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)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _multiaddr_from_socket(socket):
|
|
|
|
return multiaddr.Multiaddr("/ip4/%s/tcp/%s" % socket.getsockname())
|