async def disconnect(self, peer_id: ID) -> None: disconnect_req = p2pd_pb.DisconnectRequest(peer=peer_id.to_bytes()) req = p2pd_pb.Request(type=p2pd_pb.Request.DISCONNECT, disconnect=disconnect_req) stream = await self.daemon_connector.open_connection() await write_pbmsg(stream, req) resp = p2pd_pb.Response() await read_pbmsg_safe(stream, resp) await stream.close() raise_if_failed(resp)
async def untag_peer(self, peer_id: ID, tag: str) -> None: """UNTAG_PEER """ connmgr_req = p2pd_pb.ConnManagerRequest( type=p2pd_pb.ConnManagerRequest.UNTAG_PEER, peer=peer_id.to_bytes(), tag=tag ) req = p2pd_pb.Request(type=p2pd_pb.Request.CONNMANAGER, connManager=connmgr_req) stream = await self.daemon_connector.open_connection() await write_pbmsg(stream, req) resp = p2pd_pb.Response() await read_pbmsg_safe(stream, resp) await stream.close() raise_if_failed(resp)
async def connect(self, peer_id: ID, maddrs: Iterable[Multiaddr]) -> None: stream = await self.daemon_connector.open_connection() maddrs_bytes = [i.to_bytes() for i in maddrs] connect_req = p2pd_pb.ConnectRequest(peer=peer_id.to_bytes(), addrs=maddrs_bytes) req = p2pd_pb.Request(type=p2pd_pb.Request.CONNECT, connect=connect_req) await write_pbmsg(stream, req) resp = p2pd_pb.Response() await read_pbmsg_safe(stream, resp) await stream.close() raise_if_failed(resp)
async def find_peers_connected_to_peer( self, peer_id: ID) -> Tuple[PeerInfo, ...]: """FIND_PEERS_CONNECTED_TO_PEER """ dht_req = p2pd_pb.DHTRequest( type=p2pd_pb.DHTRequest.FIND_PEERS_CONNECTED_TO_PEER, peer=peer_id.to_bytes(), ) resps = await self._do_dht(dht_req) try: pinfos = tuple( PeerInfo.from_pb(dht_resp.peer) for dht_resp in resps) except AttributeError as e: raise ControlFailure( f"dht_resp should contains peer info: resps={resps}, e={e}") return pinfos # type: ignore
async def get_public_key(self, peer_id: ID) -> crypto_pb.PublicKey: """GET_PUBLIC_KEY """ dht_req = p2pd_pb.DHTRequest(type=p2pd_pb.DHTRequest.GET_PUBLIC_KEY, peer=peer_id.to_bytes()) resps = await self._do_dht(dht_req) if len(resps) != 1: raise ControlFailure( f"should only get one response, resps={resps}") try: public_key_pb_bytes = resps[0].value except AttributeError as e: raise ControlFailure( f"dht_resp should contains `value`: resps={resps}, e={e}") public_key_pb = crypto_pb.PublicKey() public_key_pb.ParseFromString(public_key_pb_bytes) return public_key_pb
async def find_peer(self, peer_id: ID) -> PeerInfo: """FIND_PEER """ dht_req = p2pd_pb.DHTRequest(type=p2pd_pb.DHTRequest.FIND_PEER, peer=peer_id.to_bytes()) resps = await self._do_dht(dht_req) if len(resps) != 1: raise ControlFailure( f"should only get one response from `find_peer`, resps={resps}" ) dht_resp = resps[0] try: pinfo = dht_resp.peer except AttributeError as e: raise ControlFailure( f"dht_resp should contains peer info: dht_resp={dht_resp}, e={e}" ) return PeerInfo.from_pb(pinfo) # type: ignore
async def stream_open( self, peer_id: ID, protocols: Sequence[str] ) -> Tuple[StreamInfo, anyio.abc.SocketStream]: stream = await self.daemon_connector.open_connection() stream_open_req = p2pd_pb.StreamOpenRequest(peer=peer_id.to_bytes(), proto=list(protocols)) req = p2pd_pb.Request(type=p2pd_pb.Request.STREAM_OPEN, streamOpen=stream_open_req) await write_pbmsg(stream, req) resp = p2pd_pb.Response() await read_pbmsg_safe(stream, resp) raise_if_failed(resp) pb_stream_info = resp.streamInfo stream_info = StreamInfo.from_pb(pb_stream_info) return stream_info, stream