Skip to content

Commit

Permalink
move Promise into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
jadamcrain committed May 20, 2024
1 parent b1b4cf4 commit 9541d3c
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 45 deletions.
3 changes: 2 additions & 1 deletion dnp3/src/master/association.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::app::{Sequence, Timeout};
use crate::link::EndpointAddress;
use crate::master::error::{AssociationError, TaskError, TimeSyncError};
use crate::master::extract::extract_measurements;
use crate::master::handler::{AssociationHandler, Promise};
use crate::master::handler::AssociationHandler;
use crate::master::messages::AssociationMsgType;
use crate::master::poll::{PollHandle, PollMap, PollMsg};
use crate::master::request::{Classes, EventClasses, TimeSyncProcedure};
Expand All @@ -22,6 +22,7 @@ use crate::master::tasks::{AppTask, AssociationTask, ReadTask, Task};
use crate::master::{AssociationInformation, ReadHandler, ReadType, TaskType};
use crate::util::Smallest;

use crate::master::promise::Promise;
use crate::transport::FragmentAddr;
use crate::util::session::RunError;
use tokio::time::Instant;
Expand Down
2 changes: 1 addition & 1 deletion dnp3/src/master/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod test {
use crate::app::control::CommandStatus;
use crate::app::parse::parser::HeaderCollection;
use crate::app::*;
use crate::master::handler::{HeaderInfo, ReadHandler};
use crate::master::{HeaderInfo, ReadHandler};

use super::*;

Expand Down
27 changes: 1 addition & 26 deletions dnp3/src/master/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::master::association::AssociationConfig;
use crate::master::error::{AssociationError, CommandError, PollError, TaskError, TimeSyncError};
use crate::master::messages::{AssociationMsg, AssociationMsgType, MasterMsg, Message};
use crate::master::poll::{PollHandle, PollMsg};
use crate::master::promise::Promise;
use crate::master::request::{CommandHeaders, CommandMode, ReadRequest, TimeSyncProcedure};
use crate::master::tasks::command::CommandTask;
use crate::master::tasks::deadbands::WriteDeadBandsTask;
Expand Down Expand Up @@ -531,32 +532,6 @@ impl AssociationHandle {
}
}

/// A generic callback type that must be invoked once and only once.
/// The user can select to implement it using FnOnce or a
/// one-shot reply channel
pub(crate) enum Promise<T> {
/// nothing happens when the promise is completed
None,
/// one-shot reply channel is consumed when the promise is completed
OneShot(tokio::sync::oneshot::Sender<T>),
}

impl<T> Promise<T> {
pub(crate) fn one_shot() -> (Self, tokio::sync::oneshot::Receiver<T>) {
let (tx, rx) = tokio::sync::oneshot::channel();
(Self::OneShot(tx), rx)
}

pub(crate) fn complete(self, value: T) {
match self {
Promise::None => {}
Promise::OneShot(s) => {
s.send(value).ok();
}
}
}
}

/// Task types used in [`AssociationInformation`]
#[cfg_attr(not(feature = "ffi"), non_exhaustive)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
2 changes: 1 addition & 1 deletion dnp3/src/master/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::decode::DecodeLevel;
use crate::link::EndpointAddress;
use crate::master::error::PollError;
use crate::master::error::{AssociationError, TaskError};
use crate::master::handler::Promise;
use crate::master::poll::PollMsg;
use crate::master::promise::Promise;
use crate::master::tasks::Task;
use crate::master::{AssociationConfig, AssociationHandler, AssociationInformation, ReadHandler};
use crate::transport::FragmentAddr;
Expand Down
1 change: 1 addition & 0 deletions dnp3/src/master/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) mod convert;
pub(crate) mod extract;
pub(crate) mod messages;
pub(crate) mod poll;
pub(crate) mod promise;
pub(crate) mod task;
pub(crate) mod tasks;

Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::app::format::write::HeaderWriter;
use crate::app::Shutdown;
use crate::master::association::Next;
use crate::master::error::PollError;
use crate::master::handler::{AssociationHandle, Promise};
use crate::master::handler::AssociationHandle;
use crate::master::request::ReadRequest;
use crate::util::Smallest;

use crate::master::promise::Promise;
use tokio::time::Instant;

