py-libp2p/libp2p/protocol_muxer/multiselect.py

99 lines
3.3 KiB
Python
Raw Normal View History

2019-08-08 14:22:06 +08:00
from typing import Dict, Tuple
2019-08-07 18:02:30 +08:00
from libp2p.typing import StreamHandlerFn, TProtocol
2019-08-07 18:02:30 +08:00
2019-09-15 16:58:22 +08:00
from .exceptions import MultiselectCommunicatorError, MultiselectError
2019-08-07 18:02:30 +08:00
from .multiselect_communicator_interface import IMultiselectCommunicator
2019-08-03 13:36:19 +08:00
from .multiselect_muxer_interface import IMultiselectMuxer
2019-01-10 02:38:56 +08:00
MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0"
PROTOCOL_NOT_FOUND_MSG = "na"
2019-01-10 02:38:56 +08:00
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."""
2019-08-07 18:02:30 +08:00
handlers: Dict[TProtocol, StreamHandlerFn]
def __init__(self) -> None:
self.handlers = {}
2019-08-07 18:02:30 +08:00
def add_handler(self, protocol: TProtocol, handler: StreamHandlerFn) -> None:
"""
Store the handler with the given protocol.
:param protocol: protocol name
:param handler: handler function
"""
self.handlers[protocol] = handler
async def negotiate(
self, communicator: IMultiselectCommunicator
) -> Tuple[TProtocol, StreamHandlerFn]:
"""
Negotiate performs protocol selection.
:param stream: stream to negotiate on
:return: selected protocol name, handler function
2019-09-19 22:19:36 +08:00
:raise MultiselectError: raised when negotiation failed
"""
await self.handshake(communicator)
while True:
2019-09-15 16:58:22 +08:00
try:
command = await communicator.read()
except MultiselectCommunicatorError as error:
2019-09-19 22:19:36 +08:00
raise MultiselectError(error)
if command == "ls":
# TODO: handle ls command
pass
else:
2019-08-07 18:02:30 +08:00
protocol = TProtocol(command)
if protocol in self.handlers:
try:
await communicator.write(protocol)
except MultiselectCommunicatorError as error:
raise MultiselectError(error)
return protocol, self.handlers[protocol]
try:
await communicator.write(PROTOCOL_NOT_FOUND_MSG)
except MultiselectCommunicatorError as error:
raise MultiselectError(error)
2019-08-07 18:02:30 +08:00
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
"""
Perform handshake to agree on multiselect protocol.
:param communicator: communicator to use
2019-09-19 22:19:36 +08:00
:raise MultiselectError: raised when handshake failed
"""
try:
await communicator.write(MULTISELECT_PROTOCOL_ID)
except MultiselectCommunicatorError as error:
raise MultiselectError(error)
2019-09-15 16:58:22 +08:00
try:
handshake_contents = await communicator.read()
except MultiselectCommunicatorError as error:
2019-09-19 22:19:36 +08:00
raise MultiselectError(error)
if not validate_handshake(handshake_contents):
raise MultiselectError(
"multiselect protocol ID mismatch: "
f"received handshake_contents={handshake_contents}"
)
2019-08-07 18:02:30 +08:00
def validate_handshake(handshake_contents: str) -> bool:
"""
Determine if handshake is valid and should be confirmed.
:param handshake_contents: contents of handshake message
:return: true if handshake is complete, false otherwise
"""
return handshake_contents == MULTISELECT_PROTOCOL_ID