/// <inheritdoc /> public Task StopAsync() { log.Debug("Stopping"); Swarm.ConnectionEstablished -= Swarm_ConnectionEstablished; Swarm.PeerDisconnected -= Swarm_PeerDisconnected; Swarm.RemoveProtocol(this); RemoteTopics.Clear(); localTopics.Clear(); return Task.CompletedTask; }
/// <summary> /// Process a subscription request from another peer. /// </summary> /// <param name="sub"> /// The subscription request. /// </param> /// <param name="remote"> /// The remote <see cref="Peer"/>. /// </param> /// <seealso cref="RemoteTopics"/> /// <remarks> /// Maintains the <see cref="RemoteTopics"/>. /// </remarks> public void ProcessSubscription(Subscription sub, Peer remote) { if (sub.Subscribe) { log.Debug($"Subscribe '{sub.Topic}' by {remote}"); RemoteTopics.AddInterest(sub.Topic, remote); } else { log.Debug($"Unsubscribe '{sub.Topic}' by {remote}"); RemoteTopics.RemoveInterest(sub.Topic, remote); } }
/// <inheritdoc /> public Task PublishAsync(PublishedMessage message, CancellationToken cancel) { if (tracker.RecentlySeen(message.MessageId)) return Task.CompletedTask; // Find a set of peers that are interested in the topic(s). // Exclude author and sender var peers = message.Topics .SelectMany(topic => RemoteTopics.GetPeers(topic)) .Where(peer => peer != message.Sender) .Where(peer => peer != message.Forwarder); // Forward the message. var forward = new PubSubMessage { PublishedMessages = new PublishedMessage[] { message } }; return SendAsync(forward, peers, cancel); }
/// <summary> /// Raised when the peer has no more connections. /// </summary> /// <param name="sender"></param> /// <param name="peer"></param> /// <remarks> /// Removes the <paramref name="peer"/> from the /// <see cref="RemoteTopics"/>. /// </remarks> void Swarm_PeerDisconnected(object sender, Peer peer) { RemoteTopics.Clear(peer); }
/// <inheritdoc /> public IEnumerable<Peer> InterestedPeers(string topic) { return RemoteTopics.GetPeers(topic); }