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.

libp2p.protocol_muxer.multiselect module

class libp2p.protocol_muxer.multiselect.Multiselect(default_handlers: Dict[TProtocol, Callable[[INetStream], Awaitable[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, handler: Callable[[INetStream], Awaitable[None]]) None

Store the handler with the given protocol.

Parameters:
  • protocol – protocol name

  • handler – handler function

handlers: Dict[TProtocol, Callable[[INetStream], Awaitable[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) Tuple[TProtocol, Callable[[INetStream], Awaitable[None]]]

Negotiate performs protocol selection.

Parameters:

stream – stream to negotiate on

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:

stream – stream to communicate with multiselect over

Raises:

MultiselectClientError – raised when handshake failed

async select_one_of(protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator) 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:
  • protocol – protocol to select

  • stream – stream to communicate with multiselect over

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_client_interface module

class libp2p.protocol_muxer.multiselect_client_interface.IMultiselectClient

Bases: ABC

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

abstract async handshake(communicator: IMultiselectCommunicator) None

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

Parameters:

stream – stream to communicate with multiselect over

Raises:

Exception – multiselect protocol ID mismatch

abstract async select_one_of(protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator) 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:
  • protocol – protocol to select

  • stream – stream to communicate with multiselect over

Returns:

selected protocol

abstract 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:

Exception – error in protocol selection

Returns:

selected protocol

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

libp2p.protocol_muxer.multiselect_communicator_interface module

class libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator

Bases: ABC

Communicator helper class that ensures both the client and multistream module will follow the same multistream protocol, which is necessary for them to work.

abstract async read() str

Reads message from stream until EOF.

abstract async write(msg_str: str) None

Write message to stream.

Parameters:

msg_str – message to write

libp2p.protocol_muxer.multiselect_muxer_interface module

class libp2p.protocol_muxer.multiselect_muxer_interface.IMultiselectMuxer

Bases: ABC

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

abstract add_handler(protocol: TProtocol, handler: Callable[[INetStream], Awaitable[None]]) None

Store the handler with the given protocol.

Parameters:
  • protocol – protocol name

  • handler – handler function

get_protocols() Tuple[TProtocol, ...]
handlers: Dict[TProtocol, Callable[[INetStream], Awaitable[None]]]
abstract async negotiate(communicator: IMultiselectCommunicator) Tuple[TProtocol, Callable[[INetStream], Awaitable[None]]]

Negotiate performs protocol selection.

Parameters:

stream – stream to negotiate on

Returns:

selected protocol name, handler function

Raises:

Exception – negotiation failed exception

Module contents