WIP swarm listen
This commit is contained in:
parent
bc59a051ab
commit
684cb90908
|
@ -55,6 +55,7 @@ class BasicHost(IHost):
|
||||||
:param proto_id: protocol id that stream runs on
|
:param proto_id: protocol id that stream runs on
|
||||||
:return: true if successful
|
:return: true if successful
|
||||||
"""
|
"""
|
||||||
|
# TODO: host should return a mux stream not a raw stream
|
||||||
stream = self.network.new_stream(peer_id)
|
stream = self.network.new_stream(peer_id)
|
||||||
stream.set_protocol(protocol_id)
|
stream.set_protocol(protocol_id)
|
||||||
return stream
|
return stream
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
from ..peer.peerstore import PeerStore
|
from peer.peerstore import PeerStore
|
||||||
from ..network.swarm import Swarm
|
from network.swarm import Swarm
|
||||||
from ..host.basic_host import BasicHost
|
from host.basic_host import BasicHost
|
||||||
from ..transport.upgrader import TransportUpgrader
|
from transport.upgrader import TransportUpgrader
|
||||||
from Crypto.PublicKey import RSA
|
from Crypto.PublicKey import RSA
|
||||||
|
|
||||||
class Libp2p(object):
|
class Libp2p(object):
|
||||||
|
|
||||||
def __init__(self, idOpt, \
|
def __init__(self, idOpt, \
|
||||||
transportOpt = ["/ip4/127.0.0.1/tcp/10000"], \
|
transportOpt = ["/ip4/127.0.0.1/tcp/10000/app/1.0.0"], \
|
||||||
muxerOpt = ["mplex/6.7.0"], \
|
muxerOpt = ["mplex/6.7.0"], \
|
||||||
secOpt = ["secio"], \
|
secOpt = ["secio"], \
|
||||||
peerstore = PeerStore()):
|
peerstore = PeerStore()):
|
||||||
|
@ -15,7 +15,6 @@ class Libp2p(object):
|
||||||
if idOpt:
|
if idOpt:
|
||||||
self.idOpt = idOpt
|
self.idOpt = idOpt
|
||||||
else:
|
else:
|
||||||
# TODO generate RSA public key pair
|
|
||||||
new_key = RSA.generate(2048, e=65537)
|
new_key = RSA.generate(2048, e=65537)
|
||||||
self.idOpt = new_key.publickey().exportKey("PEM")
|
self.idOpt = new_key.publickey().exportKey("PEM")
|
||||||
self.private_key = new_key.exportKey("PEM")
|
self.private_key = new_key.exportKey("PEM")
|
||||||
|
@ -29,7 +28,7 @@ class Libp2p(object):
|
||||||
|
|
||||||
swarm = Swarm(self.idOpt, self.peerstore)
|
swarm = Swarm(self.idOpt, self.peerstore)
|
||||||
host = BasicHost(swarm)
|
host = BasicHost(swarm)
|
||||||
upgrader = TransportUpgrader(self.secOpt, self.transportOpt)
|
upgrader = TransportUpgrader(self.secOpt, self.muxerOpt)
|
||||||
|
|
||||||
# TODO transport upgrade
|
# TODO transport upgrade
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,9 @@ from abc import ABC, abstractmethod
|
||||||
class INetwork(ABC):
|
class INetwork(ABC):
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def set_stream_handler(self, stream_handler):
|
def set_stream_handler(self, protocol_id, stream_handler):
|
||||||
"""
|
"""
|
||||||
|
:param protocol_id: protocol id used on stream
|
||||||
:param stream_handler: a stream handler instance
|
:param stream_handler: a stream handler instance
|
||||||
:return: true if successful
|
:return: true if successful
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import uuid
|
import uuid
|
||||||
from .network_interface import INetwork
|
from .network_interface import INetwork
|
||||||
from .stream.net_stream import NetStream
|
from .stream.net_stream import NetStream
|
||||||
|
from .multiaddr import MultiAddr
|
||||||
|
from .connection.raw_connection import RawConnection
|
||||||
|
|
||||||
class Swarm(INetwork):
|
class Swarm(INetwork):
|
||||||
|
|
||||||
|
@ -10,13 +12,15 @@ class Swarm(INetwork):
|
||||||
self.upgrader = upgrader
|
self.upgrader = upgrader
|
||||||
self.connections = dict()
|
self.connections = dict()
|
||||||
self.listeners = dict()
|
self.listeners = dict()
|
||||||
|
self.stream_handlers = dict()
|
||||||
|
|
||||||
def set_stream_handler(self, stream_handler):
|
def set_stream_handler(self, protocol_id, stream_handler):
|
||||||
"""
|
"""
|
||||||
|
:param protocol_id: protocol id used on stream
|
||||||
:param stream_handler: a stream handler instance
|
:param stream_handler: a stream handler instance
|
||||||
:return: true if successful
|
:return: true if successful
|
||||||
"""
|
"""
|
||||||
pass
|
self.stream_handlers[protocol_id] = stream_handler
|
||||||
|
|
||||||
def new_stream(self, peer_id, protocol_id):
|
def new_stream(self, peer_id, protocol_id):
|
||||||
"""
|
"""
|
||||||
|
@ -64,23 +68,47 @@ class Swarm(INetwork):
|
||||||
:return: true if at least one success
|
:return: true if at least one success
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Create a closure C that takes in a multiaddr and
|
|
||||||
# returns a function object O that takes in a reader and writer.
|
|
||||||
# This function O looks up the stream handler
|
|
||||||
# for the given protocol, creates the net_stream
|
|
||||||
# for the listener and calls the stream handler function
|
|
||||||
# passing in the net_stream
|
|
||||||
|
|
||||||
# For each multiaddr in args
|
# For each multiaddr in args
|
||||||
# Check if a listener for multiaddr exists already
|
# Check if a listener for multiaddr exists already
|
||||||
# If listener already exists, continue
|
# If listener already exists, continue
|
||||||
# Otherwise, do the following:
|
# Otherwise, do the following:
|
||||||
# Pass multiaddr into C and get back function H
|
# Pass multiaddr into conn handler
|
||||||
# listener = transport.create_listener(H)
|
# Have conn handler delegate to stream handler
|
||||||
# Call listener listen with the multiaddr
|
# Call listener listen with the multiaddr
|
||||||
# Map multiaddr to listener
|
# Map multiaddr to listener
|
||||||
|
|
||||||
return True
|
for multiaddr_str in args:
|
||||||
|
if multiaddr_str in self.listeners:
|
||||||
|
return True
|
||||||
|
|
||||||
|
multiaddr = MultiAddr(multiaddr_str)
|
||||||
|
multiaddr_dict = multiaddr.to_options()
|
||||||
|
|
||||||
|
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)
|
||||||
|
muxed_conn = self.upgrader.upgrade_connection(raw_conn)
|
||||||
|
|
||||||
|
# TODO: make generic protocols work
|
||||||
|
muxed_stream, stream_id, protocol_id = muxed_conn.accept_stream()
|
||||||
|
net_stream = NetStream(muxed_stream)
|
||||||
|
net_stream.set_protocol(protocol_id)
|
||||||
|
|
||||||
|
# Give to stream handler
|
||||||
|
# TODO: handle case when stream handler is set
|
||||||
|
self.stream_handlers[protocol_id](net_stream)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Success
|
||||||
|
listener = self.transport.create_listener(conn_handler)
|
||||||
|
listener.listen(multiaddr)
|
||||||
|
return True
|
||||||
|
except IOError:
|
||||||
|
# Failed. Continue looping.
|
||||||
|
print("Failed to connect to: " + multiaddr)
|
||||||
|
|
||||||
|
# No multiaddr succeeded
|
||||||
|
return False
|
||||||
|
|
||||||
def add_transport(self, transport):
|
def add_transport(self, transport):
|
||||||
# TODO: Support more than one transport
|
# TODO: Support more than one transport
|
||||||
|
|
|
@ -22,7 +22,7 @@ def test_simple_messages():
|
||||||
# associate the peer with local ip address (see default parameters of Libp2p())
|
# associate the peer with local ip address (see default parameters of Libp2p())
|
||||||
hostA.get_peerstore().add_addr("hostB", "/ip4/127.0.0.1/tcp/10000")
|
hostA.get_peerstore().add_addr("hostB", "/ip4/127.0.0.1/tcp/10000")
|
||||||
|
|
||||||
stream = hostA.new_stream("hostB", "/echo/1.0.0")
|
stream = hostA.new_stream("hostB", "/app/1.0.0")
|
||||||
message = "hello"
|
message = "hello"
|
||||||
stream.write(message.encode())
|
stream.write(message.encode())
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from transport.transport_interface import ITransport
|
from transport.transport_interface import ITransport
|
||||||
from transport.listener_interface import IListener
|
from transport.listener_interface import IListener
|
||||||
from transport.connection.raw_connection import RawConnection
|
from network.connection.raw_connection import RawConnection
|
||||||
|
|
||||||
class TCP(ITransport):
|
class TCP(ITransport):
|
||||||
|
|
||||||
|
@ -22,17 +22,16 @@ class TCP(ITransport):
|
||||||
:return: return True if successful
|
:return: return True if successful
|
||||||
"""
|
"""
|
||||||
# TODO check for exceptions
|
# TODO check for exceptions
|
||||||
_multiaddr = multiaddr
|
|
||||||
if "ipfs" in multiaddr.get_protocols():
|
if "ipfs" in multiaddr.get_protocols():
|
||||||
# ipfs_id = multiaddr.get_ipfs_id()
|
# ipfs_id = multiaddr.get_ipfs_id()
|
||||||
_multiaddr = multiaddr.remove_protocol("ipfs")
|
_multiaddr = multiaddr.remove_protocol("ipfs")
|
||||||
|
|
||||||
self.multiaddrs.append(_multiaddr)
|
self.multiaddrs.append(multiaddr)
|
||||||
_multiaddr_dict = _multiaddr.to_dict()
|
multiaddr_dict = _multiaddr.to_options()
|
||||||
_loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
_coroutine = asyncio.start_server(self.handler, _multiaddr_dict.host,\
|
coroutine = asyncio.start_server(self.handler, multiaddr_dict.host,\
|
||||||
_multiaddr_dict.port, loop=_loop)
|
multiaddr_dict.port, loop=loop)
|
||||||
self.server = _loop.run_until_complete(_coroutine)
|
self.server = loop.run_until_complete(coroutine)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_addrs(self):
|
def get_addrs(self):
|
||||||
|
@ -72,7 +71,6 @@ class TCP(ITransport):
|
||||||
port = _multiaddr_dict.port
|
port = _multiaddr_dict.port
|
||||||
reader, writer = open_conn(host, port)
|
reader, writer = open_conn(host, port)
|
||||||
return RawConnection(host, port, reader, writer)
|
return RawConnection(host, port, reader, writer)
|
||||||
# TODO dial behavior not fully understood
|
|
||||||
|
|
||||||
def create_listener(self, handler_function, options=None):
|
def create_listener(self, handler_function, options=None):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user