Protocol muxing (#82)

* Implement protocol muxing

* Integrate protocol muxing into new stream and listen's conn handler

* Fix bugs in protocol muxing

* Remove blank line

* Add comments and fix linting issues

* Fix order of parameters to select_one_of to match interface

* Use array of protocol ids in new stream instead of protocol id

* Add basic protocol muxer tests

* Add todo

* Modify new stream to take in protocol ids

* Add check to all tests to ensure protocol id is saved to net stream properly

* Lint tests

* Fix lint issues

* Add todo

* Modify port numbers in tests

* Fix linting issues

* Add more documentation to functions

* Add docs describing classes and fix indent error
This commit is contained in:
stuckinaboot
2018-11-28 13:51:50 -05:00
committed by GitHub
parent 3c14a609fc
commit 7fa674dee2
15 changed files with 438 additions and 19 deletions

View File

@ -18,11 +18,11 @@ class INetwork(ABC):
"""
@abstractmethod
def new_stream(self, peer_id, protocol_id):
def new_stream(self, peer_id, protocol_ids):
"""
:param peer_id: peer_id of destination
:param protocol_id: protocol id
:return: stream instance
:param protocol_ids: available protocol ids to use for stream
:return: net stream instance
"""
@abstractmethod

View File

@ -1,9 +1,12 @@
from peer.id import ID
from protocol_muxer.multiselect_client import MultiselectClient
from protocol_muxer.multiselect import Multiselect
from .network_interface import INetwork
from .stream.net_stream import NetStream
from .multiaddr import MultiAddr
from .connection.raw_connection import RawConnection
class Swarm(INetwork):
# pylint: disable=too-many-instance-attributes, cell-var-from-loop
@ -17,6 +20,10 @@ class Swarm(INetwork):
self.stream_handlers = dict()
self.transport = None
# Protocol muxing
self.multiselect = Multiselect()
self.multiselect_client = MultiselectClient()
def get_peer_id(self):
return self.self_id
@ -26,9 +33,10 @@ class Swarm(INetwork):
:param stream_handler: a stream handler instance
:return: true if successful
"""
self.stream_handlers[protocol_id] = stream_handler
self.multiselect.add_handler(protocol_id, stream_handler)
return True
async def new_stream(self, peer_id, protocol_id):
async def new_stream(self, peer_id, protocol_ids):
"""
:param peer_id: peer_id of destination
:param protocol_id: protocol id
@ -58,10 +66,15 @@ class Swarm(INetwork):
# Use muxed conn to open stream, which returns
# a muxed stream
muxed_stream = await muxed_conn.open_stream(protocol_id, peer_id, multiaddr)
# TODO: Remove protocol id from being passed into muxed_conn
muxed_stream = await muxed_conn.open_stream(protocol_ids[0], peer_id, multiaddr)
# Create a net stream
# Perform protocol muxing to determine protocol to use
selected_protocol = await self.multiselect_client.select_one_of(protocol_ids, muxed_stream)
# Create a net stream with the selected protocol
net_stream = NetStream(muxed_stream)
net_stream.set_protocol(selected_protocol)
return net_stream
@ -93,14 +106,20 @@ class Swarm(INetwork):
multiaddr_dict['port'], reader, writer)
muxed_conn = self.upgrader.upgrade_connection(raw_conn, False)
muxed_stream, _, protocol_id = await muxed_conn.accept_stream()
# TODO: Remove protocol id from muxed_conn accept stream or
# move protocol muxing into accept_stream
muxed_stream, _, _ = await muxed_conn.accept_stream()
# Perform protocol muxing to determine protocol to use
selected_protocol, handler = await self.multiselect.negotiate(muxed_stream)
net_stream = NetStream(muxed_stream)
net_stream.set_protocol(protocol_id)
net_stream.set_protocol(selected_protocol)
# Give to stream handler
# TODO: handle case when stream handler is set
# TODO: handle case of multiple protocols over same raw connection
await self.stream_handlers[protocol_id](net_stream)
await handler(net_stream)
try:
# Success