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 console::{ConsoleArgs, ConsoleCommands};
|
||||
pub use init::InitArgs;
|
||||
pub use node::{ NodeArgs, NodeCommand, JoinNodeArgs , ConsoleNodeArgs};
|
||||
pub use node::{ NodeArgs, NodeCommand, PeerArgs , ConsoleNodeArgs};
|
||||
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 futures::StreamExt;
|
||||
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;
|
||||
|
||||
|
@ -35,47 +35,53 @@ pub async fn parse_and_run_console_node_command(s:Vec<String>) -> Result<(), Err
|
|||
}
|
||||
|
||||
#[derive(Args, Debug)]
|
||||
pub struct JoinNodeArgs {
|
||||
#[arg(long)]
|
||||
pub peer_ip: IpAddr,
|
||||
#[arg(long)]
|
||||
pub peer_port: u16,
|
||||
//#[arg(long)]
|
||||
//pub peer_id: String,
|
||||
pub struct PeerArgs {
|
||||
#[arg(value_parser = clap::value_parser!(PeerArg))]
|
||||
pub peer: PeerArg,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum PeerArg {
|
||||
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)]
|
||||
pub config: ConfigArgs,
|
||||
pub peer: PeerArgs,
|
||||
pub pass: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum NodeCommand {
|
||||
Ping(JoinNodeArgs),
|
||||
Join(JoinNodeArgs),
|
||||
Add(PeerArgs),
|
||||
Ping(PeerArgs),
|
||||
Join(PeerArgs),
|
||||
List,
|
||||
Delete(PeerArgs),
|
||||
}
|
||||
|
||||
|
||||
impl JoinNodeArgs {
|
||||
pub async fn ping(self) -> Result<(), Error> {
|
||||
let mut swarm = self.config.try_into_node_config().await?.try_into_swarm().await?;
|
||||
|
||||
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;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
impl PeerArgs {
|
||||
pub async fn run(self) -> Result<(), Error> {
|
||||
println!("{self:?}");
|
||||
todo!()
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
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)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Command,
|
||||
#[command(flatten)]
|
||||
pub config: ConfigArgs,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
|
@ -18,11 +20,10 @@ enum Command {
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
match Cli::parse().command {
|
||||
Command::Node(x) => match x.command {
|
||||
NodeCommand::Ping(y) => y.ping().await.unwrap(),
|
||||
NodeCommand::Join(y) => println!("{y:?}"),
|
||||
},
|
||||
let cli = Cli::parse();
|
||||
let _ = GLOBAL.get_or_init_node_config(cli.config.try_into_node_config().await.unwrap()).await;
|
||||
match cli.command {
|
||||
Command::Node(x) => x.run().await.unwrap(),
|
||||
Command::Init(x) => x.init_config().await,
|
||||
Command::Server(x) => x.start_server().await.unwrap(),
|
||||
Command::Console(x) => x.start_console(ConsoleCommands::default()).await.unwrap(),
|
||||
|
|
Loading…
Add table
Reference in a new issue