protocol_privmsg.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_std::sync::{Arc, Mutex};
  19. use async_trait::async_trait;
  20. use darkfi_serial::{SerialDecodable, SerialEncodable};
  21. use log::debug;
  22. use smol::Executor;
  23. use darkfi::{net, Result};
  24. use crate::{buffers::SeenIds, Privmsg};
  25. #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
  26. struct InvObject(String);
  27. pub struct ProtocolPrivmsg {
  28. jobsman: net::ProtocolJobsManagerPtr,
  29. notify: smol::channel::Sender<Privmsg>,
  30. msg_sub: net::MessageSubscription<Privmsg>,
  31. p2p: net::P2pPtr,
  32. channel: net::ChannelPtr,
  33. seen: Arc<Mutex<SeenIds>>,
  34. }
  35. impl ProtocolPrivmsg {
  36. pub async fn init(
  37. channel: net::ChannelPtr,
  38. notify: smol::channel::Sender<Privmsg>,
  39. p2p: net::P2pPtr,
  40. seen: Arc<Mutex<SeenIds>>,
  41. ) -> net::ProtocolBasePtr {
  42. let message_subsytem = channel.get_message_subsystem();
  43. message_subsytem.add_dispatch::<Privmsg>().await;
  44. let msg_sub =
  45. channel.clone().subscribe_msg::<Privmsg>().await.expect("Missing Privmsg dispatcher!");
  46. Arc::new(Self {
  47. notify,
  48. msg_sub,
  49. jobsman: net::ProtocolJobsManager::new("ProtocolPrivmsg", channel.clone()),
  50. p2p,
  51. channel,
  52. seen,
  53. })
  54. }
  55. async fn handle_receive_msg(self: Arc<Self>) -> Result<()> {
  56. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_msg() [START]");
  57. let exclude_list = vec![self.channel.address()];
  58. loop {
  59. let msg = self.msg_sub.receive().await?;
  60. let msg = (*msg).to_owned();
  61. {
  62. let ids = &mut self.seen.lock().await;
  63. if !ids.push(msg.id) {
  64. continue
  65. }
  66. }
  67. self.notify.send(msg.clone()).await?;
  68. self.p2p.broadcast_with_exclude(msg, &exclude_list).await?;
  69. }
  70. }
  71. }
  72. #[async_trait]
  73. impl net::ProtocolBase for ProtocolPrivmsg {
  74. /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the
  75. /// protocol task manager, then queues the reply. Sends out a ping and
  76. /// waits for pong reply. Waits for ping and replies with a pong.
  77. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  78. debug!(target: "ircd", "ProtocolPrivmsg::start() [START]");
  79. self.jobsman.clone().start(executor.clone());
  80. self.jobsman.clone().spawn(self.clone().handle_receive_msg(), executor.clone()).await;
  81. debug!(target: "ircd", "ProtocolPrivmsg::start() [END]");
  82. Ok(())
  83. }
  84. fn name(&self) -> &'static str {
  85. "ProtocolPrivmsg"
  86. }
  87. }
  88. impl net::Message for Privmsg {
  89. fn name() -> &'static str {
  90. "privmsg"
  91. }
  92. }