This commit is contained in:
zixuanzh
2018-11-12 13:02:49 -05:00
parent b4272918d9
commit c5c9d3e5c9
17 changed files with 66 additions and 84 deletions

View File

@ -1,6 +1,6 @@
import asyncio
from .raw_connection_interface import IRawConnection
class RawConnection(IRawConnection):
def __init__(self, ip, port, reader, writer):
@ -12,15 +12,3 @@ class RawConnection(IRawConnection):
def close(self):
self.writer.close()
# 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)

View File

@ -1,15 +1,7 @@
from abc import ABC, abstractmethod
from abc import ABC
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

View File

@ -1,5 +1,6 @@
from abc import ABC, abstractmethod
class INetwork(ABC):
@abstractmethod

View File

@ -1,10 +1,11 @@
import asyncio
from .net_stream_interface import INetStream
class NetStream(INetStream):
def __init__(self, muxed_stream):
self.muxed_stream = muxed_stream
self.protocol_id = None
def get_protocol(self):
"""
@ -26,12 +27,12 @@ class NetStream(INetStream):
"""
return await self.muxed_stream.read()
async def write(self, bytes):
async def write(self, data):
"""
write to stream
:return: number of bytes written
"""
return await self.muxed_stream.write(bytes)
return await self.muxed_stream.write(data)
def close(self):
"""

View File

@ -1,11 +1,7 @@
from abc import ABC, abstractmethod
class INetStream(ABC):
def __init__(self, peer_id, multi_addr, connection):
self.peer_id = peer_id
self.multi_addr = multi_addr
self.connection = connection
class INetStream(ABC):
@abstractmethod
def get_protocol(self):

View File

@ -4,6 +4,7 @@ from .stream.net_stream import NetStream
from .multiaddr import MultiAddr
from .connection.raw_connection import RawConnection
class Swarm(INetwork):
def __init__(self, my_peer_id, peerstore, upgrader):
@ -13,6 +14,7 @@ class Swarm(INetwork):
self.connections = dict()
self.listeners = dict()
self.stream_handlers = dict()
self.transport = None
def set_stream_handler(self, protocol_id, stream_handler):
"""
@ -37,11 +39,8 @@ class Swarm(INetwork):
multiaddr = addrs[0]
if peer_id in self.connections:
"""
If muxed connection already exists for peer_id,
set muxed connection equal to
existing muxed connection
"""
# If muxed connection already exists for peer_id,
# set muxed connection equal to existing muxed connection
muxed_conn = self.connections[peer_id]
else:
# Transport dials peer (gets back a raw conn)
@ -68,8 +67,7 @@ class Swarm(INetwork):
"""
:param *args: one or many multiaddrs to start listening on
:return: true if at least one success
"""
"""
For each multiaddr in args
Check if a listener for multiaddr exists already
If listener already exists, continue
@ -87,8 +85,10 @@ class Swarm(INetwork):
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)
# 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, False)
muxed_stream, stream_id, protocol_id = await muxed_conn.accept_stream()

View File

@ -1,4 +0,0 @@
host.go --> config.go
config.go: newNode --> swarm.go: newSwarm
newSwarm | initializes data stores