|
|
@@ -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?)
|