libp2p.protocol_muxer package

Submodules

libp2p.protocol_muxer.exceptions module

exception libp2p.protocol_muxer.exceptions.MultiselectClientError

Bases: libp2p.exceptions.BaseLibp2pError

Raised when an error occurs in protocol selection process.

exception libp2p.protocol_muxer.exceptions.MultiselectCommunicatorError

Bases: libp2p.exceptions.BaseLibp2pError

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

exception libp2p.protocol_muxer.exceptions.MultiselectError

Bases: libp2p.exceptions.BaseLibp2pError

Raised when an error occurs in multiselect process.

libp2p.protocol_muxer.multiselect module

class libp2p.protocol_muxer.multiselect.Multiselect(default_handlers: Dict[NewType.<locals>.new_type, Callable[[INetStream], Awaitable[None]]] = None)

Bases: libp2p.protocol_muxer.multiselect_muxer_interface.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: NewType.<locals>.new_type, handler: Callable[[INetStream], Awaitable[None]]) → None

Store the handler with the given protocol.

Parameters
  • protocol – protocol name

  • handler – handler function

handlers: Dict[TProtocol, StreamHandlerFn] = None
async handshake(communicator: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator) → None

Perform handshake to agree on multiselect protocol.

Parameters

communicator – communicator to use

Raises

MultiselectError – raised when handshake failed

async negotiate(communicator: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator) → Tuple[NewType.<locals>.new_type, Callable[[libp2p.network.stream.net_stream_interface.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: libp2p.protocol_muxer.multiselect_client_interface.IMultiselectClient

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

async handshake(communicator: libp2p.protocol_muxer.multiselect_communicator_interface.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[NewType.<locals>.new_type], communicator: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator) → NewType.<locals>.new_type

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: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator, protocol: NewType.<locals>.new_type) → NewType.<locals>.new_type

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.ABC

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

async handshake(communicator: libp2p.protocol_muxer.multiselect_communicator_interface.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[NewType.<locals>.new_type], communicator: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator) → NewType.<locals>.new_type

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

async try_select(communicator: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator, protocol: NewType.<locals>.new_type) → NewType.<locals>.new_type

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: libp2p.io.abc.ReadWriteCloser)

Bases: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator

async read() → str
Raises

MultiselectCommunicatorError – raised when failed to read from underlying reader

read_writer: ReadWriteCloser = None
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.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.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: NewType.<locals>.new_type, handler: Callable[[INetStream], Awaitable[None]]) → None

Store the handler with the given protocol.

Parameters
  • protocol – protocol name

  • handler – handler function

get_protocols() → Tuple[NewType.<locals>.new_type, ...]
handlers: Dict[TProtocol, StreamHandlerFn] = None
abstract async negotiate(communicator: libp2p.protocol_muxer.multiselect_communicator_interface.IMultiselectCommunicator) → Tuple[NewType.<locals>.new_type, Callable[[libp2p.network.stream.net_stream_interface.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