From 35f75c4dcd269929c89fcbaf0d86bf045ec1a99d Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Wed, 24 Apr 2019 22:36:09 -0400 Subject: [PATCH] move transport to swarm constructor --- libp2p/__init__.py | 16 +++++++++------- libp2p/network/swarm.py | 8 ++------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/libp2p/__init__.py b/libp2p/__init__.py index 2e1cf88..38a285a 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -67,16 +67,18 @@ def initialize_default_swarm( if not id_opt: id_opt = generate_id() + # TODO parse transport_opt to determine transport transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"] - transport = [multiaddr.Multiaddr(t) for t in transport_opt] - # TODO wire muxer up with swarm - # muxer = muxer_opt or ["mplex/6.7.0"] + transport = TCP() + + # TODO TransportUpgrader is not doing anything really + # TODO parse muxer and sec to pass into TransportUpgrader + muxer = muxer_opt or ["mplex/6.7.0"] sec = sec_opt or ["secio"] + upgrader = TransportUpgrader(sec, muxer) + peerstore = peerstore_opt or PeerStore() - upgrader = TransportUpgrader(sec, transport) - swarm_opt = Swarm(id_opt, peerstore, upgrader) - tcp = TCP() - swarm_opt.add_transport(tcp) + swarm_opt = Swarm(id_opt, peerstore, upgrader, transport) return swarm_opt diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index b30567b..ff62c52 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -12,14 +12,14 @@ from .connection.raw_connection import RawConnection class Swarm(INetwork): # pylint: disable=too-many-instance-attributes, cell-var-from-loop - def __init__(self, peer_id, peerstore, upgrader): + def __init__(self, peer_id, peerstore, upgrader, transport): self.self_id = peer_id self.peerstore = peerstore self.upgrader = upgrader + self.transport = transport self.connections = dict() self.listeners = dict() self.stream_handlers = dict() - self.transport = None # Protocol muxing self.multiselect = Multiselect() @@ -183,10 +183,6 @@ class Swarm(INetwork): return True return False - def add_transport(self, transport): - # TODO: Support more than one transport - self.transport = transport - def create_generic_protocol_handler(swarm): """ Create a generic protocol handler from the given swarm. We use swarm