protocol_seed.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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. use std::sync::Arc;
  19. use async_trait::async_trait;
  20. use log::debug;
  21. use smol::Executor;
  22. use crate::Result;
  23. use super::{
  24. super::{
  25. message, message_subscriber::MessageSubscription, ChannelPtr, HostsPtr, P2pPtr, SettingsPtr,
  26. },
  27. ProtocolBase, ProtocolBasePtr,
  28. };
  29. /// Implements the seed protocol.
  30. pub struct ProtocolSeed {
  31. channel: ChannelPtr,
  32. hosts: HostsPtr,
  33. settings: SettingsPtr,
  34. addr_sub: MessageSubscription<message::AddrsMessage>,
  35. }
  36. impl ProtocolSeed {
  37. /// Create a new seed protocol.
  38. pub async fn init(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr {
  39. let hosts = p2p.hosts();
  40. let settings = p2p.settings();
  41. //// Create a subscription to address message.
  42. let addr_sub = channel
  43. .clone()
  44. .subscribe_msg::<message::AddrsMessage>()
  45. .await
  46. .expect("Missing addr dispatcher!");
  47. Arc::new(Self { channel, hosts, settings, addr_sub })
  48. }
  49. /// Sends own external addresses over a channel. Imports own external addresses
  50. /// from settings, then adds that addresses to an address message and
  51. /// sends it out over the channel.
  52. pub async fn send_self_address(&self) -> Result<()> {
  53. // Do nothing if external addresses are not configured
  54. if self.settings.external_addr.is_empty() {
  55. return Ok(())
  56. }
  57. let ext_addrs = self.settings.external_addr.clone();
  58. debug!(target: "net", "ProtocolSeed::send_self_address() ext_addrs={:?}", ext_addrs);
  59. let ext_addr_msg = message::ExtAddrsMessage { ext_addrs };
  60. self.channel.clone().send(ext_addr_msg).await
  61. }
  62. }
  63. #[async_trait]
  64. impl ProtocolBase for ProtocolSeed {
  65. /// Starts the seed protocol. Creates a subscription to the address message,
  66. /// then sends our address to the seed server. Sends a get-address
  67. /// message and receives an address message.
  68. async fn start(self: Arc<Self>, _executor: Arc<Executor<'_>>) -> Result<()> {
  69. debug!(target: "net::protocol_seed::send_self_address()", "ProtocolSeed::start() [START]");
  70. // Send own address to the seed server.
  71. self.send_self_address().await?;
  72. // Send get address message.
  73. let get_addr = message::GetAddrsMessage {};
  74. self.channel.clone().send(get_addr).await?;
  75. // Receive addresses.
  76. let addrs_msg = self.addr_sub.receive().await?;
  77. debug!(target: "net::protocol_seed::send_self_address()", "ProtocolSeed::start() received {} addrs", addrs_msg.addrs.len());
  78. self.hosts.store(addrs_msg.addrs.clone()).await;
  79. debug!(target: "net::protocol_seed::send_self_address()", "ProtocolSeed::start() [END]");
  80. Ok(())
  81. }
  82. fn name(&self) -> &'static str {
  83. "ProtocolSeed"
  84. }
  85. }