From 2052dec9444ed8953cce916d397d4e28f8ed0caa Mon Sep 17 00:00:00 2001 From: asxalex Date: Thu, 4 Jul 2024 20:21:18 +0800 Subject: [PATCH] export get_edge --- src/lib.rs | 8 +++++++- src/network/node.rs | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 21ee249..6d44f2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ use std::time::Duration; use std::net::SocketAddr; +pub use network::get_edge; use network::{async_main, init_edge, NodeConfig}; use tokio::sync::mpsc::Receiver; use tokio_util::sync::CancellationToken; @@ -20,9 +21,14 @@ use sdlan_sn_rs::{ utils::{create_or_load_uuid, get_sdlan_sock_from_socketaddr, Result, SDLanError}, }; -pub async fn run_sdlan(args: CommandLine, start_stop_chan: Receiver) -> Result<()> { +pub async fn run_sdlan( + args: CommandLine, + // start_stop_sender: Sender, + // start_stop_receiver: Receiver, +) -> Result<()> { let _guard = log::init_log(); + let (start_stop_sender, start_stop_chan) = channel(20); let edge_uuid = create_or_load_uuid("")?; let node_conf = parse_config(edge_uuid, &args).await?; diff --git a/src/network/node.rs b/src/network/node.rs index dec278a..951ad59 100644 --- a/src/network/node.rs +++ b/src/network/node.rs @@ -4,6 +4,7 @@ use sdlan_sn_rs::config::{AF_INET, AF_INET6}; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicU8, Ordering}; use std::sync::{Arc, RwLock}; use tokio::io::AsyncReadExt; +use tokio::sync::mpsc::Sender; use crate::pb::{encode_to_tcp_message, SdlEmpty}; use crate::tcp::{get_tcp_conn, PacketType}; @@ -22,7 +23,12 @@ use sdlan_sn_rs::utils::{Result, SDLanError}; static EDGE: OnceCell = OnceCell::new(); -pub async fn init_edge(token: &str, node_conf: NodeConfig, tos: u32) -> Result<()> { +pub async fn init_edge( + token: &str, + node_conf: NodeConfig, + tos: u32, + start_stop: Sender, +) -> Result<()> { if token.len() == 0 { println!("failed to load token"); return Err(SDLanError::NormalError("no token is specified")); @@ -58,6 +64,7 @@ pub async fn init_edge(token: &str, node_conf: NodeConfig, tos: u32) -> Result<( token, privatekey, tcp_pong.clone(), + start_stop, ); do_init_edge(edge)?; @@ -86,6 +93,8 @@ pub struct Node { pub tcp_pong: Arc, + start_stop_sender: Sender, + // user token info pub _token: String, @@ -125,6 +134,14 @@ pub struct Node { unsafe impl Sync for Node {} impl Node { + pub async fn start(&self, token: String) { + self.start_stop_sender.send(token).await; + } + + pub async fn stop(&self) { + self.start_stop_sender.send("".to_owned()).await; + } + pub fn new( pubkey: String, config: NodeConfig, @@ -133,12 +150,15 @@ impl Node { token: &str, private: RsaPrivateKey, tcp_pong: Arc, + start_stop: Sender, ) -> Self { Self { packet_id: AtomicU32::new(1), network_id: AtomicU32::new(0), _token: token.to_owned(), + start_stop_sender: start_stop, + tcp_pong, device_config: DeviceConfig::new(),