Browse Source

Using the shared stuff

master
Julio Biason 3 years ago
parent
commit
035f5985ba
  1. 2
      ms/server/Cargo.toml
  2. 23
      ms/server/src/main.rs

2
ms/server/Cargo.toml

@ -11,3 +11,5 @@ env_logger = "0.9.0"
log = "0.4.14" log = "0.4.14"
serde = { version = "1.0.130", features = ["derive"] } serde = { version = "1.0.130", features = ["derive"] }
tokio = { version = "1.14.0", features = ["rt", "net", "macros", "sync", "time", "io-util"] } tokio = { version = "1.14.0", features = ["rt", "net", "macros", "sync", "time", "io-util"] }
shared = { path = "../shared" }

23
ms/server/src/main.rs

@ -1,6 +1,3 @@
use chrono::offset::Utc;
use chrono::DateTime;
use serde::Serialize;
use std::net::SocketAddr; use std::net::SocketAddr;
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener; use tokio::net::TcpListener;
@ -9,25 +6,12 @@ use tokio::sync::broadcast;
use tokio::time::sleep; use tokio::time::sleep;
use tokio::time::Duration; use tokio::time::Duration;
#[derive(Debug, Clone, Serialize)] use shared::Message;
struct Message {
received: DateTime<Utc>,
message: String,
}
impl Message {
pub fn new(message: &str) -> Self {
Self {
received: Utc::now(),
message: message.into(),
}
}
}
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {
env_logger::init(); env_logger::init();
let (tx, mut rx) = broadcast::channel::<Message>(30); let (tx, rx) = broadcast::channel::<Message>(30);
tokio::join!(consumer(rx), producer(tx.clone()), listener(tx)); tokio::join!(consumer(rx), producer(tx.clone()), listener(tx));
} }
@ -40,7 +24,8 @@ async fn producer(sink: broadcast::Sender<Message>) {
Ok(_) => {} // log::debug!("Send: {}", message), Ok(_) => {} // log::debug!("Send: {}", message),
Err(err) => log::error!("Error sending message: {:?}", err), Err(err) => log::error!("Error sending message: {:?}", err),
} }
sleep(Duration::from_millis(500)).await; // Como o sink não tem await, o runtime não consegue escutar pelas portas
sleep(Duration::from_millis(1)).await;
} }
} }

Loading…
Cancel
Save