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

46 lines
1.2 KiB
Python
Raw Normal View History

import ast
2019-07-27 17:10:03 +08:00
from typing import (
Union,
)
2019-07-27 17:10:03 +08:00
from libp2p.kademlia.kad_peerinfo import (
KadPeerInfo,
create_kad_peerinfo,
)
from libp2p.kademlia.network import KademliaServer
from libp2p.peer.id import ID
2019-03-27 03:44:01 +08:00
from libp2p.routing.interfaces import IPeerRouting
class KadmeliaPeerRouter(IPeerRouting):
# pylint: disable=too-few-public-methods
2019-03-27 03:44:01 +08:00
2019-07-27 17:10:03 +08:00
server: KademliaServer
def __init__(self, dht_server: KademliaServer) -> None:
2019-03-27 03:44:01 +08:00
self.server = dht_server
2019-07-27 17:10:03 +08:00
async def find_peer(self, peer_id: ID) -> KadPeerInfo:
2019-03-27 03:44:01 +08:00
"""
2019-05-06 02:32:41 +08:00
Find a specific peer
:param peer_id: peer to search for
:return: KadPeerInfo of specified peer
2019-03-27 03:44:01 +08:00
"""
# 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)
return decode_peerinfo(value)
2019-03-27 03:44:01 +08:00
2019-07-27 17:10:03 +08:00
def decode_peerinfo(encoded: Union[bytes, str]) -> KadPeerInfo:
if isinstance(encoded, bytes):
encoded = encoded.decode()
try:
lines = ast.literal_eval(encoded)
except SyntaxError:
return None
ip = lines[1] # pylint: disable=invalid-name
port = lines[2]
peer_id = lines[3]
peer_info = create_kad_peerinfo(peer_id, ip, port)
return peer_info