py-libp2p/examples/sharding/receiver.py

67 lines
2.2 KiB
Python
Raw Normal View History

2019-04-07 02:23:52 +08:00
import multiaddr
2019-04-07 05:16:37 +08:00
import asyncio
2019-04-07 02:23:52 +08:00
from libp2p import new_node
2019-04-07 05:16:37 +08:00
from libp2p.peer.peerinfo import info_from_p2p_addr
2019-04-07 02:23:52 +08:00
from libp2p.pubsub.pubsub import Pubsub
from libp2p.pubsub.floodsub import FloodSub
2019-04-07 05:16:37 +08:00
from tests.pubsub.utils import message_id_generator
2019-04-07 02:23:52 +08:00
TOPIC = "eth"
2019-04-07 05:16:37 +08:00
SUPPORTED_PUBSUB_PROTOCOLS = ["/floodsub/1.0.0"]
2019-04-07 02:23:52 +08:00
class ReceiverNode():
"""
Node which has an internal balance mapping, meant to serve as
a dummy crypto blockchain. There is no actual blockchain, just a simple
map indicating how much crypto each user in the mappings holds
"""
def __init__(self):
self.next_msg_id_func = message_id_generator(0)
@classmethod
async def create(cls, ack_protocol, topic):
"""
Create a new DummyAccountNode and attach a libp2p node, a floodsub, and a pubsub
instance to this new node
We use create as this serves as a factory function and allows us
to use async await, unlike the init function
"""
2019-04-07 05:16:37 +08:00
self = ReceiverNode()
libp2p_node = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
await libp2p_node.get_network().listen(multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/0"))
self.libp2p_node = libp2p_node
self.floodsub = FloodSub(SUPPORTED_PUBSUB_PROTOCOLS)
self.pubsub = Pubsub(self.libp2p_node, self.floodsub, "a")
self.pubsub_messages = await self.pubsub.subscribe(topic)
self.topic = topic
self.ack_protocol = ack_protocol
return self
2019-04-07 05:16:37 +08:00
async def wait_for_end(self, ack_stream):
msg = (await ack_stream.read()).decode()
if msg == "end":
2019-04-07 11:09:26 +08:00
print("END RECEIVED, KILL NOW")
2019-04-07 05:16:37 +08:00
self.should_listen = False
async def start_receiving(self, sender_node_info):
await self.libp2p_node.connect(sender_node_info)
ack_stream = await self.libp2p_node.new_stream(sender_node_info.peer_id, [self.ack_protocol])
2019-04-07 05:16:37 +08:00
asyncio.ensure_future(self.wait_for_end(ack_stream))
self.should_listen = True
ack_msg = self.topic
encoded_ack_msg = ack_msg.encode()
2019-04-07 05:16:37 +08:00
while self.should_listen:
msg = await self.pubsub_messages.get()
await ack_stream.write(encoded_ack_msg)
2019-04-08 02:25:59 +08:00
print("Receiver closed")