Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Implemented test connections.
Browse files Browse the repository at this point in the history
Added proper tests for x & y modem protocols.
  • Loading branch information
mkrueger committed Sep 30, 2023
1 parent cebe254 commit d750f21
Show file tree
Hide file tree
Showing 19 changed files with 356 additions and 377 deletions.
2 changes: 1 addition & 1 deletion src/com/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod websocket;
#[cfg(not(target_arch = "wasm32"))]
pub mod ssh;

use crate::{addresses::Terminal, ui::connection::OpenConnectionData};
use crate::{addresses::Terminal, ui::connect::OpenConnectionData};
pub type TermComResult<T> = Result<T, Box<dyn Error + Send + Sync>>;

pub trait Com: Sync + Send {
Expand Down
6 changes: 5 additions & 1 deletion src/features/auto_login.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use web_time::Instant;

use crate::{ui::connection::Connection, util::PatternRecognizer, Address, Options, TerminalResult};
use crate::{
ui::connect::{Connection, DataConnection},
util::PatternRecognizer,
Address, Options, TerminalResult,
};
use std::time::Duration;

use super::iemsi_com::IEmsi;
Expand Down
6 changes: 5 additions & 1 deletion src/features/iemsi_com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use std::fmt;

use icy_engine::{get_crc16, get_crc32, update_crc32};

use crate::{addresses::Address, ui::connection::Connection, Options, TerminalResult, VERSION};
use crate::{
addresses::Address,
ui::connect::{Connection, DataConnection},
Options, TerminalResult, VERSION,
};

/// EMSI Inquiry is transmitted by the calling system to identify it as
/// EMSI capable. If an `EMSI_REQ` sequence is received in response, it is
Expand Down
1 change: 1 addition & 0 deletions src/protocol/file_storage_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl FileStorageHandler for TestStorageHandler {
self.file.get_mut(file_name).unwrap().extend_from_slice(data);
}
}

fn close(&mut self) {
self.cur_file_name = None;
self.cur_file_size = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]

