Compare commits
No commits in common. "72d3a1f54bb0633cae1304a9c33acd63b60c79ae" and "4b5758ba51b9a671bca74a9a870e6be123e1fd28" have entirely different histories.
72d3a1f54b
...
4b5758ba51
7 changed files with 48 additions and 99 deletions
|
@ -27,7 +27,6 @@ tempfile = { version = "3.20.0", optional = true }
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
tokio = { version = "1.45.0", features = ["macros", "rt"] }
|
tokio = { version = "1.45.0", features = ["macros", "rt"] }
|
||||||
toml = "0.8.22"
|
toml = "0.8.22"
|
||||||
tracing = "0.1.41"
|
|
||||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||||
uuid = { version = "1.17.0", features = ["v4"] }
|
uuid = { version = "1.17.0", features = ["v4"] }
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
mod writer;
|
|
||||||
|
|
||||||
use std::{collections::HashMap, ffi::OsString, hash::Hash, time::Duration};
|
use std::{collections::HashMap, ffi::OsString, hash::Hash, time::Duration};
|
||||||
|
|
||||||
use clap::{Args, Parser};
|
use clap::{Args, Parser};
|
||||||
use futures::{future::BoxFuture, StreamExt};
|
use futures::{future::BoxFuture, StreamExt};
|
||||||
use libp2p::{core::transport::dummy::DummyTransport, noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
|
use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
|
||||||
use rustyline::ExternalPrinter;
|
use tokio::time::sleep;
|
||||||
use tokio::{sync::Mutex, time::sleep};
|
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
use writer::ConsoleWriter;
|
|
||||||
|
|
||||||
use crate::{error::Error, global::GLOBAL};
|
use crate::{error::Error, global::GLOBAL};
|
||||||
|
|
||||||
|
@ -82,17 +78,6 @@ impl ConsoleArgs {
|
||||||
GLOBAL.launch_swarm().await
|
GLOBAL.launch_swarm().await
|
||||||
});
|
});
|
||||||
let mut rl = rustyline::DefaultEditor::new()?;
|
let mut rl = rustyline::DefaultEditor::new()?;
|
||||||
let mut printer = rl.create_external_printer()?;
|
|
||||||
tracing_subscriber::fmt()
|
|
||||||
.with_env_filter(EnvFilter::from_default_env())
|
|
||||||
.with_writer(std::sync::Mutex::new(ConsoleWriter::try_from(&mut rl)?)
|
|
||||||
).init();
|
|
||||||
tokio::spawn(async move {
|
|
||||||
loop{
|
|
||||||
tracing::event!(tracing::Level::ERROR, "test");
|
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
loop {
|
loop {
|
||||||
match rl.readline(">> ") {
|
match rl.readline(">> ") {
|
||||||
Ok(line) => {
|
Ok(line) => {
|
|
@ -1,31 +0,0 @@
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
use rustyline::{DefaultEditor, ExternalPrinter};
|
|
||||||
|
|
||||||
use crate::error::Error;
|
|
||||||
|
|
||||||
pub struct ConsoleWriter {
|
|
||||||
printer: Box<dyn ExternalPrinter + 'static + Send>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<&mut DefaultEditor> for ConsoleWriter {
|
|
||||||
type Error = Error;
|
|
||||||
fn try_from(e: &mut DefaultEditor) -> Result<Self, Error> {
|
|
||||||
Ok(ConsoleWriter {
|
|
||||||
printer: Box::new(e.create_external_printer()?)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::io::Write for ConsoleWriter {
|
|
||||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
|
||||||
let msg = String::from_utf8_lossy(buf).into_owned();
|
|
||||||
let size = msg.as_bytes().len();
|
|
||||||
self.printer.as_mut().print(msg);
|
|
||||||
Ok(size)
|
|
||||||
}
|
|
||||||
fn flush(&mut self) -> std::io::Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,5 +9,5 @@ mod server;
|
||||||
pub use config::ConfigArgs;
|
pub use config::ConfigArgs;
|
||||||
pub use console::{ConsoleArgs, ConsoleCommands};
|
pub use console::{ConsoleArgs, ConsoleCommands};
|
||||||
pub use init::InitArgs;
|
pub use init::InitArgs;
|
||||||
pub use node::{ NodeArgs, NodeCommand, PeerArgs , ConsoleNodeArgs};
|
pub use node::{ NodeArgs, NodeCommand, JoinNodeArgs , ConsoleNodeArgs};
|
||||||
pub use server::ServerArgs;
|
pub use server::ServerArgs;
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{net::IpAddr, ops::Mul, path::PathBuf, str::FromStr};
|
use std::{net::IpAddr, path::PathBuf};
|
||||||
|
|
||||||
use clap::{Args, Parser, Subcommand};
|
use clap::{Args, Parser, Subcommand};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr, PeerId
|
multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr
|
||||||
};
|
};
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
@ -35,53 +35,47 @@ pub async fn parse_and_run_console_node_command(s:Vec<String>) -> Result<(), Err
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
pub struct PeerArgs {
|
pub struct JoinNodeArgs {
|
||||||
#[arg(value_parser = clap::value_parser!(PeerArg))]
|
#[arg(long)]
|
||||||
pub peer: PeerArg,
|
pub peer_ip: IpAddr,
|
||||||
}
|
#[arg(long)]
|
||||||
#[derive(Clone, Debug)]
|
pub peer_port: u16,
|
||||||
pub enum PeerArg {
|
//#[arg(long)]
|
||||||
Addr(Multiaddr),
|
//pub peer_id: String,
|
||||||
Id(PeerId),
|
|
||||||
Number(u32),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for PeerArg {
|
|
||||||
type Err = String;
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
if let Ok(x) = s.parse::<Multiaddr>() {
|
|
||||||
Ok(Self::Addr(x))
|
|
||||||
} else if let Ok(x) = s.parse::<PeerId>() {
|
|
||||||
Ok(Self::Id(x))
|
|
||||||
} else if let Ok(x) = s.parse::<u32>() {
|
|
||||||
Ok(Self::Number(x))
|
|
||||||
} else {
|
|
||||||
Err(format!("Invalid value: {s}").to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
|
||||||
pub struct NodeJoinArgs {
|
|
||||||
#[command(flatten)]
|
#[command(flatten)]
|
||||||
pub peer: PeerArgs,
|
pub config: ConfigArgs,
|
||||||
pub pass: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Subcommand)]
|
#[derive(Debug, Subcommand)]
|
||||||
pub enum NodeCommand {
|
pub enum NodeCommand {
|
||||||
Add(PeerArgs),
|
Ping(JoinNodeArgs),
|
||||||
Ping(PeerArgs),
|
Join(JoinNodeArgs),
|
||||||
Join(PeerArgs),
|
|
||||||
List,
|
|
||||||
Delete(PeerArgs),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl PeerArgs {
|
impl JoinNodeArgs {
|
||||||
pub async fn run(self) -> Result<(), Error> {
|
pub async fn ping(self) -> Result<(), Error> {
|
||||||
println!("{self:?}");
|
let mut swarm = self.config.try_into_node_config().await?.try_into_swarm().await?;
|
||||||
todo!()
|
|
||||||
|
let mut remote: Multiaddr = Multiaddr::empty();
|
||||||
|
remote.push(match self.peer_ip {
|
||||||
|
IpAddr::V4(x) => Protocol::Ip4(x),
|
||||||
|
IpAddr::V6(x) => Protocol::Ip6(x),
|
||||||
|
});
|
||||||
|
remote.push(Protocol::Tcp(self.peer_port));
|
||||||
|
let addr = remote.to_string();
|
||||||
|
swarm.dial(remote)?;
|
||||||
|
println!("Dialed {addr}");
|
||||||
|
|
||||||
|
loop{
|
||||||
|
match swarm.select_next_some().await {
|
||||||
|
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
|
||||||
|
SwarmEvent::Behaviour(event) => {
|
||||||
|
println!("{event:?}");
|
||||||
|
event.run().await;
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -34,6 +34,9 @@ pub struct NodeConfig {
|
||||||
|
|
||||||
impl NodeConfig {
|
impl NodeConfig {
|
||||||
pub async fn try_into_swarm (self) -> Result<Swarm<p2p::Behaviour>, Error> {
|
pub async fn try_into_swarm (self) -> Result<Swarm<p2p::Behaviour>, Error> {
|
||||||
|
let _ = tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(EnvFilter::from_default_env())
|
||||||
|
.try_init();
|
||||||
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(self.secret)
|
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(self.secret)
|
||||||
.with_tokio()
|
.with_tokio()
|
||||||
.with_tcp(
|
.with_tcp(
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use lazy_supplements::{cli::{ConfigArgs, ConsoleArgs, ConsoleCommands, InitArgs, NodeArgs, NodeCommand, ServerArgs}, global::{Global, GLOBAL}, *};
|
use lazy_supplements::{cli::{ConsoleArgs, ConsoleCommands, InitArgs, NodeArgs, NodeCommand, ServerArgs}, *};
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
command: Command,
|
command: Command,
|
||||||
#[command(flatten)]
|
|
||||||
pub config: ConfigArgs,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Subcommand)]
|
#[derive(Debug, Subcommand)]
|
||||||
|
@ -20,10 +18,11 @@ enum Command {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let cli = Cli::parse();
|
match Cli::parse().command {
|
||||||
let _ = GLOBAL.get_or_init_node_config(cli.config.try_into_node_config().await.unwrap()).await;
|
Command::Node(x) => match x.command {
|
||||||
match cli.command {
|
NodeCommand::Ping(y) => y.ping().await.unwrap(),
|
||||||
Command::Node(x) => x.run().await.unwrap(),
|
NodeCommand::Join(y) => println!("{y:?}"),
|
||||||
|
},
|
||||||
Command::Init(x) => x.init_config().await,
|
Command::Init(x) => x.init_config().await,
|
||||||
Command::Server(x) => x.start_server().await.unwrap(),
|
Command::Server(x) => x.start_server().await.unwrap(),
|
||||||
Command::Console(x) => x.start_console(ConsoleCommands::default()).await.unwrap(),
|
Command::Console(x) => x.start_console(ConsoleCommands::default()).await.unwrap(),
|
||||||
|
|
Loading…
Add table
Reference in a new issue