debugmsg.rs 3.4 KB

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