From 31ffa5cd672bdef251482286e22c35c81a28840b Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 18 Feb 2022 20:24:03 +0100 Subject: [PATCH] Start implementing rooms --- Cargo.lock | 1 + cove-tui/Cargo.toml | 2 +- cove-tui/src/main.rs | 1 + cove-tui/src/room.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 cove-tui/src/room.rs diff --git a/Cargo.lock b/Cargo.lock index 5de1ea1..8b26d40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,6 +121,7 @@ name = "cove-tui" version = "0.1.0" dependencies = [ "anyhow", + "cove-core", "crossterm", "tokio", "tui", diff --git a/cove-tui/Cargo.toml b/cove-tui/Cargo.toml index aaad749..cc712b9 100644 --- a/cove-tui/Cargo.toml +++ b/cove-tui/Cargo.toml @@ -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" diff --git a/cove-tui/src/main.rs b/cove-tui/src/main.rs index d379da1..363e229 100644 --- a/cove-tui/src/main.rs +++ b/cove-tui/src/main.rs @@ -1,4 +1,5 @@ mod replies; +mod room; use std::io::{self, Stdout}; use std::time::Duration; diff --git a/cove-tui/src/room.rs b/cove-tui/src/room.rs new file mode 100644 index 0000000..aa53e69 --- /dev/null +++ b/cove-tui/src/room.rs @@ -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, + others: HashMap, + stop: Sender<()>, +} + +impl Room { + pub async fn create(name: String) -> Arc> { + 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>) { + 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(()); + } +}