From f155278db783770b4fc44931e9ac0cb14131f981 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 21 Sep 2025 15:03:21 +0800 Subject: finish ctx --- src/tasks/client.rs | 21 ++++++++++++++++++--- src/tasks/server.rs | 22 ++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) (limited to 'src/tasks') diff --git a/src/tasks/client.rs b/src/tasks/client.rs index ea7525a..e4510aa 100644 --- a/src/tasks/client.rs +++ b/src/tasks/client.rs @@ -3,6 +3,7 @@ use std::fmt::Display; use crate::tasks::{IntoTaskErr, Task as BaseTask}; use serde::Deserialize; +#[derive(Clone, Copy)] pub struct Task(pub BaseTask); #[derive(Debug, Deserialize, Clone, Copy)] @@ -21,11 +22,11 @@ impl Display for Name { pub const TASKS: &[Task] = &[Task(BaseTask { name: Name::Nop, exec: no_op }), Task(BaseTask { name: Name::Exec, exec: exec }), NETWORK_T]; pub const NETWORK_T: Task = Task(BaseTask { name: Name::Network, exec: network }); -fn no_op(_: &[&str]) -> Result { +fn no_op(_: &[&str], _: &()) -> Result { Ok("".to_string()) } -fn exec(cmds: &[&str]) -> Result { +fn exec(cmds: &[&str], _: &()) -> Result { use std::process::Command; // TODO: fmt and add exit code, and add support for both stdout and stderr, and // support for no output @@ -41,7 +42,7 @@ fn exec(cmds: &[&str]) -> Result { } } -fn network(switch: &[&str]) -> Result { +fn network(switch: &[&str], _: &()) -> Result { use std::process::{Command, Stdio}; let switch: bool = switch[0].parse::().map_err(|e| e.to_string())?; @@ -128,6 +129,20 @@ impl TryFrom<&str> for &'static Task { } } +// ts uses Copy trait, try avoid that +impl TryFrom<&str> for Task { + type Error = IntoTaskErr; + + fn try_from(value: &str) -> Result { + for task in TASKS { + if task.0.name.to_string() == value { + return Ok(*task); + } + } + Err(IntoTaskErr::NotFound(value.to_string())) + } +} + impl From for BaseTask { fn from(value: Task) -> Self { value.0 diff --git a/src/tasks/server.rs b/src/tasks/server.rs index 0896dca..b1cfc15 100644 --- a/src/tasks/server.rs +++ b/src/tasks/server.rs @@ -2,8 +2,13 @@ use std::fmt::Display; use crate::tasks::{IntoTaskErr, Task as BaseTask, TaskOutput}; use serde::Deserialize; +use tokio::net::TcpStream; -pub struct Task(pub BaseTask); +pub struct ServerCtx { + stream: TcpStream, +} + +pub struct Task(pub BaseTask); #[derive(Debug, Deserialize, Clone, Copy)] pub enum Name { @@ -16,9 +21,14 @@ impl Display for Name { } } -pub const TASKS: &[Task] = &[Task::(BaseTask { name: Name::GetIp, exec: get_ip })]; +pub const TASKS: &[Task] = &[Task(BaseTask { name: Name::GetIp, exec: get_ip })]; -fn get_ip(cmds: &[&str], _: i32) -> TaskOutput {} +fn get_ip(_: &[&str], ctx: &ServerCtx) -> TaskOutput { + match ctx.stream.peer_addr() { + Ok(addr) => Ok(addr.ip().to_string()), + Err(e) => Err(e.to_string()), + } +} impl TryFrom<&str> for &'static Task { type Error = IntoTaskErr; @@ -33,14 +43,14 @@ impl TryFrom<&str> for &'static Task { } } -impl From for BaseTask { +impl From for BaseTask { fn from(value: Task) -> Self { value.0 } } -impl AsRef> for Task { - fn as_ref(&self) -> &BaseTask { +impl AsRef> for Task { + fn as_ref(&self) -> &BaseTask { &self.0 } } -- cgit v1.2.3