From bab97f5648115869a76909ee28af2c3546945ce5 Mon Sep 17 00:00:00 2001 From: Stuckinaboot Date: Wed, 31 Oct 2018 22:40:01 +0100 Subject: [PATCH] Implement new stream --- network/network_interface.py | 3 ++- network/swarm.py | 29 +++++++++++++++++++++++++++-- network/walkthrough.txt | 4 ++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 network/walkthrough.txt diff --git a/network/network_interface.py b/network/network_interface.py index a7b6a69..51589eb 100644 --- a/network/network_interface.py +++ b/network/network_interface.py @@ -15,9 +15,10 @@ class INetwork(ABC): pass @abstractmethod - def new_stream(self, peer_id): + def new_stream(self, peer_id, protocol_id): """ :param peer_id: peer_id of destination + :param protocol_id: protocol id :return: stream instance """ pass diff --git a/network/swarm.py b/network/swarm.py index 50f7b31..6190e32 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -1,10 +1,13 @@ from .network_interface import INetwork +from ..connection.muxed_connection import MuxedConnection +from ..connection.raw_connection import RawConnection class Swarm(INetwork): def __init__(self, my_peer_id, peer_store): self.my_peer_id = my_peer_id self.peer_store = peer_store + self.connections = {} def set_stream_handler(self, stream_handler): """ @@ -13,12 +16,34 @@ class Swarm(INetwork): """ pass - def new_stream(self, peer_id): + def new_stream(self, peer_id, protocol_id): """ :param peer_id: peer_id of destination + :param protocol_id: protocol id :return: stream instance """ - pass + + """ + Determine if a connection to peer_id already exists + If a connection to peer_id exists, then + c = existing connection, + otherwise c = new muxed connection to peer_id + s = c.open_stream(protocol_id) + return s + """ + muxed_connection = None + if peer_id in self.connections: + muxed_connection = self.connections[peer_id] + else: + addrs = self.peer_store.addrs(peer_id) + ip = addrs.get_protocol_value("ip") + port = addrs.get_protocol_value("port") + if len(addrs) > 0: + conn = RawConnection(ip, port) + muxed_connection = MuxedConnection(conn, True) + else: + raise Exception("No IP and port in addr") + return muxed_connection.open_stream(protocol_id, "") def listen(self, *args): """ diff --git a/network/walkthrough.txt b/network/walkthrough.txt new file mode 100644 index 0000000..3183d2a --- /dev/null +++ b/network/walkthrough.txt @@ -0,0 +1,4 @@ +host.go --> config.go + config.go: newNode --> swarm.go: newSwarm + newSwarm | initializes data stores +