py-libp2p/libp2p/protocol_muxer/multiselect_client_interface.py

47 lines
1.6 KiB
Python
Raw Normal View History

from abc import ABC, abstractmethod
2019-08-07 18:02:30 +08:00
from typing import Sequence
from libp2p.protocol_muxer.multiselect_communicator_interface import (
IMultiselectCommunicator,
)
2019-08-07 18:02:30 +08:00
from libp2p.typing import TProtocol
2019-01-10 02:38:56 +08:00
class IMultiselectClient(ABC):
"""
Client for communicating with receiver's multiselect
module in order to select a protocol id to communicate over
"""
2019-09-14 22:24:53 +08:00
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
"""
2019-09-14 22:24:53 +08:00
Ensure that the client and multiselect
are both using the same multiselect protocol
:param stream: stream to communicate with multiselect over
2019-09-14 22:24:53 +08:00
:raise Exception: multiselect protocol ID mismatch
"""
@abstractmethod
2019-08-07 18:02:30 +08:00
async def select_one_of(
self, protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator
2019-08-07 18:02:30 +08:00
) -> TProtocol:
"""
For each protocol, send message to multiselect selecting protocol
and fail if multiselect does not return same protocol. Returns first
protocol that multiselect agrees on (i.e. that multiselect selects)
:param protocol: protocol to select
:param stream: stream to communicate with multiselect over
:return: selected protocol
"""
2019-09-14 22:24:53 +08:00
async def try_select(
self, communicator: IMultiselectCommunicator, protocol: TProtocol
) -> TProtocol:
"""
Try to select the given protocol or raise exception if fails
:param communicator: communicator to use to communicate with counterparty
:param protocol: protocol to select
:raise Exception: error in protocol selection
:return: selected protocol
"""