client.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 client = RpcClient::new(endpoint, Some(ex)).await?;
  29. let req = JsonRequest::new("ping", json!([]));
  30. let rep = client.request(req).await?;
  31. println!("{:#?}", rep);
  32. let req = JsonRequest::new("kill", json!([]));
  33. let rep = client.request(req).await?;
  34. println!("{:#?}", rep);
  35. Ok(())
  36. }
  37. fn main() -> Result<()> {
  38. simplelog::TermLogger::init(
  39. simplelog::LevelFilter::Debug,
  40. simplelog::ConfigBuilder::new().build(),
  41. simplelog::TerminalMode::Mixed,
  42. simplelog::ColorChoice::Auto,
  43. )?;
  44. let n_threads = std::thread::available_parallelism().unwrap().get();
  45. let ex = Arc::new(Executor::new());
  46. let (signal, shutdown) = smol::channel::unbounded::<()>();
  47. let (_, result) = easy_parallel::Parallel::new()
  48. .each(0..n_threads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  49. .finish(|| {
  50. smol::future::block_on(async {
  51. realmain(ex.clone()).await?;
  52. drop(signal);
  53. Ok::<(), darkfi::Error>(())
  54. })
  55. });
  56. result
  57. }