Clean up code
This commit is contained in:
parent
2b1d23722d
commit
cec4ce203a
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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() {
|
||||
|
@ -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<Connection> {
|
||||
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<Handler> {
|
||||
// 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<F>(nc: &Connection, answer_request: F) -> io::Result<Handler>
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user