Browse Source

Merge pull request #36 from zixuanzh/refactor

Reorganized for sprint
pull/37/head
Robert Zajac 6 years ago
committed by GitHub
parent
commit
eaafcae036
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      connection/raw_connection.py
  2. 8
      connection/raw_connection_interface.py
  3. 35
      libp2p/libp2p.py
  4. 0
      muxer/mplex/__init__.py
  5. 0
      muxer/mplex/muxed_connection.py
  6. 0
      muxer/mplex/muxed_connection_interface.py
  7. 0
      muxer/mplex/muxed_stream.py
  8. 0
      muxer/mplex/muxed_stream_interface.py
  9. 0
      muxer/mplex/smux_multiplex.py
  10. 0
      muxer/yamux/__init__.py
  11. 2
      network/swarm.py
  12. 0
      transport/connection/__init__.py
  13. 16
      transport/connection/raw_connection.py
  14. 15
      transport/connection/raw_connection_interface.py
  15. 0
      transport/listener_interface.py
  16. 0
      transport/stream/__init__.py
  17. 0
      transport/stream/stream.py
  18. 0
      transport/stream/stream_interface.py
  19. 54
      transport/tcp.py
  20. 0
      transport/tcp/tcp.py
  21. 18
      transport/upgrader.py

14
connection/raw_connection.py

@ -1,14 +0,0 @@
import asyncio
from .raw_connection_interface import IRawConnection
class RawConnection(IRawConnection):
def __init__(self, ip, port):
self.conn_ip = ip
self.conn_port = port
self.reader = None
self.writer = None
async def open_connection(self):
self.reader, self.writer = \
await asyncio.open_connection(self.conn_ip, self.conn_port)

8
connection/raw_connection_interface.py

@ -1,8 +0,0 @@
from abc import ABC, abstractmethod
class IRawConnection(ABC):
@abstractmethod
async def open_connection(self):
pass

35
libp2p/libp2p.py

@ -0,0 +1,35 @@
from .config import Config
from ..peer.peerstore import PeerStore
from ..network.swarm import Swarm
from ..host.basic_host import BasicHost
from ..transport.upgrader import TransportUpgrader
class Libp2p(object):
def __init__(self, idOpt, \
transportOpt = ["/ip4/0.0.0.0/tcp/0"], \
muxerOpt = ["mplex/6.7.0"], \
secOpt = ["secio"], \
peerstore = PeerStore()):
if idOpt:
self.idOpt = idOpt
else:
# TODO generate RSA public key pair
self.transportOpt = transportOpt
self.muxerOpt = muxerOpt
self.secOpt = secOpt
self.peerstore = peerstore
def new_node(self):
swarm = Swarm(self.id, self.peerstore)
host = BasicHost(swarm)
upgrader = TransportUpgrader(self.secOpt, self.transportOpt)
# TODO transport upgrade
# TODO listen on addrs
# TODO swarm add transports

0
connection/__init__.py → muxer/mplex/__init__.py

0
muxer/muxed_connection.py → muxer/mplex/muxed_connection.py

0
muxer/muxed_connection_interface.py → muxer/mplex/muxed_connection_interface.py

0
muxer/muxed_stream.py → muxer/mplex/muxed_stream.py

0
muxer/muxed_stream_interface.py → muxer/mplex/muxed_stream_interface.py

0
muxer/smux_multiplex.py → muxer/mplex/smux_multiplex.py

0
stream/__init__.py → muxer/yamux/__init__.py

2
network/swarm.py

@ -4,7 +4,7 @@ from ..connection.raw_connection import RawConnection
class Swarm(INetwork):
def __init__(self, my_peer_id, peer_store):
def __init__(self, my_peer_id, peerstore):
self.my_peer_id = my_peer_id
self.peer_store = peer_store
self.connections = {}

0
libp2p/config.py → transport/connection/__init__.py

16
transport/connection/raw_connection.py

@ -0,0 +1,16 @@
import asyncio
from .raw_connection_interface import IRawConnection
class RawConnection(IRawConnection):
def __init__(self, ip, port):
self.conn_ip = ip
self.conn_port = port
self.reader, self.writer = self.open_connection()
async def open_connection(self):
"""
opens a connection on self.ip and self.port
:return: a raw connection
"""
return await asyncio.open_connection(self.conn_ip, self.conn_port)

15
transport/connection/raw_connection_interface.py

@ -0,0 +1,15 @@
from abc import ABC, abstractmethod
class IRawConnection(ABC):
"""
A Raw Connection provides a Reader and a Writer
open_connection should return such a connection
"""
@abstractmethod
async def open_connection(self):
"""
opens a connection on ip and port
:return: a raw connection
"""
pass

0
network/listener_interface.py → transport/listener_interface.py

0
transport/stream/__init__.py

0
stream/stream.py → transport/stream/stream.py

0
stream/stream_interface.py → transport/stream/stream_interface.py

54
transport/tcp.py

@ -1,54 +0,0 @@
import asyncio
from .transport_interface import ITransport
from .listener_interface import IListener
class TCP(ITransport):
def __init__(self):
self.multiaddr = None
class Listener(IListener):
def listen(self, multiaddr):
"""
put listener in listening mode and wait for incoming connections
:param multiaddr: multiaddr of peer
:return: return True if successful
"""
pass
def get_addrs(self):
"""
retrieve list of addresses the listener is listening on
:return: return list of addrs
"""
pass
def close(self, options=None):
"""
close the listener such that no more connections
can be open on this transport instance
:param options: optional object potential with timeout
a timeout value in ms that fires and destroy all connections
:return: return True if successful
"""
pass
def dial(self, multiaddr, options=None):
"""
dial a transport to peer listening on multiaddr
:param multiaddr: multiaddr of peer
:param options: optional object
:return: list of multiaddrs
"""
pass
def create_listener(self, handler_function, options=None):
"""
create listener on transport
:param options: optional object with properties the listener must have
:param handler_function: a function called when a new conntion is received
that takes a connection as argument which implements interface-connection
:return: a listener object that implements listener_interface.py
"""
pass

0
network/tcp.py → transport/tcp/tcp.py

18
transport/upgrader.py

@ -0,0 +1,18 @@
class TransportUpgrader(object):
def __init__(self, secOpt, muxerOpt):
self.sec = secOpt
self.muxer = muxerOpt
def upgrade_listener(self, transport, listeners):
"""
upgrade multiaddr listeners to libp2p-transport listeners
"""
pass
def upgrade_security(self):
pass
def upgrade_muxer(self):
pass
Loading…
Cancel
Save