mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 07:00:54 +00:00
Protocol muxing (#82)
* Implement protocol muxing * Integrate protocol muxing into new stream and listen's conn handler * Fix bugs in protocol muxing * Remove blank line * Add comments and fix linting issues * Fix order of parameters to select_one_of to match interface * Use array of protocol ids in new stream instead of protocol id * Add basic protocol muxer tests * Add todo * Modify new stream to take in protocol ids * Add check to all tests to ensure protocol id is saved to net stream properly * Lint tests * Fix lint issues * Add todo * Modify port numbers in tests * Fix linting issues * Add more documentation to functions * Add docs describing classes and fix indent error
This commit is contained in:
0
protocol_muxer/__init__.py
Normal file
0
protocol_muxer/__init__.py
Normal file
92
protocol_muxer/multiselect.py
Normal file
92
protocol_muxer/multiselect.py
Normal file
@ -0,0 +1,92 @@
|
||||
from .multiselect_muxer_interface import IMultiselectMuxer
|
||||
from .multiselect_communicator import MultiselectCommunicator
|
||||
|
||||
MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0"
|
||||
PROTOCOL_NOT_FOUND_MSG = "na"
|
||||
|
||||
class Multiselect(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
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.handlers = {}
|
||||
|
||||
def add_handler(self, protocol, handler):
|
||||
"""
|
||||
Store the handler with the given protocol
|
||||
:param protocol: protocol name
|
||||
:param handler: handler function
|
||||
"""
|
||||
self.handlers[protocol] = handler
|
||||
|
||||
async def negotiate(self, stream):
|
||||
"""
|
||||
Negotiate performs protocol selection
|
||||
:param stream: stream to negotiate on
|
||||
:return: selected protocol name, handler function
|
||||
:raise Exception: negotiation failed exception
|
||||
"""
|
||||
|
||||
# Create a communicator to handle all communication across the stream
|
||||
communicator = MultiselectCommunicator(stream)
|
||||
|
||||
# Perform handshake to ensure multiselect protocol IDs match
|
||||
await self.handshake(communicator)
|
||||
|
||||
# Read and respond to commands until a valid protocol ID is sent
|
||||
while True:
|
||||
# Read message
|
||||
command = await communicator.read_stream_until_eof()
|
||||
|
||||
# Command is ls or a protocol
|
||||
if command == "ls":
|
||||
# TODO: handle ls command
|
||||
pass
|
||||
else:
|
||||
protocol = command
|
||||
if protocol in self.handlers:
|
||||
# Tell counterparty we have decided on a protocol
|
||||
await communicator.write(protocol)
|
||||
|
||||
# Return the decided on protocol
|
||||
return protocol, self.handlers[protocol]
|
||||
# Tell counterparty this protocol was not found
|
||||
await communicator.write(PROTOCOL_NOT_FOUND_MSG)
|
||||
|
||||
async def handshake(self, communicator):
|
||||
"""
|
||||
Perform handshake to agree on multiselect protocol
|
||||
:param communicator: communicator to use
|
||||
:raise Exception: error in handshake
|
||||
"""
|
||||
|
||||
# TODO: Use format used by go repo for messages
|
||||
|
||||
# Send our MULTISELECT_PROTOCOL_ID to other party
|
||||
await communicator.write(MULTISELECT_PROTOCOL_ID)
|
||||
|
||||
# Read in the protocol ID from other party
|
||||
handshake_contents = await communicator.read_stream_until_eof()
|
||||
|
||||
# Confirm that the protocols are the same
|
||||
if not self.validate_handshake(handshake_contents):
|
||||
raise MultiselectError("multiselect protocol ID mismatch")
|
||||
|
||||
# Handshake succeeded if this point is reached
|
||||
|
||||
def validate_handshake(self, handshake_contents):
|
||||
"""
|
||||
Determine if handshake is valid and should be confirmed
|
||||
:param handshake_contents: contents of handshake message
|
||||
:return: true if handshake is complete, false otherwise
|
||||
"""
|
||||
|
||||
# TODO: Modify this when format used by go repo for messages
|
||||
# is added
|
||||
return handshake_contents == MULTISELECT_PROTOCOL_ID
|
||||
|
||||
class MultiselectError(ValueError):
|
||||
"""Raised when an error occurs in multiselect process"""
|
||||
121
protocol_muxer/multiselect_client.py
Normal file
121
protocol_muxer/multiselect_client.py
Normal file
@ -0,0 +1,121 @@
|
||||
from .multiselect_client_interface import IMultiselectClient
|
||||
from .multiselect_communicator import MultiselectCommunicator
|
||||
|
||||
MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0"
|
||||
PROTOCOL_NOT_FOUND_MSG = "na"
|
||||
|
||||
class MultiselectClient(IMultiselectClient):
|
||||
"""
|
||||
Client for communicating with receiver's multiselect
|
||||
module in order to select a protocol id to communicate over
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
async def handshake(self, communicator):
|
||||
"""
|
||||
Ensure that the client and multiselect
|
||||
are both using the same multiselect protocol
|
||||
:param stream: stream to communicate with multiselect over
|
||||
:raise Exception: multiselect protocol ID mismatch
|
||||
"""
|
||||
|
||||
# TODO: Use format used by go repo for messages
|
||||
|
||||
# Send our MULTISELECT_PROTOCOL_ID to counterparty
|
||||
await communicator.write(MULTISELECT_PROTOCOL_ID)
|
||||
|
||||
# Read in the protocol ID from other party
|
||||
handshake_contents = await communicator.read_stream_until_eof()
|
||||
|
||||
# Confirm that the protocols are the same
|
||||
if not self.validate_handshake(handshake_contents):
|
||||
raise MultiselectClientError("multiselect protocol ID mismatch")
|
||||
|
||||
# Handshake succeeded if this point is reached
|
||||
|
||||
def validate_handshake(self, handshake_contents):
|
||||
"""
|
||||
Determine if handshake is valid and should be confirmed
|
||||
:param handshake_contents: contents of handshake message
|
||||
:return: true if handshake is complete, false otherwise
|
||||
"""
|
||||
|
||||
# TODO: Modify this when format used by go repo for messages
|
||||
# is added
|
||||
return handshake_contents == MULTISELECT_PROTOCOL_ID
|
||||
|
||||
async def select_protocol_or_fail(self, protocol, stream):
|
||||
"""
|
||||
Send message to multiselect selecting protocol
|
||||
and fail if multiselect does not return same protocol
|
||||
:param protocol: protocol to select
|
||||
:param stream: stream to communicate with multiselect over
|
||||
:return: selected protocol
|
||||
"""
|
||||
|
||||
# Create a communicator to handle all communication across the stream
|
||||
communicator = MultiselectCommunicator(stream)
|
||||
|
||||
# Perform handshake to ensure multiselect protocol IDs match
|
||||
await self.handshake(communicator)
|
||||
|
||||
# Try to select the given protocol
|
||||
selected_protocol = await self.try_select(communicator, protocol)
|
||||
|
||||
return selected_protocol
|
||||
|
||||
async def select_one_of(self, protocols, stream):
|
||||
"""
|
||||
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
|
||||
"""
|
||||
|
||||
# Create a communicator to handle all communication across the stream
|
||||
communicator = MultiselectCommunicator(stream)
|
||||
|
||||
# Perform handshake to ensure multiselect protocol IDs match
|
||||
await self.handshake(communicator)
|
||||
|
||||
# For each protocol, attempt to select that protocol
|
||||
# and return the first protocol selected
|
||||
for protocol in protocols:
|
||||
try:
|
||||
selected_protocol = await self.try_select(communicator, protocol)
|
||||
return selected_protocol
|
||||
except MultiselectClientError:
|
||||
pass
|
||||
|
||||
# No protocols were found, so return no protocols supported error
|
||||
raise MultiselectClientError("protocols not supported")
|
||||
|
||||
async def try_select(self, communicator, protocol):
|
||||
"""
|
||||
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
|
||||
"""
|
||||
|
||||
# Tell counterparty we want to use protocol
|
||||
await communicator.write(protocol)
|
||||
|
||||
# Get what counterparty says in response
|
||||
response = await communicator.read_stream_until_eof()
|
||||
|
||||
# Return protocol if response is equal to protocol or raise error
|
||||
if response == protocol:
|
||||
return protocol
|
||||
if response == PROTOCOL_NOT_FOUND_MSG:
|
||||
raise MultiselectClientError("protocol not supported")
|
||||
else:
|
||||
raise MultiselectClientError("unrecognized response: " + response)
|
||||
|
||||
class MultiselectClientError(ValueError):
|
||||
"""Raised when an error occurs in protocol selection process"""
|
||||
28
protocol_muxer/multiselect_client_interface.py
Normal file
28
protocol_muxer/multiselect_client_interface.py
Normal file
@ -0,0 +1,28 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class IMultiselectClient(ABC):
|
||||
"""
|
||||
Client for communicating with receiver's multiselect
|
||||
module in order to select a protocol id to communicate over
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def select_protocol_or_fail(self, protocol, stream):
|
||||
"""
|
||||
Send message to multiselect selecting protocol
|
||||
and fail if multiselect does not return same protocol
|
||||
:param protocol: protocol to select
|
||||
:param stream: stream to communicate with multiselect over
|
||||
:return: selected protocol
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def select_one_of(self, protocols, stream):
|
||||
"""
|
||||
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
|
||||
"""
|
||||
25
protocol_muxer/multiselect_communicator.py
Normal file
25
protocol_muxer/multiselect_communicator.py
Normal file
@ -0,0 +1,25 @@
|
||||
from .multiselect_communicator_interface import IMultiselectCommunicator
|
||||
|
||||
class MultiselectCommunicator(IMultiselectCommunicator):
|
||||
"""
|
||||
Communicator helper class that ensures both the client
|
||||
and multistream module will follow the same multistream protocol,
|
||||
which is necessary for them to work
|
||||
"""
|
||||
|
||||
def __init__(self, stream):
|
||||
self.stream = stream
|
||||
|
||||
async def write(self, msg_str):
|
||||
"""
|
||||
Write message to stream
|
||||
:param msg_str: message to write
|
||||
"""
|
||||
await self.stream.write(msg_str.encode())
|
||||
|
||||
async def read_stream_until_eof(self):
|
||||
"""
|
||||
Reads message from stream until EOF
|
||||
"""
|
||||
read_str = (await self.stream.read()).decode()
|
||||
return read_str
|
||||
21
protocol_muxer/multiselect_communicator_interface.py
Normal file
21
protocol_muxer/multiselect_communicator_interface.py
Normal file
@ -0,0 +1,21 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class IMultiselectCommunicator(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
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def write(self, msg_str):
|
||||
"""
|
||||
Write message to stream
|
||||
:param msg_str: message to write
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def read_stream_until_eof(self):
|
||||
"""
|
||||
Reads message from stream until EOF
|
||||
"""
|
||||
25
protocol_muxer/multiselect_muxer_interface.py
Normal file
25
protocol_muxer/multiselect_muxer_interface.py
Normal file
@ -0,0 +1,25 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class IMultiselectMuxer(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
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_handler(self, protocol, handler):
|
||||
"""
|
||||
Store the handler with the given protocol
|
||||
:param protocol: protocol name
|
||||
:param handler: handler function
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def negotiate(self, stream):
|
||||
"""
|
||||
Negotiate performs protocol selection
|
||||
:param stream: stream to negotiate on
|
||||
:return: selected protocol name, handler function
|
||||
:raise Exception: negotiation failed exception
|
||||
"""
|
||||
Reference in New Issue
Block a user