2019-08-20 15:27:07 +08:00
|
|
|
from collections import OrderedDict
|
2019-08-16 14:12:10 +08:00
|
|
|
|
|
|
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
|
|
from libp2p.peer.id import ID
|
|
|
|
from libp2p.protocol_muxer.multiselect import Multiselect
|
|
|
|
from libp2p.protocol_muxer.multiselect_client import MultiselectClient
|
2019-09-05 23:24:17 +08:00
|
|
|
from libp2p.protocol_muxer.multiselect_communicator import MultiselectCommunicator
|
2019-08-16 14:12:10 +08:00
|
|
|
from libp2p.security.secure_conn_interface import ISecureConn
|
2019-09-18 15:44:45 +08:00
|
|
|
from libp2p.transport.typing import TMuxerClass, TMuxerOptions
|
2019-08-16 14:12:10 +08:00
|
|
|
from libp2p.typing import TProtocol
|
|
|
|
|
|
|
|
from .abc import IMuxedConn
|
|
|
|
|
|
|
|
# FIXME: add negotiate timeout to `MuxerMultistream`
|
|
|
|
DEFAULT_NEGOTIATE_TIMEOUT = 60
|
|
|
|
|
|
|
|
|
|
|
|
class MuxerMultistream:
|
|
|
|
"""
|
|
|
|
MuxerMultistream is a multistream stream muxed transport multiplexer.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-16 14:12:10 +08:00
|
|
|
go implementation: github.com/libp2p/go-stream-muxer-multistream/multistream.go
|
|
|
|
"""
|
|
|
|
|
2019-08-20 15:27:07 +08:00
|
|
|
# NOTE: Can be changed to `typing.OrderedDict` since Python 3.7.2.
|
2019-09-18 15:44:45 +08:00
|
|
|
transports: "OrderedDict[TProtocol, TMuxerClass]"
|
2019-08-16 14:12:10 +08:00
|
|
|
multiselect: Multiselect
|
|
|
|
multiselect_client: MultiselectClient
|
|
|
|
|
2019-09-24 12:51:59 +08:00
|
|
|
def __init__(self, muxer_transports_by_protocol: TMuxerOptions) -> None:
|
2019-08-20 15:27:07 +08:00
|
|
|
self.transports = OrderedDict()
|
2019-08-16 14:12:10 +08:00
|
|
|
self.multiselect = Multiselect()
|
|
|
|
self.multiselect_client = MultiselectClient()
|
2019-09-24 12:51:59 +08:00
|
|
|
for protocol, transport in muxer_transports_by_protocol.items():
|
|
|
|
self.add_transport(protocol, transport)
|
2019-08-16 14:12:10 +08:00
|
|
|
|
2019-09-18 15:44:45 +08:00
|
|
|
def add_transport(self, protocol: TProtocol, transport: TMuxerClass) -> None:
|
2019-08-20 15:27:07 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Add a protocol and its corresponding transport to multistream-
|
2019-10-24 14:41:10 +08:00
|
|
|
select(multiselect). The order that a protocol is added is exactly the
|
|
|
|
precedence it is negotiated in multiselect.
|
|
|
|
|
2019-08-20 15:27:07 +08:00
|
|
|
:param protocol: the protocol name, which is negotiated in multiselect.
|
|
|
|
:param transport: the corresponding transportation to the ``protocol``.
|
|
|
|
"""
|
|
|
|
# If protocol is already added before, remove it and add it again.
|
2019-11-20 23:06:37 +08:00
|
|
|
self.transports.pop(protocol, None)
|
2019-08-16 14:12:10 +08:00
|
|
|
self.transports[protocol] = transport
|
|
|
|
self.multiselect.add_handler(protocol, None)
|
|
|
|
|
2019-09-18 15:44:45 +08:00
|
|
|
async def select_transport(self, conn: IRawConnection) -> TMuxerClass:
|
2019-08-16 14:12:10 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Select a transport that both us and the node on the other end of conn
|
|
|
|
support and agree on.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-16 14:12:10 +08:00
|
|
|
:param conn: conn to choose a transport over
|
2019-08-19 23:18:00 +08:00
|
|
|
:return: selected muxer transport
|
2019-08-16 14:12:10 +08:00
|
|
|
"""
|
|
|
|
protocol: TProtocol
|
2019-09-05 23:24:17 +08:00
|
|
|
communicator = MultiselectCommunicator(conn)
|
2019-10-25 01:28:19 +08:00
|
|
|
if conn.is_initiator:
|
2019-08-16 14:12:10 +08:00
|
|
|
protocol = await self.multiselect_client.select_one_of(
|
2019-08-20 15:27:07 +08:00
|
|
|
tuple(self.transports.keys()), communicator
|
2019-08-16 14:12:10 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
protocol, _ = await self.multiselect.negotiate(communicator)
|
|
|
|
return self.transports[protocol]
|
|
|
|
|
2019-09-12 00:38:12 +08:00
|
|
|
async def new_conn(self, conn: ISecureConn, peer_id: ID) -> IMuxedConn:
|
2019-08-16 14:12:10 +08:00
|
|
|
transport_class = await self.select_transport(conn)
|
2019-09-12 00:38:12 +08:00
|
|
|
return transport_class(conn, peer_id)
|