FloodSub: Add publish_any (#1193)

* FloodSub: Add `publish_any`

to emit messages even if we are not subscribed to the topic.

* Review suggestion: Add `publish_many_inner`
This commit is contained in:
Jens Krause 2019-07-05 18:28:52 +02:00 committed by Pierre Krieger
parent 68c36d87d3
commit c11cb4d1a1

View File

@ -167,17 +167,30 @@ impl<TSubstream> Floodsub<TSubstream> {
true true
} }
/// Publishes a message to the network. /// Publishes a message to the network, if we're subscribed to the topic only.
///
/// > **Note**: Doesn't do anything if we're not subscribed to the topic.
pub fn publish(&mut self, topic: impl Into<TopicHash>, data: impl Into<Vec<u8>>) { pub fn publish(&mut self, topic: impl Into<TopicHash>, data: impl Into<Vec<u8>>) {
self.publish_many(iter::once(topic), data) self.publish_many(iter::once(topic), data)
} }
/// Publishes a message to the network, even if we're not subscribed to the topic.
pub fn publish_any(&mut self, topic: impl Into<TopicHash>, data: impl Into<Vec<u8>>) {
self.publish_many_any(iter::once(topic), data)
}
/// Publishes a message with multiple topics to the network. /// Publishes a message with multiple topics to the network.
/// ///
///
/// > **Note**: Doesn't do anything if we're not subscribed to any of the topics. /// > **Note**: Doesn't do anything if we're not subscribed to any of the topics.
pub fn publish_many(&mut self, topic: impl IntoIterator<Item = impl Into<TopicHash>>, data: impl Into<Vec<u8>>) { pub fn publish_many(&mut self, topic: impl IntoIterator<Item = impl Into<TopicHash>>, data: impl Into<Vec<u8>>) {
self.publish_many_inner(topic, data, true)
}
/// Publishes a message with multiple topics to the network, even if we're not subscribed to any of the topics.
pub fn publish_many_any(&mut self, topic: impl IntoIterator<Item = impl Into<TopicHash>>, data: impl Into<Vec<u8>>) {
self.publish_many_inner(topic, data, false)
}
fn publish_many_inner(&mut self, topic: impl IntoIterator<Item = impl Into<TopicHash>>, data: impl Into<Vec<u8>>, check_self_subscriptions: bool) {
let message = FloodsubMessage { let message = FloodsubMessage {
source: self.local_peer_id.clone(), source: self.local_peer_id.clone(),
data: data.into(), data: data.into(),
@ -188,12 +201,15 @@ impl<TSubstream> Floodsub<TSubstream> {
topics: topic.into_iter().map(|t| t.into().clone()).collect(), topics: topic.into_iter().map(|t| t.into().clone()).collect(),
}; };
// Don't publish the message if we're not subscribed ourselves to any of the topics. let self_subscribed = self.subscribed_topics.iter().any(|t| message.topics.iter().any(|u| t.hash() == u));
if !self.subscribed_topics.iter().any(|t| message.topics.iter().any(|u| t.hash() == u)) { if self_subscribed {
return;
}
self.received.add(&message); self.received.add(&message);
}
// Don't publish the message if we have to check subscriptions
// and we're not subscribed ourselves to any of the topics.
if check_self_subscriptions && !self_subscribed {
return
}
// Send to peers we know are subscribed to the topic. // Send to peers we know are subscribed to the topic.
for (peer_id, sub_topic) in self.connected_peers.iter() { for (peer_id, sub_topic) in self.connected_peers.iter() {