mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-03-24 06:01:26 +00:00
Refactor interop tests and factories
- Add `close` and `disconnect` in `Host` - Add `close` and `close_peer` in `Network` - Change `IListener.close` to async, to await for server's closing - Add factories for security transports, and modify `HostFactory`
This commit is contained in:
@ -107,3 +107,9 @@ class BasicHost(IHost):
|
||||
return
|
||||
|
||||
await self._network.dial_peer(peer_info.peer_id)
|
||||
|
||||
async def disconnect(self, peer_id: ID) -> None:
|
||||
await self._network.close_peer(peer_id)
|
||||
|
||||
async def close(self) -> None:
|
||||
await self._network.close()
|
||||
|
||||
@ -71,3 +71,11 @@ class IHost(ABC):
|
||||
:param peer_info: peer_info of the host we want to connect to
|
||||
:type peer_info: peer.peerinfo.PeerInfo
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def disconnect(self, peer_id: ID) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def close(self) -> None:
|
||||
pass
|
||||
|
||||
@ -70,3 +70,11 @@ class INetwork(ABC):
|
||||
:param notifee: object implementing Notifee interface
|
||||
:return: true if notifee registered successfully, false otherwise
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def close(self) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def close_peer(self, peer_id: ID) -> None:
|
||||
pass
|
||||
|
||||
@ -264,12 +264,24 @@ class Swarm(INetwork):
|
||||
def add_router(self, router: IPeerRouting) -> None:
|
||||
self.router = router
|
||||
|
||||
# TODO: `tear_down`
|
||||
async def tear_down(self) -> None:
|
||||
# Reference: https://github.com/libp2p/go-libp2p-swarm/blob/8be680aef8dea0a4497283f2f98470c2aeae6b65/swarm.go#L118 # noqa: E501
|
||||
pass
|
||||
async def close(self) -> None:
|
||||
# TODO: Prevent from new listeners and conns being added.
|
||||
# Reference: https://github.com/libp2p/go-libp2p-swarm/blob/8be680aef8dea0a4497283f2f98470c2aeae6b65/swarm.go#L124-L134 # noqa: E501
|
||||
|
||||
# TODO: `disconnect`?
|
||||
# Close listeners
|
||||
await asyncio.gather(
|
||||
*[listener.close() for listener in self.listeners.values()]
|
||||
)
|
||||
|
||||
# Close connections
|
||||
await asyncio.gather(
|
||||
*[connection.close() for connection in self.connections.values()]
|
||||
)
|
||||
|
||||
async def close_peer(self, peer_id: ID) -> None:
|
||||
connection = self.connections[peer_id]
|
||||
del self.connections[peer_id]
|
||||
await connection.close()
|
||||
|
||||
|
||||
def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn:
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
from typing import Dict, Optional, Tuple
|
||||
from typing import Any # noqa: F401
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from libp2p.network.typing import GenericProtocolHandlerFn
|
||||
from libp2p.peer.id import ID
|
||||
@ -34,6 +35,8 @@ class Mplex(IMuxedConn):
|
||||
stream_queue: "asyncio.Queue[StreamID]"
|
||||
next_channel_id: int
|
||||
|
||||
_tasks: List["asyncio.Future[Any]"]
|
||||
|
||||
# TODO: `generic_protocol_handler` should be refactored out of mplex conn.
|
||||
def __init__(
|
||||
self,
|
||||
@ -63,8 +66,10 @@ class Mplex(IMuxedConn):
|
||||
|
||||
self.stream_queue = asyncio.Queue()
|
||||
|
||||
self._tasks = []
|
||||
|
||||
# Kick off reading
|
||||
asyncio.ensure_future(self.handle_incoming())
|
||||
self._tasks.append(asyncio.ensure_future(self.handle_incoming()))
|
||||
|
||||
@property
|
||||
def initiator(self) -> bool:
|
||||
@ -74,6 +79,8 @@ class Mplex(IMuxedConn):
|
||||
"""
|
||||
close the stream muxer and underlying secured connection
|
||||
"""
|
||||
for task in self._tasks:
|
||||
task.cancel()
|
||||
await self.secured_conn.close()
|
||||
|
||||
def is_closed(self) -> bool:
|
||||
@ -135,7 +142,7 @@ class Mplex(IMuxedConn):
|
||||
"""
|
||||
stream_id = await self.stream_queue.get()
|
||||
stream = MplexStream(name, stream_id, self)
|
||||
asyncio.ensure_future(self.generic_protocol_handler(stream))
|
||||
self._tasks.append(asyncio.ensure_future(self.generic_protocol_handler(stream)))
|
||||
|
||||
async def send_message(
|
||||
self, flag: HeaderTags, data: bytes, stream_id: StreamID
|
||||
|
||||
@ -21,9 +21,8 @@ class IListener(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def close(self) -> bool:
|
||||
async def close(self) -> None:
|
||||
"""
|
||||
close the listener such that no more connections
|
||||
can be open on this transport instance
|
||||
:return: return True if successful
|
||||
"""
|
||||
|
||||
@ -45,20 +45,16 @@ class TCPListener(IListener):
|
||||
# TODO check if server is listening
|
||||
return self.multiaddrs
|
||||
|
||||
def close(self) -> bool:
|
||||
async def close(self) -> None:
|
||||
"""
|
||||
close the listener such that no more connections
|
||||
can be open on this transport instance
|
||||
:return: return True if successful
|
||||
"""
|
||||
if self.server is None:
|
||||
return False
|
||||
return
|
||||
self.server.close()
|
||||
_loop = asyncio.get_event_loop()
|
||||
_loop.run_until_complete(self.server.wait_closed())
|
||||
_loop.close()
|
||||
await self.server.wait_closed()
|
||||
self.server = None
|
||||
return True
|
||||
|
||||
|
||||
class TCP(ITransport):
|
||||
|
||||
Reference in New Issue
Block a user