privmsg.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use std::sync::Arc;
  2. use async_channel::Sender;
  3. use async_executor::Executor;
  4. use async_std::sync::Mutex;
  5. use async_trait::async_trait;
  6. use fxhash::FxHashSet;
  7. use log::debug;
  8. use darkfi::{
  9. net,
  10. util::serial::{SerialDecodable, SerialEncodable},
  11. Result,
  12. };
  13. pub type PrivmsgId = u32;
  14. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  15. pub struct Privmsg {
  16. pub id: PrivmsgId,
  17. pub nickname: String,
  18. pub channel: String,
  19. pub message: String,
  20. }
  21. impl net::Message for Privmsg {
  22. fn name() -> &'static str {
  23. "privmsg"
  24. }
  25. }
  26. pub struct SeenPrivmsgIds {
  27. ids: Mutex<FxHashSet<PrivmsgId>>,
  28. }
  29. pub type SeenPrivmsgIdsPtr = Arc<SeenPrivmsgIds>;
  30. impl SeenPrivmsgIds {
  31. pub fn new() -> Arc<Self> {
  32. Arc::new(Self { ids: Mutex::new(FxHashSet::default()) })
  33. }
  34. pub async fn add_seen(&self, id: u32) {
  35. self.ids.lock().await.insert(id);
  36. }
  37. pub async fn is_seen(&self, id: u32) -> bool {
  38. self.ids.lock().await.contains(&id)
  39. }
  40. }
  41. pub struct ProtocolPrivmsg {
  42. notify_queue_sender: Sender<Arc<Privmsg>>,
  43. privmsg_sub: net::MessageSubscription<Privmsg>,
  44. jobsman: net::ProtocolJobsManagerPtr,
  45. seen_ids: SeenPrivmsgIdsPtr,
  46. p2p: net::P2pPtr,
  47. }
  48. #[async_trait]
  49. impl net::ProtocolBase for ProtocolPrivmsg {
  50. /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the
  51. /// protocol task manager, then queues the reply. Sends out a ping and
  52. /// waits for pong reply. Waits for ping and replies with a pong.
  53. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  54. debug!(target: "ircd", "ProtocolPrivMsg::start() [START]");
  55. self.jobsman.clone().start(executor.clone());
  56. self.jobsman.clone().spawn(self.clone().handle_receive_privmsg(), executor.clone()).await;
  57. debug!(target: "ircd", "ProtocolPrivmsg::start() [END]");
  58. Ok(())
  59. }
  60. fn name(&self) -> &'static str {
  61. "ProtocolPrivMsg"
  62. }
  63. }
  64. impl ProtocolPrivmsg {
  65. pub async fn init(
  66. channel: net::ChannelPtr,
  67. notify_queue_sender: Sender<Arc<Privmsg>>,
  68. seen_ids: SeenPrivmsgIdsPtr,
  69. p2p: net::P2pPtr,
  70. ) -> net::ProtocolBasePtr {
  71. let message_subsystem = channel.get_message_subsystem();
  72. message_subsystem.add_dispatch::<Privmsg>().await;
  73. let sub = channel.subscribe_msg::<Privmsg>().await.expect("Missing Privmsg dispatcher!");
  74. Arc::new(Self {
  75. notify_queue_sender,
  76. privmsg_sub: sub,
  77. jobsman: net::ProtocolJobsManager::new("PrivmsgProtocol", channel),
  78. seen_ids,
  79. p2p,
  80. })
  81. }
  82. async fn handle_receive_privmsg(self: Arc<Self>) -> Result<()> {
  83. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_privmsg() [START]");
  84. loop {
  85. let privmsg = self.privmsg_sub.receive().await?;
  86. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_privmsg() received {:?}", privmsg);
  87. // Do we already have this message?
  88. if self.seen_ids.is_seen(privmsg.id).await {
  89. continue
  90. }
  91. self.seen_ids.add_seen(privmsg.id).await;
  92. // If not, then broadcast to network.
  93. let privmsg_copy = (*privmsg).clone();
  94. self.p2p.broadcast(privmsg_copy).await?;
  95. self.notify_queue_sender.send(privmsg).await.expect("notify_queue_sender send failed!");
  96. }
  97. }
  98. }