<commit_before>use std::collections::HashMap;
use std::sync::mpsc;
use std::sync::{Arc, RwLock};
use std::sync::atomic::{AtomicUsize, Ordering};

use mio::*;
use mio::tcp::*;

use client::WebSocketClient;
use interface::WebSocketEvent;

pub const SERVER_TOKEN: Token = Token(0);

#[derive(Clone)]
pub struct WebSocketServerState {
    clients: Arc<RwLock<HashMap<Token, WebSocketClient>>>,
    token_counter: Arc<AtomicUsize>
}

impl WebSocketServerState {
    pub fn new() -> WebSocketServerState {
        WebSocketServerState {
            token_counter: Arc::new(AtomicUsize::new(1)),
            clients: Arc::new(RwLock::new(HashMap::new()))
        }
    }

    fn new_client(&mut self, client_socket: TcpStream, tx: mpsc::Sender<WebSocketEvent>,
                  event_loop_tx: Sender<WebSocketEvent>) -> Token {
        let new_token = Token(self.token_counter.fetch_add(1, Ordering::SeqCst));

        if let Ok(ref mut clients) = self.clients.write() {
            clients.insert(new_token, WebSocketClient::new(client_socket, new_token, tx.clone(), event_loop_tx));
        }

        new_token
    }

    pub fn get_peers(&self) -> Option<Vec<Token>> {
        if let Ok(clients) = self.clients.read() {
            let tokens: Vec<Token> = clients.iter().map(|(tkn, _)| tkn.clone()).collect();
            Some(tokens)
        } else {
            None<commit_msg>Simplify code with Iterator.cloned<commit_after>use std::collections::HashMap;
use std::sync::mpsc;
use std::sync::{Arc, RwLock};
use std::sync::atomic::{AtomicUsize, Ordering};

use mio::*;
use mio::tcp::*;

use client::WebSocketClient;
use interface::WebSocketEvent;

pub const SERVER_TOKEN: Token = Token(0);

#[derive(Clone)]
pub struct WebSocketServerState {
    clients: Arc<RwLock<HashMap<Token, WebSocketClient>>>,
    token_counter: Arc<AtomicUsize>
}

impl WebSocketServerState {
    pub fn new() -> WebSocketServerState {
        WebSocketServerState {
            token_counter: Arc::new(AtomicUsize::new(1)),
            clients: Arc::new(RwLock::new(HashMap::new()))
        }
    }

    fn new_client(&mut self, client_socket: TcpStream, tx: mpsc::Sender<WebSocketEvent>,
                  event_loop_tx: Sender<WebSocketEvent>) -> Token {
        let new_token = Token(self.token_counter.fetch_add(1, Ordering::SeqCst));

        if let Ok(ref mut clients) = self.clients.write() {
            clients.insert(new_token, WebSocketClient::new(client_socket, new_token, tx.clone(), event_loop_tx));
        }

        new_token
    }

    pub fn get_peers(&self) -> Option<Vec<Token>> {
        if let Ok(clients) = self.clients.read() {
            Some(clients.keys().cloned().collect::<Vec<_>>())
        } else {
            None
{'fraction_non_alphanumeric': 0.1453211009174312, 'fraction_numerical': 0.0022018348623853213, 'mean_word_length': 3.4469820554649266, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4758449', 'n_tokens_mistral': 884, 'n_tokens_neox': 895, 'n_words': 167}