|
|
@@ -1,10 +1,15 @@
|
|
|
-use log::*;
|
|
|
+use async_trait::async_trait;
|
|
|
+use log::debug;
|
|
|
use smol::Executor;
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
use crate::{
|
|
|
error::Result,
|
|
|
- net::{message, ChannelPtr, HostsPtr, SettingsPtr},
|
|
|
+ net::{
|
|
|
+ message,
|
|
|
+ protocol::{ProtocolBase, ProtocolBasePtr},
|
|
|
+ ChannelPtr, HostsPtr, P2pPtr, SettingsPtr,
|
|
|
+ },
|
|
|
};
|
|
|
|
|
|
/// Implements the seed protocol.
|
|
|
@@ -20,10 +25,35 @@ impl ProtocolSeed {
|
|
|
Arc::new(Self { channel, hosts, settings })
|
|
|
}
|
|
|
|
|
|
+ pub async fn new2(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr {
|
|
|
+ let hosts = p2p.hosts();
|
|
|
+ let settings = p2p.settings();
|
|
|
+
|
|
|
+ Arc::new(Self { channel, hosts, settings })
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Sends own external address over a channel. Imports own external address
|
|
|
+ /// from settings, then adds that address to an address message and
|
|
|
+ /// sends it out over the channel.
|
|
|
+ pub async fn send_self_address(&self) -> Result<()> {
|
|
|
+ match self.settings.external_addr {
|
|
|
+ Some(addr) => {
|
|
|
+ debug!(target: "net", "ProtocolSeed::send_own_address() addr={}", addr);
|
|
|
+ let addr = message::AddrsMessage { addrs: vec![addr] };
|
|
|
+ Ok(self.channel.clone().send(addr).await?)
|
|
|
+ }
|
|
|
+ // Do nothing if external address is not configured
|
|
|
+ None => Ok(()),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[async_trait]
|
|
|
+impl ProtocolBase for ProtocolSeed {
|
|
|
/// Starts the seed protocol. Creates a subscription to the address message,
|
|
|
/// then sends our address to the seed server. Sends a get-address
|
|
|
/// message and receives an address message.
|
|
|
- pub async fn start(self: Arc<Self>, _executor: Arc<Executor<'_>>) -> Result<()> {
|
|
|
+ async fn start(self: Arc<Self>, _executor: Arc<Executor<'_>>) -> Result<()> {
|
|
|
debug!(target: "net", "ProtocolSeed::start() [START]");
|
|
|
// Create a subscription to address message.
|
|
|
let addr_sub = self
|
|
|
@@ -48,19 +78,4 @@ impl ProtocolSeed {
|
|
|
debug!(target: "net", "ProtocolSeed::start() [END]");
|
|
|
Ok(())
|
|
|
}
|
|
|
-
|
|
|
- /// Sends own external address over a channel. Imports own external address
|
|
|
- /// from settings, then adds that address to an address message and
|
|
|
- /// sends it out over the channel.
|
|
|
- pub async fn send_self_address(&self) -> Result<()> {
|
|
|
- match self.settings.external_addr {
|
|
|
- Some(addr) => {
|
|
|
- debug!(target: "net", "ProtocolSeed::send_own_address() addr={}", addr);
|
|
|
- let addr = message::AddrsMessage { addrs: vec![addr] };
|
|
|
- Ok(self.channel.clone().send(addr).await?)
|
|
|
- }
|
|
|
- // Do nothing if external address is not configured
|
|
|
- None => Ok(()),
|
|
|
- }
|
|
|
- }
|
|
|
}
|