2019-07-27 11:27:47 +08:00
|
|
|
import asyncio
|
2019-07-26 17:30:51 +08:00
|
|
|
|
|
|
|
from multiaddr import Multiaddr
|
|
|
|
|
2019-07-28 22:30:51 +08:00
|
|
|
from libp2p.network.network_interface import INetwork
|
|
|
|
from libp2p.network.notifee_interface import INotifee
|
2019-07-30 17:31:08 +08:00
|
|
|
from libp2p.peer.id import ID
|
2019-07-28 22:30:51 +08:00
|
|
|
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
|
|
|
|
|
|
|
|
from libp2p.network.stream.net_stream_interface import INetStream
|
2019-07-27 11:27:47 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
class PubsubNotifee(INotifee):
|
2019-07-30 17:31:08 +08:00
|
|
|
# pylint: disable=too-many-instance-attributes, cell-var-from-loop, unsubscriptable-object
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-30 17:31:08 +08:00
|
|
|
initiator_peers_queue: asyncio.Queue[ID]
|
2019-07-26 17:30:51 +08:00
|
|
|
|
2019-07-30 17:31:08 +08:00
|
|
|
def __init__(self, initiator_peers_queue: asyncio.Queue[ID]) -> 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-07-27 11:27:47 +08:00
|
|
|
self.initiator_peers_queue = initiator_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-07-28 14:30:15 +08:00
|
|
|
async def connected(self, network: INetwork, conn: IMuxedConn) -> None:
|
2019-03-24 01:52:02 +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.
|
|
|
|
:param network: network the connection was opened on
|
|
|
|
:param conn: connection that was opened
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Only add peer_id if we are initiator (otherwise we would end up
|
|
|
|
# with two pubsub streams between us and the peer)
|
|
|
|
if conn.initiator:
|
|
|
|
await self.initiator_peers_queue.put(conn.peer_id)
|
|
|
|
|
2019-07-28 14:30:15 +08:00
|
|
|
async def disconnected(self, network: INetwork, conn: IMuxedConn) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
pass
|
|
|
|
|
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
|