mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
* refactored : host_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : network_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : notifee_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : net_connection_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored: raw_connection_interface, secure_conn_interface and stream_muxer abc.py * refactored: addrbook_interface * refactored :peerdata_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :peermetadata_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_client_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_communicator_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_muxer_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :interfaces Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :security_transport_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :listener_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * moved all interfaces and typing files Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * fixed documentation and moved pubsub abc.py Co-authored-by: Khwahish Patel <khwahish.p1@ahduni.edu.in> * added exclude-members in custom_types docs * added : newsfragment for moving all interfaces to libp2p.abc --------- Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> Co-authored-by: Mystical <125946525+mystical-prog@users.noreply.github.com>
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
from libp2p.abc import (
|
|
INetworkService,
|
|
IPeerRouting,
|
|
)
|
|
from libp2p.host.basic_host import (
|
|
BasicHost,
|
|
)
|
|
from libp2p.host.exceptions import (
|
|
ConnectionFailure,
|
|
)
|
|
from libp2p.peer.peerinfo import (
|
|
PeerInfo,
|
|
)
|
|
|
|
|
|
# RoutedHost is a p2p Host that includes a routing system.
|
|
# This allows the Host to find the addresses for peers when it does not have them.
|
|
class RoutedHost(BasicHost):
|
|
_router: IPeerRouting
|
|
|
|
def __init__(self, network: INetworkService, router: IPeerRouting):
|
|
super().__init__(network)
|
|
self._router = router
|
|
|
|
async def connect(self, peer_info: PeerInfo) -> None:
|
|
"""
|
|
Ensure there is a connection between this host and the peer
|
|
with given `peer_info.peer_id`. See (basic_host).connect for more
|
|
information.
|
|
|
|
RoutedHost's Connect differs in that if the host has no addresses for a
|
|
given peer, it will use its routing system to try to find some.
|
|
|
|
:param peer_info: peer_info of the peer we want to connect to
|
|
:type peer_info: peer.peerinfo.PeerInfo
|
|
"""
|
|
# check if we were given some addresses, otherwise, find some with the
|
|
# routing system.
|
|
if not peer_info.addrs:
|
|
found_peer_info = await self._router.find_peer(peer_info.peer_id)
|
|
if not found_peer_info:
|
|
raise ConnectionFailure("Unable to find Peer address")
|
|
self.peerstore.add_addrs(peer_info.peer_id, found_peer_info.addrs, 10)
|
|
self.peerstore.add_addrs(peer_info.peer_id, peer_info.addrs, 10)
|
|
|
|
# there is already a connection to this peer
|
|
if peer_info.peer_id in self._network.connections:
|
|
return
|
|
|
|
await self._network.dial_peer(peer_info.peer_id)
|