PR feedback

- Use the order in `MuxerMultistream` as the precedence in multiselect
This commit is contained in:
mhchia
2019-08-20 15:27:07 +08:00
parent 8596f7390f
commit 550c23f9f9
2 changed files with 31 additions and 11 deletions

View File

@ -1,5 +1,6 @@
from abc import ABC
from typing import Dict, Mapping
from collections import OrderedDict
from typing import Mapping
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
@ -25,14 +26,15 @@ class SecurityMultistream(ABC):
Go implementation: github.com/libp2p/go-conn-security-multistream/ssms.go
"""
transports: Dict[TProtocol, ISecureTransport]
# NOTE: Can be changed to `typing.OrderedDict` since Python 3.7.2.
transports: "OrderedDict[TProtocol, ISecureTransport]"
multiselect: Multiselect
multiselect_client: MultiselectClient
def __init__(
self, secure_transports_by_protocol: Mapping[TProtocol, ISecureTransport]
) -> None:
self.transports = {}
self.transports = OrderedDict()
self.multiselect = Multiselect()
self.multiselect_client = MultiselectClient()
@ -40,8 +42,17 @@ class SecurityMultistream(ABC):
self.add_transport(protocol, transport)
def add_transport(self, protocol: TProtocol, transport: ISecureTransport) -> None:
"""
Add a protocol and its corresponding transport to multistream-select(multiselect).
The order that a protocol is added is exactly the precedence it is negotiated in
multiselect.
: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.
if protocol in self.transports:
del self.transports[protocol]
self.transports[protocol] = transport
# Note: None is added as the handler for the given protocol since
# we only care about selecting the protocol, not any handler function
self.multiselect.add_handler(protocol, None)