|
|
|
@ -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);
|
|
|
|
|