Use ids instead of names
This commit is contained in:
parent
4d4df0ea06
commit
3f108a166e
1 changed files with 11 additions and 9 deletions
|
|
@ -18,13 +18,13 @@ type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
type Username = String;
|
type Username = String;
|
||||||
type Message = String;
|
type Message = String;
|
||||||
type Address = SocketAddr;
|
type Id = i32;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
struct BroadcastMessage(Username, Message);
|
struct BroadcastMessage(Username, Message);
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
struct Users(Arc<Mutex<HashMap<Username, Address>>>);
|
struct Users(Arc<Mutex<HashMap<Id, Username>>>);
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
|
|
@ -36,6 +36,7 @@ async fn main() -> Result<()> {
|
||||||
let (tx, _) = broadcast::channel(256);
|
let (tx, _) = broadcast::channel(256);
|
||||||
|
|
||||||
let db = Users::default();
|
let db = Users::default();
|
||||||
|
let mut id = 0;
|
||||||
|
|
||||||
// Infinite loop to always listen to new connections on this IP/PORT
|
// Infinite loop to always listen to new connections on this IP/PORT
|
||||||
loop {
|
loop {
|
||||||
|
|
@ -51,6 +52,7 @@ async fn main() -> Result<()> {
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let mut name = String::default();
|
let mut name = String::default();
|
||||||
|
id += 1;
|
||||||
|
|
||||||
// We read exactly one line per loop. A line ends with \n.
|
// We read exactly one line per loop. A line ends with \n.
|
||||||
// So if the client doesn't frame their package with \n at the end,
|
// So if the client doesn't frame their package with \n at the end,
|
||||||
|
|
@ -59,8 +61,8 @@ async fn main() -> Result<()> {
|
||||||
Some(Ok(username)) => {
|
Some(Ok(username)) => {
|
||||||
if !username.is_empty() && username.is_ascii() {
|
if !username.is_empty() && username.is_ascii() {
|
||||||
name = username.clone();
|
name = username.clone();
|
||||||
db.0.lock().unwrap().insert(username.clone(), address);
|
db.0.lock().unwrap().insert(id, username.clone());
|
||||||
let message = compose_message(username.clone(), db.clone());
|
let message = compose_message(id, db.clone());
|
||||||
info!("Adding username: {username} to db");
|
info!("Adding username: {username} to db");
|
||||||
let _ = framed.send(message).await;
|
let _ = framed.send(message).await;
|
||||||
info!("Send message to client");
|
info!("Send message to client");
|
||||||
|
|
@ -103,7 +105,7 @@ async fn main() -> Result<()> {
|
||||||
info!("No next frame");
|
info!("No next frame");
|
||||||
let b =
|
let b =
|
||||||
BroadcastMessage(name.clone(), format!("* {} has left the room", name));
|
BroadcastMessage(name.clone(), format!("* {} has left the room", name));
|
||||||
db.0.lock().unwrap().remove(&name.clone());
|
db.0.lock().unwrap().remove(&id);
|
||||||
let _ = tx.send(b);
|
let _ = tx.send(b);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -124,14 +126,14 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compose_message(name: String, db: Users) -> String {
|
fn compose_message(id: i32, db: Users) -> String {
|
||||||
format!(
|
format!(
|
||||||
"* The room contains: {}",
|
"* The room contains: {}",
|
||||||
db.0.lock()
|
db.0.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.keys()
|
.iter()
|
||||||
.filter(|n| n.as_str() != name)
|
.filter(|(i, _)| **i != id)
|
||||||
.map(|n| n.to_string())
|
.map(|(_, n)| n.to_string())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ")
|
.join(", ")
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue