Implement new stream

This commit is contained in:
Stuckinaboot 2018-10-31 22:40:01 +01:00
parent 8959d491b2
commit bab97f5648
3 changed files with 33 additions and 3 deletions

View File

@ -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

View File

@ -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):
"""

4
network/walkthrough.txt Normal file
View File

@ -0,0 +1,4 @@
host.go --> config.go
config.go: newNode --> swarm.go: newSwarm
newSwarm | initializes data stores