aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_server/src/interaction/peer.rs
blob: e12c07541ca8bb24ac257bbc4655356e5d3d4fc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (C) 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;

pub struct Peer {
  pub bytes: Framed<TcpStream, BytesCodec>,
  pub rx:    mpsc::UnboundedReceiver<bytes::BytesMut>,
}
impl Peer {
  pub async fn new(
    state: Arc<Mutex<Shared>>,
    bytes: Framed<TcpStream, BytesCodec>,
    username: String,
  ) -> std::io::Result<Self> {
    let (tx, rx) = mpsc::unbounded_channel();
    state.lock().await.peers.insert(username, tx);

    Ok(Self {
      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();
  }
}