Create trait for status events

This commit is contained in:
Jon Häggblad
2024-09-23 17:40:44 +02:00
parent 0bdf750be9
commit 2d34a5ec3d
7 changed files with 37 additions and 4 deletions
Generated
+1
View File
@@ -4646,6 +4646,7 @@ dependencies = [
"nym-ecash-contract-common",
"nym-ecash-time",
"nym-network-defaults",
"nym-task",
"nym-validator-client",
"rand",
"thiserror",
+4 -3
View File
@@ -14,14 +14,15 @@ thiserror = { workspace = true }
url = { workspace = true }
zeroize = { workspace = true }
nym-ecash-time = { path = "../ecash-time" }
nym-credential-storage = { path = "../credential-storage" }
nym-credentials = { path = "../credentials" }
nym-credentials-interface = { path = "../credentials-interface" }
nym-crypto = { path = "../crypto", features = ["rand", "asymmetric", "stream_cipher", "aes", "hashing"] }
nym-network-defaults = { path = "../network-defaults" }
nym-validator-client = { path = "../client-libs/validator-client", default-features = false }
nym-ecash-contract-common = { path = "../cosmwasm-smart-contracts/ecash-contract" }
nym-ecash-time = { path = "../ecash-time" }
nym-network-defaults = { path = "../network-defaults" }
nym-task = { path = "../task" }
nym-validator-client = { path = "../client-libs/validator-client", default-features = false }
[target."cfg(not(target_arch = \"wasm32\"))".dependencies.nym-validator-client]
path = "../client-libs/validator-client"
+6
View File
@@ -11,3 +11,9 @@ pub enum BandwidthStatusMessage {
#[error("no bandwidth left")]
NoBandwidth,
}
impl nym_task::manager::TaskEvent for BandwidthStatusMessage {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
+6
View File
@@ -221,3 +221,9 @@ pub enum ClientCoreStatusMessage {
#[error("The connected gateway is very slow, or the connection to it is very slow")]
GatewayIsVerySlow,
}
impl nym_task::manager::TaskEvent for ClientCoreStatusMessage {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
+6
View File
@@ -28,3 +28,9 @@ impl From<ConnectionError> for Socks5ClientCoreError {
}
}
}
impl nym_task::manager::TaskEvent for Socks5ClientCoreError {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
+13 -1
View File
@@ -3,6 +3,7 @@
use futures::{future::pending, FutureExt, SinkExt, StreamExt};
use log::{log, Level};
use std::any::Any;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{error::Error, time::Duration};
use tokio::sync::{
@@ -22,7 +23,8 @@ pub(crate) type SentError = Box<dyn Error + Send + Sync>;
type ErrorSender = mpsc::UnboundedSender<SentError>;
type ErrorReceiver = mpsc::UnboundedReceiver<SentError>;
pub type SentStatus = Box<dyn Error + Send + Sync>;
// pub type SentStatus = Box<dyn Error + Send + Sync>;
pub type SentStatus = Box<dyn TaskEvent>;
pub type StatusSender = futures::channel::mpsc::Sender<SentStatus>;
pub type StatusReceiver = futures::channel::mpsc::Receiver<SentStatus>;
@@ -40,6 +42,10 @@ enum TaskError {
UnexpectedHalt { shutdown_name: Option<String> },
}
pub trait TaskEvent: std::error::Error + Send + Sync + Any {
fn as_any(&self) -> &dyn Any;
}
// TODO: possibly we should create a `Status` trait instead of reusing `Error`
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
pub enum TaskStatus {
@@ -49,6 +55,12 @@ pub enum TaskStatus {
ReadyWithGateway(String),
}
impl TaskEvent for TaskStatus {
fn as_any(&self) -> &dyn Any {
self
}
}
/// 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)]
+1
View File
@@ -655,6 +655,7 @@ where
.next()
.await
.ok_or(Error::Socks5NotStarted)?
.as_any()
.downcast_ref::<TaskStatus>()
.ok_or(Error::Socks5NotStarted)?
{