typed muxed_connection_interface.py

This commit is contained in:
Chih Cheng Liang 2019-08-02 18:03:34 +08:00
parent b64ed9fd6f
commit dadac423f2
No known key found for this signature in database
GPG Key ID: C86B5E6612B1487A
2 changed files with 22 additions and 11 deletions

View File

@ -42,7 +42,7 @@ class Mplex(IMuxedConn):
for new muxed streams
:param peer_id: peer_id of peer the connection is to
"""
super(Mplex, self).__init__(secured_conn, generic_protocol_handler, peer_id)
super().__init__(secured_conn, generic_protocol_handler, peer_id)
self.secured_conn = secured_conn
self.raw_conn = secured_conn.get_conn()
@ -111,7 +111,6 @@ class Mplex(IMuxedConn):
async def accept_stream(self) -> None:
"""
accepts a muxed stream opened by the other end
:return: the accepted stream
"""
stream_id = await self.stream_queue.get()
stream = MplexStream(stream_id, False, self)

View File

@ -1,6 +1,13 @@
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from multiaddr import Multiaddr
from libp2p.security.secure_conn_interface import ISecureConn
from libp2p.network.swarm import GenericProtocolHandlerFn
from libp2p.peer.id import ID
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
class IMuxedConn(ABC):
@ -9,10 +16,15 @@ class IMuxedConn(ABC):
"""
initiator: bool
peer_id: ID
peer_id: "ID"
@abstractmethod
def __init__(self, conn, generic_protocol_handler, peer_id):
def __init__(
self,
conn: "ISecureConn",
generic_protocol_handler: "GenericProtocolHandlerFn",
peer_id: "ID",
) -> None:
"""
create a new muxed connection
:param conn: an instance of secured connection
@ -22,21 +34,22 @@ class IMuxedConn(ABC):
"""
@abstractmethod
def close(self):
def close(self) -> None:
"""
close connection
:return: true if successful
"""
@abstractmethod
def is_closed(self):
def is_closed(self) -> bool:
"""
check connection is fully closed
:return: true if successful
"""
@abstractmethod
def open_stream(self, protocol_id, multi_addr):
async def open_stream(
self, protocol_id: str, multi_addr: "Multiaddr"
) -> "IMuxedStream":
"""
creates a new muxed_stream
:param protocol_id: protocol_id of stream
@ -45,8 +58,7 @@ class IMuxedConn(ABC):
"""
@abstractmethod
def accept_stream(self):
async def accept_stream(self) -> None:
"""
accepts a muxed stream opened by the other end
:return: the accepted stream
"""