protocol_address.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use log::*;
  2. use smol::Executor;
  3. use std::sync::Arc;
  4. use crate::net::error::NetResult;
  5. use crate::net::message_subscriber::MessageSubscription;
  6. use crate::net::messages;
  7. use crate::net::protocols::{ProtocolJobsManager, ProtocolJobsManagerPtr};
  8. use crate::net::{ChannelPtr, HostsPtr};
  9. /// Protocol for address and get-address messages.
  10. pub struct ProtocolAddress {
  11. channel: ChannelPtr,
  12. addrs_sub: MessageSubscription<messages::AddrsMessage>,
  13. get_addrs_sub: MessageSubscription<messages::GetAddrsMessage>,
  14. hosts: HostsPtr,
  15. jobsman: ProtocolJobsManagerPtr,
  16. }
  17. impl ProtocolAddress {
  18. /// Create a new address protocol. Makes an address and get-address subscription and adds them
  19. /// to the address protocol instance.
  20. pub async fn new(channel: ChannelPtr, hosts: HostsPtr) -> Arc<Self> {
  21. // Creates a subscription to address message.
  22. let addrs_sub = channel
  23. .clone()
  24. .subscribe_msg::<messages::AddrsMessage>()
  25. .await
  26. .expect("Missing addrs dispatcher!");
  27. // Creates a subscription to get-address message.
  28. let get_addrs_sub = channel
  29. .clone()
  30. .subscribe_msg::<messages::GetAddrsMessage>()
  31. .await
  32. .expect("Missing getaddrs dispatcher!");
  33. Arc::new(Self {
  34. channel: channel.clone(),
  35. addrs_sub,
  36. get_addrs_sub,
  37. hosts,
  38. jobsman: ProtocolJobsManager::new("ProtocolAddress", channel),
  39. })
  40. }
  41. /// Starts the address protocol. Runs receive address and get address protocols on the protocol
  42. /// task manager. Then sends get-address message.
  43. pub async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) {
  44. debug!(target: "net", "ProtocolAddress::start() [START]");
  45. self.jobsman.clone().start(executor.clone());
  46. self.jobsman
  47. .clone()
  48. .spawn(self.clone().handle_receive_addrs(), executor.clone())
  49. .await;
  50. self.jobsman
  51. .clone()
  52. .spawn(self.clone().handle_receive_get_addrs(), executor)
  53. .await;
  54. // Send get_address message.
  55. let get_addrs = messages::GetAddrsMessage {};
  56. let _ = self.channel.clone().send(get_addrs).await;
  57. debug!(target: "net", "ProtocolAddress::start() [END]");
  58. }
  59. /// Handles receiving the address message. Loops to continually recieve address messages on the
  60. /// address subsciption. Adds the recieved addresses to the list of hosts.
  61. async fn handle_receive_addrs(self: Arc<Self>) -> NetResult<()> {
  62. debug!(target: "net", "ProtocolAddress::handle_receive_addrs() [START]");
  63. loop {
  64. let addrs_msg = self.addrs_sub.receive().await?;
  65. debug!(
  66. target: "net",
  67. "ProtocolAddress::handle_receive_addrs() received {} addrs",
  68. addrs_msg.addrs.len()
  69. );
  70. for (i, addr) in addrs_msg.addrs.iter().enumerate() {
  71. debug!(" addr[{}]: {}", i, addr);
  72. }
  73. self.hosts.store(addrs_msg.addrs.clone()).await;
  74. }
  75. }
  76. /// Handles receiving the get-address message. Continually recieves get-address messages on the
  77. /// get-address subsciption. Then replies with an address message.
  78. async fn handle_receive_get_addrs(self: Arc<Self>) -> NetResult<()> {
  79. debug!(target: "net", "ProtocolAddress::handle_receive_get_addrs() [START]");
  80. loop {
  81. let _get_addrs = self.get_addrs_sub.receive().await?;
  82. debug!(target: "net", "ProtocolAddress::handle_receive_get_addrs() received GetAddrs message");
  83. // Loads the list of hosts.
  84. let addrs = self.hosts.load_all().await;
  85. debug!(
  86. target: "net",
  87. "ProtocolAddress::handle_receive_get_addrs() sending {} addrs",
  88. addrs.len()
  89. );
  90. // Creates an address messages containing host address.
  91. let addrs_msg = messages::AddrsMessage { addrs };
  92. // Sends the address message across the channel.
  93. self.channel.clone().send(addrs_msg).await?;
  94. }
  95. }
  96. }