use crate::ui::connection::Connection;
use crate::ui::connect::DataConnection;
use crate::TerminalResult;
use std::fs;
use std::path::PathBuf;
Expand Down Expand Up @@ -239,16 +239,16 @@ impl TransferState {
pub trait Protocol {
fn update(
&mut self,
com: &mut Connection,
com: &mut dyn DataConnection,
transfer_state: &Arc<Mutex<TransferState>>,
storage_handler: &mut dyn FileStorageHandler,
) -> TerminalResult<bool>;

fn initiate_send(&mut self, com: &mut Connection, files: Vec<FileDescriptor>, transfer_state: &mut TransferState) -> TerminalResult<()>;
fn initiate_send(&mut self, com: &mut dyn DataConnection, files: Vec<FileDescriptor>, transfer_state: &mut TransferState) -> TerminalResult<()>;

fn initiate_recv(&mut self, com: &mut Connection, transfer_state: &mut TransferState) -> TerminalResult<()>;
fn initiate_recv(&mut self, com: &mut dyn DataConnection, transfer_state: &mut TransferState) -> TerminalResult<()>;

fn cancel(&mut self, com: &mut Connection) -> TerminalResult<()>;
fn cancel(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()>;

fn use_raw_transfer(&self) -> bool {
false
Expand Down
12 changes: 6 additions & 6 deletions src/protocol/xymodem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};

use crate::{ui::connection::Connection, TerminalResult};
use crate::{ui::connect::DataConnection, TerminalResult};
mod constants;
mod err;
mod ry;
Expand Down Expand Up @@ -48,7 +48,7 @@ impl XYmodem {
impl super::Protocol for XYmodem {
fn update(
&mut self,
com: &mut Connection,
com: &mut dyn DataConnection,
transfer_state: &Arc<Mutex<TransferState>>,
storage_handler: &mut dyn FileStorageHandler,
) -> TerminalResult<bool> {
Expand All @@ -68,7 +68,7 @@ impl super::Protocol for XYmodem {
Ok(true)
}

fn initiate_send(&mut self, _com: &mut Connection, files: Vec<FileDescriptor>, transfer_state: &mut TransferState) -> TerminalResult<()> {
fn initiate_send(&mut self, _com: &mut dyn DataConnection, files: Vec<FileDescriptor>, transfer_state: &mut TransferState) -> TerminalResult<()> {
if !self.config.is_ymodem() && files.len() != 1 {
return Err(TransmissionError::XModem1File.into());
}
Expand All @@ -85,7 +85,7 @@ impl super::Protocol for XYmodem {
Ok(())
}

fn initiate_recv(&mut self, com: &mut Connection, transfer_state: &mut TransferState) -> TerminalResult<()> {
fn initiate_recv(&mut self, com: &mut dyn DataConnection, transfer_state: &mut TransferState) -> TerminalResult<()> {
let mut ry = ry::Ry::new(self.config);
ry.recv(com)?;
self.ry = Some(ry);
Expand All @@ -95,7 +95,7 @@ impl super::Protocol for XYmodem {
Ok(())
}

fn cancel(&mut self, com: &mut Connection) -> TerminalResult<()> {
fn cancel(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
cancel(com)
}

Expand All @@ -104,7 +104,7 @@ impl super::Protocol for XYmodem {
}
}

fn cancel(com: &mut Connection) -> TerminalResult<()> {
fn cancel(com: &mut dyn DataConnection) -> TerminalResult<()> {
com.send(vec![CAN, CAN, CAN, CAN, CAN, CAN])?;
Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions src/protocol/xymodem/ry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
xymodem::constants::{ACK, EOT, EXT_BLOCK_LENGTH, NAK, SOH, STX},
FileStorageHandler, TransferState,
},
ui::connection::Connection,
ui::connect::DataConnection,
TerminalResult,
};

Expand Down Expand Up @@ -48,7 +48,7 @@ impl Ry {

pub fn update(
&mut self,
com: &mut Connection,
com: &mut dyn DataConnection,
transfer_state: &Arc<Mutex<TransferState>>,
storage_handler: &mut dyn FileStorageHandler,
) -> TerminalResult<()> {
Expand All @@ -59,7 +59,6 @@ impl Ry {
transfer_info.check_size = self.configuration.get_check_and_size();
transfer_info.update_bps();
}

match self.recv_state {
RecvState::None => {}

Expand All @@ -76,6 +75,7 @@ impl Ry {
self.recv_state = RecvState::ReadBlock(DEFAULT_BLOCK_LENGTH, 0);
}
} else if start == STX {
storage_handler.open_unnamed_file();
self.recv_state = RecvState::ReadBlock(EXT_BLOCK_LENGTH, 0);
} else {
if retries < 3 {
Expand Down Expand Up @@ -234,18 +234,18 @@ impl Ry {
Ok(())
}

pub fn cancel(&mut self, com: &mut Connection) -> TerminalResult<()> {
pub fn cancel(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
self.recv_state = RecvState::None;
super::cancel(com)
}

pub fn recv(&mut self, com: &mut Connection) -> TerminalResult<()> {
pub fn recv(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
self.await_data(com)?;
self.recv_state = RecvState::StartReceive(0);
Ok(())
}

fn await_data(&mut self, com: &mut Connection) -> TerminalResult<usize> {
fn await_data(&mut self, com: &mut dyn DataConnection) -> TerminalResult<usize> {
if self.configuration.is_streaming() {
com.send(vec![b'G'])?;
} else if self.configuration.use_crc() {
Expand Down
48 changes: 27 additions & 21 deletions src/protocol/xymodem/sy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
xymodem::constants::{ACK, CPMEOF, EOT, EXT_BLOCK_LENGTH, NAK, SOH, STX},
FileDescriptor, TransferState,
},
ui::connection::Connection,
ui::connect::DataConnection,
TerminalResult,
};

Expand Down Expand Up @@ -55,7 +55,10 @@ impl Sy {
data: Vec::new(),
errors: 0,
bytes_send: 0,
block_number: 0,
block_number: match configuration.variant {
XYModemVariant::YModem | XYModemVariant::YModemG => 1,
_ => 0,
},
cur_file: 0,
transfer_stopped: false,
}
Expand All @@ -65,7 +68,7 @@ impl Sy {
matches!(self.send_state, SendState::None)
}

pub fn update(&mut self, com: &mut Connection, transfer_state: &Arc<Mutex<TransferState>>) -> TerminalResult<()> {
pub fn update(&mut self, com: &mut dyn DataConnection, transfer_state: &Arc<Mutex<TransferState>>) -> TerminalResult<()> {
if let Ok(mut transfer_state) = transfer_state.lock() {
transfer_state.update_time();
let transfer_info = &mut transfer_state.send_state;
Expand All @@ -79,7 +82,7 @@ impl Sy {
transfer_info.check_size = self.configuration.get_check_and_size();
transfer_info.update_bps();
}
// println!("send state: {:?} {:?}", self.send_state, self.configuration.variant);
println!("send state: {:?} {:?}", self.send_state, self.configuration.variant);

match self.send_state {
SendState::None => {}
Expand Down Expand Up @@ -198,11 +201,16 @@ impl Sy {
}
SendState::YModemEndHeader(step) => match step {
0 => {
if self.read_command(com)? == NAK {
let read_command = self.read_command(com)?;
if read_command == NAK {
com.send(vec![EOT])?;
self.send_state = SendState::YModemEndHeader(1);
return Ok(());
}
if read_command == ACK {
self.send_state = SendState::None;
return Ok(());
}
self.cancel(com)?;
}
1 => {
Expand All @@ -228,7 +236,7 @@ impl Sy {
Ok(())
}

fn check_eof(&mut self, com: &mut Connection) -> TerminalResult<()> {
fn check_eof(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
if self.bytes_send >= self.files[self.cur_file].size {
self.eot(com)?;
if self.configuration.is_ymodem() {
Expand All @@ -241,34 +249,32 @@ impl Sy {
}

#[allow(clippy::unused_self)]
fn read_command(&self, com: &mut Connection) -> TerminalResult<u8> {
fn read_command(&self, com: &mut dyn DataConnection) -> TerminalResult<u8> {
let ch = com.read_u8()?;
/* let cmd = match ch {
/*
let cmd = match ch {
b'C' => "[C]",
EOT => "[EOT]",
ACK => "[ACK]",
NAK => "[NAK]",
CAN => "[CAN]",
_ => ""
};
if cmd.len() > 0 {
"GOT CMD: {}", cmd);
} else {
println!("GOT CMD: #{} (0x{:X})", ch, ch);
}*/
println!("GOT CMD: #{} (0x{:X})", cmd, ch);*/

Ok(ch)
}

#[allow(clippy::unused_self)]
fn eot(&self, com: &mut Connection) -> TerminalResult<usize> {
fn eot(&self, com: &mut dyn DataConnection) -> TerminalResult<usize> {
// println!("[EOT]");
com.send(vec![EOT])?;
self.read_command(com)?; // read ACK

Ok(1)
}

pub fn get_mode(&mut self, com: &mut Connection) -> TerminalResult<()> {
pub fn get_mode(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
let ch = self.read_command(com)?;
match ch {
NAK => {
Expand All @@ -292,7 +298,7 @@ impl Sy {
}
}

fn send_block(&mut self, com: &mut Connection, data: &[u8], pad_byte: u8) -> TerminalResult<()> {
fn send_block(&mut self, com: &mut dyn DataConnection, data: &[u8], pad_byte: u8) -> TerminalResult<()> {
let block_len = if data.len() <= DEFAULT_BLOCK_LENGTH { SOH } else { STX };
let mut block = Vec::new();
block.push(block_len);
Expand All @@ -317,7 +323,7 @@ impl Sy {
Ok(())
}

fn send_ymodem_header(&mut self, com: &mut Connection) -> TerminalResult<()> {
fn send_ymodem_header(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
if self.cur_file < self.files.len() {
// restart from 0
let mut block = Vec::new();
Expand All @@ -334,7 +340,7 @@ impl Sy {
}
}

fn send_data_block(&mut self, com: &mut Connection, offset: usize) -> TerminalResult<bool> {
fn send_data_block(&mut self, com: &mut dyn DataConnection, offset: usize) -> TerminalResult<bool> {
let data_len = self.data.len();
if offset >= data_len {
return Ok(false);
Expand All @@ -350,7 +356,7 @@ impl Sy {
Ok(true)
}

pub fn cancel(&mut self, com: &mut Connection) -> TerminalResult<()> {
pub fn cancel(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
self.send_state = SendState::None;
super::cancel(com)
}
Expand All @@ -362,7 +368,7 @@ impl Sy {
self.bytes_send = 0;
}

pub fn end_ymodem(&mut self, com: &mut Connection) -> TerminalResult<()> {
pub fn end_ymodem(&mut self, com: &mut dyn DataConnection) -> TerminalResult<()> {
self.send_block(com, &[0], 0)?;
self.transfer_stopped = true;
Ok(())
Expand Down
Loading

0 comments on commit d750f21

Please sign in to comment.