Start implementing rooms

This commit is contained in:
Joscha 2022-02-18 20:24:03 +01:00
parent 939d9b7586
commit 31ffa5cd67
4 changed files with 66 additions and 1 deletions

View file

@ -5,7 +5,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.53"
# cove-core = { path = "../cove-core" }
cove-core = { path = "../cove-core" }
crossterm = "0.22.1"
# futures = "0.3.21"
# serde_json = "1.0.78"

View file

@ -1,4 +1,5 @@
mod replies;
mod room;
use std::io::{self, Stdout};
use std::time::Duration;

63
cove-tui/src/room.rs Normal file
View file

@ -0,0 +1,63 @@
use std::collections::HashMap;
use std::sync::Arc;
use cove_core::conn::ConnTx;
use cove_core::{Session, SessionId};
use tokio::sync::oneshot::{self, Sender};
use tokio::sync::Mutex;
pub enum ConnectedState {
ChoosingNick,
Identifying,
Online,
}
pub enum RoomState {
Connecting,
Reconnecting,
Connected { state: ConnectedState, tx: ConnTx },
DoesNotExist,
}
pub struct Room {
name: String,
state: RoomState,
nick: Option<String>,
others: HashMap<SessionId, Session>,
stop: Sender<()>,
}
impl Room {
pub async fn create(name: String) -> Arc<Mutex<Self>> {
let (tx, rx) = oneshot::channel();
let room = Self {
name,
state: RoomState::Connecting,
nick: None,
others: HashMap::new(),
stop: tx,
};
let room = Arc::new(Mutex::new(room));
let room_clone = room.clone();
tokio::spawn(async {
tokio::select! {
_ = rx => {},
_ = Self::connect(room_clone) => {}
}
});
room
}
async fn connect(room: Arc<Mutex<Self>>) {
todo!()
}
pub fn stop(self) {
// If the send goes wrong because the other end has hung up, it's
// already stopped and there's nothing to do.
let _ = self.stop.send(());
}
}