2019-08-08 14:22:06 +08:00
|
|
|
from typing import Dict, Tuple
|
2019-08-07 18:02:30 +08:00
|
|
|
|
2019-08-15 23:31:26 +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
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0"
|
|
|
|
PROTOCOL_NOT_FOUND_MSG = "na"
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
class Multiselect(IMultiselectMuxer):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Multiselect module that is responsible for responding to a multiselect
|
|
|
|
client and deciding on a specific protocol and handler pair to use for
|
|
|
|
communication."""
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
handlers: Dict[TProtocol, StreamHandlerFn]
|
|
|
|
|
2019-09-25 10:12:04 +08:00
|
|
|
def __init__(
|
|
|
|
self, default_handlers: Dict[TProtocol, StreamHandlerFn] = None
|
|
|
|
) -> None:
|
|
|
|
if not default_handlers:
|
|
|
|
default_handlers = {}
|
2019-09-25 08:19:22 +08:00
|
|
|
self.handlers = default_handlers
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
def add_handler(self, protocol: TProtocol, handler: StreamHandlerFn) -> None:
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Store the handler with the given protocol.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
:param protocol: protocol name
|
|
|
|
:param handler: handler function
|
|
|
|
"""
|
|
|
|
self.handlers[protocol] = handler
|
|
|
|
|
2019-08-14 05:36:42 +08:00
|
|
|
async def negotiate(
|
2019-08-15 23:31:26 +08:00
|
|
|
self, communicator: IMultiselectCommunicator
|
2019-08-14 05:36:42 +08:00
|
|
|
) -> Tuple[TProtocol, StreamHandlerFn]:
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Negotiate performs protocol selection.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
: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
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
|
|
|
await self.handshake(communicator)
|
|
|
|
|
|
|
|
while True:
|
2019-09-15 16:58:22 +08:00
|
|
|
try:
|
|
|
|
command = await communicator.read()
|
|
|
|
except MultiselectCommunicatorError as error:
|
2019-12-20 00:31:18 +08:00
|
|
|
raise MultiselectError() from error
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
if command == "ls":
|
|
|
|
# TODO: handle ls command
|
|
|
|
pass
|
|
|
|
else:
|
2019-08-07 18:02:30 +08:00
|
|
|
protocol = TProtocol(command)
|
2018-11-29 02:51:50 +08:00
|
|
|
if protocol in self.handlers:
|
2019-09-19 21:51:23 +08:00
|
|
|
try:
|
|
|
|
await communicator.write(protocol)
|
|
|
|
except MultiselectCommunicatorError as error:
|
2019-12-20 00:31:18 +08:00
|
|
|
raise MultiselectError() from error
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
return protocol, self.handlers[protocol]
|
2019-09-19 21:51:23 +08:00
|
|
|
try:
|
|
|
|
await communicator.write(PROTOCOL_NOT_FOUND_MSG)
|
|
|
|
except MultiselectCommunicatorError as error:
|
2019-12-20 00:31:18 +08:00
|
|
|
raise MultiselectError() from error
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Perform handshake to agree on multiselect protocol.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
:param communicator: communicator to use
|
2019-09-19 22:19:36 +08:00
|
|
|
:raise MultiselectError: raised when handshake failed
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
2019-09-19 21:51:23 +08:00
|
|
|
try:
|
|
|
|
await communicator.write(MULTISELECT_PROTOCOL_ID)
|
|
|
|
except MultiselectCommunicatorError as error:
|
2019-12-20 00:31:18 +08:00
|
|
|
raise MultiselectError() from error
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-09-15 16:58:22 +08:00
|
|
|
try:
|
|
|
|
handshake_contents = await communicator.read()
|
|
|
|
except MultiselectCommunicatorError as error:
|
2019-12-20 00:31:18 +08:00
|
|
|
raise MultiselectError() from error
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-10-25 01:25:09 +08:00
|
|
|
if not is_valid_handshake(handshake_contents):
|
2019-08-17 21:41:17 +08:00
|
|
|
raise MultiselectError(
|
2019-08-20 16:42:34 +08:00
|
|
|
"multiselect protocol ID mismatch: "
|
|
|
|
f"received handshake_contents={handshake_contents}"
|
2019-08-17 21:41:17 +08:00
|
|
|
)
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
|
2019-10-25 01:25:09 +08:00
|
|
|
def is_valid_handshake(handshake_contents: str) -> bool:
|
2019-01-10 09:24:41 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Determine if handshake is valid and should be confirmed.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-01-10 09:24:41 +08:00
|
|
|
:param handshake_contents: contents of handshake message
|
|
|
|
:return: true if handshake is complete, false otherwise
|
|
|
|
"""
|
|
|
|
return handshake_contents == MULTISELECT_PROTOCOL_ID
|