/// Periodic poll representation
Expand Down
25 changes: 25 additions & 0 deletions dnp3/src/master/promise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// A generic callback type that must be invoked once and only once.
/// The user can select to implement it using FnOnce or a
/// one-shot reply channel
pub(crate) enum Promise<T> {
/// nothing happens when the promise is completed
None,
/// one-shot reply channel is consumed when the promise is completed
OneShot(tokio::sync::oneshot::Sender<T>),
}

impl<T> Promise<T> {
pub(crate) fn one_shot() -> (Self, tokio::sync::oneshot::Receiver<T>) {
let (tx, rx) = tokio::sync::oneshot::channel();
(Self::OneShot(tx), rx)
}

pub(crate) fn complete(self, value: T) {
match self {
Promise::None => {}
Promise::OneShot(s) => {
s.send(value).ok();
}
}
}
}
2 changes: 1 addition & 1 deletion dnp3/src/master/tasks/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::app::format::write::HeaderWriter;
use crate::app::parse::parser::{HeaderCollection, Response};
use crate::app::FunctionCode;
use crate::master::error::{CommandError, CommandResponseError, TaskError};
use crate::master::handler::Promise;
use crate::master::promise::Promise;
use crate::master::request::*;
use crate::master::tasks::{AppTask, NonReadTask, Task};

Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/deadbands.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::app::format::write::HeaderWriter;
use crate::app::parse::parser::Response;
use crate::app::FunctionCode;
use crate::master::promise::Promise;
use crate::master::tasks::{AppTask, NonReadTask, Task};
use crate::master::{DeadBandHeader, DeadBandHeaderVariants, Promise, TaskError, WriteError};
use crate::master::{DeadBandHeader, DeadBandHeaderVariants, TaskError, WriteError};

