2018-11-29 02:51:50 +08:00
|
|
|
from .multiselect_muxer_interface import IMultiselectMuxer
|
|
|
|
from .multiselect_communicator import MultiselectCommunicator
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
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):
|
2019-04-11 05:44:00 +08:00
|
|
|
print("negotiate")
|
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
|
|
|
|
"""
|
|
|
|
# Create a communicator to handle all communication across the stream
|
|
|
|
communicator = MultiselectCommunicator(stream)
|
|
|
|
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender pre handshake")
|
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
# Perform handshake to ensure multiselect protocol IDs match
|
|
|
|
await self.handshake(communicator)
|
|
|
|
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender post handshake")
|
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
# Read and respond to commands until a valid protocol ID is sent
|
|
|
|
while True:
|
|
|
|
# Read message
|
|
|
|
command = await communicator.read_stream_until_eof()
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender command " + command)
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
# Command is ls or a protocol
|
|
|
|
if command == "ls":
|
|
|
|
# TODO: handle ls command
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
protocol = command
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender checking protocol in handlers")
|
2018-11-29 02:51:50 +08:00
|
|
|
if protocol in self.handlers:
|
|
|
|
# Tell counterparty we have decided on a protocol
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender writing protocol " + protocol)
|
2018-11-29 02:51:50 +08:00
|
|
|
await communicator.write(protocol)
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender protocol written")
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
# Return the decided on protocol
|
|
|
|
return protocol, self.handlers[protocol]
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender protocol not found")
|
2018-11-29 02:51:50 +08:00
|
|
|
# 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
|
|
|
|
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender pre write " + MULTISELECT_PROTOCOL_ID)
|
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
# Send our MULTISELECT_PROTOCOL_ID to other party
|
|
|
|
await communicator.write(MULTISELECT_PROTOCOL_ID)
|
|
|
|
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender pre read")
|
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
# Read in the protocol ID from other party
|
|
|
|
handshake_contents = await communicator.read_stream_until_eof()
|
|
|
|
|
2019-04-11 05:44:00 +08:00
|
|
|
print("sender pre validate " + handshake_contents)
|
|
|
|
|
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-04-11 05:44:00 +08:00
|
|
|
print("sender multiselect protocol ID mismatch")
|
2018-11-29 02:51:50 +08:00
|
|
|
raise MultiselectError("multiselect protocol ID mismatch")
|
|
|
|
|
|
|
|
# Handshake succeeded if this point is reached
|
|
|
|
|
|
|
|
|
2019-01-10 09:24:41 +08:00
|
|
|
def validate_handshake(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
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
class MultiselectError(ValueError):
|
|
|
|
"""Raised when an error occurs in multiselect process"""
|