diff --git a/transport/connection/__init__.py b/network/connection/__init__.py similarity index 100% rename from transport/connection/__init__.py rename to network/connection/__init__.py diff --git a/transport/connection/raw_connection.py b/network/connection/raw_connection.py similarity index 100% rename from transport/connection/raw_connection.py rename to network/connection/raw_connection.py diff --git a/transport/connection/raw_connection_interface.py b/network/connection/raw_connection_interface.py similarity index 100% rename from transport/connection/raw_connection_interface.py rename to network/connection/raw_connection_interface.py diff --git a/network/swarm.py b/network/swarm.py index edd4446a..e1ab9553 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -4,7 +4,7 @@ from transport.connection.raw_connection import RawConnection class Swarm(INetwork): - def __init__(self, my_peer_id, peerstore): + def __init__(self, my_peer_id, peerstore, upgrader): self.my_peer_id = my_peer_id self.peerstore = peerstore self.connections = {} @@ -18,19 +18,17 @@ class Swarm(INetwork): def new_stream(self, peer_id, protocol_id): """ - Determine if a connection to peer_id already exists - If a connection to peer_id exists, then - c = existing connection, - otherwise c = new muxed connection to peer_id - s = c.open_stream(protocol_id) - return s - :param peer_id: peer_id of destination :param protocol_id: protocol id :return: stream instance """ muxed_connection = None if peer_id in self.connections: + """ + If muxed connection already exists for peer_id, + set muxed connection equal to + existing muxed connection + """ muxed_connection = self.connections[peer_id] else: addrs = self.peerstore.addrs(peer_id) diff --git a/transport/stream/__init__.py b/transport/stream/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/transport/stream/stream.py b/transport/stream/stream.py deleted file mode 100644 index 34daf97f..00000000 --- a/transport/stream/stream.py +++ /dev/null @@ -1,58 +0,0 @@ -import asyncio -from .stream_interface import IStream - -class Stream(IStream): - - def __init__(self, peer_id, multi_addr, connection): - IStream.__init__(self, peer_id, multi_addr, connection) - self.peer_id = peer_id - - self.multi_addr = multi_addr - - self.stream_ip = multi_addr.get_protocol_value("ip4") - self.stream_port = multi_addr.get_protocol_value("tcp") - - self.reader = connection.reader - self.writer = connection.writer - - # TODO should construct protocol id from constructor - self.protocol_id = None - - def get_protocol(self): - """ - :return: protocol id that stream runs on - """ - return self.protocol_id - - def set_protocol(self, protocol_id): - """ - :param protocol_id: protocol id that stream runs on - :return: true if successful - """ - self.protocol_id = protocol_id - - def read(self): - """ - read from stream - :return: bytes of input - """ - return self.reader.read(-1) - - def write(self, _bytes): - """ - write to stream - :return: number of bytes written - """ - return self.write_to_stream(_bytes) - - async def write_to_stream(self, _bytes): - to_return = self.writer.write(_bytes) - await self.writer.drain() - return to_return - - def close(self): - """ - close stream - :return: true if successful - """ - self.writer.close() diff --git a/transport/stream/stream_interface.py b/transport/stream/stream_interface.py deleted file mode 100644 index 6d63a2f0..00000000 --- a/transport/stream/stream_interface.py +++ /dev/null @@ -1,47 +0,0 @@ -from abc import ABC, abstractmethod - -class IStream(ABC): - - def __init__(self, peer_id, multi_addr, connection): - self.peer_id = peer_id - self.multi_addr = multi_addr - self.connection = connection - - @abstractmethod - def get_protocol(self): - """ - :return: protocol id that stream runs on - """ - pass - - @abstractmethod - def set_protocol(self, protocol_id): - """ - :param protocol_id: protocol id that stream runs on - :return: true if successful - """ - pass - - @abstractmethod - def read(self): - """ - read from stream - :return: bytes of input - """ - pass - - @abstractmethod - def write(self, _bytes): - """ - write to stream - :return: number of bytes written - """ - pass - - @abstractmethod - def close(self): - """ - close stream - :return: true if successful - """ - pass diff --git a/transport/upgrader.py b/transport/upgrader.py index 64014244..e70d4377 100644 --- a/transport/upgrader.py +++ b/transport/upgrader.py @@ -1,18 +1,24 @@ class TransportUpgrader(object): - def __init__(self, secOpt, muxerOpt): - self.sec = secOpt - self.muxer = muxerOpt + def __init__(self, secOpt, muxerOpt): + self.sec = secOpt + self.muxer = muxerOpt - def upgrade_listener(self, transport, listeners): - """ - upgrade multiaddr listeners to libp2p-transport listeners + def upgrade_listener(self, transport, listeners): + """ + upgrade multiaddr listeners to libp2p-transport listeners - """ - pass - - def upgrade_security(self): - pass + """ + pass + + def upgrade_security(self): + pass - def upgrade_muxer(self): - pass \ No newline at end of file + def upgrade_connection(self, conn): + """ + upgrade raw connection to muxed connection + """ + # For PoC, no security + # Default to mplex + pass + \ No newline at end of file