use tokio::sync::{mpsc, oneshot};
use super::{
command::Command,
connection_new::Connection,
output::{Level, Ty},
ConnId, UserInfo,
};
impl Connection {
pub async fn connect(
&self,
conn_tx: mpsc::UnboundedSender<String>,
user_info: UserInfo,
) -> ConnId {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx
.send(Command::Connect {
conn_tx,
res_tx,
user_info,
})
.unwrap_or_else(|e| {
log::error!("error={:?}", e);
});
res_rx.await.unwrap()
}
pub fn disconnect(&self, conn: ConnId) {
self.cmd_tx
.send(Command::Disconnect { conn })
.unwrap_or_else(|e| {
log::error!("error={:?}", e);
});
}
pub async fn total(&self) -> usize {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx
.send(Command::Total { res_tx })
.unwrap_or_else(|e| {
log::error!("error={:?}", e);
});
res_rx.await.unwrap_or(0)
}
pub async fn users_online(&self) -> Vec<String> {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx
.send(Command::OnlineUsers { res_tx })
.unwrap_or_else(|e| {
log::error!("error={:?}", e);
});
res_rx.await.unwrap_or(Vec::new())
}
pub async fn all_connections(&self) -> Vec<(String, String, String)> {
let (res_tx, res_rx) = oneshot::channel();
self.cmd_tx
.send(Command::AllConnections { res_tx })
.unwrap_or_else(|e| {
log::error!("error={:?}", e);
});
res_rx.await.unwrap_or(Vec::new())
}
pub fn closing(&self, conn: ConnId) {
self.cmd_tx
.send(Command::Closed { conn })
.unwrap_or_else(|e| {
log::error!("error={:?}", e);
});
}
pub fn broadcast(&self, level: Level, ty: Ty, msg: String) {
self.cmd_tx
.send(Command::Broadcast { level, ty, msg })
.unwrap_or_else(|e| {
log::error!("error={:?}", e);
});
}
}