From cec4ce203ac9c2626d1413ec6ed1597c3d311c3e Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Sun, 7 May 2023 19:28:49 +0200 Subject: [PATCH] Clean up code --- Readme.md | 5 +++++ nats-client/src/chat_message.rs | 2 ++ nats-client/src/main.rs | 1 + nats-client/src/nats_cli.rs | 14 ++++++++++---- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index f342c74..0aed891 100644 --- a/Readme.md +++ b/Readme.md @@ -4,3 +4,8 @@ - admin is allowed everything - client may pusblish and subscribe to the chat channel - guest may only read chat messages + +## Commands +- Writing anything will publish text as a messages +- "invalid" will send a non json string +- "request xyz" will send xyz as a request, getting a automated response diff --git a/nats-client/src/chat_message.rs b/nats-client/src/chat_message.rs index e08a902..59359f3 100644 --- a/nats-client/src/chat_message.rs +++ b/nats-client/src/chat_message.rs @@ -3,6 +3,8 @@ use std::{error::Error, fmt::Display}; use serde::{Deserialize, Serialize}; +/// The object which will be sent via NATS. +/// In this example, it is a ChatMessage #[derive(Serialize, Deserialize)] pub struct ChatMessage { pub author: String, diff --git a/nats-client/src/main.rs b/nats-client/src/main.rs index 1fe6904..fa01da6 100644 --- a/nats-client/src/main.rs +++ b/nats-client/src/main.rs @@ -70,6 +70,7 @@ fn ask_user_name() -> String { input_text } +/// Main loop waiting for user input and posting the written messages fn user_message_writing_loop(username: &str, nc: &Connection) -> io::Result<()> { let stdin = io::stdin(); for line in stdin.lock().lines() { diff --git a/nats-client/src/nats_cli.rs b/nats-client/src/nats_cli.rs index fe579b1..c847fef 100644 --- a/nats-client/src/nats_cli.rs +++ b/nats-client/src/nats_cli.rs @@ -9,14 +9,16 @@ use crate::chat_message::ChatMessage; const SERVER: &str = "127.0.0.1"; const SUBJECT_MESSAGES: &str = "telestion.chat"; -const SUBJECT_REQEUST_REPLY_MESSAGES: &str = "telestion.request-reply"; +const CHANNEL_REQEUST_REPLY_MESSAGES: &str = "telestion.request-reply"; +/// Connect to the server with username and password pub fn connect_to_nats(username: &str, password: &str) -> io::Result { nats::Options::with_user_pass(username, password) .with_name("Chat Application") .connect(SERVER) } +/// Subscribe to chats pub fn subscribe_to_chat_messages(username: String, nc: &Connection) -> io::Result { // This runs the closure in a separate Thread let subscription = @@ -36,6 +38,8 @@ pub fn subscribe_to_chat_messages(username: String, nc: &Connection) -> io::Resu Ok(subscription) } +/// This function implements the reply part of the NATS request-reply protocol +/// It subscribes to the request-channel and sends an answer back pub fn subscribe_to_requests(nc: &Connection, answer_request: F) -> io::Result where F: Fn(ChatMessage) -> ChatMessage + Send + 'static, @@ -43,7 +47,7 @@ where // TODO: This is problematic due to the multithreading. Use multithreading safe things here!!! let nc = nc.clone(); // This runs the closure in a separate Thread - let subscription = nc.subscribe(SUBJECT_REQEUST_REPLY_MESSAGES)?.with_handler( + let subscription = nc.subscribe(CHANNEL_REQEUST_REPLY_MESSAGES)?.with_handler( move |msg| -> Result<(), io::Error> { let reply_address = msg .reply @@ -65,6 +69,7 @@ pub fn publish_message(message: &ChatMessage, nc: &Connection) -> io::Result<()> publish_message_to_channel(SUBJECT_MESSAGES, message, nc) } +/// Publish an message which is not json to check how the error will be handled pub fn publish_invalid_message(nc: &Connection) -> io::Result<()> { nc.publish(SUBJECT_MESSAGES, "This is not a valid json")?; Ok(()) @@ -81,10 +86,11 @@ fn publish_message_to_channel( Ok(()) } +/// Do a request with the NATS request-reply mechanism waiting 5 seconds for an answer before timing out. pub fn post_request(message: &ChatMessage, nc: &Connection) -> io::Result<()> { let json_string_utf8 = serde_json::to_string(message).expect("Cannot serialize message"); let response = nc.request_timeout( - SUBJECT_REQEUST_REPLY_MESSAGES, + CHANNEL_REQEUST_REPLY_MESSAGES, json_string_utf8, Duration::from_secs(5), ); @@ -105,7 +111,7 @@ pub fn post_request(message: &ChatMessage, nc: &Connection) -> io::Result<()> { eprintln!("There is no one else online!"); return Ok(()); } else if e.kind() == ErrorKind::TimedOut { - eprintln!("There was no fast enough response!"); + eprintln!("Timed out waiting for a reply."); return Ok(()); } else { return Err(e);