use super::*;
pub(super) fn is_answer(input: &[u8]) -> bool {
input.contains(&kermit_proto::codec::SOH)
}
#[derive(Debug, Default)]
pub struct Transcript {
pub files: Vec<Vec<u8>>,
pub stored: Vec<String>,
pub text: Vec<u8>,
}
#[derive(Debug)]
pub(super) enum Phase {
Pump,
Wire,
}
#[derive(Debug)]
pub struct Exchange {
pub(super) client: Client,
pub(super) sending: bool,
pub(super) out: VecDeque<Vec<u8>>,
pub(super) phase: Phase,
pub(super) transcript: Transcript,
pub(super) current: Option<Vec<u8>>,
pub(super) outcome: Option<Result<(), Failure>>,
pub(super) answered: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Failure {
Remote(String),
Link(String),
}
pub(super) fn config() -> Config {
let mut c = Config::default();
c.timeout = TIMEOUT;
c.retries = RETRIES;
c.linger = Duration::ZERO;
c
}
pub(super) fn ms_cycles(m: &Machine, ms: u64) -> u64 {
ms * u64::from(m.model().clock_hz()) / 1000
}
impl Failure {
pub fn message(&self) -> &str {
match self {
Failure::Remote(e) | Failure::Link(e) => e,
}
}
}
impl Exchange {
pub fn new<L: Line + ?Sized>(
wire: &mut Wire,
m: &mut L,
command: Command,
) -> crate::Result<Exchange> {
Self::with(wire, m, command, config())
}
pub(super) fn with<L: Line + ?Sized>(
wire: &mut Wire,
m: &mut L,
command: Command,
config: Config,
) -> crate::Result<Exchange> {
wire.discard(m);
let sending = matches!(command, Command::Send(_));
let mut client = Client::new(config);
client
.start(wire.now(), command)
.map_err(|e| format!("Kermit: {e}"))?;
Ok(Exchange {
client,
sending,
out: VecDeque::new(),
phase: Phase::Pump,
transcript: Transcript::default(),
current: None,
outcome: None,
answered: false,
})
}
pub fn step<L: Line + ?Sized>(
&mut self,
wire: &mut Wire,
m: &mut L,
stop: u64,
) -> crate::Result<Option<Result<Transcript, Failure>>> {
loop {
match self.phase {
Phase::Pump => {
let now = wire.now();
while let Some(p) = self.client.poll_output(now) {
self.out.push_back(p);
}
self.events();
if let Some(packet) = self.out.pop_front() {
if !wire.start_write(m, packet) {
self.phase = Phase::Wire;
}
continue;
}
let deadline = match (self.client.next_timeout(), &self.outcome) {
(None, Some(_)) => {
let outcome = self.outcome.take().unwrap_or(Ok(()));
let t = std::mem::take(&mut self.transcript);
return Ok(Some(outcome.map(|()| t)));
}
(Some(t), _) => t.max(now + MIN_WAIT),
(None, None) => now + IDLE_WAIT,
};
wire.start_read(m, deadline);
self.phase = Phase::Wire;
}
Phase::Wire => {
let budget = stop.saturating_sub(m.cycles());
match wire.step(m, budget)? {
WireStep::Pending => return Ok(None),
WireStep::Sent => {}
WireStep::Quiet(input) | WireStep::Deadline(input) => {
let now = wire.now();
if !input.is_empty() {
self.answered |= is_answer(&input);
self.client.handle_input(now, &input);
}
self.client.handle_timeout(now);
}
}
self.phase = Phase::Pump;
}
}
}
}
fn events(&mut self) {
while let Some(event) = self.client.poll_event() {
match event {
Event::FileStart { name } if self.sending => {
self.transcript.stored.push(charset::decode(&name));
}
Event::FileStart { .. } => self.current = Some(Vec::new()),
Event::Data(data) => {
if let Some(file) = self.current.as_mut() {
file.extend_from_slice(&data);
}
}
Event::FileEnd { discarded } => {
if let Some(file) = self.current.take()
&& !discarded
{
self.transcript.files.push(file);
}
}
Event::ServerText(text) => self.transcript.text.extend_from_slice(&text),
Event::Done => self.outcome = Some(Ok(())),
Event::Error(kermit_proto::Error::Remote(text)) => {
self.outcome = Some(Err(Failure::Remote(format!(
"calculator error: {}",
charset::decode(&text)
))));
}
Event::Error(e) => self.outcome = Some(Err(Failure::Link(format!("Kermit: {e}")))),
_ => {}
}
}
}
}