2019-02-25 09:58:23 +08:00
|
|
|
import asyncio
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
from libp2p.protocol_muxer.multiselect_client import MultiselectClient
|
|
|
|
from libp2p.protocol_muxer.multiselect import Multiselect
|
2019-02-25 09:58:23 +08:00
|
|
|
from libp2p.peer.id import id_b58_decode
|
2019-01-10 02:38:56 +08:00
|
|
|
|
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 .connection.raw_connection import RawConnection
|
2018-10-15 01:47:06 +08:00
|
|
|
|
2018-10-15 13:52:25 +08:00
|
|
|
class Swarm(INetwork):
|
2018-11-27 07:24:29 +08:00
|
|
|
# pylint: disable=too-many-instance-attributes, cell-var-from-loop
|
2018-10-15 13:52:25 +08:00
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
def __init__(self, peer_id, peerstore, upgrader):
|
|
|
|
self.self_id = 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-29 02:51:50 +08:00
|
|
|
# Protocol muxing
|
|
|
|
self.multiselect = Multiselect()
|
|
|
|
self.multiselect_client = MultiselectClient()
|
|
|
|
|
2019-03-01 07:18:58 +08:00
|
|
|
# Create Notifee array
|
|
|
|
self.notifees = []
|
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
# Create generic protocol handler
|
|
|
|
self.generic_protocol_handler = create_generic_protocol_handler(self)
|
|
|
|
|
2018-11-19 00:22:56 +08:00
|
|
|
def get_peer_id(self):
|
2018-11-27 07:24:29 +08:00
|
|
|
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-29 02:51:50 +08:00
|
|
|
self.multiselect.add_handler(protocol_id, stream_handler)
|
|
|
|
return True
|
2018-10-15 13:52:25 +08:00
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
async def dial_peer(self, peer_id):
|
2018-11-01 05:40:01 +08:00
|
|
|
"""
|
2018-11-29 23:06:40 +08:00
|
|
|
dial_peer try to create a connection to peer_id
|
|
|
|
:param peer_id: peer if we want to dial
|
|
|
|
:raises SwarmException: raised when no address if found for peer_id
|
|
|
|
:return: muxed connection
|
2018-11-01 05:40:01 +08:00
|
|
|
"""
|
2018-11-29 23:06:40 +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")
|
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
# TODO: define logic to choose which address to use, or try them all ?
|
2018-11-13 00:00:43 +08:00
|
|
|
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:
|
2019-03-01 07:18:58 +08:00
|
|
|
# Dial peer (connection to peer does not yet exist)
|
2018-11-12 05:42:10 +08:00
|
|
|
# Transport dials peer (gets back a raw conn)
|
2019-02-25 09:58:23 +08:00
|
|
|
raw_conn = await self.transport.dial(multiaddr, self.self_id)
|
2018-11-12 05:42:10 +08:00
|
|
|
|
|
|
|
# Use upgrader to upgrade raw conn to muxed conn
|
2019-02-25 09:58:23 +08:00
|
|
|
muxed_conn = self.upgrader.upgrade_connection(raw_conn, self.generic_protocol_handler)
|
2018-11-12 05:42:10 +08:00
|
|
|
|
|
|
|
# Store muxed connection in connections
|
|
|
|
self.connections[peer_id] = muxed_conn
|
|
|
|
|
2019-03-01 07:18:58 +08:00
|
|
|
# Call notifiers since event occurred
|
|
|
|
for notifee in self.notifees:
|
|
|
|
notifee.connected(self, muxed_conn)
|
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
return muxed_conn
|
|
|
|
|
|
|
|
async def new_stream(self, peer_id, protocol_ids):
|
|
|
|
"""
|
|
|
|
:param peer_id: peer_id of destination
|
|
|
|
:param protocol_id: protocol id
|
|
|
|
:return: net stream instance
|
|
|
|
"""
|
|
|
|
# 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]
|
|
|
|
|
|
|
|
muxed_conn = await self.dial_peer(peer_id)
|
|
|
|
|
2018-11-12 05:42:10 +08:00
|
|
|
# Use muxed conn to open stream, which returns
|
|
|
|
# a muxed stream
|
2018-11-29 02:51:50 +08:00
|
|
|
# TODO: Remove protocol id from being passed into muxed_conn
|
2019-02-25 09:58:23 +08:00
|
|
|
muxed_stream = await muxed_conn.open_stream(protocol_ids[0], multiaddr)
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
# Perform protocol muxing to determine protocol to use
|
|
|
|
selected_protocol = await self.multiselect_client.select_one_of(protocol_ids, muxed_stream)
|
2018-11-12 05:42:10 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
# Create a net stream with the selected protocol
|
2018-11-12 05:42:10 +08:00
|
|
|
net_stream = NetStream(muxed_stream)
|
2018-11-29 02:51:50 +08:00
|
|
|
net_stream.set_protocol(selected_protocol)
|
2018-11-12 05:42:10 +08:00
|
|
|
|
2019-03-01 07:18:58 +08:00
|
|
|
# Call notifiers since event occurred
|
|
|
|
for notifee in self.notifees:
|
|
|
|
notifee.opened_stream(self, net_stream)
|
|
|
|
|
2018-11-12 05:42:10 +08:00
|
|
|
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-29 23:06:40 +08:00
|
|
|
for multiaddr in args:
|
|
|
|
if str(multiaddr) in self.listeners:
|
2018-11-12 09:29:17 +08:00
|
|
|
return True
|
|
|
|
|
2018-11-13 00:00:43 +08:00
|
|
|
async def conn_handler(reader, writer):
|
2019-02-25 09:58:23 +08:00
|
|
|
# Read in first message (should be peer_id of initiator) and ack
|
|
|
|
peer_id = id_b58_decode((await reader.read(1024)).decode())
|
|
|
|
|
|
|
|
writer.write("received peer id".encode())
|
|
|
|
await writer.drain()
|
|
|
|
|
2018-11-13 02:02:49 +08:00
|
|
|
# Upgrade reader/write to a net_stream and pass \
|
|
|
|
# to appropriate stream handler (using multiaddr)
|
2018-11-29 23:06:40 +08:00
|
|
|
raw_conn = RawConnection(multiaddr.value_for_protocol('ip4'),
|
2018-11-30 02:42:05 +08:00
|
|
|
multiaddr.value_for_protocol('tcp'), reader, writer, False)
|
2019-02-25 09:58:23 +08:00
|
|
|
muxed_conn = self.upgrader.upgrade_connection(raw_conn, \
|
|
|
|
self.generic_protocol_handler)
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
# Store muxed_conn with peer id
|
|
|
|
self.connections[peer_id] = muxed_conn
|
2018-11-12 09:29:17 +08:00
|
|
|
|
2019-03-01 07:18:58 +08:00
|
|
|
# Call notifiers since event occurred
|
|
|
|
for notifee in self.notifees:
|
|
|
|
notifee.connected(self, muxed_conn)
|
|
|
|
|
2018-11-12 09:29:17 +08:00
|
|
|
try:
|
|
|
|
# Success
|
|
|
|
listener = self.transport.create_listener(conn_handler)
|
2018-11-29 23:06:40 +08:00
|
|
|
self.listeners[str(multiaddr)] = listener
|
2018-11-13 00:00:43 +08:00
|
|
|
await listener.listen(multiaddr)
|
2019-03-01 07:18:58 +08:00
|
|
|
|
|
|
|
# Call notifiers since event occurred
|
|
|
|
for notifee in self.notifees:
|
|
|
|
notifee.listen(self, 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
|
|
|
|
2019-03-01 07:18:58 +08:00
|
|
|
def notify(self, notifee):
|
|
|
|
"""
|
|
|
|
:param notifee: object implementing Notifee interface
|
|
|
|
"""
|
|
|
|
# TODO: Add check to ensure notifee conforms to Notifee interface
|
|
|
|
self.notifees.append(notifee)
|
|
|
|
|
2018-11-12 05:42:10 +08:00
|
|
|
def add_transport(self, transport):
|
|
|
|
# TODO: Support more than one transport
|
|
|
|
self.transport = transport
|
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
def create_generic_protocol_handler(swarm):
|
|
|
|
"""
|
|
|
|
Create a generic protocol handler from the given swarm. We use swarm
|
|
|
|
to extract the multiselect module so that generic_protocol_handler
|
|
|
|
can use multiselect when generic_protocol_handler is called
|
|
|
|
from a different class
|
|
|
|
"""
|
|
|
|
multiselect = swarm.multiselect
|
|
|
|
|
|
|
|
async def generic_protocol_handler(muxed_stream):
|
|
|
|
# Perform protocol muxing to determine protocol to use
|
|
|
|
_, handler = await multiselect.negotiate(muxed_stream)
|
|
|
|
|
|
|
|
# Give to stream handler
|
|
|
|
asyncio.ensure_future(handler(muxed_stream))
|
|
|
|
|
|
|
|
return generic_protocol_handler
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2018-11-12 05:42:10 +08:00
|
|
|
class SwarmException(Exception):
|
|
|
|
pass
|