Skip to content

Commit

Permalink
Stability fixes, and introducing an example config for a router address
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Hallerbäck committed Aug 28, 2024
1 parent fba7da0 commit ecf181f
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 59 deletions.
99 changes: 70 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ async-trait = "0.1.81"
async-recursion = "1.1.1"
sha2 = "0.10.8"
once_cell = "1.19.0"
ctrlc-async = "3.2.2"
60 changes: 46 additions & 14 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,9 @@ struct Cli {
}

// Create a global instance of WindowManager
static WINDOW_MANAGER: Lazy<WindowManager> = Lazy::new(|| {
let manager = WindowManager::init(2);
manager
});
static WINDOW_MANAGER: Lazy<WindowManager> = Lazy::new(|| WindowManager::init(2));

fn test_cb_chat(public_key: &str, message: &str) {
fn cb_chat(public_key: &str, message: &str) {
let pub_key_decoded = match base64::decode(public_key) {
Err(_) => {
return;
Expand All @@ -97,7 +94,7 @@ fn test_cb_chat(public_key: &str, message: &str) {
}
}

fn test_cb_chat_input(
fn cb_chat_input(
_pub_key_fingerprint: &str,
session_id: &str,
topic_out: &str,
Expand All @@ -106,15 +103,23 @@ fn test_cb_chat_input(
let input = WINDOW_MANAGER.getch(1, &prompt);
let input = input.trim();
let topic = Topic::Internal.as_str();
let msg = SessionMessage::new_internal(
let mut msg = SessionMessage::new_internal(
session_id.to_string(),
input.to_string(),
topic_out.to_string(),
);
(topic.to_string(), msg.serialize().unwrap())
if input == "!exit" {
// Special message that terminate the session
msg = SessionMessage::new_internal(
"internal".to_owned(),
"terminate".to_owned(),
topic.to_string(),
);
}
return (topic.to_string(), msg.serialize().unwrap());
}

fn test_cb_discovered(public_key: &str) -> bool {
fn cb_discovered(public_key: &str) -> bool {
let pub_key_decoded = match base64::decode(public_key) {
Err(_) => {
return false;
Expand All @@ -137,7 +142,11 @@ fn test_cb_discovered(public_key: &str) -> bool {
}
}

fn test_cb_initialized(public_key: &str) -> bool {
fn cb_terminate() {
WINDOW_MANAGER.cleanup();
}

fn cb_initialized(public_key: &str) -> bool {
let pub_key_decoded = match base64::decode(public_key) {
Err(_) => {
return false;
Expand All @@ -160,6 +169,7 @@ fn test_cb_initialized(public_key: &str) -> bool {
);
let input = WINDOW_MANAGER.getch(1, ">> ");
if input.to_lowercase().starts_with('y') {
WINDOW_MANAGER.printw(1, "-- Chat initialized, exit by typing '!exit'");
return true;
} else {
WINDOW_MANAGER.printw(1, "-- chat not accepted");
Expand All @@ -173,6 +183,18 @@ fn test_cb_initialized(public_key: &str) -> bool {
}
}

fn terminate(tx: mpsc::Sender<(String, String)>) {
tokio::spawn(async move {
let topic = Topic::Internal.as_str();
let msg = SessionMessage::new_internal(
"internal".to_owned(),
"terminate".to_owned(),
topic.to_string(),
);
tx.send((topic.to_string(), msg.to_string())).await;
});
}

#[tokio::main]
async fn main() {
let cli = Cli::parse();
Expand Down Expand Up @@ -255,15 +277,18 @@ async fn main() {
};
}

session.register_callback_chat(Box::new(test_cb_chat)).await;
session.register_callback_chat(Box::new(cb_chat)).await;
session
.register_callback_initialized(Box::new(test_cb_initialized))
.register_callback_initialized(Box::new(cb_initialized))
.await;
session
.register_callback_discovered(Box::new(test_cb_discovered))
.register_callback_discovered(Box::new(cb_discovered))
.await;
session
.register_callback_chat_input(Box::new(test_cb_chat_input))
.register_callback_chat_input(Box::new(cb_chat_input))
.await;
session
.register_callback_terminate(Box::new(cb_terminate))
.await;

let mut i = 0;
Expand Down Expand Up @@ -358,5 +383,12 @@ async fn main() {
WINDOW_MANAGER.printw(1, &format!("-- Awaiting connection requests from peers..."));
}

let tx = session.get_tx().await;
ctrlc_async::set_handler(move || {
let tx_clone = tx.clone();
terminate(tx_clone);
})
.expect("Error setting Ctrl-C handler");

session.serve().await;
}
Loading

0 comments on commit ecf181f

Please sign in to comment.