protocol_privmsg.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use async_std::sync::Arc;
  2. use async_executor::Executor;
  3. use async_trait::async_trait;
  4. use log::debug;
  5. use darkfi::{net, Result};
  6. use crate::{
  7. buffers::{ArcPrivmsgsBuffer, SeenMsgIds},
  8. Privmsg,
  9. };
  10. pub struct ProtocolPrivmsg {
  11. jobsman: net::ProtocolJobsManagerPtr,
  12. notify: async_channel::Sender<Privmsg>,
  13. msg_sub: net::MessageSubscription<Privmsg>,
  14. p2p: net::P2pPtr,
  15. msg_ids: SeenMsgIds,
  16. msgs: ArcPrivmsgsBuffer,
  17. channel: net::ChannelPtr,
  18. }
  19. impl ProtocolPrivmsg {
  20. pub async fn init(
  21. channel: net::ChannelPtr,
  22. notify: async_channel::Sender<Privmsg>,
  23. p2p: net::P2pPtr,
  24. msg_ids: SeenMsgIds,
  25. msgs: ArcPrivmsgsBuffer,
  26. ) -> net::ProtocolBasePtr {
  27. let message_subsytem = channel.get_message_subsystem();
  28. message_subsytem.add_dispatch::<Privmsg>().await;
  29. let msg_sub =
  30. channel.subscribe_msg::<Privmsg>().await.expect("Missing Privmsg dispatcher!");
  31. Arc::new(Self {
  32. notify,
  33. msg_sub,
  34. jobsman: net::ProtocolJobsManager::new("ProtocolPrivmsg", channel.clone()),
  35. p2p,
  36. msg_ids,
  37. msgs,
  38. channel,
  39. })
  40. }
  41. async fn handle_receive_msg(self: Arc<Self>) -> Result<()> {
  42. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_msg() [START]");
  43. let exclude_list = vec![self.channel.address()];
  44. // once a channel get started
  45. let mut msgs_buffer = self.msgs.lock().await;
  46. msgs_buffer.update();
  47. for m in msgs_buffer.iter() {
  48. self.channel.send(m.clone()).await?;
  49. }
  50. drop(msgs_buffer);
  51. loop {
  52. let msg = self.msg_sub.receive().await?;
  53. let msg = (*msg).to_owned();
  54. let mut msg_ids = self.msg_ids.lock().await;
  55. if msg_ids.contains(&msg.id) {
  56. continue
  57. }
  58. msg_ids.push(msg.id);
  59. drop(msg_ids);
  60. // add the msg to the buffer
  61. let mut msgs = self.msgs.lock().await;
  62. msgs.push(&msg);
  63. msgs.update();
  64. drop(msgs);
  65. self.notify.send(msg.clone()).await?;
  66. self.p2p.broadcast_with_exclude(msg, &exclude_list).await?;
  67. }
  68. }
  69. }
  70. #[async_trait]
  71. impl net::ProtocolBase for ProtocolPrivmsg {
  72. /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the
  73. /// protocol task manager, then queues the reply. Sends out a ping and
  74. /// waits for pong reply. Waits for ping and replies with a pong.
  75. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  76. debug!(target: "ircd", "ProtocolPrivmsg::start() [START]");
  77. self.jobsman.clone().start(executor.clone());
  78. self.jobsman.clone().spawn(self.clone().handle_receive_msg(), executor.clone()).await;
  79. debug!(target: "ircd", "ProtocolPrivmsg::start() [END]");
  80. Ok(())
  81. }
  82. fn name(&self) -> &'static str {
  83. "ProtocolPrivmsg"
  84. }
  85. }
  86. impl net::Message for Privmsg {
  87. fn name() -> &'static str {
  88. "privmsg"
  89. }
  90. }