Преглед изворни кода

rpc: move set_outbound_connections from darkirc into rpc p2p handler

jkds пре 7 месеци
родитељ
комит
b60d550a3f
2 измењених фајлова са 32 додато и 32 уклоњено
  1. 1 31
      bin/darkirc/src/rpc.rs
  2. 31 1
      src/rpc/p2p_method.rs

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

@@ -45,7 +45,7 @@ impl RequestHandler<()> for DarkIrc {
             "dnet.subscribe_events" => self.dnet_subscribe_events(req.id, req.params).await,
             "p2p.get_info" => self.p2p_get_info(req.id, req.params).await,
             "p2p.set_outbound_connections" => {
-                self.set_outbound_connections(req.id, req.params).await
+                self.p2p_set_outbound_connections(req.id, req.params).await
             }
 
             "deg.switch" => self.deg_switch(req.id, req.params).await,
@@ -89,36 +89,6 @@ impl DarkIrc {
         JsonResponse::new(JsonValue::Boolean(true), id).into()
     }
 
-    // RPCAPI:
-    // Set the number of outbound connections for the P2P stack.
-    // Takes a positive integer representing the desired number of outbound connection slots.
-    // Returns `true` on success. If the number is greater than current, new slots are added.
-    // If the number is less than current, slots are removed (prioritizing empty slots).
-    //
-    // --> {"jsonrpc": "2.0", "method": "p2p.set_outbound_connections", "params": [5], "id": 42}
-    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
-    async fn set_outbound_connections(&self, id: u16, params: JsonValue) -> JsonResult {
-        let Some(params) = params.get::<Vec<JsonValue>>() else {
-            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
-        };
-        if params.len() != 1 || !params[0].is_number() {
-            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
-        }
-
-        let n_f64 = params[0].get::<f64>().unwrap();
-        let n = *n_f64 as u32;
-
-        if *n_f64 != n as f64 || n == 0 {
-            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
-        }
-
-        if let Err(e) = self.p2p.session_outbound().set_outbound_connections(n as usize).await {
-            return JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
-        }
-
-        JsonResponse::new(JsonValue::Boolean(true), id).into()
-    }
-
     // RPCAPI:
     // Initializes a subscription to p2p dnet events.
     // Once a subscription is established, `darkirc` will send JSON-RPC notifications of

+ 31 - 1
src/rpc/p2p_method.rs

@@ -19,7 +19,7 @@
 use async_trait::async_trait;
 
 use super::{
-    jsonrpc::{JsonResponse, JsonResult},
+    jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult},
     util::*,
 };
 use crate::net;
@@ -57,5 +57,35 @@ pub trait HandlerP2p: Sync + Send {
         JsonResponse::new(result, id).into()
     }
 
+    // RPCAPI:
+    // Set the number of outbound connections for the P2P stack.
+    // Takes a positive integer representing the desired number of outbound connection slots.
+    // Returns `true` on success. If the number is greater than current, new slots are added.
+    // If the number is less than current, slots are removed (prioritizing empty slots).
+    //
+    // --> {"jsonrpc": "2.0", "method": "p2p.set_outbound_connections", "params": [5], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
+    async fn p2p_set_outbound_connections(&self, id: u16, params: JsonValue) -> JsonResult {
+        let Some(params) = params.get::<Vec<JsonValue>>() else {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        };
+        if params.len() != 1 || !params[0].is_number() {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        let n_f64 = params[0].get::<f64>().unwrap();
+        let n = *n_f64 as u32;
+
+        if *n_f64 != n as f64 || n == 0 {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        if let Err(e) = self.p2p().session_outbound().set_outbound_connections(n as usize).await {
+            return JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
+        }
+
+        JsonResponse::new(JsonValue::Boolean(true), id).into()
+    }
+
     fn p2p(&self) -> net::P2pPtr;
 }