test-client.rs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. use anyhow::Result;
  2. fn print_usage() {
  3. let brief = format!("test-client: make queries to CBORPC server on stdin/out, display output on stderr.\nUsage: test-client <protocol name> <method name> <message> ...",);
  4. eprint!("{}", &brief);
  5. }
  6. #[tokio::main]
  7. async fn main() -> Result<()> {
  8. let args: Vec<_> = std::env::args().collect();
  9. if args.len() % 3 != 0 {
  10. print_usage();
  11. std::process::exit(10);
  12. }
  13. let mut client = cborpc::Client::from_async_stream(tokio::io::stdin(), tokio::io::stdout());
  14. let mut query_idx = 0;
  15. while query_idx < args.len() {
  16. let protocol_name = args[query_idx].to_string();
  17. let method_name = args[query_idx + 1].to_string();
  18. let message = &args[query_idx + 2]; // strings only
  19. eprintln!(">>> {}/{}({})", protocol_name, method_name, message);
  20. let response = client
  21. .call(&cborpc::MethodCall {
  22. protocol_name,
  23. method_name,
  24. message: ciborium::into_vec(message).unwrap(),
  25. })
  26. .await?;
  27. eprintln!("<<< success={} {:?}", response.success, response.message);
  28. eprintln!("???");
  29. query_idx += 3;
  30. }
  31. Ok(())
  32. }