2019-03-24 01:52:02 +08:00
|
|
|
import asyncio
|
2019-08-03 18:44:40 +08:00
|
|
|
from collections import namedtuple
|
2019-07-24 14:54:30 +08:00
|
|
|
import time
|
2019-08-03 18:44:40 +08:00
|
|
|
from typing import Any, Awaitable, Callable, Dict, List, Tuple, Union, TYPE_CHECKING
|
2019-07-22 18:12:54 +08:00
|
|
|
|
2019-04-06 09:46:03 +08:00
|
|
|
from lru import LRU
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-28 22:30:51 +08:00
|
|
|
from libp2p.host.host_interface import IHost
|
|
|
|
from libp2p.network.stream.net_stream_interface import INetStream
|
2019-08-03 13:36:19 +08:00
|
|
|
from libp2p.peer.id import ID
|
2019-07-24 16:29:02 +08:00
|
|
|
|
|
|
|
from .pb import rpc_pb2
|
|
|
|
from .pubsub_notifee import PubsubNotifee
|
2019-07-29 12:42:13 +08:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .pubsub_router_interface import IPubsubRouter
|
2019-07-24 14:54:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_msg_id(msg: rpc_pb2.Message) -> Tuple[bytes, bytes]:
|
|
|
|
# NOTE: `string(from, seqno)` in Go
|
|
|
|
return (msg.seqno, msg.from_id)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
|
2019-08-03 18:44:40 +08:00
|
|
|
TopicValidator = namedtuple("TopicValidator", ["validator", "is_async"])
|
|
|
|
|
|
|
|
|
2019-08-04 12:33:22 +08:00
|
|
|
ValidatorFn = Union[Callable[[ID, rpc_pb2.Message], bool], Awaitable[bool]]
|
2019-08-04 10:44:12 +08:00
|
|
|
|
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
class Pubsub:
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
host: IHost
|
|
|
|
my_id: ID
|
2019-07-24 16:29:02 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
router: "IPubsubRouter"
|
2019-07-24 16:29:02 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
peer_queue: "asyncio.Queue[ID]"
|
2019-07-24 16:29:02 +08:00
|
|
|
|
|
|
|
protocols: List[str]
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
incoming_msgs_from_peers: "asyncio.Queue[rpc_pb2.Message]"
|
|
|
|
outgoing_messages: "asyncio.Queue[rpc_pb2.Message]"
|
2019-07-24 16:29:02 +08:00
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
seen_messages: LRU
|
2019-07-29 12:42:13 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
my_topics: Dict[str, "asyncio.Queue[rpc_pb2.Message]"]
|
2019-07-29 12:42:13 +08:00
|
|
|
|
2019-08-01 12:05:28 +08:00
|
|
|
peer_topics: Dict[str, List[ID]]
|
|
|
|
peers: Dict[ID, INetStream]
|
2019-07-24 16:29:02 +08:00
|
|
|
|
2019-08-03 18:44:40 +08:00
|
|
|
topic_validators: Dict[str, TopicValidator]
|
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
# NOTE: Be sure it is increased atomically everytime.
|
|
|
|
counter: int # uint64
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
def __init__(
|
|
|
|
self, host: IHost, router: "IPubsubRouter", my_id: ID, cache_size: int = None
|
|
|
|
) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Construct a new Pubsub object, which is responsible for handling all
|
|
|
|
Pubsub-related messages and relaying messages as appropriate to the
|
|
|
|
Pubsub router (which is responsible for choosing who to send messages to).
|
|
|
|
Since the logic for choosing peers to send pubsub messages to is
|
|
|
|
in the router, the same Pubsub impl can back floodsub, gossipsub, etc.
|
|
|
|
"""
|
|
|
|
self.host = host
|
|
|
|
self.router = router
|
|
|
|
self.my_id = my_id
|
|
|
|
|
|
|
|
# Attach this new Pubsub object to the router
|
|
|
|
self.router.attach(self)
|
|
|
|
|
|
|
|
# Register a notifee
|
|
|
|
self.peer_queue = asyncio.Queue()
|
|
|
|
self.host.get_network().notify(PubsubNotifee(self.peer_queue))
|
|
|
|
|
|
|
|
# Register stream handlers for each pubsub router protocol to handle
|
|
|
|
# the pubsub streams opened on those protocols
|
|
|
|
self.protocols = self.router.get_protocols()
|
|
|
|
for protocol in self.protocols:
|
|
|
|
self.host.set_stream_handler(protocol, self.stream_handler)
|
|
|
|
|
2019-05-07 11:44:13 +08:00
|
|
|
# Use asyncio queues for proper context switching
|
2019-03-24 01:52:02 +08:00
|
|
|
self.incoming_msgs_from_peers = asyncio.Queue()
|
|
|
|
self.outgoing_messages = asyncio.Queue()
|
|
|
|
|
2019-04-06 09:46:03 +08:00
|
|
|
# keeps track of seen messages as LRU cache
|
|
|
|
if cache_size is None:
|
|
|
|
self.cache_size = 128
|
|
|
|
else:
|
|
|
|
self.cache_size = cache_size
|
2019-05-07 11:44:13 +08:00
|
|
|
|
2019-04-06 09:46:03 +08:00
|
|
|
self.seen_messages = LRU(self.cache_size)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-05-07 11:44:13 +08:00
|
|
|
# Map of topics we are subscribed to blocking queues
|
2019-03-24 01:52:02 +08:00
|
|
|
# for when the given topic receives a message
|
|
|
|
self.my_topics = {}
|
|
|
|
|
|
|
|
# Map of topic to peers to keep track of what peers are subscribed to
|
|
|
|
self.peer_topics = {}
|
|
|
|
|
|
|
|
# Create peers map, which maps peer_id (as string) to stream (to a given peer)
|
|
|
|
self.peers = {}
|
|
|
|
|
2019-08-03 18:44:40 +08:00
|
|
|
# Map of topic to topic validator
|
|
|
|
self.topic_validators = {}
|
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
self.counter = time.time_ns()
|
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
# Call handle peer to keep waiting for updates to peer queue
|
|
|
|
asyncio.ensure_future(self.handle_peer_queue())
|
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
def get_hello_packet(self) -> bytes:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Generate subscription message with all topics we are subscribed to
|
2019-04-02 04:23:20 +08:00
|
|
|
only send hello packet if we have subscribed topics
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
2019-07-28 18:07:48 +08:00
|
|
|
packet = rpc_pb2.RPC()
|
|
|
|
for topic_id in self.my_topics:
|
2019-08-04 02:25:25 +08:00
|
|
|
packet.subscriptions.extend([rpc_pb2.RPC.SubOpts(subscribe=True, topicid=topic_id)])
|
2019-03-29 03:25:33 +08:00
|
|
|
return packet.SerializeToString()
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-28 14:30:15 +08:00
|
|
|
async def continuously_read_stream(self, stream: INetStream) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Read from input stream in an infinite loop. Process
|
2019-03-31 06:49:50 +08:00
|
|
|
messages from other nodes
|
2019-03-24 01:52:02 +08:00
|
|
|
:param stream: stream to continously read from
|
|
|
|
"""
|
2019-07-25 16:58:00 +08:00
|
|
|
peer_id = stream.mplex_conn.peer_id
|
2019-03-30 04:23:30 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
while True:
|
2019-07-24 16:29:02 +08:00
|
|
|
incoming: bytes = (await stream.read())
|
|
|
|
rpc_incoming: rpc_pb2.RPC = rpc_pb2.RPC()
|
2019-03-29 03:25:33 +08:00
|
|
|
rpc_incoming.ParseFromString(incoming)
|
2019-04-01 10:16:28 +08:00
|
|
|
|
2019-03-31 05:59:08 +08:00
|
|
|
if rpc_incoming.publish:
|
2019-04-01 10:16:28 +08:00
|
|
|
# deal with RPC.publish
|
2019-07-25 16:58:00 +08:00
|
|
|
for msg in rpc_incoming.publish:
|
|
|
|
if not self._is_subscribed_to_msg(msg):
|
|
|
|
continue
|
|
|
|
# TODO(mhchia): This will block this read_stream loop until all data are pushed.
|
|
|
|
# Should investigate further if this is an issue.
|
2019-07-29 12:09:35 +08:00
|
|
|
await self.push_msg(msg_forwarder=peer_id, msg=msg)
|
2019-03-30 04:23:30 +08:00
|
|
|
|
2019-03-31 05:59:08 +08:00
|
|
|
if rpc_incoming.subscriptions:
|
2019-04-01 10:16:28 +08:00
|
|
|
# deal with RPC.subscriptions
|
|
|
|
# We don't need to relay the subscription to our
|
|
|
|
# peers because a given node only needs its peers
|
|
|
|
# to know that it is subscribed to the topic (doesn't
|
|
|
|
# need everyone to know)
|
|
|
|
for message in rpc_incoming.subscriptions:
|
2019-07-15 16:28:29 +08:00
|
|
|
self.handle_subscription(peer_id, message)
|
2019-04-01 10:16:28 +08:00
|
|
|
|
2019-07-29 22:50:02 +08:00
|
|
|
# NOTE: Check if `rpc_incoming.control` is set through `HasField`.
|
|
|
|
# This is necessary because `control` is an optional field in pb2.
|
2019-08-04 03:45:55 +08:00
|
|
|
# Ref: https://developers.google.com/protocol-buffers/docs/reference/python-generated#singular-fields-proto2 # noqa: E501
|
2019-07-29 22:50:02 +08:00
|
|
|
if rpc_incoming.HasField("control"):
|
2019-05-07 11:44:13 +08:00
|
|
|
# Pass rpc to router so router could perform custom logic
|
|
|
|
await self.router.handle_rpc(rpc_incoming, peer_id)
|
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
# Force context switch
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
2019-08-04 11:03:29 +08:00
|
|
|
def set_topic_validator(
|
2019-08-04 10:44:12 +08:00
|
|
|
self, topic: str, validator: ValidatorFn, is_async_validator: bool
|
|
|
|
) -> None:
|
2019-08-04 12:33:22 +08:00
|
|
|
"""
|
|
|
|
Register a validator under the given topic. One topic can only have one validtor.
|
|
|
|
"""
|
2019-08-04 10:44:12 +08:00
|
|
|
self.topic_validators[topic] = TopicValidator(validator, is_async_validator)
|
|
|
|
|
|
|
|
def remove_topic_validator(self, topic: str) -> None:
|
2019-08-04 12:33:22 +08:00
|
|
|
"""
|
|
|
|
Remove the validator from the given topic.
|
|
|
|
"""
|
2019-08-04 10:44:12 +08:00
|
|
|
if topic in self.topic_validators:
|
|
|
|
del self.topic_validators[topic]
|
|
|
|
|
2019-08-04 11:23:20 +08:00
|
|
|
def get_msg_validators(self, msg: rpc_pb2.Message) -> Tuple[TopicValidator, ...]:
|
2019-08-04 12:33:22 +08:00
|
|
|
"""
|
|
|
|
Get all validators corresponding to the topics in the message.
|
|
|
|
"""
|
2019-08-04 11:23:20 +08:00
|
|
|
return (
|
|
|
|
self.topic_validators[topic]
|
|
|
|
for topic in msg.topicIDs
|
|
|
|
if topic in self.topic_validators
|
|
|
|
)
|
|
|
|
|
2019-07-28 14:30:15 +08:00
|
|
|
async def stream_handler(self, stream: INetStream) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Stream handler for pubsub. Gets invoked whenever a new stream is created
|
|
|
|
on one of the supported pubsub protocols.
|
|
|
|
:param stream: newly created stream
|
|
|
|
"""
|
|
|
|
# Add peer
|
|
|
|
# Map peer to stream
|
2019-07-24 16:29:02 +08:00
|
|
|
peer_id: ID = stream.mplex_conn.peer_id
|
2019-08-01 12:05:28 +08:00
|
|
|
self.peers[peer_id] = stream
|
2019-03-24 01:52:02 +08:00
|
|
|
self.router.add_peer(peer_id, stream.get_protocol())
|
|
|
|
|
|
|
|
# Send hello packet
|
2019-07-24 16:29:02 +08:00
|
|
|
hello: bytes = self.get_hello_packet()
|
2019-03-31 05:59:08 +08:00
|
|
|
|
2019-03-29 03:25:33 +08:00
|
|
|
await stream.write(hello)
|
2019-03-24 01:52:02 +08:00
|
|
|
# Pass stream off to stream reader
|
2019-03-31 05:59:08 +08:00
|
|
|
asyncio.ensure_future(self.continuously_read_stream(stream))
|
2019-07-28 18:07:48 +08:00
|
|
|
# Force context switch
|
|
|
|
await asyncio.sleep(0)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
async def handle_peer_queue(self) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Continuously read from peer queue and each time a new peer is found,
|
|
|
|
open a stream to the peer using a supported pubsub protocol
|
|
|
|
TODO: Handle failure for when the peer does not support any of the
|
|
|
|
pubsub protocols we support
|
|
|
|
"""
|
|
|
|
while True:
|
2019-04-02 04:55:44 +08:00
|
|
|
|
2019-07-28 14:30:15 +08:00
|
|
|
peer_id: ID = await self.peer_queue.get()
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Open a stream to peer on existing connection
|
|
|
|
# (we know connection exists since that's the only way
|
|
|
|
# an element gets added to peer_queue)
|
2019-07-28 14:30:15 +08:00
|
|
|
stream: INetStream = await self.host.new_stream(peer_id, self.protocols)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Add Peer
|
|
|
|
# Map peer to stream
|
2019-08-01 12:05:28 +08:00
|
|
|
self.peers[peer_id] = stream
|
2019-03-24 01:52:02 +08:00
|
|
|
self.router.add_peer(peer_id, stream.get_protocol())
|
|
|
|
|
|
|
|
# Send hello packet
|
2019-07-24 16:29:02 +08:00
|
|
|
hello: bytes = self.get_hello_packet()
|
2019-03-29 03:25:33 +08:00
|
|
|
await stream.write(hello)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-28 18:07:48 +08:00
|
|
|
# TODO: Investigate whether this should be replaced by `handlePeerEOF`
|
|
|
|
# Ref: https://github.com/libp2p/go-libp2p-pubsub/blob/49274b0e8aecdf6cad59d768e5702ff00aa48488/comm.go#L80 # noqa: E501
|
2019-03-24 01:52:02 +08:00
|
|
|
# Pass stream off to stream reader
|
2019-03-31 05:59:08 +08:00
|
|
|
asyncio.ensure_future(self.continuously_read_stream(stream))
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Force context switch
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
# FIXME: `sub_message` can be further type hinted with mypy_protobuf
|
2019-07-30 17:31:08 +08:00
|
|
|
def handle_subscription(self, origin_id: ID, sub_message: Any) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Handle an incoming subscription message from a peer. Update internal
|
|
|
|
mapping to mark the peer as subscribed or unsubscribed to topics as
|
|
|
|
defined in the subscription message
|
2019-04-01 10:16:28 +08:00
|
|
|
:param origin_id: id of the peer who subscribe to the message
|
|
|
|
:param sub_message: RPC.SubOpts
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
2019-04-01 10:16:28 +08:00
|
|
|
if sub_message.subscribe:
|
|
|
|
if sub_message.topicid not in self.peer_topics:
|
2019-08-01 12:05:28 +08:00
|
|
|
self.peer_topics[sub_message.topicid] = [origin_id]
|
|
|
|
elif origin_id not in self.peer_topics[sub_message.topicid]:
|
2019-04-01 10:16:28 +08:00
|
|
|
# Add peer to topic
|
2019-08-01 12:05:28 +08:00
|
|
|
self.peer_topics[sub_message.topicid].append(origin_id)
|
2019-04-01 10:16:28 +08:00
|
|
|
else:
|
2019-05-07 11:44:13 +08:00
|
|
|
if sub_message.topicid in self.peer_topics:
|
2019-08-01 12:05:28 +08:00
|
|
|
if origin_id in self.peer_topics[sub_message.topicid]:
|
|
|
|
self.peer_topics[sub_message.topicid].remove(origin_id)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
# FIXME(mhchia): Change the function name?
|
|
|
|
# FIXME(mhchia): `publish_message` can be further type hinted with mypy_protobuf
|
|
|
|
async def handle_talk(self, publish_message: Any) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
2019-04-01 10:16:28 +08:00
|
|
|
Put incoming message from a peer onto my blocking queue
|
2019-07-24 16:29:02 +08:00
|
|
|
:param publish_message: RPC.Message format
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Check if this message has any topics that we are subscribed to
|
2019-03-31 06:49:50 +08:00
|
|
|
for topic in publish_message.topicIDs:
|
2019-03-24 01:52:02 +08:00
|
|
|
if topic in self.my_topics:
|
|
|
|
# we are subscribed to a topic this message was sent for,
|
|
|
|
# so add message to the subscription output queue
|
2019-04-03 10:34:01 +08:00
|
|
|
# for each topic
|
|
|
|
await self.my_topics[topic].put(publish_message)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
async def subscribe(self, topic_id: str) -> "asyncio.Queue[rpc_pb2.Message]":
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Subscribe ourself to a topic
|
|
|
|
:param topic_id: topic_id to subscribe to
|
|
|
|
"""
|
2019-04-02 04:23:20 +08:00
|
|
|
|
2019-07-15 16:32:05 +08:00
|
|
|
# Already subscribed
|
|
|
|
if topic_id in self.my_topics:
|
|
|
|
return self.my_topics[topic_id]
|
|
|
|
|
2019-04-03 10:34:01 +08:00
|
|
|
# Map topic_id to blocking queue
|
|
|
|
self.my_topics[topic_id] = asyncio.Queue()
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Create subscribe message
|
2019-07-24 16:29:02 +08:00
|
|
|
packet: rpc_pb2.RPC = rpc_pb2.RPC()
|
2019-08-01 06:00:12 +08:00
|
|
|
packet.subscriptions.extend(
|
|
|
|
[rpc_pb2.RPC.SubOpts(subscribe=True, topicid=topic_id.encode("utf-8"))]
|
|
|
|
)
|
2019-04-02 04:55:44 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
# Send out subscribe message to all peers
|
2019-03-31 05:59:08 +08:00
|
|
|
await self.message_all_peers(packet.SerializeToString())
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Tell router we are joining this topic
|
2019-05-07 11:44:13 +08:00
|
|
|
await self.router.join(topic_id)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-04-03 10:34:01 +08:00
|
|
|
# Return the asyncio queue for messages on this topic
|
|
|
|
return self.my_topics[topic_id]
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
async def unsubscribe(self, topic_id: str) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Unsubscribe ourself from a topic
|
|
|
|
:param topic_id: topic_id to unsubscribe from
|
|
|
|
"""
|
|
|
|
|
2019-07-18 13:26:31 +08:00
|
|
|
# Return if we already unsubscribed from the topic
|
|
|
|
if topic_id not in self.my_topics:
|
|
|
|
return
|
2019-03-24 01:52:02 +08:00
|
|
|
# Remove topic_id from map if present
|
2019-07-18 13:26:31 +08:00
|
|
|
del self.my_topics[topic_id]
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Create unsubscribe message
|
2019-07-24 16:29:02 +08:00
|
|
|
packet: rpc_pb2.RPC = rpc_pb2.RPC()
|
2019-08-01 06:00:12 +08:00
|
|
|
packet.subscriptions.extend(
|
|
|
|
[rpc_pb2.RPC.SubOpts(subscribe=False, topicid=topic_id.encode("utf-8"))]
|
|
|
|
)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Send out unsubscribe message to all peers
|
2019-03-31 05:59:08 +08:00
|
|
|
await self.message_all_peers(packet.SerializeToString())
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Tell router we are leaving this topic
|
2019-05-07 11:44:13 +08:00
|
|
|
await self.router.leave(topic_id)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-30 15:31:02 +08:00
|
|
|
async def message_all_peers(self, raw_msg: bytes) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Broadcast a message to peers
|
|
|
|
:param raw_msg: raw contents of the message to broadcast
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Broadcast message
|
2019-07-30 13:33:48 +08:00
|
|
|
for stream in self.peers.values():
|
2019-03-24 01:52:02 +08:00
|
|
|
# Write message to stream
|
2019-07-24 16:29:02 +08:00
|
|
|
await stream.write(raw_msg)
|
2019-07-22 18:12:54 +08:00
|
|
|
|
2019-07-23 16:10:14 +08:00
|
|
|
async def publish(self, topic_id: str, data: bytes) -> None:
|
2019-07-25 14:08:16 +08:00
|
|
|
"""
|
|
|
|
Publish data to a topic
|
|
|
|
:param topic_id: topic which we are going to publish the data to
|
|
|
|
:param data: data which we are publishing
|
|
|
|
"""
|
2019-07-23 16:10:14 +08:00
|
|
|
msg = rpc_pb2.Message(
|
|
|
|
data=data,
|
|
|
|
topicIDs=[topic_id],
|
2019-07-28 16:05:29 +08:00
|
|
|
# Origin is ourself.
|
2019-07-23 16:10:14 +08:00
|
|
|
from_id=self.host.get_id().to_bytes(),
|
|
|
|
seqno=self._next_seqno(),
|
|
|
|
)
|
2019-07-24 14:35:14 +08:00
|
|
|
|
2019-07-22 18:12:54 +08:00
|
|
|
# TODO: Sign with our signing key
|
2019-07-24 14:35:14 +08:00
|
|
|
|
2019-07-25 14:08:16 +08:00
|
|
|
await self.push_msg(self.host.get_id(), msg)
|
2019-07-23 16:10:14 +08:00
|
|
|
|
2019-07-29 12:09:35 +08:00
|
|
|
async def push_msg(self, msg_forwarder: ID, msg: rpc_pb2.Message) -> None:
|
2019-07-25 14:08:16 +08:00
|
|
|
"""
|
|
|
|
Push a pubsub message to others.
|
2019-07-29 12:09:35 +08:00
|
|
|
:param msg_forwarder: the peer who forward us the message.
|
2019-07-25 14:08:16 +08:00
|
|
|
:param msg: the message we are going to push out.
|
|
|
|
"""
|
2019-07-27 18:41:16 +08:00
|
|
|
# TODO: Check if the `source` is in the blacklist. If yes, reject.
|
2019-07-24 14:35:14 +08:00
|
|
|
|
2019-07-27 18:41:16 +08:00
|
|
|
# TODO: Check if the `from` is in the blacklist. If yes, reject.
|
2019-07-24 14:35:14 +08:00
|
|
|
|
2019-07-27 18:41:16 +08:00
|
|
|
# TODO: Check if signing is required and if so signature should be attached.
|
2019-07-24 14:35:14 +08:00
|
|
|
|
2019-07-23 16:10:14 +08:00
|
|
|
if self._is_msg_seen(msg):
|
|
|
|
return
|
2019-07-24 14:35:14 +08:00
|
|
|
|
2019-07-22 18:12:54 +08:00
|
|
|
# TODO: - Validate the message. If failed, reject it.
|
2019-07-24 14:35:14 +08:00
|
|
|
|
2019-07-23 16:10:14 +08:00
|
|
|
self._mark_msg_seen(msg)
|
|
|
|
await self.handle_talk(msg)
|
2019-07-29 12:09:35 +08:00
|
|
|
await self.router.publish(msg_forwarder, msg)
|
2019-07-23 16:10:14 +08:00
|
|
|
|
|
|
|
def _next_seqno(self) -> bytes:
|
2019-07-25 14:08:16 +08:00
|
|
|
"""
|
|
|
|
Make the next message sequence id.
|
|
|
|
"""
|
2019-07-23 16:10:14 +08:00
|
|
|
self.counter += 1
|
2019-08-01 06:00:12 +08:00
|
|
|
return self.counter.to_bytes(8, "big")
|
2019-07-23 16:10:14 +08:00
|
|
|
|
|
|
|
def _is_msg_seen(self, msg: rpc_pb2.Message) -> bool:
|
|
|
|
msg_id = get_msg_id(msg)
|
|
|
|
return msg_id in self.seen_messages
|
|
|
|
|
|
|
|
def _mark_msg_seen(self, msg: rpc_pb2.Message) -> None:
|
|
|
|
msg_id = get_msg_id(msg)
|
|
|
|
# FIXME: Mapping `msg_id` to `1` is quite awkward. Should investigate if there is a
|
|
|
|
# more appropriate way.
|
|
|
|
self.seen_messages[msg_id] = 1
|
2019-07-25 16:58:00 +08:00
|
|
|
|
|
|
|
def _is_subscribed_to_msg(self, msg: rpc_pb2.Message) -> bool:
|
2019-07-30 23:41:28 +08:00
|
|
|
if not self.my_topics:
|
2019-07-25 16:58:00 +08:00
|
|
|
return False
|
|
|
|
return all([topic in self.my_topics for topic in msg.topicIDs])
|