use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use super::acsi::AssocId;
use super::files::{FileError, FileWriter};
use crate::common::Instant;
use crate::proto::mms::file::FileNameBuf;
use crate::proto::mms::{ConfirmedRequest, ConfirmedResponse, Mms, ServiceError, file_error, obtain_file_error, service_error};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Outcome {
Stored,
Failed(Refusal),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Refusal {
pub class: u32,
pub code: i64,
pub side: i64,
}
impl Refusal {
pub const fn destination(code: i64) -> Refusal {
Refusal { class: service_error::FILE, code, side: obtain_file_error::DESTINATION_FILE }
}
pub const fn source(code: i64) -> Refusal {
Refusal { class: service_error::FILE, code, side: obtain_file_error::SOURCE_FILE }
}
pub const fn from_store(e: FileError) -> Refusal {
Refusal::destination(e.mms_code())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Stage {
Open,
Read(i32),
Close(i32),
}
struct Transfer {
obtain_invoke: Option<i64>,
source: FileNameBuf,
writer: Option<Box<dyn FileWriter>>,
stage: Stage,
waiting: Option<i64>,
pending: bool,
declared: u64,
received: u64,
deadline: Instant,
failure: Option<Refusal>,
}
impl core::fmt::Debug for Transfer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Transfer")
.field("stage", &self.stage)
.field("waiting", &self.waiting)
.field("declared", &self.declared)
.field("received", &self.received)
.field("failure", &self.failure)
.finish_non_exhaustive()
}
}
#[derive(Debug)]
pub struct Obtains {
transfers: BTreeMap<AssocId, Transfer>,
max: usize,
timeout_ms: u64,
}
pub type Finished = (AssocId, i64, Outcome);
impl Obtains {
pub fn new(max: usize, timeout_ms: u64) -> Obtains {
Obtains { transfers: BTreeMap::new(), max, timeout_ms }
}
pub fn admit(&self, assoc: AssocId) -> Result<(), Refusal> {
if self.transfers.contains_key(&assoc) || self.transfers.len() >= self.max {
return Err(Refusal::destination(file_error::FILE_BUSY));
}
Ok(())
}
pub fn start(&mut self, assoc: AssocId, source: FileNameBuf, writer: Box<dyn FileWriter>, now: Instant) {
self.transfers.insert(
assoc,
Transfer {
obtain_invoke: None,
source,
writer: Some(writer),
stage: Stage::Open,
waiting: None,
pending: true,
declared: 0,
received: 0,
deadline: now.plus_millis(self.timeout_ms),
failure: None,
},
);
}
pub fn bind(&mut self, assoc: AssocId, obtain_invoke: i64) {
if let Some(t) = self.transfers.get_mut(&assoc) {
t.obtain_invoke = Some(obtain_invoke);
}
}
pub fn next_call(&self, assoc: AssocId) -> Option<ConfirmedRequest<'_>> {
let t = self.transfers.get(&assoc)?;
if !t.pending || t.waiting.is_some() {
return None;
}
Some(match t.stage {
Stage::Open => ConfirmedRequest::FileOpen { name: t.source.as_name(), position: 0 },
Stage::Read(frsm) => ConfirmedRequest::FileRead(frsm),
Stage::Close(frsm) => ConfirmedRequest::FileClose(frsm),
})
}
pub fn issued(&mut self, assoc: AssocId, invoke: i64) {
if let Some(t) = self.transfers.get_mut(&assoc) {
t.waiting = Some(invoke);
t.pending = false;
}
}
pub fn failed(&mut self, assoc: AssocId, invoke: Option<i64>) -> Option<Finished> {
let t = self.transfers.get(&assoc)?;
if invoke.is_some() && t.waiting != invoke {
return None;
}
self.finish(assoc, Some(Refusal::source(file_error::OTHER)))
}
pub fn on_response(&mut self, assoc: AssocId, invoke: i64, pdu: &Mms<'_>) -> Option<Finished> {
let t = self.transfers.get_mut(&assoc)?;
if t.waiting != Some(invoke) {
return None;
}
t.waiting = None;
let error = match pdu {
Mms::ConfirmedError { error, .. } => Some(ServiceError::parse(error).unwrap_or_default()),
_ => None,
};
let service = match pdu {
Mms::ConfirmedResponse { service, .. } => Some(service),
_ => None,
};
match (t.stage, service, error) {
(Stage::Open, Some(ConfirmedResponse::FileOpen { frsm_id, attributes }), _) => {
t.stage = Stage::Read(*frsm_id);
t.declared = u64::from(attributes.size);
if let Err(e) = t.writer.as_mut().map_or(Ok(()), |w| w.expect(attributes.size)) {
t.failure = Some(Refusal::from_store(e));
t.stage = Stage::Close(*frsm_id);
}
}
(Stage::Open, _, Some(e)) => {
let code = if e.class == service_error::FILE { e.code } else { file_error::FILE_NON_EXISTENT };
return self.finish(assoc, Some(Refusal::source(code)));
}
(Stage::Read(frsm), Some(ConfirmedResponse::FileRead { data, more_follows }), _) => {
t.received = t.received.saturating_add(data.len() as u64);
if t.received > t.declared {
t.failure = Some(Refusal::source(file_error::POSITION_INVALID));
t.stage = Stage::Close(frsm);
} else if let Err(e) = t.writer.as_mut().map_or(Ok(()), |w| w.write(data)) {
t.failure = Some(Refusal::from_store(e));
t.stage = Stage::Close(frsm);
} else if !*more_follows {
if t.received != t.declared {
t.failure = Some(Refusal::source(file_error::POSITION_INVALID));
}
t.stage = Stage::Close(frsm);
}
}
(Stage::Read(frsm), _, _) => {
t.failure = Some(Refusal::source(file_error::OTHER));
t.stage = Stage::Close(frsm);
}
(Stage::Close(_), _, _) => {
let failure = t.failure;
return self.finish(assoc, failure);
}
(Stage::Open, _, None) => return self.finish(assoc, Some(Refusal::source(file_error::OTHER))),
}
t.pending = true;
None
}
pub fn on_timeout(&mut self, now: Instant) -> Vec<Finished> {
let late: Vec<AssocId> = self.transfers.iter().filter(|(_, t)| now >= t.deadline).map(|(a, _)| *a).collect();
let timeout = Refusal { class: service_error::SERVICE_PREEMPT, code: service_error::TIMEOUT, side: obtain_file_error::SOURCE_FILE };
late.into_iter().filter_map(|assoc| self.finish(assoc, Some(timeout))).collect()
}
pub fn on_closed(&mut self, assoc: AssocId) {
self.transfers.remove(&assoc);
}
pub fn next_timeout(&self) -> Option<Instant> {
self.transfers.values().map(|t| t.deadline).min()
}
pub fn len(&self) -> usize {
self.transfers.len()
}
pub fn is_empty(&self) -> bool {
self.transfers.is_empty()
}
fn finish(&mut self, assoc: AssocId, failure: Option<Refusal>) -> Option<Finished> {
let mut t = self.transfers.remove(&assoc)?;
let outcome = match (failure, t.writer.take()) {
(None, Some(writer)) => match writer.commit() {
Ok(()) => Outcome::Stored,
Err(e) => Outcome::Failed(Refusal::from_store(e)),
},
(None, None) => Outcome::Failed(Refusal::destination(file_error::OTHER)),
(Some(refusal), _) => Outcome::Failed(refusal),
};
Some((assoc, t.obtain_invoke?, outcome))
}
}
#[cfg(test)]
mod tests {
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec;
use super::*;
use crate::common::Limits;
use crate::proto::mms::file::FileAttributes;
#[derive(Clone, Default)]
struct Memory(Arc<std::sync::Mutex<(Vec<u8>, bool)>>, u64);
impl FileWriter for Memory {
fn expect(&mut self, size: u32) -> Result<(), FileError> {
if u64::from(size) > self.1 { Err(FileError::TooLarge) } else { Ok(()) }
}
fn write(&mut self, chunk: &[u8]) -> Result<(), FileError> {
let mut g = self.0.lock().map_err(|_| FileError::Io)?;
if g.0.len() as u64 + chunk.len() as u64 > self.1 {
return Err(FileError::TooLarge);
}
g.0.extend_from_slice(chunk);
Ok(())
}
fn commit(self: Box<Self>) -> Result<(), FileError> {
self.0.lock().map_err(|_| FileError::Io)?.1 = true;
Ok(())
}
}
fn open(size: u32) -> Vec<u8> {
Mms::ConfirmedResponse { invoke_id: 0, service: ConfirmedResponse::FileOpen { frsm_id: 5, attributes: FileAttributes { size, last_modified: None } } }
.to_vec()
.unwrap()
}
fn read(data: &[u8], more: bool) -> Vec<u8> {
Mms::ConfirmedResponse { invoke_id: 0, service: ConfirmedResponse::FileRead { data, more_follows: more } }.to_vec().unwrap()
}
fn close() -> Vec<u8> {
Mms::ConfirmedResponse { invoke_id: 0, service: ConfirmedResponse::FileClose }.to_vec().unwrap()
}
fn drive(engine: &mut Obtains, answers: &[Vec<u8>]) -> (Vec<String>, Option<Finished>) {
let mut calls = Vec::new();
for (n, answer) in answers.iter().enumerate() {
let Some(call) = engine.next_call(1) else { break };
calls.push(alloc::format!("{call:?}").split([' ', '(', '{']).next().unwrap_or("").into());
let invoke = 100 + i64::try_from(n).unwrap();
engine.issued(1, invoke);
let pdu = Mms::parse(answer, &Limits::DEFAULT).unwrap();
if let Some(done) = engine.on_response(1, invoke, &pdu) {
return (calls, Some(done));
}
}
(calls, None)
}
fn started(limit: u64) -> (Obtains, Memory) {
let mut engine = Obtains::new(2, 1000);
let memory = Memory(Arc::default(), limit);
engine.start(1, FileNameBuf::from_path("local.xml").unwrap(), Box::new(memory.clone()), Instant(0));
engine.bind(1, 7);
(engine, memory)
}
#[test]
fn a_transfer_opens_reads_closes_and_stores() {
let (mut engine, memory) = started(100);
let (calls, done) = drive(&mut engine, &[open(6), read(b"abc", true), read(b"def", false), close()]);
assert_eq!(calls, ["FileOpen", "FileRead", "FileRead", "FileClose"]);
assert_eq!(done, Some((1, 7, Outcome::Stored)));
assert_eq!(*memory.0.lock().unwrap(), (b"abcdef".to_vec(), true));
assert!(engine.is_empty());
}
#[test]
fn a_declared_size_over_the_limit_is_refused_before_any_read() {
let (mut engine, memory) = started(4);
let (calls, done) = drive(&mut engine, &[open(6), close()]);
assert_eq!(calls, ["FileOpen", "FileClose"], "closed, never read");
assert_eq!(done, Some((1, 7, Outcome::Failed(Refusal::destination(file_error::INSUFFICIENT_SPACE)))));
assert!(!memory.0.lock().unwrap().1, "nothing committed");
}
#[test]
fn more_data_than_declared_fails_the_transfer() {
let (mut engine, _) = started(100);
let (_, done) = drive(&mut engine, &[open(2), read(b"abc", false), close()]);
assert_eq!(done, Some((1, 7, Outcome::Failed(Refusal::source(file_error::POSITION_INVALID)))));
}
#[test]
fn a_client_that_cannot_open_its_file_ends_the_transfer_without_a_close() {
let (mut engine, _) = started(100);
let encoded = ServiceError::encode(crate::ber::Tag::context_constructed(2), 11, file_error::FILE_NON_EXISTENT).unwrap();
let error = crate::ber::Cursor::new(&encoded).next_required().unwrap();
let pdu = Mms::ConfirmedError { invoke_id: 100, modifier_position: None, error };
assert!(engine.next_call(1).is_some());
engine.issued(1, 100);
assert_eq!(engine.on_response(1, 100, &pdu), Some((1, 7, Outcome::Failed(Refusal::source(file_error::FILE_NON_EXISTENT)))));
}
#[test]
fn a_stalled_client_is_timed_out_and_only_its_own_transfer_ends() {
let (mut engine, _) = started(100);
engine.start(2, FileNameBuf::from_path("other").unwrap(), Box::new(Memory(Arc::default(), 10)), Instant(0).plus_millis(500));
engine.bind(2, 9);
assert_eq!(engine.admit(3), Err(Refusal::destination(file_error::FILE_BUSY)), "two in flight, max two");
assert_eq!(engine.admit(1), Err(Refusal::destination(file_error::FILE_BUSY)), "one per association");
let done = engine.on_timeout(Instant(0).plus_millis(1000));
assert_eq!(done.len(), 1);
assert_eq!(done[0].0, 1);
assert!(matches!(done[0].2, Outcome::Failed(Refusal { class: 5, code: 1, .. })));
assert_eq!(engine.len(), 1, "the other transfer goes on");
}
#[test]
fn an_unexpected_answer_never_panics_and_fails_the_transfer() {
let (mut engine, _) = started(100);
let identify =
Mms::ConfirmedResponse { invoke_id: 0, service: ConfirmedResponse::Status { logical: 0, physical: 0, local_detail: None } }.to_vec().unwrap();
let (_, done) = drive(&mut engine, &[identify]);
assert!(matches!(done, Some((1, 7, Outcome::Failed(_)))));
let (mut engine, _) = started(100);
let _ = engine.next_call(1);
engine.issued(1, 1);
assert_eq!(engine.on_response(1, 99, &Mms::parse(&close(), &Limits::DEFAULT).unwrap()), None);
let _ = vec![0u8; 0];
}
}