2019-08-07 18:02:30 +08:00
|
|
|
from typing import Sequence
|
|
|
|
|
2019-08-15 23:31:26 +08:00
|
|
|
from libp2p.typing import TProtocol
|
2019-08-07 18:02:30 +08:00
|
|
|
|
2019-09-15 16:58:22 +08:00
|
|
|
from .exceptions import MultiselectClientError, MultiselectCommunicatorError
|
2018-11-29 02:51:50 +08:00
|
|
|
from .multiselect_client_interface import IMultiselectClient
|
2019-08-07 18:02:30 +08:00
|
|
|
from .multiselect_communicator_interface import IMultiselectCommunicator
|
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 MultiselectClient(IMultiselectClient):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Client for communicating with receiver's multiselect module in order to
|
|
|
|
select a protocol id to communicate over."""
|
2019-01-10 09:24:41 +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
|
|
|
Ensure that the client and multiselect are both using the same
|
2019-10-24 14:41:10 +08:00
|
|
|
multiselect protocol.
|
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
:param stream: stream to communicate with multiselect over
|
2019-09-19 22:19:36 +08:00
|
|
|
:raise MultiselectClientError: 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 MultiselectClientError() 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 MultiselectClientError() 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):
|
2018-11-29 02:51:50 +08:00
|
|
|
raise MultiselectClientError("multiselect protocol ID mismatch")
|
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
async def select_one_of(
|
2019-08-15 23:31:26 +08:00
|
|
|
self, protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator
|
2019-08-07 18:02:30 +08:00
|
|
|
) -> TProtocol:
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
For each protocol, send message to multiselect selecting protocol and
|
|
|
|
fail if multiselect does not return same protocol. Returns first
|
2018-11-29 02:51:50 +08:00
|
|
|
protocol that multiselect agrees on (i.e. that multiselect selects)
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
:param protocol: protocol to select
|
|
|
|
:param stream: stream to communicate with multiselect over
|
|
|
|
:return: selected protocol
|
2019-09-19 22:19:36 +08:00
|
|
|
:raise MultiselectClientError: raised when protocol negotiation failed
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
|
|
|
await self.handshake(communicator)
|
|
|
|
|
|
|
|
for protocol in protocols:
|
|
|
|
try:
|
|
|
|
selected_protocol = await self.try_select(communicator, protocol)
|
|
|
|
return selected_protocol
|
|
|
|
except MultiselectClientError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
raise MultiselectClientError("protocols not supported")
|
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
async def try_select(
|
|
|
|
self, communicator: IMultiselectCommunicator, protocol: TProtocol
|
|
|
|
) -> TProtocol:
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Try to select the given protocol or raise exception if fails.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
:param communicator: communicator to use to communicate with counterparty
|
|
|
|
:param protocol: protocol to select
|
2019-09-19 22:19:36 +08:00
|
|
|
:raise MultiselectClientError: raised when protocol negotiation failed
|
2018-11-29 02:51:50 +08:00
|
|
|
:return: selected protocol
|
|
|
|
"""
|
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 MultiselectClientError() from error
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-09-15 16:58:22 +08:00
|
|
|
try:
|
|
|
|
response = await communicator.read()
|
|
|
|
except MultiselectCommunicatorError as error:
|
2019-12-20 00:31:18 +08:00
|
|
|
raise MultiselectClientError() from error
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
if response == protocol:
|
|
|
|
return protocol
|
|
|
|
if response == PROTOCOL_NOT_FOUND_MSG:
|
2019-03-03 20:59:38 +08:00
|
|
|
raise MultiselectClientError("protocol not supported")
|
2019-12-20 00:31:18 +08:00
|
|
|
raise MultiselectClientError(f"unrecognized response: {response}")
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-01-10 09:24:41 +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
|