Просмотр исходного кода

net: if it's an outbound session + has an external_addr, then:

* start another process in the jobsman
* loop send our addr
* sleep for 20 min
lunar-mining 4 лет назад
Родитель
Сommit
ac5c97cb76

+ 6 - 1
src/net/channel.rs

@@ -19,7 +19,7 @@ use crate::{
 use super::{
     message,
     message_subscriber::{MessageSubscription, MessageSubsystem},
-    Session, SessionWeakPtr, TransportStream,
+    Session, SessionBitflag, SessionWeakPtr, TransportStream,
 };
 
 /// Atomic pointer to async channel.
@@ -327,4 +327,9 @@ impl Channel {
     fn session(&self) -> Arc<dyn Session> {
         self.session.upgrade().unwrap()
     }
+
+    pub fn session_type_id(&self) -> SessionBitflag {
+        let session = self.session();
+        session.type_id()
+    }
 }

+ 2 - 2
src/net/mod.rs

@@ -97,8 +97,8 @@ pub use message_subscriber::MessageSubscription;
 pub use p2p::{P2p, P2pPtr};
 pub use protocol::{ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr};
 pub use session::{
-    Session, SessionWeakPtr, SESSION_ALL, SESSION_INBOUND, SESSION_MANUAL, SESSION_OUTBOUND,
-    SESSION_SEED,
+    Session, SessionBitflag, SessionWeakPtr, SESSION_ALL, SESSION_INBOUND, SESSION_MANUAL,
+    SESSION_OUTBOUND, SESSION_SEED,
 };
 pub use settings::{Settings, SettingsPtr};
 pub use transport::{

+ 35 - 2
src/net/protocol/protocol_address.rs

@@ -3,14 +3,19 @@ use std::sync::Arc;
 use async_trait::async_trait;
 use log::debug;
 use smol::Executor;
+use url::Url;
 
-use crate::Result;
+use crate::{util::async_util, Result};
 
 use super::{
-    super::{message, message_subscriber::MessageSubscription, ChannelPtr, HostsPtr, P2pPtr},
+    super::{
+        message, message_subscriber::MessageSubscription, ChannelPtr, HostsPtr, P2pPtr, SettingsPtr,
+    },
     ProtocolBase, ProtocolBasePtr, ProtocolJobsManager, ProtocolJobsManagerPtr,
 };
 
+const SEND_ADDR_SLEEP_SECONDS: u64 = 10;
+
 /// Defines address and get-address messages.
 pub struct ProtocolAddress {
     channel: ChannelPtr,
@@ -18,12 +23,14 @@ pub struct ProtocolAddress {
     get_addrs_sub: MessageSubscription<message::GetAddrsMessage>,
     hosts: HostsPtr,
     jobsman: ProtocolJobsManagerPtr,
+    settings: SettingsPtr,
 }
 
 impl ProtocolAddress {
     /// Create a new address protocol. Makes an address and get-address
     /// subscription and adds them to the address protocol instance.
     pub async fn init(channel: ChannelPtr, p2p: P2pPtr) -> ProtocolBasePtr {
+        let settings = p2p.settings();
         let hosts = p2p.hosts();
 
         // Creates a subscription to address message.
@@ -46,6 +53,7 @@ impl ProtocolAddress {
             get_addrs_sub,
             hosts,
             jobsman: ProtocolJobsManager::new("ProtocolAddress", channel),
+            settings,
         })
     }
 
@@ -92,6 +100,16 @@ impl ProtocolAddress {
             self.channel.clone().send(addrs_msg).await?;
         }
     }
+
+    async fn send_addrs(self: Arc<Self>, addrs: Vec<Url>) -> Result<()> {
+        debug!(target: "net", "ProtocolAddress::send_addrs() [START]");
+        loop {
+            let addrs = addrs.clone();
+            let addr_msg = message::AddrsMessage { addrs };
+            self.channel.clone().send(addr_msg).await?;
+            async_util::sleep(SEND_ADDR_SLEEP_SECONDS).await;
+        }
+    }
 }
 
 #[async_trait]
@@ -100,6 +118,21 @@ impl ProtocolBase for ProtocolAddress {
     /// protocols on the protocol task manager. Then sends get-address
     /// message.
     async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
+        let type_id = self.channel.session_type_id();
+
+        // if it's an outbound session + has an external address
+        // send our address
+        if type_id == 0b0010 && self.settings.external_addr.is_some() {
+            self.jobsman.clone().start(executor.clone());
+            self.jobsman
+                .clone()
+                .spawn(
+                    self.clone().send_addrs(vec![self.settings.external_addr.clone().unwrap()]),
+                    executor.clone(),
+                )
+                .await;
+        }
+
         debug!(target: "net", "ProtocolAddress::start() [START]");
         self.jobsman.clone().start(executor.clone());
         self.jobsman.clone().spawn(self.clone().handle_receive_addrs(), executor.clone()).await;

+ 9 - 1
src/net/protocol/protocol_registry.rs

@@ -2,7 +2,7 @@ use async_std::sync::Mutex;
 use std::future::Future;
 
 use futures::future::BoxFuture;
-use log::debug;
+use log::{debug, warn};
 
 use super::{
     super::{session::SessionBitflag, ChannelPtr, P2pPtr},
@@ -49,11 +49,19 @@ impl ProtocolRegistry {
         for (session_flags, construct) in self.protocol_constructors.lock().await.iter() {
             // Skip protocols that are not registered for this session
             if selector_id & session_flags == 0 {
+                // debug
+                //warn!("Skipping {:?}, {:?}", selector_id, session_flags);
                 continue
             }
 
             let protocol: ProtocolBasePtr = construct(channel.clone(), p2p.clone()).await;
             debug!(target: "net", "Attached {}", protocol.name());
+
+            // debug
+            //if protocol.name() == "ProtocolAddress" {
+            //    warn!("PROTOCOL ADDRESS ATTACHED");
+            //}
+
             protocols.push(protocol)
         }
         protocols