client.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_std::sync::Arc;
  19. use serde_json::json;
  20. use smol::Executor;
  21. use url::Url;
  22. use darkfi::{
  23. rpc::{client::RpcClient, jsonrpc::JsonRequest},
  24. Result,
  25. };
  26. async fn realmain(ex: Arc<Executor<'_>>) -> Result<()> {
  27. //let endpoint = Url::parse("tcp://127.0.0.1:55422").unwrap();
  28. let endpoint = Url::parse("unix:///tmp/rpc.sock").unwrap();
  29. let client = RpcClient::new(endpoint, Some(ex)).await?;
  30. let req = JsonRequest::new("ping", json!([]));
  31. let rep = client.request(req).await?;
  32. println!("{:#?}", rep);
  33. let req = JsonRequest::new("kill", json!([]));
  34. let rep = client.request(req).await?;
  35. println!("{:#?}", rep);
  36. Ok(())
  37. }
  38. fn main() -> Result<()> {
  39. simplelog::TermLogger::init(
  40. simplelog::LevelFilter::Debug,
  41. simplelog::ConfigBuilder::new().build(),
  42. simplelog::TerminalMode::Mixed,
  43. simplelog::ColorChoice::Auto,
  44. )?;
  45. let n_threads = std::thread::available_parallelism().unwrap().get();
  46. let ex = Arc::new(Executor::new());
  47. let (signal, shutdown) = smol::channel::unbounded::<()>();
  48. let (_, result) = easy_parallel::Parallel::new()
  49. .each(0..n_threads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  50. .finish(|| {
  51. smol::future::block_on(async {
  52. realmain(ex.clone()).await?;
  53. drop(signal);
  54. Ok::<(), darkfi::Error>(())
  55. })
  56. });
  57. result
  58. }