Просмотр исходного кода

generalized jsonserver.rs to work with different services

lunar-mining 5 лет назад
Родитель
Сommit
b9b33b638e
3 измененных файлов с 23 добавлено и 26 удалено
  1. 4 1
      src/bin/darkfid.rs
  2. 2 3
      src/rpc/adapters/user_adapter.rs
  3. 17 22
      src/rpc/jsonserver.rs

+ 4 - 1
src/bin/darkfid.rs

@@ -308,8 +308,11 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<DarkfidConfig>) -> Resul
         (withdraw_req_send, withdraw_rep_recv),
     )?);
 
+    let rpc_url: std::net::SocketAddr = config.rpc_url.parse()?;
+
     // start the rpc server
-    jsonserver::start(ex.clone(), config.clone(), adapter.clone()).await?;
+    let io = Arc::new(adapter.handle_input()?);
+    jsonserver::start(ex.clone(), rpc_url, io).await?;
 
     futures_broker_task.cancel().await;
     Ok(())

+ 2 - 3
src/rpc/adapters/user_adapter.rs

@@ -43,9 +43,8 @@ impl UserAdapter {
     }
 
     pub fn handle_input(
-        self: Arc<Self>,
-        mut io: jsonrpc_core::IoHandler,
-    ) -> Result<jsonrpc_core::IoHandler> {
+        self: Arc<Self>) -> Result<jsonrpc_core::IoHandler> {
+        let mut io = jsonrpc_core::IoHandler::new();
         io.add_sync_method("say_hello", |_| {
             Ok(jsonrpc_core::Value::String("hello world!".into()))
         });

+ 17 - 22
src/rpc/jsonserver.rs

@@ -1,6 +1,3 @@
-use crate::cli::DarkfidConfig;
-//use crate::cli::{TransferParams, WithdrawParams};
-use crate::rpc::adapters::UserAdapter;
 use crate::{Error, Result};
 
 use async_executor::Executor;
@@ -19,6 +16,7 @@ pub async fn listen(
     rpc: Arc<RpcInterface>,
     listener: Async<TcpListener>,
     tls: Option<TlsAcceptor>,
+    io: Arc<jsonrpc_core::IoHandler>,
 ) -> Result<()> {
     // Format the full host address.
     let host = match &tls {
@@ -38,10 +36,11 @@ pub async fn listen(
             None => {
                 let stream = async_dup::Arc::new(stream);
                 let rpc = rpc.clone();
+                let io2 = io.clone();
                 executor.spawn(async move {
                     if let Err(err) = async_h1::accept(stream, move |req| {
                         let rpc = rpc.clone();
-                        rpc.serve(req)
+                        rpc.serve(req, io2.clone())
                     })
                     .await
                     {
@@ -76,16 +75,16 @@ pub async fn listen(
 
 pub async fn start(
     executor: Arc<Executor<'_>>,
-    config: Arc<DarkfidConfig>,
-    adapter: Arc<UserAdapter>,
+    rpc_url: std::net::SocketAddr,
+    io: Arc<jsonrpc_core::IoHandler>,
 ) -> Result<()> {
-    let rpc = RpcInterface::new(adapter)?;
-    let rpc_url: std::net::SocketAddr = config.rpc_url.parse()?;
+    let rpc = RpcInterface::new()?;
     let http = listen(
         executor.clone(),
         rpc.clone(),
         Async::<TcpListener>::bind(rpc_url)?,
         None,
+        io,
     );
 
     let http_task = executor.spawn(http);
@@ -99,33 +98,28 @@ pub async fn start(
     Ok(())
 }
 
-// json RPC server goes here
 #[allow(dead_code)]
 pub struct RpcInterface {
     pub started: Mutex<bool>,
     stop_send: async_channel::Sender<()>,
     stop_recv: async_channel::Receiver<()>,
-    adapter: Arc<UserAdapter>,
 }
 
 impl RpcInterface {
-    pub fn new(adapter: Arc<UserAdapter>) -> Result<Arc<Self>> {
+    pub fn new() -> Result<Arc<Self>> {
         let (stop_send, stop_recv) = async_channel::unbounded::<()>();
         Ok(Arc::new(Self {
             started: Mutex::new(false),
             stop_send,
             stop_recv,
-            adapter: adapter.clone(),
         }))
     }
 
-    pub async fn serve(self: Arc<Self>, mut req: Request) -> http_types::Result<Response> {
+    pub async fn serve(self: Arc<Self>, mut req: Request, io: Arc<jsonrpc_core::IoHandler>) -> http_types::Result<Response> {
         info!("RPC serving {}", req.url());
 
         let request = req.body_string().await?;
 
-        let io = self.handle_input().await?;
-
         debug!(target: "rpc", "JsonRpcInterface::serve() [PROCESSING INPUT]");
         let response = io
             .handle_request_sync(&request)
@@ -138,13 +132,14 @@ impl RpcInterface {
         Ok(res)
     }
 
-    pub async fn handle_input(self: Arc<Self>) -> Result<jsonrpc_core::IoHandler> {
-        debug!(target: "rpc", "JsonRpcInterface::handle_input() [START]");
-        let io = jsonrpc_core::IoHandler::new();
-        let io = self.adapter.clone().handle_input(io.clone())?;
-        debug!(target: "rpc", "JsonRpcInterface::handle_input() [END]");
-        Ok(io)
-    }
+    //pub async fn handle_input(self: Arc<Self>) -> Result<jsonrpc_core::IoHandler> {
+    //    debug!(target: "rpc", "JsonRpcInterface::handle_input() [START]");
+    //    let io = jsonrpc_core::IoHandler::new();
+    //    // this is where adapter is needed
+    //    //let io = self.adapter.clone().handle_input(io.clone())?;
+    //    debug!(target: "rpc", "JsonRpcInterface::handle_input() [END]");
+    //    Ok(io)
+    //}
 
     pub async fn wait_for_quit(self: Arc<Self>) -> Result<()> {
         Ok(self.stop_recv.recv().await?)