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):
|
|
|
|
"""
|
|
|
|
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:
|
2018-11-29 02:51:50 +08:00
|
|
|
self.handlers = {}
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
Store the handler with the given protocol
|
|
|
|
: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
|
|
|
"""
|
|
|
|
Negotiate performs protocol selection
|
|
|
|
:param stream: stream to negotiate on
|
|
|
|
:return: selected protocol name, handler function
|
|
|
|
:raise Exception: negotiation failed exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
# 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
|
2019-09-15 16:58:22 +08:00
|
|
|
try:
|
|
|
|
command = await communicator.read()
|
|
|
|
except MultiselectCommunicatorError as error:
|
|
|
|
raise MultiselectError(str(error))
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
# Command is ls or a protocol
|
|
|
|
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:
|
|
|
|
# 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)
|
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
|
2018-11-29 02:51:50 +08:00
|
|
|
"""
|
|
|
|
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
|
2019-09-15 16:58:22 +08:00
|
|
|
try:
|
|
|
|
handshake_contents = await communicator.read()
|
|
|
|
except MultiselectCommunicatorError as error:
|
|
|
|
raise MultiselectError(str(error))
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
# Confirm that the protocols are the same
|
2019-01-10 09:24:41 +08:00
|
|
|
if not validate_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
|
|
|
|
|
|
|
# Handshake succeeded if this point is reached
|
|
|
|
|
|
|
|
|
2019-08-07 18:02:30 +08:00
|
|
|
def validate_handshake(handshake_contents: str) -> bool:
|
2019-01-10 09:24:41 +08:00
|
|
|
"""
|
|
|
|
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
|