<commit_before><?php
namespace Movim\Daemon;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Movim\Daemon\Session;
use Dflydev\FigCookies\Cookies;

use Symfony\Component\Console\Input\InputInterface;

class Core implements MessageComponentInterface
{
    private $sessions = [];
    private $input;

    public $loop;
    public $baseuri;

    public $single = ['visio'];
    public $singlelocks = [];

    public function __construct($loop, $baseuri, InputInterface $input)
    {
        $this->input = $input;

        $this->setWebsocket($baseuri, $this->input->getOption('port'));

        $this->loop    = $loop;
        $this->baseuri = $baseuri;

        $sd = new \Modl\SessionxDAO;
        $sd->clear();

        $this->registerCleaner();
    }

    public function setWebsocket($baseuri, $port)
    {
        $explode = parse_url($baseuri);

        echo
            "\n".
            "--- ".colorize("Server Configuration - Apache", 'purple')." ---".
            "\n";
        echo colorize("Enable the Secure WebSocket to WebSocket tunneling", 'yellow')."\n# a2enmod proxy_wstunnel \n";
        echo colorize("Add this in your configuration file (default-ssl.conf)", 'yellow')."\nProxyPass /ws/ ws://localhost:{$port}/\n";

        echo
            "\n".
            "--- ".colorize("Server Configuration - nginx", 'purple')." ---".
            "\n";
        echo colorize("Add this in your configuration file", 'yellow')."\n";
        echo "location /ws/ {
    proxy_pass http://localhost:{$port}/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade \$http_upgrade;
    proxy_set_header Connection \"Upgrade\";
    proxy_set_header Host \$host;
    proxy_set_header X-Real-IP \$remote_addr;
    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_redirect off;
}

";

        $path = $explode['host'].$explode['path'];

        if($explode['scheme'] == 'https') {
            $ws = 'wss://'.$path.'ws/';
            $secured = 'true';
            echo colorize("Encrypted ", 'green')."\n";
        } else {
            $ws = 'ws://'.$path.'ws/';
            $secured = 'false';
            echo colorize("Unencrypted ", 'red')."\n";
        }

        file_put_contents(CACHE_PATH.'websocket', $secured);

