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

This commit is contained in:
skoupidi
2024-12-05 16:22:43 +02:00
parent 441a029f58
commit bc22307e8b
15 changed files with 27 additions and 27 deletions

View File

@@ -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());

View File

@@ -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());

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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());

View File

@@ -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,

View File

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

View File

@@ -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());

View File

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

View File

@@ -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());

View File

@@ -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());

View File

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

View File

@@ -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,

View File

@@ -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());