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
use crate::Miner;
use async_std::sync::{Arc, Mutex};
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct MinerList {
    pub miners: Arc<Mutex<HashMap<u32, Miner>>>,
}

impl MinerList {
    pub fn new() -> Self {
        MinerList {
            miners: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    pub async fn add_miner(&self, session_id: u32, miner: Miner) {
        self.miners.lock().await.insert(session_id as u32, miner);
    }

    //@todo we could return the miner from this function, but I'm not sure we see a need for that
    //right now, but think on it.
    pub async fn remove_miner(&self, session_id: u32) {
        self.miners.lock().await.remove(&(session_id as u32));
    }

    pub async fn get_miner_by_id(&self, session_id: u32) -> Option<Miner> {
        self.miners.lock().await.get(&(session_id as u32)).cloned()
    }

    pub async fn update_miner_by_session_id(&self, session_id: u32, miner: Miner) {
        self.miners.lock().await.insert(session_id as u32, miner);
    }
}

impl Default for MinerList {
    fn default() -> Self {
        Self::new()
    }
}