From 0b5d38094fce580127f7be4cb6df88c116602cee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Fri, 8 Mar 2024 21:40:05 +0100 Subject: [PATCH] wip --- common/client-core/src/error.rs | 12 +++++++++ common/socks5-client-core/src/error.rs | 9 +++++++ .../src/socks/mixnet_responses.rs | 5 ++-- common/task/src/manager.rs | 27 ++++++++++++++++++- sdk/rust/nym-sdk/src/mixnet/client.rs | 1 + 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/common/client-core/src/error.rs b/common/client-core/src/error.rs index e4496e461a..89e2ecbd4f 100644 --- a/common/client-core/src/error.rs +++ b/common/client-core/src/error.rs @@ -4,6 +4,7 @@ use crate::client::mix_traffic::transceiver::ErasedGatewayError; use nym_crypto::asymmetric::identity::Ed25519RecoveryError; use nym_gateway_client::error::GatewayClientError; +use nym_task::manager::TaskStatusTrait; use nym_topology::gateway::GatewayConversionError; use nym_topology::NymTopologyError; use nym_validator_client::ValidatorClientError; @@ -165,3 +166,14 @@ pub enum ClientCoreStatusMessage { #[error("The connected gateway is very slow, or the connection to it is very slow")] GatewayIsVerySlow, } + +// impl TaskStatusTrait for ClientCoreStatusMessage {} +// impl TaskStatusTrait for T { +// fn as_any(&self) -> &dyn Any { +// self +// } +// +// fn as_any_mut(&mut self) -> &mut dyn Any { +// self +// } +// } diff --git a/common/socks5-client-core/src/error.rs b/common/socks5-client-core/src/error.rs index a88f707897..b9564f9098 100644 --- a/common/socks5-client-core/src/error.rs +++ b/common/socks5-client-core/src/error.rs @@ -1,6 +1,7 @@ use crate::socks::types::SocksProxyError; use nym_client_core::error::ClientCoreError; use nym_socks5_requests::{ConnectionError, ConnectionId}; +use nym_task::manager::TaskStatusTrait; #[derive(thiserror::Error, Debug)] pub enum Socks5ClientCoreError { @@ -20,6 +21,14 @@ pub enum Socks5ClientCoreError { }, } +#[derive(thiserror::Error, Debug)] +pub enum Socks5ClientCoreStatusMessage { + #[error(transparent)] + Socks5Error(#[from] Socks5ClientCoreError), +} + +// impl TaskStatusTrait for Socks5ClientCoreStatusMessage {} + impl From for Socks5ClientCoreError { fn from(value: ConnectionError) -> Self { Socks5ClientCoreError::NetworkRequesterError { diff --git a/common/socks5-client-core/src/socks/mixnet_responses.rs b/common/socks5-client-core/src/socks/mixnet_responses.rs index 05cb5bd48d..a556cf11c1 100644 --- a/common/socks5-client-core/src/socks/mixnet_responses.rs +++ b/common/socks5-client-core/src/socks/mixnet_responses.rs @@ -1,7 +1,7 @@ // Copyright 2020-2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::error::Socks5ClientCoreError; +use crate::error::{Socks5ClientCoreError, Socks5ClientCoreStatusMessage}; use futures::channel::mpsc; use futures::StreamExt; use log::*; @@ -136,7 +136,8 @@ impl MixnetResponseListener { if let Some(received_responses) = received_responses { for reconstructed_message in received_responses { if let Err(err) = self.on_message(reconstructed_message) { - self.shutdown.send_status_msg(Box::new(err)); + let msg = Socks5ClientCoreStatusMessage::from(err); + self.shutdown.send_status_msg(Box::new(msg)); } } } else { diff --git a/common/task/src/manager.rs b/common/task/src/manager.rs index 6a913fdc47..ce0d3bfbd7 100644 --- a/common/task/src/manager.rs +++ b/common/task/src/manager.rs @@ -3,6 +3,7 @@ use futures::{future::pending, FutureExt, SinkExt, StreamExt}; use log::{log, Level}; +use std::any::Any; use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::{error::Error, time::Duration}; @@ -23,7 +24,7 @@ pub(crate) type SentError = Box; type ErrorSender = mpsc::UnboundedSender; type ErrorReceiver = mpsc::UnboundedReceiver; -pub type SentStatus = Box; +pub type SentStatus = Box; pub type StatusSender = futures::channel::mpsc::Sender; pub type StatusReceiver = futures::channel::mpsc::Receiver; @@ -48,6 +49,30 @@ pub enum TaskStatus { Ready, } +pub trait TaskStatusTrait: + std::fmt::Debug + std::fmt::Display + Send + Sync + 'static + Any +{ + // As Any requires 'static, it implicitly enforces the 'static lifetime here as well. + + // This method tries to cast the trait object back to a reference of its concrete type. + fn as_any(&self) -> &dyn Any; + + // Optionally, for downcasting to mutable references. + fn as_any_mut(&mut self) -> &mut dyn Any; +} + +impl TaskStatusTrait for T { + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } +} + +// impl TaskStatusTrait for TaskStatus {} + /// Listens to status and error messages from tasks, as well as notifying them to gracefully /// shutdown. Keeps track of if task stop unexpectedly, such as in a panic. #[derive(Debug)] diff --git a/sdk/rust/nym-sdk/src/mixnet/client.rs b/sdk/rust/nym-sdk/src/mixnet/client.rs index 1c18a5f3d0..69532c4af8 100644 --- a/sdk/rust/nym-sdk/src/mixnet/client.rs +++ b/sdk/rust/nym-sdk/src/mixnet/client.rs @@ -655,6 +655,7 @@ where .next() .await .ok_or(Error::Socks5NotStarted)? + .as_any() .downcast_ref::() .ok_or(Error::Socks5NotStarted)? {