        return $ws;
    }

    public function onOpen(ConnectionInterface $conn)
    {
        $sid = $this->getSid($conn);
        if($sid != null) {
            $path = $this->getPath($conn);

            if(in_array($path, $this->single)) {
                if(array_key_exists($sid, $this->singlelocks)
                && array_key_exists($path, $this->singlelocks[$sid])) {
                    $conn->close(1008);
                } else {
                    $this->singlelocks[$sid][$path] = true;
                }
            }

            if(!array_key_exists($sid, $this->sessions)) {
                $language = $this->getLanguage($conn);
                $offset = $this->getOffset($conn);
                $this->sessions[$sid] = new Session(
                    $this->loop,
                    $sid,
                    $this->baseuri,
                    $language,
                    $offset,
                    $this->input->getOption('verbose'),
                    $this->input->getOption('debug')
                );
            }

            $this->sessions[$sid]->attach($this->loop, $conn);
        }
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
        $sid = $this->getSid($from);
        if($sid != null && isset($this->sessions[$sid])) {
            $this->sessions[$sid]->messageIn($msg);
        }
    }

    public function onClose(ConnectionInterface $conn)
    {
        $sid = $this->getSid($conn);
        if($sid != null && isset($this->sessions[$sid])) {
            $path = $this->getPath($conn);

            if(in_array($path, $this->single)) {
                if(array_key_exists($sid, $this->singlelocks)
                && array_key_exists($path, $this->singlelocks[$sid])) {
                    unset($this->singlelocks[$sid][$path]);
                }
            }

            $this->sessions[$sid]->detach($this->loop, $conn);
            if($this->sessions[$sid]->process == null) {
                unset($this->sessions[$sid]);<commit_msg>Fix the single socket lock in Core<commit_after><?php
namespace Movim\Daemon;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Movim\Daemon\Session;
use Dflydev\FigCookies\Cookies;

use Symfony\Component\Console\Input\InputInterface;

class Core implements MessageComponentInterface
{
    private $sessions = [];
    private $input;

    public $loop;
    public $baseuri;

    public $single = ['visio'];
    public $singlelocks = [];

    public function __construct($loop, $baseuri, InputInterface $input)
    {
        $this->input = $input;

        $this->setWebsocket($baseuri, $this->input->getOption('port'));

        $this->loop    = $loop;
        $this->baseuri = $baseuri;

        $sd = new \Modl\SessionxDAO;
        $sd->clear();

        $this->registerCleaner();
    }

    public function setWebsocket($baseuri, $port)
    {
        $explode = parse_url($baseuri);

        echo
            "\n".
            "--- ".colorize("Server Configuration - Apache", 'purple')." ---".
            "\n";
        echo colorize("Enable the Secure WebSocket to WebSocket tunneling", 'yellow')."\n# a2enmod proxy_wstunnel \n";
        echo colorize("Add this in your configuration file (default-ssl.conf)", 'yellow')."\nProxyPass /ws/ ws://localhost:{$port}/\n";

        echo
            "\n".
            "--- ".colorize("Server Configuration - nginx", 'purple')." ---".
            "\n";
        echo colorize("Add this in your configuration file", 'yellow')."\n";
        echo "location /ws/ {
    proxy_pass http://localhost:{$port}/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade \$http_upgrade;
    proxy_set_header Connection \"Upgrade\";
    proxy_set_header Host \$host;
    proxy_set_header X-Real-IP \$remote_addr;
    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_redirect off;
}

";

        $path = $explode['host'].$explode['path'];

        if($explode['scheme'] == 'https') {
            $ws = 'wss://'.$path.'ws/';
            $secured = 'true';
            echo colorize("Encrypted ", 'green')."\n";
        } else {
            $ws = 'ws://'.$path.'ws/';
            $secured = 'false';
            echo colorize("Unencrypted ", 'red')."\n";
        }

        file_put_contents(CACHE_PATH.'websocket', $secured);

        return $ws;
    }

    public function onOpen(ConnectionInterface $conn)
    {
        $sid = $this->getSid($conn);
        if($sid != null) {
            $path = $this->getPath($conn);

            if(in_array($path, $this->single)) {
                if(array_key_exists($sid, $this->singlelocks)
                && array_key_exists($path, $this->singlelocks[$sid])) {
                    $this->singlelocks[$sid][$path]++;
                    $conn->close(1008);
                } else {
                    $this->singlelocks[$sid][$path] = 1;
                }
            }

            if(!array_key_exists($sid, $this->sessions)) {
                $language = $this->getLanguage($conn);
                $offset = $this->getOffset($conn);
                $this->sessions[$sid] = new Session(
                    $this->loop,
                    $sid,
                    $this->baseuri,
                    $language,
                    $offset,
                    $this->input->getOption('verbose'),
                    $this->input->getOption('debug')
                );
            }

            $this->sessions[$sid]->attach($this->loop, $conn);
        }
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
        $sid = $this->getSid($from);
        if($sid != null && isset($this->sessions[$sid])) {
            $this->sessions[$sid]->messageIn($msg);
        }
    }

    public function onClose(ConnectionInterface $conn)
    {
        $sid = $this->getSid($conn);

        if($sid != null && isset($this->sessions[$sid])) {
            $path = $this->getPath($conn);

            if(in_array($path, $this->single)) {
                if(array_key_exists($sid, $this->singlelocks)
                && array_key_exists($path, $this->singlelocks[$sid])) {
                    $this->singlelocks[$sid][$path]--;
                    if($this->singlelocks[$sid][$path] == 0) {
                        unset($this->singlelocks[$sid][$path]);
                    }
                }
            }

            $this->sessions[$sid]->detach($this->loop, $conn);
            if($this->sessions[$sid]->process == null) {
                unset($this->sessions[$sid]);
{'fraction_non_alphanumeric': 0.16486850977747808, 'fraction_numerical': 0.0017981568891885817, 'mean_word_length': 1.904373368146214, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 96, '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': '6242545', 'n_tokens_mistral': 2704, 'n_tokens_neox': 2573, 'n_words': 453}