mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 07:00:54 +00:00
PR feedback
- Use the order in `MuxerMultistream` as the precedence in multiselect
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user