remove if TYPE_CHECKING as much as possible
This commit is contained in:
parent
4c9a930f84
commit
87ef2e4618
|
@ -1,40 +1,37 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import TYPE_CHECKING, Tuple, Dict
|
from typing import Tuple, Dict
|
||||||
|
|
||||||
|
from multiaddr import Multiaddr
|
||||||
|
from libp2p.security.secure_conn_interface import ISecureConn
|
||||||
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
||||||
|
from libp2p.network.swarm import GenericProtocolHandlerFn
|
||||||
|
from libp2p.peer.id import ID
|
||||||
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
|
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
|
||||||
|
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
|
||||||
|
|
||||||
from .constants import HeaderTags
|
from .constants import HeaderTags
|
||||||
from .utils import encode_uvarint, decode_uvarint_from_stream
|
from .utils import encode_uvarint, decode_uvarint_from_stream
|
||||||
from .mplex_stream import MplexStream
|
from .mplex_stream import MplexStream
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from multiaddr import Multiaddr
|
|
||||||
from libp2p.security.secure_conn_interface import ISecureConn
|
|
||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
||||||
from libp2p.network.swarm import GenericProtocolHandlerFn
|
|
||||||
from libp2p.peer.id import ID
|
|
||||||
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
|
|
||||||
|
|
||||||
|
|
||||||
class Mplex(IMuxedConn):
|
class Mplex(IMuxedConn):
|
||||||
"""
|
"""
|
||||||
reference: https://github.com/libp2p/go-mplex/blob/master/multiplex.go
|
reference: https://github.com/libp2p/go-mplex/blob/master/multiplex.go
|
||||||
"""
|
"""
|
||||||
|
|
||||||
secured_conn: "ISecureConn"
|
secured_conn: ISecureConn
|
||||||
raw_conn: "IRawConnection"
|
raw_conn: IRawConnection
|
||||||
initiator: bool
|
initiator: bool
|
||||||
generic_protocol_handler = None
|
generic_protocol_handler = None
|
||||||
peer_id: "ID"
|
peer_id: ID
|
||||||
buffers: Dict[int, asyncio.Queue[bytes]]
|
buffers: Dict[int, asyncio.Queue[bytes]]
|
||||||
stream_queue: asyncio.Queue[int]
|
stream_queue: asyncio.Queue[int]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
secured_conn: "ISecureConn",
|
secured_conn: ISecureConn,
|
||||||
generic_protocol_handler: "GenericProtocolHandlerFn",
|
generic_protocol_handler: GenericProtocolHandlerFn,
|
||||||
peer_id: "ID",
|
peer_id: ID,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
create a new muxed connection
|
create a new muxed connection
|
||||||
|
@ -95,8 +92,8 @@ class Mplex(IMuxedConn):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def open_stream(
|
async def open_stream(
|
||||||
self, protocol_id: str, multi_addr: "Multiaddr"
|
self, protocol_id: str, multi_addr: Multiaddr
|
||||||
) -> "IMuxedStream":
|
) -> IMuxedStream:
|
||||||
"""
|
"""
|
||||||
creates a new muxed_stream
|
creates a new muxed_stream
|
||||||
:param protocol_id: protocol_id of stream
|
:param protocol_id: protocol_id of stream
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
|
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
|
||||||
|
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
|
||||||
|
|
||||||
|
|
||||||
from .constants import HeaderTags
|
from .constants import HeaderTags
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
|
|
||||||
|
|
||||||
|
|
||||||
class MplexStream(IMuxedStream):
|
class MplexStream(IMuxedStream):
|
||||||
"""
|
"""
|
||||||
reference: https://github.com/libp2p/go-mplex/blob/master/stream.go
|
reference: https://github.com/libp2p/go-mplex/blob/master/stream.go
|
||||||
|
@ -16,16 +13,14 @@ class MplexStream(IMuxedStream):
|
||||||
|
|
||||||
stream_id: int
|
stream_id: int
|
||||||
initiator: bool
|
initiator: bool
|
||||||
mplex_conn: "IMuxedConn"
|
mplex_conn: IMuxedConn
|
||||||
read_deadline: float
|
read_deadline: float
|
||||||
write_deadline: float
|
write_deadline: float
|
||||||
local_closed: bool
|
local_closed: bool
|
||||||
remote_closed: bool
|
remote_closed: bool
|
||||||
stream_lock: asyncio.Lock
|
stream_lock: asyncio.Lock
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, stream_id: int, initiator: bool, mplex_conn: IMuxedConn) -> None:
|
||||||
self, stream_id: int, initiator: bool, mplex_conn: "IMuxedConn"
|
|
||||||
) -> None:
|
|
||||||
"""
|
"""
|
||||||
create new MuxedStream in muxer
|
create new MuxedStream in muxer
|
||||||
:param stream_id: stream stream id
|
:param stream_id: stream stream id
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from multiaddr import Multiaddr
|
||||||
|
from libp2p.security.secure_conn_interface import ISecureConn
|
||||||
if TYPE_CHECKING:
|
from libp2p.network.swarm import GenericProtocolHandlerFn
|
||||||
from multiaddr import Multiaddr
|
from libp2p.peer.id import ID
|
||||||
from libp2p.security.secure_conn_interface import ISecureConn
|
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
|
||||||
from libp2p.network.swarm import GenericProtocolHandlerFn
|
from libp2p.stream_muxer.mplex.constants import HeaderTags
|
||||||
from libp2p.peer.id import ID
|
|
||||||
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
|
|
||||||
from libp2p.stream_muxer.mplex.constants import HeaderTags
|
|
||||||
|
|
||||||
|
|
||||||
class IMuxedConn(ABC):
|
class IMuxedConn(ABC):
|
||||||
|
@ -17,14 +14,14 @@ class IMuxedConn(ABC):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
initiator: bool
|
initiator: bool
|
||||||
peer_id: "ID"
|
peer_id: ID
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
conn: "ISecureConn",
|
conn: ISecureConn,
|
||||||
generic_protocol_handler: "GenericProtocolHandlerFn",
|
generic_protocol_handler: GenericProtocolHandlerFn,
|
||||||
peer_id: "ID",
|
peer_id: ID,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
create a new muxed connection
|
create a new muxed connection
|
||||||
|
@ -57,8 +54,8 @@ class IMuxedConn(ABC):
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def open_stream(
|
async def open_stream(
|
||||||
self, protocol_id: str, multi_addr: "Multiaddr"
|
self, protocol_id: str, multi_addr: Multiaddr
|
||||||
) -> "IMuxedStream":
|
) -> IMuxedStream:
|
||||||
"""
|
"""
|
||||||
creates a new muxed_stream
|
creates a new muxed_stream
|
||||||
:param protocol_id: protocol_id of stream
|
:param protocol_id: protocol_id of stream
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import List
|
||||||
|
from multiaddr import Multiaddr
|
||||||
if TYPE_CHECKING:
|
|
||||||
from typing import List
|
|
||||||
from multiaddr import Multiaddr
|
|
||||||
|
|
||||||
|
|
||||||
class IListener(ABC):
|
class IListener(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def listen(self, maddr: "Multiaddr") -> bool:
|
async def listen(self, maddr: Multiaddr) -> bool:
|
||||||
"""
|
"""
|
||||||
put listener in listening mode and wait for incoming connections
|
put listener in listening mode and wait for incoming connections
|
||||||
:param maddr: multiaddr of peer
|
:param maddr: multiaddr of peer
|
||||||
|
@ -17,7 +14,7 @@ class IListener(ABC):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_addrs(self) -> "List[Multiaddr]":
|
def get_addrs(self) -> List[Multiaddr]:
|
||||||
"""
|
"""
|
||||||
retrieve list of addresses the listener is listening on
|
retrieve list of addresses the listener is listening on
|
||||||
:return: return list of addrs
|
:return: return list of addrs
|
||||||
|
|
|
@ -1,26 +1,23 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
from typing import List, Callable
|
||||||
from multiaddr import Multiaddr
|
from multiaddr import Multiaddr
|
||||||
|
|
||||||
|
from socket import socket
|
||||||
|
from libp2p.transport.typing import THandler
|
||||||
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
||||||
from libp2p.network.connection.raw_connection import RawConnection
|
from libp2p.network.connection.raw_connection import RawConnection
|
||||||
|
|
||||||
from libp2p.transport.listener_interface import IListener
|
from libp2p.transport.listener_interface import IListener
|
||||||
from libp2p.transport.transport_interface import ITransport
|
from libp2p.transport.transport_interface import ITransport
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, List, Callable
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from socket import socket
|
|
||||||
from libp2p.transport.typing import THandler
|
|
||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
||||||
|
|
||||||
|
|
||||||
class TCPListener(IListener):
|
class TCPListener(IListener):
|
||||||
multiaddrs: List[Multiaddr]
|
multiaddrs: List[Multiaddr]
|
||||||
server = None
|
server = None
|
||||||
handler = None
|
handler = None
|
||||||
|
|
||||||
def __init__(self, handler_function: "THandler" = None) -> None:
|
def __init__(self, handler_function: THandler = None) -> None:
|
||||||
self.multiaddrs = []
|
self.multiaddrs = []
|
||||||
self.server = None
|
self.server = None
|
||||||
self.handler = handler_function
|
self.handler = handler_function
|
||||||
|
@ -69,7 +66,7 @@ class TCP(ITransport):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.listener = TCPListener()
|
self.listener = TCPListener()
|
||||||
|
|
||||||
async def dial(self, maddr: Multiaddr, self_id: ID) -> "IRawConnection":
|
async def dial(self, maddr: Multiaddr, self_id: ID) -> IRawConnection:
|
||||||
"""
|
"""
|
||||||
dial a transport to peer listening on multiaddr
|
dial a transport to peer listening on multiaddr
|
||||||
:param maddr: multiaddr of peer
|
:param maddr: multiaddr of peer
|
||||||
|
@ -94,7 +91,7 @@ class TCP(ITransport):
|
||||||
|
|
||||||
return RawConnection(host, str(port), reader, writer, True)
|
return RawConnection(host, str(port), reader, writer, True)
|
||||||
|
|
||||||
def create_listener(self, handler_function: "THandler") -> TCPListener:
|
def create_listener(self, handler_function: THandler) -> TCPListener:
|
||||||
"""
|
"""
|
||||||
create listener on transport
|
create listener on transport
|
||||||
:param handler_function: a function called when a new connection is received
|
:param handler_function: a function called when a new connection is received
|
||||||
|
@ -104,5 +101,5 @@ class TCP(ITransport):
|
||||||
return TCPListener(handler_function)
|
return TCPListener(handler_function)
|
||||||
|
|
||||||
|
|
||||||
def _multiaddr_from_socket(socket: "socket") -> Multiaddr:
|
def _multiaddr_from_socket(socket: socket) -> Multiaddr:
|
||||||
return Multiaddr("/ip4/%s/tcp/%s" % socket.getsockname())
|
return Multiaddr("/ip4/%s/tcp/%s" % socket.getsockname())
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from multiaddr import Multiaddr
|
||||||
|
from libp2p.peer.id import ID
|
||||||
if TYPE_CHECKING:
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
||||||
from multiaddr import Multiaddr
|
from .listener_interface import IListener
|
||||||
from libp2p.peer.id import ID
|
from .typing import THandler
|
||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
||||||
from .listener_interface import IListener
|
|
||||||
from .typing import THandler
|
|
||||||
|
|
||||||
|
|
||||||
class ITransport(ABC):
|
class ITransport(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def dial(self, maddr: "Multiaddr", self_id: "ID") -> "IRawConnection":
|
async def dial(self, maddr: Multiaddr, self_id: ID) -> IRawConnection:
|
||||||
"""
|
"""
|
||||||
dial a transport to peer listening on multiaddr
|
dial a transport to peer listening on multiaddr
|
||||||
:param multiaddr: multiaddr of peer
|
:param multiaddr: multiaddr of peer
|
||||||
|
@ -21,7 +18,7 @@ class ITransport(ABC):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def create_listener(self, handler_function: "THandler") -> "IListener":
|
def create_listener(self, handler_function: THandler) -> IListener:
|
||||||
"""
|
"""
|
||||||
create listener on transport
|
create listener on transport
|
||||||
:param handler_function: a function called when a new conntion is received
|
:param handler_function: a function called when a new conntion is received
|
||||||
|
|
|
@ -1,27 +1,23 @@
|
||||||
from libp2p.security.security_multistream import SecurityMultistream
|
from typing import Dict, Sequence
|
||||||
|
|
||||||
from libp2p.stream_muxer.mplex.mplex import Mplex
|
from libp2p.stream_muxer.mplex.mplex import Mplex
|
||||||
|
from libp2p.security.security_multistream import SecurityMultistream, TProtocol
|
||||||
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
||||||
|
from libp2p.network.swarm import GenericProtocolHandlerFn
|
||||||
|
from libp2p.peer.id import ID
|
||||||
|
from libp2p.security.secure_conn_interface import ISecureConn
|
||||||
|
from libp2p.security.secure_transport_interface import ISecureTransport
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from .transport_interface import ITransport
|
||||||
|
from .listener_interface import IListener
|
||||||
if TYPE_CHECKING:
|
|
||||||
|
|
||||||
from typing import Dict, Sequence
|
|
||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
||||||
from libp2p.network.swarm import GenericProtocolHandlerFn
|
|
||||||
from libp2p.peer.id import ID
|
|
||||||
from libp2p.security.secure_conn_interface import ISecureConn
|
|
||||||
from libp2p.security.secure_transport_interface import ISecureTransport
|
|
||||||
from libp2p.security.security_multistream import TProtocol
|
|
||||||
from .transport_interface import ITransport
|
|
||||||
from .listener_interface import IListener
|
|
||||||
|
|
||||||
|
|
||||||
class TransportUpgrader:
|
class TransportUpgrader:
|
||||||
security_multistream: SecurityMultistream
|
security_multistream: SecurityMultistream
|
||||||
muxer: "Sequence[str]"
|
muxer: Sequence[str]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, secOpt: "Dict[TProtocol, ISecureTransport]", muxerOpt: "Sequence[str]"
|
self, secOpt: Dict[TProtocol, ISecureTransport], muxerOpt: Sequence[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
# Store security option
|
# Store security option
|
||||||
self.security_multistream = SecurityMultistream()
|
self.security_multistream = SecurityMultistream()
|
||||||
|
@ -31,15 +27,15 @@ class TransportUpgrader:
|
||||||
# Store muxer option
|
# Store muxer option
|
||||||
self.muxer = muxerOpt
|
self.muxer = muxerOpt
|
||||||
|
|
||||||
def upgrade_listener(self, transport: "ITransport", listeners: "IListener") -> None:
|
def upgrade_listener(self, transport: ITransport, listeners: IListener) -> None:
|
||||||
"""
|
"""
|
||||||
Upgrade multiaddr listeners to libp2p-transport listeners
|
Upgrade multiaddr listeners to libp2p-transport listeners
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def upgrade_security(
|
async def upgrade_security(
|
||||||
self, raw_conn: "IRawConnection", peer_id: "ID", initiator: bool
|
self, raw_conn: IRawConnection, peer_id: ID, initiator: bool
|
||||||
) -> "ISecureConn":
|
) -> ISecureConn:
|
||||||
"""
|
"""
|
||||||
Upgrade conn to be a secured connection
|
Upgrade conn to be a secured connection
|
||||||
"""
|
"""
|
||||||
|
@ -50,10 +46,10 @@ class TransportUpgrader:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def upgrade_connection(
|
def upgrade_connection(
|
||||||
conn: "IRawConnection",
|
conn: IRawConnection,
|
||||||
generic_protocol_handler: "GenericProtocolHandlerFn",
|
generic_protocol_handler: GenericProtocolHandlerFn,
|
||||||
peer_id: "ID",
|
peer_id: ID,
|
||||||
) -> "Mplex":
|
) -> Mplex:
|
||||||
"""
|
"""
|
||||||
Upgrade raw connection to muxed connection
|
Upgrade raw connection to muxed connection
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user