main.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use async_executor::Executor;
  2. use std::sync::Arc;
  3. use clap::{IntoApp, Parser, Subcommand};
  4. use log::{debug, error};
  5. use serde_json::{json, Value};
  6. use url::Url;
  7. use darkfi::{
  8. cli::Config,
  9. rpc::{jsonrpc, jsonrpc::JsonResult},
  10. util::async_util,
  11. Error, Result,
  12. };
  13. #[derive(Subcommand)]
  14. pub enum CliDaoSubCommands {
  15. /// Say hello to the RPC
  16. Hello {},
  17. }
  18. /// DAO cli
  19. #[derive(Parser)]
  20. #[clap(name = "dao")]
  21. pub struct CliDao {
  22. /// Increase verbosity
  23. #[clap(short, parse(from_occurrences))]
  24. pub verbose: u8,
  25. #[clap(subcommand)]
  26. pub command: Option<CliDaoSubCommands>,
  27. }
  28. pub struct Client {
  29. url: String,
  30. }
  31. impl Client {
  32. pub fn new(url: String) -> Self {
  33. Self { url }
  34. }
  35. async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
  36. let reply: JsonResult = match jsonrpc::send_request(&Url::parse(&self.url)?, json!(r)).await
  37. {
  38. Ok(v) => v,
  39. Err(e) => return Err(e),
  40. };
  41. match reply {
  42. JsonResult::Resp(r) => {
  43. debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
  44. Ok(r.result)
  45. }
  46. JsonResult::Err(e) => {
  47. debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
  48. Err(Error::JsonRpcError(e.error.message.to_string()))
  49. }
  50. JsonResult::Notif(n) => {
  51. debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
  52. Err(Error::JsonRpcError("Unexpected reply".to_string()))
  53. }
  54. }
  55. }
  56. // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42}
  57. // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42}
  58. async fn say_hello(&self) -> Result<Value> {
  59. let req = jsonrpc::request(json!("say_hello"), json!([]));
  60. Ok(self.request(req).await?)
  61. }
  62. }
  63. async fn start(options: CliDao) -> Result<()> {
  64. let rpc_addr = "tcp://127.0.0.1:7777";
  65. let client = Client::new(rpc_addr.to_string());
  66. match options.command {
  67. Some(CliDaoSubCommands::Hello {}) => {
  68. let reply = client.say_hello().await?;
  69. println!("Server replied: {}", &reply.to_string());
  70. return Ok(())
  71. }
  72. None => {}
  73. }
  74. error!("Please run 'dao help' to see usage.");
  75. Err(Error::MissingParams)
  76. }
  77. #[async_std::main]
  78. async fn main() -> Result<()> {
  79. let args = CliDao::parse();
  80. let matches = CliDao::into_app().get_matches();
  81. //let config_path = if args.config.is_some() {
  82. // expand_path(&args.config.clone().unwrap())?
  83. //} else {
  84. // join_config_path(&PathBuf::from("drk.toml"))?
  85. //};
  86. // Spawn config file if it's not in place already.
  87. //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  88. //let (lvl, conf) = log_config(matches)?;
  89. //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  90. //let config = Config::<DrkConfig>::load(config_path)?;
  91. start(args).await
  92. }