2018-11-12 05:42:10 +08:00
|
|
|
import uuid
|
2018-10-15 13:52:25 +08:00
|
|
|
from .network_interface import INetwork
|
2018-11-12 05:42:10 +08:00
|
|
|
from .stream.net_stream import NetStream
|
2018-10-15 01:47:06 +08:00
|
|
|
|
2018-10-15 13:52:25 +08:00
|
|
|
class Swarm(INetwork):
|
|
|
|
|
2018-11-12 05:09:37 +08:00
|
|
|
def __init__(self, my_peer_id, peerstore, upgrader):
|
2018-10-15 13:52:25 +08:00
|
|
|
self.my_peer_id = my_peer_id
|
2018-11-12 01:36:15 +08:00
|
|
|
self.peerstore = peerstore
|
2018-11-01 05:40:01 +08:00
|
|
|
self.connections = {}
|
2018-11-12 05:42:10 +08:00
|
|
|
self.upgrader = upgrader
|
2018-10-15 13:52:25 +08:00
|
|
|
|
|
|
|
def set_stream_handler(self, stream_handler):
|
|
|
|
"""
|
|
|
|
:param stream_handler: a stream handler instance
|
|
|
|
:return: true if successful
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2018-11-01 05:40:01 +08:00
|
|
|
def new_stream(self, peer_id, protocol_id):
|
|
|
|
"""
|
2018-11-01 06:31:52 +08:00
|
|
|
:param peer_id: peer_id of destination
|
|
|
|
:param protocol_id: protocol id
|
2018-11-12 05:42:10 +08:00
|
|
|
:return: net stream instance
|
2018-11-01 05:40:01 +08:00
|
|
|
"""
|
2018-11-12 05:42:10 +08:00
|
|
|
muxed_conn = None
|
2018-11-01 05:40:01 +08:00
|
|
|
if peer_id in self.connections:
|
2018-11-12 05:09:37 +08:00
|
|
|
"""
|
|
|
|
If muxed connection already exists for peer_id,
|
2018-11-12 05:42:10 +08:00
|
|
|
set muxed connection equal to
|
2018-11-12 05:09:37 +08:00
|
|
|
existing muxed connection
|
2018-11-12 05:42:10 +08:00
|
|
|
"""
|
|
|
|
muxed_conn = self.connections[peer_id]
|
2018-11-01 05:40:01 +08:00
|
|
|
else:
|
2018-11-12 05:42:10 +08:00
|
|
|
# Get peer info from peer store
|
2018-11-12 01:36:15 +08:00
|
|
|
addrs = self.peerstore.addrs(peer_id)
|
2018-11-12 05:42:10 +08:00
|
|
|
|
|
|
|
# Transport dials peer (gets back a raw conn)
|
|
|
|
if not addrs:
|
|
|
|
raise SwarmException("No known addresses to peer")
|
|
|
|
first_addr = addrs[0]
|
|
|
|
raw_conn = self.transport.dial(first_addr)
|
|
|
|
|
|
|
|
# Use upgrader to upgrade raw conn to muxed conn
|
|
|
|
muxed_conn = self.upgrader.upgrade_connection(raw_conn)
|
|
|
|
|
|
|
|
# Store muxed connection in connections
|
|
|
|
self.connections[peer_id] = muxed_conn
|
|
|
|
|
|
|
|
# Use muxed conn to open stream, which returns
|
|
|
|
# a muxed stream
|
|
|
|
stream_id = str(uuid.uuid4())
|
|
|
|
muxed_stream = muxed_conn.open_stream(protocol_id, stream_id, peer_id, first_addr)
|
|
|
|
|
|
|
|
# Create a net stream
|
|
|
|
net_stream = NetStream(muxed_stream)
|
|
|
|
|
|
|
|
return net_stream
|
2018-10-22 01:51:55 +08:00
|
|
|
|
|
|
|
def listen(self, *args):
|
|
|
|
"""
|
|
|
|
:param *args: one or many multiaddrs to start listening on
|
|
|
|
:return: true if at least one success
|
|
|
|
"""
|
|
|
|
pass
|
2018-11-12 05:42:10 +08:00
|
|
|
|
|
|
|
def add_transport(self, transport):
|
|
|
|
# TODO: Support more than one transport
|
|
|
|
self.transport = transport
|
|
|
|
|
|
|
|
class SwarmException(Exception):
|
|
|
|
pass
|