mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
add routing interfaces
This commit is contained in:
0
libp2p/routing/kadmelia/__init__.py
Normal file
0
libp2p/routing/kadmelia/__init__.py
Normal file
19
libp2p/routing/kadmelia/kadmelia_content_router.py
Normal file
19
libp2p/routing/kadmelia/kadmelia_content_router.py
Normal file
@ -0,0 +1,19 @@
|
||||
from libp2p.routing.interfaces import IContentRouting
|
||||
|
||||
|
||||
class KadmeliaContentRouter(IContentRouting):
|
||||
|
||||
def provide(self, cid, announce=True):
|
||||
"""
|
||||
Provide adds the given cid to the content routing system. If announce is True,
|
||||
it also announces it, otherwise it is just kept in the local
|
||||
accounting of which objects are being provided.
|
||||
"""
|
||||
pass
|
||||
|
||||
def find_provider_iter(self, cid, count):
|
||||
"""
|
||||
Search for peers who are able to provide a given key
|
||||
returns an iterator of peer.PeerInfo
|
||||
"""
|
||||
pass
|
||||
30
libp2p/routing/kadmelia/kadmelia_peer_router.py
Normal file
30
libp2p/routing/kadmelia/kadmelia_peer_router.py
Normal file
@ -0,0 +1,30 @@
|
||||
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):
|
||||
|
||||
def __init__(self, dht_server):
|
||||
self.server = dht_server
|
||||
|
||||
def find_peer(self, peer_id):
|
||||
"""
|
||||
Find specific Peer
|
||||
FindPeer searches for a peer with given peer_id, returns a peer.PeerInfo
|
||||
with relevant addresses.
|
||||
"""
|
||||
value = self.server.get(peer_id)
|
||||
return decode_peerinfo(value)
|
||||
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user