From 10fc2d99466ed569e3867db3262007bf479ee28f Mon Sep 17 00:00:00 2001 From: fluo10 Date: Thu, 5 Jun 2025 09:23:24 +0900 Subject: [PATCH] Add console command --- lazy-supplements/src/cli/console.rs | 28 ++++++++++++++++++++++++++++ lazy-supplements/src/cli/mod.rs | 2 ++ lazy-supplements/src/cli/server.rs | 15 +++------------ lazy-supplements/src/config/node.rs | 2 +- lazy-supplements/src/global/mod.rs | 25 +++++++++++++++++++++---- lazy-supplements/src/main.rs | 4 +++- 6 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 lazy-supplements/src/cli/console.rs diff --git a/lazy-supplements/src/cli/console.rs b/lazy-supplements/src/cli/console.rs new file mode 100644 index 0000000..7631d1f --- /dev/null +++ b/lazy-supplements/src/cli/console.rs @@ -0,0 +1,28 @@ +use std::time::Duration; + +use clap::Args; +use futures::StreamExt; +use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm}; +use tokio::time::sleep; +use tracing_subscriber::EnvFilter; + +use crate::{error::Error, global::GLOBAL}; + +use super::ConfigArgs; + +#[derive(Args, Debug)] +pub struct ConsoleArgs { + #[command(flatten)] + config: ConfigArgs, +} + +impl ConsoleArgs { + pub async fn start_console(self) -> Result<(), Error>{ + let _ = crate::global::GLOBAL.get_or_init_node_config(self.config.try_into_node_config().await?).await; + tokio::spawn( async { + GLOBAL.launch_swarm().await + }); + sleep(Duration::from_secs(1)).await; + Ok(()) + } +} \ No newline at end of file diff --git a/lazy-supplements/src/cli/mod.rs b/lazy-supplements/src/cli/mod.rs index 130b3e6..0884ed0 100644 --- a/lazy-supplements/src/cli/mod.rs +++ b/lazy-supplements/src/cli/mod.rs @@ -1,11 +1,13 @@ use std::path::PathBuf; mod config; +mod console; mod init; mod node; mod server; pub use config::ConfigArgs; +pub use console::ConsoleArgs; pub use init::InitArgs; pub use node::{ NodeArgs, NodeCommand, JoinNodeArgs }; pub use server::ServerArgs; \ No newline at end of file diff --git a/lazy-supplements/src/cli/server.rs b/lazy-supplements/src/cli/server.rs index 0a65dc5..62fe3ca 100644 --- a/lazy-supplements/src/cli/server.rs +++ b/lazy-supplements/src/cli/server.rs @@ -3,7 +3,7 @@ use futures::StreamExt; use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm}; use tracing_subscriber::EnvFilter; -use crate::error::Error; +use crate::{error::Error, global::GLOBAL}; use super::ConfigArgs; @@ -14,16 +14,7 @@ pub struct ServerArgs { } impl ServerArgs { pub async fn start_server(self) -> Result<(), Error>{ - let mut swarm = self.config.try_into_node_config().await?.try_into_swarm().await?; - loop{ - match swarm.select_next_some().await { - SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), - SwarmEvent::Behaviour(event) => { - println!("{event:?}"); - event.run().await; - }, - _ => {} - } - } + let _ = crate::global::GLOBAL.get_or_init_node_config(self.config.try_into_node_config().await?).await; + GLOBAL.launch_swarm().await } } \ No newline at end of file diff --git a/lazy-supplements/src/config/node.rs b/lazy-supplements/src/config/node.rs index ef45285..a64592b 100644 --- a/lazy-supplements/src/config/node.rs +++ b/lazy-supplements/src/config/node.rs @@ -23,7 +23,7 @@ fn base64_to_keypair(base64: &str) -> Result { Ok(Keypair::from_protobuf_encoding(&vec)?) } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct NodeConfig { #[serde(with = "keypair_parser")] pub secret: Keypair, diff --git a/lazy-supplements/src/global/mod.rs b/lazy-supplements/src/global/mod.rs index c51d2db..fb16691 100644 --- a/lazy-supplements/src/global/mod.rs +++ b/lazy-supplements/src/global/mod.rs @@ -1,7 +1,8 @@ use std::{collections::HashMap, net::{IpAddr, Ipv4Addr}, path::PathBuf, sync::LazyLock}; -use crate::config::{NodeConfig, RawNodeConfig}; -use libp2p::{Multiaddr, PeerId}; +use crate::{config::{NodeConfig, RawNodeConfig}, error::Error}; +use futures::StreamExt; +use libp2p::{swarm::SwarmEvent, Multiaddr, PeerId}; use sea_orm::DatabaseConnection; use tokio::sync::{OnceCell, RwLock}; @@ -59,7 +60,7 @@ pub static GLOBAL: Global = Global{ pub struct Global { pub node_config: OnceCell, pub database: OnceCell, - pub peers: OnceCell>> + pub peers: OnceCell>>, } #[cfg(test)] @@ -69,7 +70,7 @@ impl Global { pub fn get_node_config(&self) -> Option<&NodeConfig> { self.node_config.get() } - pub async fn get_or_try_init_node_config(&self, config: NodeConfig) -> &NodeConfig { + pub async fn get_or_init_node_config(&self, config: NodeConfig) -> &NodeConfig { self.node_config.get_or_init(|| async {config}).await } pub async fn get_or_init_peers(&self) -> &RwLock> { @@ -83,6 +84,22 @@ impl Global { pub async fn write_peers(&self) -> tokio::sync::RwLockWriteGuard<'_, HashMap>{ self.get_or_init_peers().await.write().await } + pub async fn launch_swarm(&self) -> Result<(), Error> { + let mut swarm = self.get_node_config().unwrap().clone().try_into_swarm().await?; + loop{ + let swarm_event = swarm.select_next_some().await; + tokio::spawn(async move{ + match swarm_event { + SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), + SwarmEvent::Behaviour(event) => { + println!("{event:?}"); + event.run().await; + }, + _ => {} + } + }); + } + } } pub static DEFAULT_RAW_NODE_CONFIG: LazyLock = LazyLock::new(|| { diff --git a/lazy-supplements/src/main.rs b/lazy-supplements/src/main.rs index c6aae67..ced05a9 100644 --- a/lazy-supplements/src/main.rs +++ b/lazy-supplements/src/main.rs @@ -1,5 +1,5 @@ use clap::{Parser, Subcommand}; -use lazy_supplements::{cli::{InitArgs, NodeArgs, NodeCommand, ServerArgs}, *}; +use lazy_supplements::{cli::{ConsoleArgs, InitArgs, NodeArgs, NodeCommand, ServerArgs}, *}; #[derive(Debug, Parser)] struct Cli { @@ -9,6 +9,7 @@ struct Cli { #[derive(Debug, Subcommand)] enum Command { + Console(ConsoleArgs), Node(NodeArgs), Init(InitArgs), Server(ServerArgs), @@ -24,5 +25,6 @@ async fn main() { }, Command::Init(x) => x.init_config().await, Command::Server(x) => x.start_server().await.unwrap(), + Command::Console(x) => x.start_console().await.unwrap(), } } \ No newline at end of file