main.rs 2.9 KB

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