<commit_before>use serde::{Serialize, Deserialize};
use std::thread;
use std::sync::{Arc, Mutex};
use uuid::Uuid;
use ws::{listen, Handler, Sender, Result as WSResult, Message, CloseCode, Handshake};
use crate::proxy::NetSock;
use std::collections::BTreeMap;

#[derive(Clone, Debug)]
pub struct Socket {
    out: Sender,
    clients: Arc<Mutex<BTreeMap<String, NetSock>>>,
    uuid: String
}

impl Socket {
    pub fn send(&mut self, msg: &str) -> WSResult<()> {
        self.out.send(Message::from(msg))
    }
}

impl Handler for Socket {

    fn on_open(&mut self, shake: Handshake) -> WSResult<()> {
        let mut guard = self.clients.lock().unwrap();
        (*guard).insert(self.uuid.clone(), NetSock::Websocket(Arc::new(Mutex::new(self.clone()))));
        Ok(())
    }

    fn on_message(&mut self, msg: Message) -> WSResult<()> {
        // Handle message data
        Ok(())
    }

    fn on_close(&mut self, code: CloseCode, reason: &str) {
        match code {
            CloseCode::Normal => println!("The client is done with the connection."),
            CloseCode::Away   => println!("The client is leaving the site."),
            _ => println!("The client encountered an error: {}", reason),
        }
    }
}

/// Start a websocket listen server bound to host and port. Returns a handle for the thread this is running in.
pub fn create_server(host: Option<String>, port: i32, clients: &Arc<Mutex<BTreeMap<String, NetSock>>>) -> thread::JoinHandle<()> {
    let mut host_bind = String::from("127.0.0.1");
    let cl = clients.clone();
    if let Some(h) = host {
        host_bind = h;
    }
    thread::spawn(move || {
        listen(&format!("{}:{}", host_bind, port), |out| Socket { out: out, clients: cl.clone(), uuid: Uuid::new_v4().to_hyphenated().to_string() } ).unwrap()
    })
}<commit_msg>Event loop handling<commit_after>use serde::{Serialize, Deserialize};
use std::thread;
use std::sync::{Arc, Mutex};
use uuid::Uuid;
use ws::{listen, Handler, Sender, Result as WSResult, Message, CloseCode, Handshake};
use crate::proxy::NetSock;
use std::collections::BTreeMap;
use ataxia_events::EventLoop;

#[derive(Clone, Debug)]
pub struct Socket {
    out: Sender,
    clients: Arc<Mutex<BTreeMap<String, NetSock>>>,
    events: Arc<EventLoop>,
    uuid: String
}

impl Socket {
    pub fn send(&mut self, msg: &str) -> WSResult<()> {
        self.out.send(Message::from(msg))
    }
}

impl Handler for Socket {

    fn on_open(&mut self, shake: Handshake) -> WSResult<()> {
        let mut guard = self.clients.lock().unwrap();
        (*guard).insert(self.uuid.clone(), NetSock::Websocket(Arc::new(Mutex::new(self.clone()))));
        Ok(())
    }

    fn on_message(&mut self, msg: Message) -> WSResult<()> {
        // Handle message data
        Ok(())
    }

    fn on_close(&mut self, code: CloseCode, reason: &str) {
        match code {
            CloseCode::Normal => println!("The client is done with the connection."),
            CloseCode::Away   => println!("The client is leaving the site."),
            _ => println!("The client encountered an error: {}", reason),
        }
    }
}

/// Start a websocket listen server bound to host and port. Returns a handle for the thread this is running in.
pub fn create_server(host: Option<String>, port: i32, clients: &Arc<Mutex<BTreeMap<String, NetSock>>>, eventloop: &Arc<EventLoop>) -> thread::JoinHandle<()> {
    let mut host_bind = String::from("127.0.0.1");
    let cl = clients.clone();
    let el = eventloop.clone();
    if let Some(h) = host {
        host_bind = h;
    }
    thread::spawn(move || {
        listen(&format!("{}:{}", host_bind, port), |out| Socket { out, clients: cl.clone(), events: el.clone(), uuid: Uuid::new_v4().to_hyphenated().to_string() } ).unwrap()
    })
}
{'fraction_non_alphanumeric': 0.17077045274027006, 'fraction_numerical': 0.004765687053216839, 'mean_word_length': 3.3525345622119818, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4101363', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1244, 'n_words': 330}