Add PeerArg
This commit is contained in:
parent
51b12870dd
commit
72d3a1f54b
3 changed files with 50 additions and 43 deletions
|
@ -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, JoinNodeArgs , ConsoleNodeArgs};
|
pub use node::{ NodeArgs, NodeCommand, PeerArgs , ConsoleNodeArgs};
|
||||||
pub use server::ServerArgs;
|
pub use server::ServerArgs;
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{net::IpAddr, path::PathBuf};
|
use std::{net::IpAddr, ops::Mul, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
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
|
multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr, PeerId
|
||||||
};
|
};
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
@ -35,47 +35,53 @@ pub async fn parse_and_run_console_node_command(s:Vec<String>) -> Result<(), Err
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
pub struct JoinNodeArgs {
|
pub struct PeerArgs {
|
||||||
#[arg(long)]
|
#[arg(value_parser = clap::value_parser!(PeerArg))]
|
||||||
pub peer_ip: IpAddr,
|
pub peer: PeerArg,
|
||||||
#[arg(long)]
|
}
|
||||||
pub peer_port: u16,
|
#[derive(Clone, Debug)]
|
||||||
//#[arg(long)]
|
pub enum PeerArg {
|
||||||
//pub peer_id: String,
|
Addr(Multiaddr),
|
||||||
|
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 config: ConfigArgs,
|
pub peer: PeerArgs,
|
||||||
|
pub pass: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Subcommand)]
|
#[derive(Debug, Subcommand)]
|
||||||
pub enum NodeCommand {
|
pub enum NodeCommand {
|
||||||
Ping(JoinNodeArgs),
|
Add(PeerArgs),
|
||||||
Join(JoinNodeArgs),
|
Ping(PeerArgs),
|
||||||
|
Join(PeerArgs),
|
||||||
|
List,
|
||||||
|
Delete(PeerArgs),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl JoinNodeArgs {
|
impl PeerArgs {
|
||||||
pub async fn ping(self) -> Result<(), Error> {
|
pub async fn run(self) -> Result<(), Error> {
|
||||||
let mut swarm = self.config.try_into_node_config().await?.try_into_swarm().await?;
|
println!("{self:?}");
|
||||||
|
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;
|
|
||||||
},
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,12 @@
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use lazy_supplements::{cli::{ConsoleArgs, ConsoleCommands, InitArgs, NodeArgs, NodeCommand, ServerArgs}, *};
|
use lazy_supplements::{cli::{ConfigArgs, ConsoleArgs, ConsoleCommands, InitArgs, NodeArgs, NodeCommand, ServerArgs}, global::{Global, GLOBAL}, *};
|
||||||
|
|
||||||
#[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)]
|
||||||
|
@ -18,11 +20,10 @@ enum Command {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
match Cli::parse().command {
|
let cli = Cli::parse();
|
||||||
Command::Node(x) => match x.command {
|
let _ = GLOBAL.get_or_init_node_config(cli.config.try_into_node_config().await.unwrap()).await;
|
||||||
NodeCommand::Ping(y) => y.ping().await.unwrap(),
|
match cli.command {
|
||||||
NodeCommand::Join(y) => println!("{y:?}"),
|
Command::Node(x) => x.run().await.unwrap(),
|
||||||
},
|
|
||||||
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