diff options
| author | Fuwn <[email protected]> | 2021-05-20 17:05:59 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-20 17:05:59 +0000 |
| commit | 9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8 (patch) | |
| tree | 8cfff6a23bb72db2660e68c63a8cf9d0539a061f /crates/whirl_server/src/interaction | |
| parent | feat(readme): add sqlfluff as a dev dep (diff) | |
| download | whirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.tar.xz whirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.zip | |
refactor(global): move crates around, stricter module isolation
Diffstat (limited to 'crates/whirl_server/src/interaction')
| -rw-r--r-- | crates/whirl_server/src/interaction/mod.rs | 5 | ||||
| -rw-r--r-- | crates/whirl_server/src/interaction/peer.rs | 49 | ||||
| -rw-r--r-- | crates/whirl_server/src/interaction/shared.rs | 28 |
3 files changed, 82 insertions, 0 deletions
diff --git a/crates/whirl_server/src/interaction/mod.rs b/crates/whirl_server/src/interaction/mod.rs new file mode 100644 index 0000000..c85e09d --- /dev/null +++ b/crates/whirl_server/src/interaction/mod.rs @@ -0,0 +1,5 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +pub mod peer; +pub mod shared; diff --git a/crates/whirl_server/src/interaction/peer.rs b/crates/whirl_server/src/interaction/peer.rs new file mode 100644 index 0000000..38c02c5 --- /dev/null +++ b/crates/whirl_server/src/interaction/peer.rs @@ -0,0 +1,49 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +use std::sync::Arc; + +use tokio::{ + net::TcpStream, + sync::{mpsc, Mutex}, +}; +use tokio_util::codec::{BytesCodec, Framed}; + +use crate::{interaction::shared::Shared, types::Rx}; + +pub struct Peer { + pub bytes: Framed<TcpStream, BytesCodec>, + pub rx: Rx, +} +impl Peer { + pub async fn new( + state: Arc<Mutex<Shared>>, + bytes: Framed<TcpStream, BytesCodec>, + username: String, + ) -> std::io::Result<Peer> { + let (tx, rx) = mpsc::unbounded_channel(); + state.lock().await.peers.insert(username, tx); + + Ok(Peer { + bytes, + rx, + }) + } + + pub async fn _change_username( + self, + state: Arc<Mutex<Shared>>, + username: &str, + new_username: &str, + ) { + // Remove peer from peers + { + state.lock().await.peers.remove(username); + } + + // Add the peer back with the new username + Self::new(state, self.bytes, new_username.to_string()) + .await + .unwrap(); + } +} diff --git a/crates/whirl_server/src/interaction/shared.rs b/crates/whirl_server/src/interaction/shared.rs new file mode 100644 index 0000000..c2ee671 --- /dev/null +++ b/crates/whirl_server/src/interaction/shared.rs @@ -0,0 +1,28 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +use std::collections::HashMap; + +use bytes::BytesMut; + +use crate::types::Tx; + +pub struct Shared { + pub peers: HashMap<String, Tx>, +} +impl Shared { + pub fn new() -> Self { + Shared { + peers: HashMap::new(), + } + } + + pub async fn broadcast(&mut self, message: &[u8]) { + for peer in self.peers.iter_mut() { + peer.1.send(BytesMut::from(message)).unwrap(); + } + } +} +impl Default for Shared { + fn default() -> Self { Self::new() } +} |