libp2p.protocol_muxer package

Submodules

libp2p.protocol_muxer.exceptions module

exception libp2p.protocol_muxer.exceptions.MultiselectClientError

Bases: BaseLibp2pError

Raised when an error occurs in protocol selection process.

exception libp2p.protocol_muxer.exceptions.MultiselectCommunicatorError

Bases: BaseLibp2pError

Raised when an error occurs during read/write via communicator.

exception libp2p.protocol_muxer.exceptions.MultiselectError

Bases: BaseLibp2pError

Raised when an error occurs in multiselect process.

exception libp2p.protocol_muxer.exceptions.ProtocolNotSupportedError

Bases: MultiselectClientError

Raised when a peer explicitly reports that a protocol is unsupported.

libp2p.protocol_muxer.multiselect module

class libp2p.protocol_muxer.multiselect.Multiselect(default_handlers: None | dict[TProtocol | None, Callable[[object], Awaitable[None]] | None] = None)

Bases: IMultiselectMuxer

Multiselect module that is responsible for responding to a multiselect client and deciding on a specific protocol and handler pair to use for communication.

add_handler(protocol: TProtocol | None, handler: Callable[[object], Awaitable[None]] | None) None

Store the handler with the given protocol.

Parameters:
  • protocol – protocol name

  • handler – handler function

get_protocols() tuple[TProtocol | None, ...]

Retrieve the protocols for which handlers have been registered.

Returns

tuple[TProtocol, …]

A tuple of registered protocol names.

handlers: dict[TProtocol | None, Callable[[object], Awaitable[None]] | None]
async handshake(communicator: IMultiselectCommunicator) None

Perform handshake to agree on multiselect protocol.

Parameters:

communicator – communicator to use

Raises:

MultiselectError – raised when handshake failed

async negotiate(communicator: IMultiselectCommunicator, negotiate_timeout: int = 30) tuple[TProtocol | None, Callable[[object], Awaitable[None]] | None]

Negotiate performs protocol selection.

Parameters:
  • stream – stream to negotiate on

  • negotiate_timeout – timeout for negotiation

Returns:

selected protocol name, handler function

Raises:

MultiselectError – raised when negotiation failed

libp2p.protocol_muxer.multiselect.is_valid_handshake(handshake_contents: str) bool

Determine if handshake is valid and should be confirmed.

Parameters:

handshake_contents – contents of handshake message

Returns:

true if handshake is complete, false otherwise

libp2p.protocol_muxer.multiselect_client module

class libp2p.protocol_muxer.multiselect_client.MultiselectClient

Bases: IMultiselectClient

Client for communicating with receiver’s multiselect module in order to select a protocol id to communicate over.

async handshake(communicator: IMultiselectCommunicator) None

Ensure that the client and multiselect are both using the same multiselect protocol.

Parameters:

communicator – communicator to use to communicate with counterparty

Raises:

MultiselectClientError – raised when handshake failed

async query_multistream_command(communicator: IMultiselectCommunicator, command: str, response_timeout: int = 30) list[str]

Send a multistream-select command over the given communicator and return parsed response.

Parameters:
  • communicator – communicator to use to communicate with counterparty

  • command – supported multistream-select command(e.g., ls)

  • negotiate_timeout – timeout for negotiation

Raises:

MultiselectClientError – If the communicator fails to process data.

Returns:

list of strings representing the response from peer.

async select_one_of(protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator, negotiate_timeout: int = 30) 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)

Parameters:
  • protocols – protocols to select from

  • communicator – communicator to use to communicate with counterparty

  • negotiate_timeout – timeout for negotiation

Returns:

selected protocol

Raises:

MultiselectClientError – raised when protocol negotiation failed

async try_select(communicator: IMultiselectCommunicator, protocol: TProtocol) TProtocol

Try to select the given protocol or raise exception if fails.

Parameters:
  • communicator – communicator to use to communicate with counterparty

  • protocol – protocol to select

Raises:

MultiselectClientError – raised when protocol negotiation failed

Returns:

selected protocol

libp2p.protocol_muxer.multiselect_client.is_valid_handshake(handshake_contents: str) bool

Determine if handshake is valid and should be confirmed.

Parameters:

handshake_contents – contents of handshake message

Returns:

true if handshake is complete, false otherwise

libp2p.protocol_muxer.multiselect_communicator module

class libp2p.protocol_muxer.multiselect_communicator.MultiselectCommunicator(read_writer: ReadWriteCloser)

Bases: IMultiselectCommunicator

async read() str
Raises:

MultiselectCommunicatorError – raised when failed to read from underlying reader

read_writer: ReadWriteCloser
async write(msg_str: str) None
Raises:

MultiselectCommunicatorError – raised when failed to write to underlying reader

Module contents