2019-08-01 06:00:12 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2019-07-26 17:30:51 +08:00
|
|
|
|
|
|
|
from multiaddr import Multiaddr
|
|
|
|
|
2019-09-15 21:41:29 +08:00
|
|
|
from libp2p.network.connection.net_connection_interface import INetConn
|
2019-07-28 22:30:51 +08:00
|
|
|
from libp2p.network.network_interface import INetwork
|
|
|
|
from libp2p.network.notifee_interface import INotifee
|
|
|
|
from libp2p.network.stream.net_stream_interface import INetStream
|
2019-07-27 11:27:47 +08:00
|
|
|
|
2019-07-30 18:00:30 +08:00
|
|
|
if TYPE_CHECKING:
|
2019-12-03 17:27:49 +08:00
|
|
|
import trio # noqa: F401
|
2019-08-08 14:22:06 +08:00
|
|
|
from libp2p.peer.id import ID # noqa: F401
|
2019-07-30 18:00:30 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
class PubsubNotifee(INotifee):
|
|
|
|
|
2019-12-03 17:27:49 +08:00
|
|
|
initiator_peers_queue: "trio.MemorySendChannel[ID]"
|
|
|
|
dead_peers_queue: "trio.MemorySendChannel[ID]"
|
2019-07-26 17:30:51 +08:00
|
|
|
|
2019-11-04 14:22:24 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
2019-12-03 17:27:49 +08:00
|
|
|
initiator_peers_queue: "trio.MemorySendChannel[ID]",
|
|
|
|
dead_peers_queue: "trio.MemorySendChannel[ID]",
|
2019-11-04 14:22:24 +08:00
|
|
|
) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
:param initiator_peers_queue: queue to add new peers to so that pubsub
|
|
|
|
can process new peers after we connect to them
|
2019-11-04 14:22:24 +08:00
|
|
|
:param dead_peers_queue: queue to add dead peers to so that pubsub
|
|
|
|
can process dead peers after we disconnect from each other
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
2019-07-27 11:27:47 +08:00
|
|
|
self.initiator_peers_queue = initiator_peers_queue
|
2019-11-04 14:22:24 +08:00
|
|
|
self.dead_peers_queue = dead_peers_queue
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-26 17:30:51 +08:00
|
|
|
async def opened_stream(self, network: INetwork, stream: INetStream) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
pass
|
|
|
|
|
2019-07-26 17:30:51 +08:00
|
|
|
async def closed_stream(self, network: INetwork, stream: INetStream) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
pass
|
|
|
|
|
2019-09-15 21:41:29 +08:00
|
|
|
async def connected(self, network: INetwork, conn: INetConn) -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Add peer_id to initiator_peers_queue, so that this peer_id can be used
|
|
|
|
to create a stream and we only want to have one pubsub stream with each
|
|
|
|
peer.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
:param network: network the connection was opened on
|
|
|
|
:param conn: connection that was opened
|
|
|
|
"""
|
2019-12-03 17:27:49 +08:00
|
|
|
try:
|
|
|
|
await self.initiator_peers_queue.send(conn.muxed_conn.peer_id)
|
|
|
|
except trio.BrokenResourceError:
|
|
|
|
# Raised when the receive channel is closed.
|
|
|
|
# TODO: Do something with loggers?
|
|
|
|
...
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-09-15 21:41:29 +08:00
|
|
|
async def disconnected(self, network: INetwork, conn: INetConn) -> None:
|
2019-11-04 14:23:00 +08:00
|
|
|
"""
|
|
|
|
Add peer_id to dead_peers_queue, so that pubsub and its router can
|
|
|
|
remove this peer_id and close the stream inbetween.
|
|
|
|
|
|
|
|
:param network: network the connection was opened on
|
|
|
|
:param conn: connection that was opened
|
|
|
|
"""
|
2019-12-03 17:27:49 +08:00
|
|
|
await self.dead_peers_queue.send(conn.muxed_conn.peer_id)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-26 17:30:51 +08:00
|
|
|
async def listen(self, network: INetwork, multiaddr: Multiaddr) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
pass
|
|
|
|
|
2019-07-26 17:30:51 +08:00
|
|
|
async def listen_close(self, network: INetwork, multiaddr: Multiaddr) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
pass
|