<commit_before>#include "server.hpp"
#include "io_service_provider.hpp"
#include "connection.hpp"

using namespace boost::asio::ip;

namespace Carbonide { namespace Server { namespace RestApi
{
//#######################################################################################################
    RestServer::RestServer(std::function <void(std::shared_ptr <RestConnection>)> handler,
                           std::function <void(std::shared_ptr <RestConnection>, InvalidRequest const&)> errorHandler, uint16_t port)
        : endpoint_(tcp::v4(), port)
        , acceptor_(nullptr)
        , handler_(handler)
        , errorHandler_(errorHandler)
        , acceptingThread_()
        , listening_(false)
        , idIncrement_(0)
        , memberLock_()
        , connections_()
    {

    }
//-------------------------------------------------------------------------------------------------------
    RestServer::~RestServer()
    {
        // running? -> stop!
        if (acceptingThread_.joinable())
            stop();
    }
//-------------------------------------------------------------------------------------------------------
    void RestServer::start()
    {
        acceptor_.reset(new tcp::acceptor(IOServiceProvider::getInstance().getIOService(), endpoint_));

        // is currently running, stop first
        if (acceptingThread_.joinable())
            stop();

        listening_.store(true);
        acceptingThread_ = std::thread([this]() {
            for (;listening_.load();)
            {
                idIncrement_.store(idIncrement_.load() + 1);
                std::shared_ptr <RestConnection> connection (new RestConnection(this, idIncrement_.load()));
                boost::system::error_code ec;
                acceptor_->accept(*connection->getStream().rdbuf(), ec);
                if (!ec) {
                    // LOCK_SCOPE
                    {
                        std::lock_guard <std::mutex> guard (memberLock_);
                        connections_.insert({connection->getId(), connection});
                    }
                    std::thread([connection, this](){
                        try {
                            connection->readHead();<commit_msg>The endpoint will now be aware of the remote address and port. This will someday be used for the identification.<commit_after>#include "server.hpp"
#include "io_service_provider.hpp"
#include "connection.hpp"

using namespace boost::asio::ip;

namespace Carbonide { namespace Server { namespace RestApi
{
//#######################################################################################################
    RestServer::RestServer(std::function <void(std::shared_ptr <RestConnection>)> handler,
                           std::function <void(std::shared_ptr <RestConnection>, InvalidRequest const&)> errorHandler, uint16_t port)
        : endpoint_(tcp::v4(), port)
        , acceptor_(nullptr)
        , handler_(handler)
        , errorHandler_(errorHandler)
        , acceptingThread_()
        , listening_(false)
        , idIncrement_(0)
        , memberLock_()
        , connections_()
    {

    }
//-------------------------------------------------------------------------------------------------------
    RestServer::~RestServer()
    {
        // running? -> stop!
        if (acceptingThread_.joinable())
            stop();
    }
//-------------------------------------------------------------------------------------------------------
    void RestServer::start()
    {
        acceptor_.reset(new tcp::acceptor(IOServiceProvider::getInstance().getIOService(), endpoint_));

        // is currently running, stop first
        if (acceptingThread_.joinable())
            stop();

        listening_.store(true);
        acceptingThread_ = std::thread([this]() {
            for (;listening_.load();)
            {
                idIncrement_.store(idIncrement_.load() + 1);
                std::shared_ptr <RestConnection> connection (new RestConnection(this, idIncrement_.load()));
                boost::system::error_code ec;
                boost::asio::ip::tcp::acceptor::endpoint_type remoteEndpoint;
                acceptor_->accept(*connection->getStream().rdbuf(), remoteEndpoint, ec);
                if (!ec) {
                    connection->setEndpoint(remoteEndpoint);
                    // LOCK_SCOPE
                    {
                        std::lock_guard <std::mutex> guard (memberLock_);
                        connections_.insert({connection->getId(), connection});
                    }
                    std::thread([connection, this](){
                        try {
                            connection->readHead();
{'fraction_non_alphanumeric': 0.24126916524701875, 'fraction_numerical': 0.002129471890971039, 'mean_word_length': 1.6521739130434783, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 26, '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': '6163835', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1034, 'n_words': 194}