protocol_raft.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. 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. 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. 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.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. match (self.id.clone(), msg.recipient_id.clone()) {
  54. // check if the ids are equal when both
  55. // the local node and recipient ids are Some(id)
  56. (Some(id), Some(m_id)) => {
  57. if id != m_id {
  58. continue
  59. }
  60. }
  61. // reject if both local node and recipient ids are None then
  62. (None, None) => continue,
  63. _ => {}
  64. }
  65. self.notify_queue_sender.send(msg).await?;
  66. }
  67. }
  68. }
  69. #[async_trait]
  70. impl net::ProtocolBase for ProtocolRaft {
  71. /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the
  72. /// protocol task manager, then queues the reply. Sends out a ping and
  73. /// waits for pong reply. Waits for ping and replies with a pong.
  74. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  75. debug!(target: "raft", "ProtocolRaft::start() [START]");
  76. self.jobsman.clone().start(executor.clone());
  77. self.jobsman.clone().spawn(self.clone().handle_receive_msg(), executor.clone()).await;
  78. debug!(target: "raft", "ProtocolRaft::start() [END]");
  79. Ok(())
  80. }
  81. fn name(&self) -> &'static str {
  82. "ProtocolRaft"
  83. }
  84. }
  85. impl net::Message for NetMsg {
  86. fn name() -> &'static str {
  87. "netmsg"
  88. }
  89. }