py-libp2p/network/swarm.py

124 lines
4.4 KiB
Python
Raw Normal View History

from peer.id import ID
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-11-12 09:29:17 +08:00
from .multiaddr import MultiAddr
from .connection.raw_connection import RawConnection
2018-10-15 01:47:06 +08:00
2018-10-15 13:52:25 +08:00
class Swarm(INetwork):
# pylint: disable=too-many-instance-attributes, cell-var-from-loop
2018-10-15 13:52:25 +08:00
def __init__(self, my_peer_id, peerstore, upgrader):
2018-11-19 00:22:56 +08:00
self._my_peer_id = my_peer_id
self.self_id = ID(my_peer_id)
2018-11-12 01:36:15 +08:00
self.peerstore = peerstore
2018-11-12 05:42:10 +08:00
self.upgrader = upgrader
2018-11-12 06:10:37 +08:00
self.connections = dict()
self.listeners = dict()
2018-11-12 09:29:17 +08:00
self.stream_handlers = dict()
2018-11-13 02:02:49 +08:00
self.transport = None
2018-10-15 13:52:25 +08:00
2018-11-19 00:22:56 +08:00
def get_peer_id(self):
return self.self_id
2018-11-19 00:22:56 +08:00
2018-11-12 09:29:17 +08:00
def set_stream_handler(self, protocol_id, stream_handler):
2018-10-15 13:52:25 +08:00
"""
2018-11-12 09:29:17 +08:00
:param protocol_id: protocol id used on stream
2018-10-15 13:52:25 +08:00
:param stream_handler: a stream handler instance
:return: true if successful
"""
2018-11-12 09:29:17 +08:00
self.stream_handlers[protocol_id] = stream_handler
2018-10-15 13:52:25 +08:00
2018-11-13 00:00:43 +08:00
async def new_stream(self, peer_id, protocol_id):
2018-11-01 05:40:01 +08:00
"""
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-13 00:00:43 +08:00
# Get peer info from peer store
addrs = self.peerstore.addrs(peer_id)
if not addrs:
raise SwarmException("No known addresses to peer")
multiaddr = addrs[0]
2018-11-01 05:40:01 +08:00
if peer_id in self.connections:
2018-11-13 02:02:49 +08:00
# If muxed connection already exists for peer_id,
# set muxed connection equal to 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
# Transport dials peer (gets back a raw conn)
2018-11-13 00:00:43 +08:00
raw_conn = await self.transport.dial(MultiAddr(multiaddr))
2018-11-12 05:42:10 +08:00
# Use upgrader to upgrade raw conn to muxed conn
2018-11-12 08:37:07 +08:00
muxed_conn = self.upgrader.upgrade_connection(raw_conn, True)
2018-11-12 05:42:10 +08:00
# Store muxed connection in connections
self.connections[peer_id] = muxed_conn
# Use muxed conn to open stream, which returns
# a muxed stream
2018-11-26 00:05:56 +08:00
muxed_stream = await muxed_conn.open_stream(protocol_id, peer_id, multiaddr)
2018-11-12 05:42:10 +08:00
# Create a net stream
net_stream = NetStream(muxed_stream)
return net_stream
2018-10-22 01:51:55 +08:00
2018-11-13 00:00:43 +08:00
async def listen(self, *args):
2018-10-22 01:51:55 +08:00
"""
:param *args: one or many multiaddrs to start listening on
:return: true if at least one success
2018-11-13 02:02:49 +08:00
2018-11-13 00:00:43 +08:00
For each multiaddr in args
Check if a listener for multiaddr exists already
If listener already exists, continue
Otherwise:
Capture multiaddr in conn handler
Have conn handler delegate to stream handler
Call listener listen with the multiaddr
Map multiaddr to listener
"""
2018-11-12 09:29:17 +08:00
for multiaddr_str in args:
if multiaddr_str in self.listeners:
return True
multiaddr = MultiAddr(multiaddr_str)
multiaddr_dict = multiaddr.to_options()
2018-11-13 00:00:43 +08:00
async def conn_handler(reader, writer):
2018-11-13 02:02:49 +08:00
# Upgrade reader/write to a net_stream and pass \
# to appropriate stream handler (using multiaddr)
raw_conn = RawConnection(multiaddr_dict['host'], \
multiaddr_dict['port'], reader, writer)
2018-11-12 09:41:06 +08:00
muxed_conn = self.upgrader.upgrade_connection(raw_conn, False)
2018-11-12 09:29:17 +08:00
muxed_stream, _, protocol_id = await muxed_conn.accept_stream()
2018-11-12 09:29:17 +08:00
net_stream = NetStream(muxed_stream)
net_stream.set_protocol(protocol_id)
# Give to stream handler
# TODO: handle case when stream handler is set
2018-11-13 00:00:43 +08:00
# TODO: handle case of multiple protocols over same raw connection
await self.stream_handlers[protocol_id](net_stream)
2018-11-12 09:29:17 +08:00
try:
# Success
listener = self.transport.create_listener(conn_handler)
self.listeners[multiaddr_str] = listener
2018-11-13 00:00:43 +08:00
await listener.listen(multiaddr)
2018-11-12 09:29:17 +08:00
return True
except IOError:
# Failed. Continue looping.
2018-11-13 00:00:43 +08:00
print("Failed to connect to: " + str(multiaddr))
2018-11-12 09:29:17 +08:00
# No multiaddr succeeded
return False
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