<commit_before><?php

namespace Movim\Daemon;

use Psr\Http\Message\ServerRequestInterface;
use React\Socket\Server as Reactor;

use React\Http\Middleware\LimitConcurrentRequestsMiddleware;
use React\Http\Middleware\RequestBodyBufferMiddleware;
use React\Http\Middleware\RequestBodyParserMiddleware;
use React\Http\Server;
use React\Http\Response;

class Api
{
    private $_core;

    public function __construct(Reactor $socket, Core $core)
    {
        $this->_core = &$core;
        $api = &$this;

        $handler = function (ServerRequestInterface $request) use ($api) {
            $response = '';

            switch($request->getUri()->getHost()) {
                case 'ajax':
                    $api->handleAjax($request->getParsedBody());
                    break;
                case 'exists':
                    $response = $api->sessionExists($request->getParsedBody());
                    break;
                case 'linked':
                    $response = $api->sessionsLinked();
                    break;
                case 'started':
                    $response = $api->sessionsStarted();
                    break;
                case 'unregister':
                    $response = $api->sessionUnregister($request->getParsedBody());
                    break;
                case 'disconnect';
                    $response = $api->sessionDisconnect($request->getParsedBody());
                    break;
                case 'session':
                    $response = $api->getSession();
                    break;
            }

            return new Response(
                200,
                ['Content-Type' => 'text/plain'],
                (string)$response
            );
        };

        (new Server($handler))->listen($socket);
    }

    public function handleAjax($post)
    {
        $sid = $post['sid'];
        if (array_key_exists($sid, $this->_core->sessions)) {
            $this->_core->sessions[$sid]->messageIn(rawurldecode($post['json']));
        }
    }

    public function sessionExists($post)
    {
        $sid = $post['sid'];

        $sessions = $this->_core->getSessions();

        return (array_key_exists($sid, $sessions)
        && $sessions[$sid] == true);
    }

    public function sessionsLinked()
    {<commit_msg>Handle errors thrown by the internal API<commit_after><?php

namespace Movim\Daemon;

use Movim\Bootstrap;

use Psr\Http\Message\ServerRequestInterface;
use React\Socket\Server as Reactor;

use React\Http\Middleware\LimitConcurrentRequestsMiddleware;
use React\Http\Middleware\RequestBodyBufferMiddleware;
use React\Http\Middleware\RequestBodyParserMiddleware;
use React\Http\Server;
use React\Http\Response;

class Api
{
    private $_core;

    public function __construct(Reactor $socket, Core $core)
    {
        $this->_core = &$core;
        $api = &$this;

        $handler = function (ServerRequestInterface $request) use ($api) {
            $response = '';

            switch($request->getUri()->getHost()) {
                case 'ajax':
                    $api->handleAjax($request->getParsedBody());
                    break;
                case 'exists':
                    $response = $api->sessionExists($request->getParsedBody());
                    break;
                case 'linked':
                    $response = $api->sessionsLinked();
                    break;
                case 'started':
                    $response = $api->sessionsStarted();
                    break;
                case 'unregister':
                    $response = $api->sessionUnregister($request->getParsedBody());
                    break;
                case 'disconnect';
                    $response = $api->sessionDisconnect($request->getParsedBody());
                    break;
                case 'session':
                    $response = $api->getSession();
                    break;
            }

            return new Response(
                200,
                ['Content-Type' => 'text/plain'],
                (string)$response
            );
        };

        $server = new Server($handler);
        $server->on('error', function (\Throwable $e) {
            (new Bootstrap)->exceptionHandler($e);
        });
        $server->listen($socket);
    }

    public function handleAjax($post)
    {
        $sid = $post['sid'];
        if (array_key_exists($sid, $this->_core->sessions)) {
            $this->_core->sessions[$sid]->messageIn(rawurldecode($post['json']));
        }
    }

    public function sessionExists($post)
    {
        $sid = $post['sid'];

        $sessions = $this->_core->getSessions();

        return (array_key_exists($sid, $sessions)
        && $sessions[$sid] == true);
    }

    public function sessionsLinked()
    {
{'fraction_non_alphanumeric': 0.13181533319406727, 'fraction_numerical': 0.0012533946104031752, 'mean_word_length': 1.7127478753541077, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '2066422', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1218, 'n_words': 232}