protocol_dchat.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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. // ANCHOR: protocol_dchat
  19. use async_trait::async_trait;
  20. use darkfi::{net, Result};
  21. use smol::Executor;
  22. use std::sync::Arc;
  23. use tracing::debug;
  24. use crate::dchatmsg::{DchatMsg, DchatMsgsBuffer};
  25. pub struct ProtocolDchat {
  26. jobsman: net::ProtocolJobsManagerPtr,
  27. msg_sub: net::MessageSubscription<DchatMsg>,
  28. msgs: DchatMsgsBuffer,
  29. }
  30. // ANCHOR_END: protocol_dchat
  31. // ANCHOR: constructor
  32. impl ProtocolDchat {
  33. pub async fn init(channel: net::ChannelPtr, msgs: DchatMsgsBuffer) -> net::ProtocolBasePtr {
  34. debug!(target: "dchat", "ProtocolDchat::init() [START]");
  35. let message_subsytem = channel.message_subsystem();
  36. message_subsytem.add_dispatch::<DchatMsg>().await;
  37. let msg_sub =
  38. channel.subscribe_msg::<DchatMsg>().await.expect("Missing DchatMsg dispatcher!");
  39. Arc::new(Self {
  40. jobsman: net::ProtocolJobsManager::new("ProtocolDchat", channel.clone()),
  41. msg_sub,
  42. msgs,
  43. })
  44. }
  45. // ANCHOR_END: constructor
  46. // ANCHOR: receive
  47. async fn handle_receive_msg(self: Arc<Self>) -> Result<()> {
  48. debug!(target: "dchat", "ProtocolDchat::handle_receive_msg() [START]");
  49. while let Ok(msg) = self.msg_sub.receive().await {
  50. let msg = (*msg).to_owned();
  51. self.msgs.lock().await.push(msg);
  52. }
  53. Ok(())
  54. }
  55. // ANCHOR_END: receive
  56. }
  57. #[async_trait]
  58. impl net::ProtocolBase for ProtocolDchat {
  59. // ANCHOR: start
  60. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  61. debug!(target: "dchat", "ProtocolDchat::ProtocolBase::start() [START]");
  62. self.jobsman.clone().start(executor.clone());
  63. self.jobsman.clone().spawn(self.clone().handle_receive_msg(), executor.clone()).await;
  64. debug!(target: "dchat", "ProtocolDchat::ProtocolBase::start() [STOP]");
  65. Ok(())
  66. }
  67. // ANCHOR_END: start
  68. fn name(&self) -> &'static str {
  69. "ProtocolDchat"
  70. }
  71. }