py-libp2p/libp2p/routing/kademlia/kademlia_peer_router.py

33 lines
1001 B
Python
Raw Normal View History

2019-03-27 03:44:01 +08:00
from libp2p.routing.interfaces import IPeerRouting
from libp2p.kademlia.utils import digest
from libp2p.peer.peerinfo import PeerInfo
from libp2p.peer.peerdata import PeerData
class KadmeliaPeerRouter(IPeerRouting):
# pylint: disable=too-few-public-methods
2019-03-27 03:44:01 +08:00
def __init__(self, dht_server):
self.server = dht_server
2019-04-28 09:59:25 +08:00
async def find_peer(self, peer_id):
2019-03-27 03:44:01 +08:00
"""
Find specific Peer
FindPeer searches for a peer with given peer_id, returns a peer.PeerInfo
with relevant addresses.
"""
# switching peer_id to xor_id used by kademlia as node_id
xor_id = peer_id.get_xor_id()
2019-04-28 09:59:25 +08:00
value = await self.server.get(xor_id)
2019-04-29 01:49:10 +08:00
return value
2019-03-27 03:44:01 +08:00
2019-04-29 01:49:10 +08:00
# def decode_peerinfo(encoded):
# if isinstance(encoded, bytes):
# encoded = encoded.decode()
# lines = encoded.splitlines()
# peer_id = lines[0]
# addrs = lines[1:]
# peer_data = PeerData()
# peer_data.add_addrs(addrs)
# return PeerInfo(peer_id, addrs)