Skip to content

Commit

Permalink
So i managed to to make everything work as in d2fc5ef, but stable. #1
Browse files Browse the repository at this point in the history
…closed.
  • Loading branch information
Lester Covey committed Apr 11, 2022
1 parent 9a19c9e commit 9664d7a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 34 deletions.
108 changes: 74 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@ enum ChatState {
Tied(String),
}

// TODO:
// So if I just use `AppState` as a `JobState`'s associated value
// everything falls apart bc of recursion
#[derive(PartialEq, Clone)]
enum JobSwitchAppState {
Auth,
Chat(Chat),
}

#[derive(PartialEq, Clone)]
enum JobState {
InProgress,
Ok,
Err,
Ok(JobSwitchAppState),
Err(JobSwitchAppState),
}

/// The job data is stored here. Job is a state when app is busy with something
Expand Down Expand Up @@ -133,7 +142,7 @@ struct App {
requested_job: u8,
sending_queue: Vec<String>,
sending_queue_sent: u8,
socket_handles: Option<(tokio::task::JoinHandle<()>, tokio::task::JoinHandle<()>)>,
writer_exists: bool,
}

impl App {
Expand All @@ -150,7 +159,7 @@ impl App {
requested_job: 0,
sending_queue: Vec::new(),
sending_queue_sent: 0,
socket_handles: None,
writer_exists: false,
}
}
// FIXME:
Expand All @@ -173,7 +182,7 @@ impl App {
requested_job: 0,
sending_queue: Vec::new(),
sending_queue_sent: 0,
socket_handles: None,
writer_exists: false,
}
}
/// Add text to App's job (if current state is `Job`, otherwise do nothing)
Expand Down Expand Up @@ -324,17 +333,12 @@ async unsafe fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()
} else if let AppState::Job(job) = &APP.state {
match &job.state {
JobState::InProgress => (),
JobState::Ok => {
//set_state(*ok.clone());
// FIXME:
// Oh I know I know...
// W/o these we sometimes get corrupt mem
thread::sleep(time::Duration::from_millis(200));
JobState::Ok(ok) => {
set_state_using_switch(ok.clone());
continue;
}
JobState::Err => {
//set_state(*err.clone());
thread::sleep(time::Duration::from_millis(200));
JobState::Err(err) => {
set_state_using_switch(err.clone());
continue;
}
}
Expand Down Expand Up @@ -398,7 +402,9 @@ unsafe fn set_state(to: AppState) {
match &to {
AppState::Chat(_) => APP.max_input_focus = 3,
AppState::Auth => {
APP.sending_queue_add(TX_DROPME_FLAG.to_string());
if APP.writer_exists {
APP.sending_queue_add(TX_DROPME_FLAG.to_string())
}
// if APP.socket_handles.is_some() {
// APP.socket_handles.as_ref().unwrap().0.abort();
// APP.socket_handles.as_ref().unwrap().1.abort();
Expand All @@ -412,6 +418,25 @@ unsafe fn set_state(to: AppState) {
APP.state = to;
}

/// Switch App's state to a corresponding one (from Job) and reset all associated variables
unsafe fn set_state_using_switch(to: JobSwitchAppState) {
match &to {
JobSwitchAppState::Chat(chat) => {
APP.max_input_focus = 3;
APP.state = AppState::Chat(chat.to_owned());
}
JobSwitchAppState::Auth => {
if APP.writer_exists {
APP.sending_queue_add(TX_DROPME_FLAG.to_string())
}
APP.max_input_focus = 1;
APP.state = AppState::Auth;
}
}
APP.input_focus = 0;
APP.inputs = [String::new(), String::new(), String::new()];
}

/// Daemon for acting on every incoming message
async unsafe fn read_ws(
with: futures_util::stream::SplitStream<
Expand All @@ -431,7 +456,10 @@ async unsafe fn read_ws(
if job.title == AUTH_JOB {
APP.job_log_add(JOB_SUCCESS);
APP.job_progress_set(100);
APP.job_state_set(JobState::Ok, false);
APP.job_state_set(
JobState::Ok(JobSwitchAppState::Chat(Chat::default())),
false,
);
} else {
// TODO:
// Panic?
Expand All @@ -442,7 +470,7 @@ async unsafe fn read_ws(
if let AppState::Job(job) = &APP.state {
if job.title == AUTH_JOB {
APP.job_log_add(AUTH_JOB_CONNECT_AUTH_FAULT);
APP.job_state_set(JobState::Err, false)
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), false)
} else {
// TODO:
// Panic?
Expand All @@ -455,7 +483,12 @@ async unsafe fn read_ws(
let subject = job.data[0].to_string();
APP.job_log_add(JOB_SUCCESS);
APP.job_progress_set(100);
APP.job_state_set(JobState::Ok, false);
APP.job_state_set(
JobState::Ok(JobSwitchAppState::Chat(Chat::with_subject(
subject,
))),
false,
);
} else {
// TODO:
// Panic?
Expand All @@ -477,7 +510,10 @@ async unsafe fn read_ws(
if let AppState::Job(job) = &APP.state {
if job.title == TIE_JOB {
APP.job_log_add(TIE_JOB_FAULT_NOUSER);
APP.job_state_set(JobState::Err, false)
APP.job_state_set(
JobState::Err(JobSwitchAppState::Chat(Chat::default())),
false,
)
} else {
// TODO:
// Panic?
Expand All @@ -487,7 +523,10 @@ async unsafe fn read_ws(
RXTX_UNTIE_FLAG => {
if let AppState::Chat(chat) = &APP.state {
if let ChatState::Tied(_) = chat.state {
APP.job_state_set(JobState::Err, true);
APP.job_state_set(
JobState::Err(JobSwitchAppState::Chat(Chat::default())),
true,
);
APP.job_log_add(TIE_BROKEN);
} else {
// TODO:
Expand All @@ -504,7 +543,7 @@ async unsafe fn read_ws(
_ => (),
},
Err(_) => {
APP.job_state_set(JobState::Err, false);
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), false);
APP.job_log_add(MESSAGE_CORRUPTED_ERROR)
}
}
Expand All @@ -513,7 +552,7 @@ async unsafe fn read_ws(
// TODO:
// This is from an earlier commit, is it ok?
if let AppState::Chat(_) = APP.state {
APP.job_state_set(JobState::Err, true);
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), true);
APP.job_log_add(CONNECTION_DROPPED_ERROR)
}
}
Expand All @@ -525,7 +564,8 @@ async unsafe fn write_ws(
Message,
>,
) {
loop {
APP.writer_exists = true;
'outer: loop {
// TODO:
// Is sleeping a good idea?
thread::sleep(time::Duration::from_millis(100));
Expand All @@ -541,16 +581,17 @@ async unsafe fn write_ws(
}
if with.send(Message::Text(i.to_string())).await.is_err() {
APP.job_log_add(AUTH_JOB_CONNECT_FAULT);
APP.job_state_set(JobState::Err, false);
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), false);
}
if i == &TX_DROPME_FLAG.to_string() {
APP.sending_queue = Vec::new();
APP.sending_queue_sent = 0;
return;
break 'outer;
}
APP.sending_queue_sent += 1;
}
}
APP.writer_exists = false;
}

/// Make new connection and return a socket
Expand Down Expand Up @@ -597,15 +638,14 @@ async unsafe fn start_auth_job() {
match connection {
Err(_) => {
APP.job_log_add(AUTH_JOB_CONNECT_FAULT);
APP.job_state_set(JobState::Err, false);
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), false);
return;
}
Ok(ok) => {
APP.job_log_add(JOB_SUCCESS);
let (write, read) = ok.split();
let r = tokio::spawn(read_ws(read));
let w = tokio::spawn(write_ws(write));
APP.socket_handles = Some((w, r));
tokio::spawn(read_ws(read));
tokio::spawn(write_ws(write));
APP.job_progress_set(90);
APP.job_log_add(AUTH_JOB_CONNECT_AUTH);
// FIXME:
Expand All @@ -621,17 +661,17 @@ async unsafe fn start_auth_job() {
};
} else {
APP.job_log_add(AUTH_JOB_PRECONNECT_FAULT_DISAPPROVED);
APP.job_state_set(JobState::Err, false);
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), false);
return;
}
} else {
APP.job_log_add(AUTH_JOB_PRECONNECT_FAULT_PARSE);
APP.job_state_set(JobState::Err, false);
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), false);
return;
}
} else {
APP.job_log_add(AUTH_JOB_PRECONNECT_FAULT_GET);
APP.job_state_set(JobState::Err, false);
APP.job_state_set(JobState::Err(JobSwitchAppState::Auth), false);
return;
}
}
Expand Down Expand Up @@ -670,8 +710,8 @@ unsafe fn job_ui<B: Backend>(f: &mut Frame<B>) {
.title_alignment(Alignment::Center)
.style(Style::default().bg(match job.state {
JobState::InProgress => Color::DarkGray,
JobState::Ok => Color::Green,
JobState::Err => Color::Red,
JobState::Ok(_) => Color::Green,
JobState::Err(_) => Color::Red,
}));
f.render_widget(main_window, chunks[0]);
{
Expand Down
1 change: 1 addition & 0 deletions src/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function on_message($connect, $data) {
$txt = decode($data)['payload'];
$flag = $txt[0];
$body = substr($txt, 1);
echo($txt . "\n");
unset($txt);
switch ($flag) {
case RX_AUTH_FLAG:
Expand Down

0 comments on commit 9664d7a

Please sign in to comment.