2018-10-27 06:06:56 +08:00
|
|
|
import asyncio
|
2019-08-05 11:22:44 +08:00
|
|
|
from socket import socket
|
|
|
|
from typing import List
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-08-02 16:32:33 +08:00
|
|
|
from multiaddr import Multiaddr
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
from libp2p.network.connection.raw_connection import RawConnection
|
2019-08-05 11:22:44 +08:00
|
|
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
|
|
from libp2p.peer.id import ID
|
2019-08-02 16:32:33 +08:00
|
|
|
from libp2p.transport.listener_interface import IListener
|
|
|
|
from libp2p.transport.transport_interface import ITransport
|
2019-08-05 11:22:44 +08:00
|
|
|
from libp2p.transport.typing import THandler
|
2019-08-02 16:32:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TCPListener(IListener):
|
|
|
|
multiaddrs: List[Multiaddr]
|
|
|
|
server = None
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def __init__(self, handler_function: THandler) -> None:
|
2019-08-02 16:32:33 +08:00
|
|
|
self.multiaddrs = []
|
|
|
|
self.server = None
|
|
|
|
self.handler = handler_function
|
|
|
|
|
|
|
|
async def listen(self, maddr: Multiaddr) -> bool:
|
|
|
|
"""
|
|
|
|
put listener in listening mode and wait for incoming connections
|
|
|
|
:param maddr: maddr of peer
|
|
|
|
:return: return True if successful
|
|
|
|
"""
|
|
|
|
self.server = await asyncio.start_server(
|
2019-08-14 05:36:42 +08:00
|
|
|
self.handler,
|
|
|
|
maddr.value_for_protocol("ip4"),
|
|
|
|
maddr.value_for_protocol("tcp"),
|
2019-08-02 16:32:33 +08:00
|
|
|
)
|
|
|
|
socket = self.server.sockets[0]
|
|
|
|
self.multiaddrs.append(_multiaddr_from_socket(socket))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_addrs(self) -> List[Multiaddr]:
|
|
|
|
"""
|
|
|
|
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) -> bool:
|
|
|
|
"""
|
|
|
|
close the listener such that no more connections
|
|
|
|
can be open on this transport instance
|
|
|
|
: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
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-10-27 06:06:56 +08:00
|
|
|
|
|
|
|
class TCP(ITransport):
|
2019-08-17 00:19:37 +08:00
|
|
|
# TODO: Remove `self_id`
|
2019-08-05 10:20:30 +08:00
|
|
|
async def dial(self, maddr: Multiaddr, self_id: ID) -> IRawConnection:
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
|
|
|
dial a transport to peer listening on multiaddr
|
2019-07-10 19:33:38 +08:00
|
|
|
:param maddr: multiaddr of peer
|
2019-02-25 12:11:54 +08:00
|
|
|
:param self_id: peer_id of the dialer (to send to receiver)
|
2019-08-16 14:12:10 +08:00
|
|
|
:return: `RawConnection` if successful
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
2019-08-25 03:02:30 +08:00
|
|
|
self.host = maddr.value_for_protocol("ip4")
|
|
|
|
self.port = int(maddr.value_for_protocol("tcp"))
|
2018-11-13 00:00:43 +08:00
|
|
|
|
2019-08-25 03:02:30 +08:00
|
|
|
reader, writer = await asyncio.open_connection(self.host, self.port)
|
2018-11-13 00:00:43 +08:00
|
|
|
|
2019-08-25 03:02:30 +08:00
|
|
|
return RawConnection(reader, writer, True)
|
2018-10-27 06:06:56 +08:00
|
|
|
|
2019-08-05 10:20:30 +08:00
|
|
|
def create_listener(self, handler_function: THandler) -> TCPListener:
|
2018-10-27 06:06:56 +08:00
|
|
|
"""
|
|
|
|
create listener on transport
|
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
|
|
|
|
"""
|
2019-08-02 16:32:33 +08:00
|
|
|
return TCPListener(handler_function)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
|
|
|
|
2019-08-05 10:20:30 +08:00
|
|
|
def _multiaddr_from_socket(socket: socket) -> Multiaddr:
|
2019-08-02 16:32:33 +08:00
|
|
|
return Multiaddr("/ip4/%s/tcp/%s" % socket.getsockname())
|