-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic support for multiaddr addresses and improvement around peer id #75
Changes from all commits
6e209e5
0bb3686
3ee5fb4
5a46566
37e862b
3d43726
eebcdef
0bd6e88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
from peer.id import ID | ||
from protocol_muxer.multiselect_client import MultiselectClient | ||
from protocol_muxer.multiselect import Multiselect | ||
from .network_interface import INetwork | ||
from .stream.net_stream import NetStream | ||
from .multiaddr import MultiAddr | ||
from .connection.raw_connection import RawConnection | ||
|
||
|
||
class Swarm(INetwork): | ||
# pylint: disable=too-many-instance-attributes, cell-var-from-loop | ||
|
||
def __init__(self, my_peer_id, peerstore, upgrader): | ||
self._my_peer_id = my_peer_id | ||
self.self_id = ID(my_peer_id) | ||
def __init__(self, peer_id, peerstore, upgrader): | ||
self.self_id = peer_id | ||
self.peerstore = peerstore | ||
self.upgrader = upgrader | ||
self.connections = dict() | ||
|
@@ -36,18 +33,21 @@ def set_stream_handler(self, protocol_id, stream_handler): | |
self.multiselect.add_handler(protocol_id, stream_handler) | ||
return True | ||
|
||
async def new_stream(self, peer_id, protocol_ids): | ||
async def dial_peer(self, peer_id): | ||
""" | ||
:param peer_id: peer_id of destination | ||
:param protocol_id: protocol id | ||
:return: net stream instance | ||
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 | ||
""" | ||
|
||
# Get peer info from peer store | ||
addrs = self.peerstore.addrs(peer_id) | ||
|
||
if not addrs: | ||
raise SwarmException("No known addresses to peer") | ||
|
||
# TODO: define logic to choose which address to use, or try them all ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we should try them all and use whichever address responds the fastest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what the go-libp2p repo is doing at the moment, and there are plans to upgrade it to a smarter dial. I forgot if it's called a traffic shaper or a dial manager. There is a bit of design freedom here. We should participate in the discussion with the rest of the community. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zixuanzh do you have a link to the issue where they discuss this dial manager ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zaibon this is the thread libp2p/go-libp2p-swarm#88, dial manager might be a make-up name. |
||
multiaddr = addrs[0] | ||
|
||
if peer_id in self.connections: | ||
|
@@ -56,14 +56,32 @@ async def new_stream(self, peer_id, protocol_ids): | |
muxed_conn = self.connections[peer_id] | ||
else: | ||
# Transport dials peer (gets back a raw conn) | ||
raw_conn = await self.transport.dial(MultiAddr(multiaddr)) | ||
raw_conn = await self.transport.dial(multiaddr) | ||
|
||
# Use upgrader to upgrade raw conn to muxed conn | ||
muxed_conn = self.upgrader.upgrade_connection(raw_conn, True) | ||
|
||
# Store muxed connection in connections | ||
self.connections[peer_id] = muxed_conn | ||
|
||
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) | ||
|
||
# Use muxed conn to open stream, which returns | ||
# a muxed stream | ||
# TODO: Remove protocol id from being passed into muxed_conn | ||
|
@@ -92,18 +110,15 @@ async def listen(self, *args): | |
Call listener listen with the multiaddr | ||
Map multiaddr to listener | ||
""" | ||
for multiaddr_str in args: | ||
if multiaddr_str in self.listeners: | ||
for multiaddr in args: | ||
if str(multiaddr) in self.listeners: | ||
return True | ||
|
||
multiaddr = MultiAddr(multiaddr_str) | ||
multiaddr_dict = multiaddr.to_options() | ||
|
||
async def conn_handler(reader, writer): | ||
# 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) | ||
raw_conn = RawConnection(multiaddr.value_for_protocol('ip4'), | ||
multiaddr.value_for_protocol('tcp'), reader, writer) | ||
muxed_conn = self.upgrader.upgrade_connection(raw_conn, False) | ||
|
||
# TODO: Remove protocol id from muxed_conn accept stream or | ||
|
@@ -124,7 +139,7 @@ async def conn_handler(reader, writer): | |
try: | ||
# Success | ||
listener = self.transport.create_listener(conn_handler) | ||
self.listeners[multiaddr_str] = listener | ||
self.listeners[str(multiaddr)] = listener | ||
await listener.listen(multiaddr) | ||
return True | ||
except IOError: | ||
|
@@ -138,5 +153,6 @@ def add_transport(self, transport): | |
# TODO: Support more than one transport | ||
self.transport = transport | ||
|
||
|
||
class SwarmException(Exception): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding reference if anyone is wondering where this comes from.