Răsfoiți Sursa

rcp/server: generalize RequestHandler so we can create multiple instances of it

skoupidi 1 an în urmă
părinte
comite
bc22307e8b

+ 1 - 1
bin/darkfid/src/rpc.rs

@@ -43,7 +43,7 @@ use crate::{
 
 #[async_trait]
 #[rustfmt::skip]
-impl RequestHandler for DarkfiNode {
+impl RequestHandler<()> for DarkfiNode {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         debug!(target: "darkfid::rpc", "--> {}", req.stringify().unwrap());
 

+ 1 - 1
bin/darkirc/src/rpc.rs

@@ -35,7 +35,7 @@ use smol::lock::MutexGuard;
 use super::DarkIrc;
 
 #[async_trait]
-impl RequestHandler for DarkIrc {
+impl RequestHandler<()> for DarkIrc {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         debug!(target: "darkirc::rpc", "--> {}", req.stringify().unwrap());
 

+ 1 - 1
bin/fud/fud/src/main.rs

@@ -108,7 +108,7 @@ pub struct Fud {
 }
 
 #[async_trait]
-impl RequestHandler for Fud {
+impl RequestHandler<()> for Fud {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         return match req.method.as_str() {
             "ping" => self.pong(req.id, req.params).await,

+ 1 - 1
bin/genev/genevd/src/rpc.rs

@@ -48,7 +48,7 @@ pub struct JsonRpcInterface {
 }
 
 #[async_trait]
-impl RequestHandler for JsonRpcInterface {
+impl RequestHandler<()> for JsonRpcInterface {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         match req.method.as_str() {
             "add" => self.add(req.id, req.params).await,

+ 1 - 1
bin/lilith/src/main.rs

@@ -241,7 +241,7 @@ impl Lilith {
 }
 
 #[async_trait]
-impl RequestHandler for Lilith {
+impl RequestHandler<()> for Lilith {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         return match req.method.as_str() {
             "ping" => self.pong(req.id, req.params).await,

+ 1 - 1
bin/minerd/src/rpc.rs

@@ -42,7 +42,7 @@ use crate::{
 };
 
 #[async_trait]
-impl RequestHandler for MinerNode {
+impl RequestHandler<()> for MinerNode {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         debug!(target: "minerd::rpc", "--> {}", req.stringify().unwrap());
 

+ 1 - 1
bin/tau/taud/src/jsonrpc.rs

@@ -66,7 +66,7 @@ pub struct JsonRpcInterface {
 }
 
 #[async_trait]
-impl RequestHandler for JsonRpcInterface {
+impl RequestHandler<()> for JsonRpcInterface {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         let rep = match req.method.as_str() {
             "add" => self.add(req.params).await,

+ 1 - 1
doc/src/learn/dchat/creating-dchatd/rpc-requests.md

@@ -25,7 +25,7 @@ any methods yet, so for now let's just return a `JsonError`.
 
 ```rust
 #[async_trait]
-impl RequestHandler for Dchat {
+impl RequestHandler<()> for Dchat {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         if req.params.as_array().is_none() {
             return JsonError::new(ErrorCode::InvalidRequest, None, req.id).into()

+ 1 - 1
example/dchat/dchatd/src/rpc.rs

@@ -32,7 +32,7 @@ use darkfi::rpc::{
 use crate::{dchatmsg::DchatMsg, Dchat};
 
 #[async_trait]
-impl RequestHandler for Dchat {
+impl RequestHandler<()> for Dchat {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         debug!(target: "dchat::rpc", "--> {}", req.stringify().unwrap());
 

+ 1 - 1
example/p2pdebug/src/rpc.rs

@@ -35,7 +35,7 @@ pub struct JsonRpcInterface {
 }
 
 #[async_trait]
-impl RequestHandler for JsonRpcInterface {
+impl RequestHandler<()> for JsonRpcInterface {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         if req.params.as_array().is_none() {
             return JsonError::new(ErrorCode::InvalidRequest, None, req.id).into()

+ 1 - 1
script/evgrd/bin/rpc.rs

@@ -35,7 +35,7 @@ use smol::lock::MutexGuard;
 use super::Daemon;
 
 #[async_trait]
-impl RequestHandler for Daemon {
+impl RequestHandler<()> for Daemon {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         debug!(target: "darkirc::rpc", "--> {}", req.stringify().unwrap());
 

+ 1 - 1
script/research/blockchain-explorer/src/rpc.rs

@@ -38,7 +38,7 @@ use crate::{
 };
 
 #[async_trait]
-impl RequestHandler for Explorerd {
+impl RequestHandler<()> for Explorerd {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         debug!(target: "blockchain-explorer::rpc", "--> {}", req.stringify().unwrap());
 

+ 1 - 1
script/research/dhtd/src/main.rs

@@ -248,7 +248,7 @@ impl Dhtd {
 }
 
 #[async_trait]
-impl RequestHandler for Dhtd {
+impl RequestHandler<()> for Dhtd {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         if !req.params.is_array() {
             return JsonError::new(InvalidParams, None, req.id).into()

+ 13 - 13
src/rpc/server.rs

@@ -42,7 +42,7 @@ use crate::{
 
 /// Asynchronous trait implementing a handler for incoming JSON-RPC requests.
 #[async_trait]
-pub trait RequestHandler: Sync + Send {
+pub trait RequestHandler<T>: Sync + Send {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult;
 
     async fn pong(&self, id: u16, _params: JsonValue) -> JsonResult {
@@ -77,10 +77,10 @@ pub trait RequestHandler: Sync + Send {
 }
 
 /// Auxiliary function to handle a request in the background.
-async fn handle_request(
+async fn handle_request<T>(
     writer: Arc<Mutex<WriteHalf<Box<dyn PtStream>>>>,
     addr: Url,
-    rh: Arc<impl RequestHandler + 'static>,
+    rh: Arc<impl RequestHandler<T> + 'static>,
     ex: Arc<smol::Executor<'_>>,
     tasks: Arc<Mutex<HashSet<Arc<StoppableTask>>>>,
     use_http: bool,
@@ -240,14 +240,14 @@ async fn handle_request(
 /// Accept function that should run inside a loop for accepting incoming
 /// JSON-RPC requests and passing them to the [`RequestHandler`].
 #[allow(clippy::type_complexity)]
-pub async fn accept(
+pub async fn accept<'a, T: 'a>(
     reader: Arc<Mutex<BufReader<ReadHalf<Box<dyn PtStream>>>>>,
     writer: Arc<Mutex<WriteHalf<Box<dyn PtStream>>>>,
     addr: Url,
-    rh: Arc<impl RequestHandler + 'static>,
+    rh: Arc<impl RequestHandler<T> + 'static>,
     conn_limit: Option<usize>,
     use_http: bool,
-    ex: Arc<smol::Executor<'_>>,
+    ex: Arc<smol::Executor<'a>>,
 ) -> Result<()> {
     // If there's a connection limit set, we will refuse connections
     // after this point.
@@ -348,12 +348,12 @@ pub async fn accept(
 
 /// Wrapper function around [`accept()`] to take the incoming connection and
 /// pass it forward.
-async fn run_accept_loop(
+async fn run_accept_loop<'a, T: 'a>(
     listener: Box<dyn PtListener>,
-    rh: Arc<impl RequestHandler + 'static>,
+    rh: Arc<impl RequestHandler<T> + 'static>,
     conn_limit: Option<usize>,
     use_http: bool,
-    ex: Arc<smol::Executor<'_>>,
+    ex: Arc<smol::Executor<'a>>,
 ) -> Result<()> {
     loop {
         match listener.next().await {
@@ -421,11 +421,11 @@ async fn run_accept_loop(
 ///
 /// The supported network schemes can be prefixed with `http+` to serve
 /// JSON-RPC over HTTP/1.1.
-pub async fn listen_and_serve(
+pub async fn listen_and_serve<'a, T: 'a>(
     accept_url: Url,
-    rh: Arc<impl RequestHandler + 'static>,
+    rh: Arc<impl RequestHandler<T> + 'static>,
     conn_limit: Option<usize>,
-    ex: Arc<smol::Executor<'_>>,
+    ex: Arc<smol::Executor<'a>>,
 ) -> Result<()> {
     // Figure out if we're using HTTP and rewrite the URL accordingly.
     let mut listen_url = accept_url.clone();
@@ -452,7 +452,7 @@ mod tests {
     }
 
     #[async_trait]
-    impl RequestHandler for RpcServer {
+    impl RequestHandler<()> for RpcServer {
         async fn handle_request(&self, req: JsonRequest) -> JsonResult {
             match req.method.as_str() {
                 "ping" => return self.pong(req.id, req.params).await,

+ 1 - 1
tests/jsonrpc.rs

@@ -55,7 +55,7 @@ impl RpcSrv {
 }
 
 #[async_trait]
-impl RequestHandler for RpcSrv {
+impl RequestHandler<()> for RpcSrv {
     async fn handle_request(&self, req: JsonRequest) -> JsonResult {
         assert!(req.params.is_array());