From 9e0a8062182cc5f69fd60c164a8a89a2ceb7e81e Mon Sep 17 00:00:00 2001 From: Chih Cheng Liang Date: Mon, 5 Aug 2019 11:17:38 +0800 Subject: [PATCH] move stream and connection interfaces to abc --- libp2p/network/network_interface.py | 2 +- libp2p/network/notifee_interface.py | 2 +- libp2p/network/stream/net_stream.py | 3 +- libp2p/network/stream/net_stream_interface.py | 2 +- libp2p/network/swarm.py | 3 +- libp2p/network/typing.py | 2 +- libp2p/pubsub/pubsub_notifee.py | 2 +- .../{muxed_connection_interface.py => abc.py} | 59 +++++++++++++++---- libp2p/stream_muxer/mplex/mplex.py | 3 +- libp2p/stream_muxer/mplex/mplex_stream.py | 3 +- libp2p/stream_muxer/muxed_stream_interface.py | 47 --------------- 11 files changed, 56 insertions(+), 72 deletions(-) rename libp2p/stream_muxer/{muxed_connection_interface.py => abc.py} (61%) delete mode 100644 libp2p/stream_muxer/muxed_stream_interface.py diff --git a/libp2p/network/network_interface.py b/libp2p/network/network_interface.py index 4989bdd..c0085e5 100644 --- a/libp2p/network/network_interface.py +++ b/libp2p/network/network_interface.py @@ -5,7 +5,7 @@ from multiaddr import Multiaddr from libp2p.peer.id import ID from libp2p.peer.peerstore import PeerStore -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn +from libp2p.stream_muxer.abc import IMuxedConn from libp2p.transport.listener_interface import IListener from .stream.net_stream_interface import INetStream diff --git a/libp2p/network/notifee_interface.py b/libp2p/network/notifee_interface.py index 5f303b8..2eadd49 100644 --- a/libp2p/network/notifee_interface.py +++ b/libp2p/network/notifee_interface.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING from multiaddr import Multiaddr from libp2p.network.stream.net_stream_interface import INetStream -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn +from libp2p.stream_muxer.abc import IMuxedConn if TYPE_CHECKING: from .network_interface import INetwork diff --git a/libp2p/network/stream/net_stream.py b/libp2p/network/stream/net_stream.py index 8ebe791..c5a7c2e 100644 --- a/libp2p/network/stream/net_stream.py +++ b/libp2p/network/stream/net_stream.py @@ -1,5 +1,4 @@ -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn -from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream +from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream from .net_stream_interface import INetStream diff --git a/libp2p/network/stream/net_stream_interface.py b/libp2p/network/stream/net_stream_interface.py index ca3858f..d3ac2ff 100644 --- a/libp2p/network/stream/net_stream_interface.py +++ b/libp2p/network/stream/net_stream_interface.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn +from libp2p.stream_muxer.abc import IMuxedConn class INetStream(ABC): diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index eccca25..c743fd0 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -8,8 +8,7 @@ from libp2p.peer.peerstore import PeerStore from libp2p.protocol_muxer.multiselect import Multiselect from libp2p.protocol_muxer.multiselect_client import MultiselectClient from libp2p.routing.interfaces import IPeerRouting -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn -from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream +from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream from libp2p.transport.listener_interface import IListener from libp2p.transport.transport_interface import ITransport from libp2p.transport.upgrader import TransportUpgrader diff --git a/libp2p/network/typing.py b/libp2p/network/typing.py index 55b577e..472510f 100644 --- a/libp2p/network/typing.py +++ b/libp2p/network/typing.py @@ -1,4 +1,4 @@ from typing import Awaitable, Callable -from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream +from libp2p.stream_muxer.abc import IMuxedStream GenericProtocolHandlerFn = Callable[[IMuxedStream], Awaitable[None]] diff --git a/libp2p/pubsub/pubsub_notifee.py b/libp2p/pubsub/pubsub_notifee.py index f8caa20..9d5132e 100644 --- a/libp2p/pubsub/pubsub_notifee.py +++ b/libp2p/pubsub/pubsub_notifee.py @@ -5,7 +5,7 @@ from multiaddr import Multiaddr from libp2p.network.network_interface import INetwork from libp2p.network.notifee_interface import INotifee from libp2p.network.stream.net_stream_interface import INetStream -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn +from libp2p.stream_muxer.abc import IMuxedConn if TYPE_CHECKING: import asyncio diff --git a/libp2p/stream_muxer/muxed_connection_interface.py b/libp2p/stream_muxer/abc.py similarity index 61% rename from libp2p/stream_muxer/muxed_connection_interface.py rename to libp2p/stream_muxer/abc.py index 9c54007..1e137eb 100644 --- a/libp2p/stream_muxer/muxed_connection_interface.py +++ b/libp2p/stream_muxer/abc.py @@ -1,16 +1,15 @@ from abc import ABC, abstractmethod -from multiaddr import Multiaddr -from libp2p.security.secure_conn_interface import ISecureConn -from libp2p.network.typing import GenericProtocolHandlerFn from libp2p.peer.id import ID +from libp2p.security.secure_conn_interface import ISecureConn from libp2p.stream_muxer.mplex.constants import HeaderTags +from multiaddr import Multiaddr from typing import TYPE_CHECKING if TYPE_CHECKING: - from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream - + # Prevent GenericProtocolHandlerFn introducing circular dependencies + from libp2p.network.typing import GenericProtocolHandlerFn # noqa: F401 class IMuxedConn(ABC): @@ -23,10 +22,7 @@ class IMuxedConn(ABC): @abstractmethod def __init__( - self, - conn: ISecureConn, - generic_protocol_handler: GenericProtocolHandlerFn, - peer_id: ID, + self, conn: ISecureConn, generic_protocol_handler: "GenericProtocolHandlerFn", peer_id: ID ) -> None: """ create a new muxed connection @@ -58,9 +54,7 @@ class IMuxedConn(ABC): """ @abstractmethod - async def open_stream( - self, protocol_id: str, multi_addr: Multiaddr - ) -> "IMuxedStream": + async def open_stream(self, protocol_id: str, multi_addr: Multiaddr) -> "IMuxedStream": """ creates a new muxed_stream :param protocol_id: protocol_id of stream @@ -82,3 +76,44 @@ class IMuxedConn(ABC): :param data: data to send in the message :param stream_id: stream the message is in """ + + +class IMuxedStream(ABC): + + mplex_conn: IMuxedConn + + @abstractmethod + async def read(self) -> bytes: + """ + reads from the underlying muxed_conn + :return: bytes of input + """ + + @abstractmethod + async def write(self, data: bytes) -> int: + """ + writes to the underlying muxed_conn + :return: number of bytes written + """ + + @abstractmethod + async def close(self) -> bool: + """ + close the underlying muxed_conn + :return: true if successful + """ + + @abstractmethod + async def reset(self) -> bool: + """ + closes both ends of the stream + tells this remote side to hang up + :return: true if successful + """ + + @abstractmethod + def set_deadline(self, ttl: float) -> bool: + """ + set deadline for muxed stream + :return: a new stream + """ diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index c359ee3..f113de1 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -6,8 +6,7 @@ from libp2p.security.secure_conn_interface import ISecureConn from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.network.typing import GenericProtocolHandlerFn from libp2p.peer.id import ID -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn -from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream +from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream from .constants import HeaderTags from .utils import encode_uvarint, decode_uvarint_from_stream diff --git a/libp2p/stream_muxer/mplex/mplex_stream.py b/libp2p/stream_muxer/mplex/mplex_stream.py index d90a593..3b8ceda 100644 --- a/libp2p/stream_muxer/mplex/mplex_stream.py +++ b/libp2p/stream_muxer/mplex/mplex_stream.py @@ -1,6 +1,5 @@ import asyncio -from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream -from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn +from libp2p.stream_muxer.abc import IMuxedStream, IMuxedConn from .constants import HeaderTags diff --git a/libp2p/stream_muxer/muxed_stream_interface.py b/libp2p/stream_muxer/muxed_stream_interface.py deleted file mode 100644 index 141ba9f..0000000 --- a/libp2p/stream_muxer/muxed_stream_interface.py +++ /dev/null @@ -1,47 +0,0 @@ -from abc import ABC, abstractmethod -from typing import TYPE_CHECKING - - -if TYPE_CHECKING: - from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn - - -class IMuxedStream(ABC): - - mplex_conn: "IMuxedConn" - - @abstractmethod - async def read(self) -> bytes: - """ - reads from the underlying muxed_conn - :return: bytes of input - """ - - @abstractmethod - async def write(self, data: bytes) -> int: - """ - writes to the underlying muxed_conn - :return: number of bytes written - """ - - @abstractmethod - async def close(self) -> bool: - """ - close the underlying muxed_conn - :return: true if successful - """ - - @abstractmethod - async def reset(self) -> bool: - """ - closes both ends of the stream - tells this remote side to hang up - :return: true if successful - """ - - @abstractmethod - def set_deadline(self, ttl: float) -> bool: - """ - set deadline for muxed stream - :return: a new stream - """