feat: rewritten the cli to support extra commands

This commit is contained in:
Lioncat2002 2022-12-07 20:35:18 +05:30
parent a6aa73fb02
commit 51fe807e4a
No known key found for this signature in database
2 changed files with 90 additions and 22 deletions

View File

@ -1,4 +1,6 @@
use {clap::Parser, solana_cli_config::ConfigInput};
use clap::Subcommand;
use clap::Parser;
/// Holds the configuration for a single run of the benchmark
#[derive(Parser, Debug)]
@ -11,6 +13,11 @@ use {clap::Parser, solana_cli_config::ConfigInput};
"
)]
pub struct Args {
#[clap(subcommand)]
pub command:Command,
/*
#[arg(short, long, default_value_t = String::from("8899"))]
pub port: String,
#[arg(short, long, default_value_t = String::from("8900"))]
@ -19,10 +26,13 @@ pub struct Args {
pub rpc_url: String,
#[arg(short, long, default_value_t = String::new())]
pub websocket_url: String,
*/
}
/*
impl Args {
pub fn resolve_address(&mut self) {
if self.rpc_url.is_empty() {
let (_, rpc_url) = ConfigInput::compute_json_rpc_url_setting(
self.rpc_url.as_str(),
@ -40,4 +50,20 @@ impl Args {
self.websocket_url = ws_url;
}
}
}
*/
#[derive(Subcommand,Debug)]
pub enum Command {
Run {
#[arg(short, long, default_value_t = String::from("8899"))]
port: String,
#[arg(short, long, default_value_t = String::from("8900"))]
subscription_port: String,
#[arg(short, long, default_value_t = String::from("http://localhost:8899"))]
rpc_url: String,
#[arg(short, long, default_value_t = String::new())]
websocket_url: String,
},
Test,
}

View File

@ -1,10 +1,11 @@
use std::{sync::Arc, net::SocketAddr};
use std::{net::SocketAddr, sync::Arc};
use clap::Parser;
use context::LiteRpcSubsrciptionControl;
use jsonrpc_core::MetaIoHandler;
use jsonrpc_http_server::{hyper, AccessControlAllowOrigin, DomainsValidation, ServerBuilder};
use pubsub::LitePubSubService;
use solana_cli_config::ConfigInput;
use solana_perf::thread::renice_this_thread;
use tokio::sync::broadcast;
@ -23,21 +24,31 @@ mod rpc;
use cli::Args;
pub fn main() {
let mut cli_config = Args::parse();
cli_config.resolve_address();
fn run(port: String, subscription_port: String, rpc_url: String, websocket_url: String) {
let rpc_url = if rpc_url.is_empty() {
let (_, rpc_url) = ConfigInput::compute_json_rpc_url_setting(
rpc_url.as_str(),
&ConfigInput::default().json_rpc_url,
);
rpc_url
} else {
rpc_url
};
let websocket_url = if websocket_url.is_empty() {
let (_, ws_url) = ConfigInput::compute_websocket_url_setting(
&websocket_url.as_str(),
"",
rpc_url.as_str(),
"",
);
ws_url
} else {
websocket_url
};
println!(
"Using rpc server {} and ws server {}",
cli_config.rpc_url, cli_config.websocket_url
rpc_url, websocket_url
);
let Args {
rpc_url: json_rpc_url,
websocket_url,
port: rpc_addr,
subscription_port,
..
} = &cli_config;
let performance_counter = PerformanceCounter::new();
launch_performance_updating_thread(performance_counter.clone());
@ -49,7 +60,7 @@ pub fn main() {
notification_reciever,
));
let subscription_port = subscription_port
let subscription_port = format!("127.0.0.1:{subscription_port}")
.parse::<SocketAddr>()
.expect("Invalid subscription port");
@ -59,8 +70,6 @@ pub fn main() {
subscription_port,
performance_counter.clone(),
);
// start recieving notifications and broadcast them
{
let pubsub_control = pubsub_control.clone();
std::thread::Builder::new()
@ -75,12 +84,11 @@ pub fn main() {
io.extend_with(lite_rpc.to_delegate());
let mut request_processor = LightRpcRequestProcessor::new(
json_rpc_url,
websocket_url,
rpc_url.as_str(),
&websocket_url,
notification_sender,
performance_counter.clone(),
);
let runtime = Arc::new(
tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
@ -91,7 +99,7 @@ pub fn main() {
.expect("Runtime"),
);
let max_request_body_size: usize = 50 * (1 << 10);
let socket_addr = rpc_addr.parse::<SocketAddr>().unwrap();
let socket_addr = port.parse::<SocketAddr>().unwrap();
{
let request_processor = request_processor.clone();
@ -113,3 +121,37 @@ pub fn main() {
request_processor.free();
websocket_service.close().unwrap();
}
fn ts_test(){
}
pub fn main() {
let cli_command = Args::parse();
match cli_command.command {
cli::Command::Run {
port,
subscription_port,
rpc_url,
websocket_url,
} => run(port, subscription_port, rpc_url, websocket_url),
cli::Command::Test => ts_test(),
}
//cli_config.resolve_address();
//println!(
// "Using rpc server {} and ws server {}",
// cli_config.rpc_url, cli_config.websocket_url
//);
//let Args {
// rpc_url: json_rpc_url,
// websocket_url,
// port: rpc_addr,
// subscription_port,
// ..
//} = &cli_config;
// start recieving notifications and broadcast them
}