Start implementing rooms
This commit is contained in:
parent
939d9b7586
commit
31ffa5cd67
4 changed files with 66 additions and 1 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -121,6 +121,7 @@ name = "cove-tui"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"cove-core",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tui",
|
"tui",
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.53"
|
anyhow = "1.0.53"
|
||||||
# cove-core = { path = "../cove-core" }
|
cove-core = { path = "../cove-core" }
|
||||||
crossterm = "0.22.1"
|
crossterm = "0.22.1"
|
||||||
# futures = "0.3.21"
|
# futures = "0.3.21"
|
||||||
# serde_json = "1.0.78"
|
# serde_json = "1.0.78"
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
mod replies;
|
mod replies;
|
||||||
|
mod room;
|
||||||
|
|
||||||
use std::io::{self, Stdout};
|
use std::io::{self, Stdout};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
|
||||||
63
cove-tui/src/room.rs
Normal file
63
cove-tui/src/room.rs
Normal 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(());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue