main.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use async_executor::Executor;
  2. use async_trait::async_trait;
  3. use darkfi::{
  4. rpc::{
  5. jsonrpc::{error as jsonerr, response as jsonresp, ErrorCode::*, JsonRequest, JsonResult},
  6. rpcserver::{listen_and_serve, RequestHandler, RpcServerConfig},
  7. },
  8. util::expand_path,
  9. Result,
  10. };
  11. use easy_parallel::Parallel;
  12. use log::debug;
  13. use serde_json::{json, Value};
  14. use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
  15. use std::{
  16. net::{IpAddr, Ipv4Addr, SocketAddr},
  17. sync::Arc,
  18. };
  19. async fn start(executor: Arc<Executor<'_>>) -> Result<()> {
  20. let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7777);
  21. let server_config = RpcServerConfig {
  22. socket_addr: rpc_addr,
  23. use_tls: false,
  24. // this is all random filler that is meaningless bc tls is disabled
  25. // TODO: cleanup
  26. identity_path: expand_path("../..")?,
  27. identity_pass: "test".to_string(),
  28. };
  29. let rpc_interface = Arc::new(JsonRpcInterface {});
  30. listen_and_serve(server_config, rpc_interface, executor).await?;
  31. Ok(())
  32. }
  33. struct JsonRpcInterface {}
  34. #[async_trait]
  35. impl RequestHandler for JsonRpcInterface {
  36. async fn handle_request(&self, req: JsonRequest, _executor: Arc<Executor<'_>>) -> JsonResult {
  37. if req.params.as_array().is_none() {
  38. return JsonResult::Err(jsonerr(InvalidParams, None, req.id))
  39. }
  40. debug!(target: "RPC", "--> {}", serde_json::to_string(&req).unwrap());
  41. match req.method.as_str() {
  42. Some("say_hello") => return self.say_hello(req.id, req.params).await,
  43. Some(_) | None => return JsonResult::Err(jsonerr(MethodNotFound, None, req.id)),
  44. }
  45. }
  46. }
  47. impl JsonRpcInterface {
  48. // --> {"method": "say_hello", "params": []}
  49. // <-- {"result": "hello world"}
  50. async fn say_hello(&self, id: Value, _params: Value) -> JsonResult {
  51. JsonResult::Resp(jsonresp(json!("hello world"), id))
  52. }
  53. }
  54. #[async_std::main]
  55. async fn main() -> Result<()> {
  56. //let args = CliDao::parse();
  57. //let matches = CliDao::command().get_matches();
  58. TermLogger::init(
  59. LevelFilter::Debug,
  60. simplelog::Config::default(),
  61. TerminalMode::Mixed,
  62. ColorChoice::Auto,
  63. )?;
  64. //let rpc_addr = "tcp:://127.0.0.1:7777";
  65. //let client = Arc::new(Client::new(rpc_addr.to_string()));
  66. let nthreads = num_cpus::get();
  67. let (signal, shutdown) = async_channel::unbounded::<()>();
  68. let ex = Arc::new(Executor::new());
  69. //let ex2 = ex.clone();
  70. let ex3 = ex.clone();
  71. let (_, result) = Parallel::new()
  72. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  73. .finish(|| {
  74. smol::future::block_on(async move {
  75. start(ex3.clone()).await?;
  76. //client.run_client(client.clone(), ex2.clone()).await?;
  77. drop(signal);
  78. Ok::<(), darkfi::Error>(())
  79. })
  80. });
  81. result
  82. }