pub(crate) struct WriteDeadBandsTask {
headers: Vec<DeadBandHeader>,
Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/empty_response.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::app::format::write::HeaderWriter;
use crate::app::parse::parser::Response;
use crate::app::FunctionCode;
use crate::master::promise::Promise;
use crate::master::tasks::{AppTask, NonReadTask, Task};
use crate::master::{Headers, Promise, TaskError, WriteError};
use crate::master::{Headers, TaskError, WriteError};

pub(crate) struct EmptyResponseTask {
function: FunctionCode,
Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/file/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::app::format::WriteError;
use crate::app::parse::free_format::FreeFormatVariation;
use crate::app::parse::parser::{HeaderDetails, Response};
use crate::app::{FunctionCode, Group70Var2};
use crate::master::promise::Promise;
use crate::master::tasks::{AppTask, NonReadTask, Task};
use crate::master::{AuthKey, FileCredentials, FileError, Promise, TaskError};
use crate::master::{AuthKey, FileCredentials, FileError, TaskError};

pub(crate) struct AuthFileTask {
pub(crate) credentials: FileCredentials,
Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/file/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::app::format::WriteError;
use crate::app::parse::free_format::FreeFormatVariation;
use crate::app::parse::parser::{HeaderDetails, Response};
use crate::app::{FileStatus, FunctionCode, Group70Var4};
use crate::master::promise::Promise;
use crate::master::tasks::file::REQUEST_ID;
use crate::master::tasks::{AppTask, NonReadTask, Task};
use crate::master::{FileError, FileHandle, Promise, TaskError};
use crate::master::{FileError, FileHandle, TaskError};

pub(crate) struct CloseFileTask {
pub(crate) handle: FileHandle,
Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/file/directory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::app::{Group70Var7, MaybeAsync};
use crate::master::{FileAction, FileError, FileInfo, FileReader, Promise};
use crate::master::promise::Promise;
use crate::master::{FileAction, FileError, FileInfo, FileReader};
use scursor::ReadCursor;

pub(crate) struct DirectoryReader {
Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/file/get_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::app::format::WriteError;
use crate::app::parse::free_format::FreeFormatVariation;
use crate::app::parse::parser::{HeaderDetails, Response};
use crate::app::{FunctionCode, Timestamp};
use crate::master::promise::Promise;
use crate::master::tasks::{AppTask, NonReadTask, Task};
use crate::master::{FileError, FileInfo, Promise, TaskError};
use crate::master::{FileError, FileInfo, TaskError};

pub(crate) struct GetFileInfoTask {
file_name: String,
Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/file/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::app::format::WriteError;
use crate::app::parse::free_format::FreeFormatVariation;
use crate::app::parse::parser::{HeaderDetails, Response};
use crate::app::{FileStatus, FunctionCode, Group70Var3, Permissions, Timestamp};
use crate::master::promise::Promise;
use crate::master::tasks::file::REQUEST_ID;
use crate::master::tasks::{AppTask, NonReadTask, Task};
use crate::master::{AuthKey, FileError, FileHandle, FileMode, OpenFile, Promise, TaskError};
use crate::master::{AuthKey, FileError, FileHandle, FileMode, OpenFile, TaskError};
pub(crate) struct OpenFileRequest {
pub(crate) file_name: String,
pub(crate) auth_key: AuthKey,
Expand Down
3 changes: 2 additions & 1 deletion dnp3/src/master/tasks/file/write_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::app::format::WriteError;
use crate::app::parse::free_format::FreeFormatVariation;
use crate::app::parse::parser::{HeaderDetails, Response};
use crate::app::{FileStatus, FunctionCode, Group70Var5};
use crate::master::promise::Promise;
use crate::master::tasks::{AppTask, NonReadTask, Task};
use crate::master::{BlockNumber, FileError, FileHandle, Promise, TaskError};
use crate::master::{BlockNumber, FileError, FileHandle, TaskError};

pub(crate) struct WriteBlockRequest {
pub(crate) handle: FileHandle,
Expand Down
2 changes: 1 addition & 1 deletion dnp3/src/master/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::app::ResponseHeader;
use crate::master::association::Association;
use crate::master::error::TaskError;
use crate::master::extract::extract_measurements;
use crate::master::handler::Promise;
use crate::master::poll::Poll;
use crate::master::promise::Promise;
use crate::master::request::{Classes, EventClasses};
use crate::master::tasks::auto::AutoTask;
use crate::master::tasks::command::CommandTask;
Expand Down
2 changes: 1 addition & 1 deletion dnp3/src/master/tasks/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::app::format::write::HeaderWriter;
use crate::master::error::TaskError;
use crate::master::handler::Promise;
use crate::master::promise::Promise;
use crate::master::request::ReadRequest;
use crate::master::tasks::{AppTask, ReadTask, Task};
use crate::master::ReadHandler;
Expand Down
2 changes: 1 addition & 1 deletion dnp3/src/master/tasks/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::app::gen::count::CountVariation;
use crate::app::parse::parser::Response;
use crate::app::FunctionCode;
use crate::master::error::TaskError;
use crate::master::handler::Promise;
use crate::master::promise::Promise;
use crate::master::tasks::{AppTask, NonReadTask, Task};

/// Type of restart to request
Expand Down
2 changes: 1 addition & 1 deletion dnp3/src/master/tasks/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::app::FunctionCode;
use crate::app::Timestamp;
use crate::master::association::Association;
use crate::master::error::{TaskError, TimeSyncError};
use crate::master::handler::Promise;
use crate::master::promise::Promise;
use crate::master::request::TimeSyncProcedure;
use crate::master::tasks::{AppTask, NonReadTask, Task};

Expand Down
4 changes: 2 additions & 2 deletions dnp3/src/master/tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::link::header::{FrameInfo, FrameType};
use crate::link::reader::LinkModes;
use crate::link::EndpointAddress;
use crate::master::association::AssociationConfig;
use crate::master::handler::{AssociationHandle, MasterChannel, ReadHandler};
use crate::master::task::MasterTask;
use crate::master::{AssociationHandle, MasterChannel, ReadHandler};
use crate::master::{
AssociationHandler, AssociationInformation, HeaderInfo, MasterChannelConfig, MasterChannelType,
};
Expand All @@ -22,7 +22,7 @@ struct DefaultAssociationHandler;
impl AssociationHandler for DefaultAssociationHandler {}

pub(crate) async fn create_association(mut config: AssociationConfig) -> TestHarness {
// use a 1 second timeout for all tests
// use a 1-second timeout for all tests
config.response_timeout = Timeout::from_secs(1).unwrap();

let (io, io_handle) = sfio_tokio_mock_io::mock();
Expand Down

0 comments on commit 9541d3c

Please sign in to comment.