protocol_raft.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use async_std::sync::{Arc, Mutex};
  2. use async_executor::Executor;
  3. use async_trait::async_trait;
  4. use log::debug;
  5. use crate::{net, Result};
  6. use super::primitives::{NetMsg, NodeId};
  7. pub struct ProtocolRaft {
  8. id: Option<NodeId>,
  9. jobsman: net::ProtocolJobsManagerPtr,
  10. notify_queue_sender: async_channel::Sender<NetMsg>,
  11. msg_sub: net::MessageSubscription<NetMsg>,
  12. p2p: net::P2pPtr,
  13. seen_msgs: Arc<Mutex<Vec<u64>>>,
  14. }
  15. impl ProtocolRaft {
  16. pub async fn init(
  17. id: Option<NodeId>,
  18. channel: net::ChannelPtr,
  19. notify_queue_sender: async_channel::Sender<NetMsg>,
  20. p2p: net::P2pPtr,
  21. seen_msgs: Arc<Mutex<Vec<u64>>>,
  22. ) -> net::ProtocolBasePtr {
  23. let message_subsytem = channel.get_message_subsystem();
  24. message_subsytem.add_dispatch::<NetMsg>().await;
  25. let msg_sub = channel.subscribe_msg::<NetMsg>().await.expect("Missing NetMsg dispatcher!");
  26. Arc::new(Self {
  27. id,
  28. notify_queue_sender,
  29. msg_sub,
  30. jobsman: net::ProtocolJobsManager::new("ProtocolRaft", channel),
  31. p2p,
  32. seen_msgs,
  33. })
  34. }
  35. async fn handle_receive_msg(self: Arc<Self>) -> Result<()> {
  36. debug!(target: "raft", "ProtocolRaft::handle_receive_msg() [START]");
  37. loop {
  38. let msg = self.msg_sub.receive().await?;
  39. debug!(
  40. target: "raft",
  41. "ProtocolRaft::handle_receive_msg() received id: {:?} method {:?}",
  42. &msg.id, &msg.method
  43. );
  44. {
  45. let mut msgs = self.seen_msgs.lock().await;
  46. if msgs.contains(&msg.id) {
  47. continue
  48. }
  49. msgs.push(msg.id);
  50. }
  51. let msg = (*msg).clone();
  52. self.p2p.broadcast(msg.clone()).await?;
  53. // check if the ids are equal when both
  54. // the local node and recipient ids are Some(id)
  55. if let Some(self_id) = &self.id {
  56. if let Some(recipient_id) = &msg.recipient_id {
  57. if self_id != recipient_id {
  58. continue
  59. }
  60. }
  61. }
  62. self.notify_queue_sender.send(msg).await?;
  63. }
  64. }
  65. }
  66. #[async_trait]
  67. impl net::ProtocolBase for ProtocolRaft {
  68. /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the
  69. /// protocol task manager, then queues the reply. Sends out a ping and
  70. /// waits for pong reply. Waits for ping and replies with a pong.
  71. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  72. debug!(target: "raft", "ProtocolRaft::start() [START]");
  73. self.jobsman.clone().start(executor.clone());
  74. self.jobsman.clone().spawn(self.clone().handle_receive_msg(), executor.clone()).await;
  75. debug!(target: "raft", "ProtocolRaft::start() [END]");
  76. Ok(())
  77. }
  78. fn name(&self) -> &'static str {
  79. "ProtocolRaft"
  80. }
  81. }
  82. impl net::Message for NetMsg {
  83. fn name() -> &'static str {
  84. "netmsg"
  85. }
  86. }