<commit_before>#include <server/server_interface.hpp>
#include <iostream>

ServerInterface::ServerInterface(zmq::context_t& context, 
                                    std::string const& commandServer,
                                    std::string const& stateServer,
                                    std::string const& matchToken)
    : m_command(context, ZMQ_REQ), m_state(context, ZMQ_SUB),
        m_matchToken(matchToken)
{
    std::cout << "Connecting to command server " << commandServer << std::endl;
    m_command.connect(commandServer.c_str());
    std::cout << "Connecting to state server " << stateServer << std::endl;
    m_state.connect(stateServer.c_str());
}

ServerInterface::~ServerInterface() {
    std::cout << "Closing sockets." << std::endl;
    m_command.close();
    m_state.close();
}

bool ServerInterface::Initialize() {
    // Sends initial message to command server
    std::cout << "Initiating handshake... ";
    std::string const* requestS = m_factory.CreateInitMessage(m_matchToken);
    zmq::message_t request (requestS->size());
    memcpy(static_cast<void*>(request.data()), requestS->data(), requestS->size());
    delete requestS;
    m_command.send(request);

    // Subscribes to state server
    m_state.setsockopt(ZMQ_SUBSCRIBE, m_matchToken.c_str(), m_matchToken.size());

    zmq::message_t reply;
    m_command.recv(&reply);
    std::string replyS (static_cast<char const*>(reply.data()), reply.size());
    bool success = m_factory.ParseInitReply(replyS);
    if (success)
        std::cout << "Success!" << std::endl;
    else
        std::cout << "Failure." << std::endl;
    return success;
}

State const* ServerInterface::GetState() {
    zmq::message_t address;
    zmq::message_t state;
    m_state.recv(&address);
    m_state.recv(&state);
    std::string stateS (static_cast<char const*>(state.data()), state.size());
    return m_factory.ParseStateMessage(stateS);
}

bool ServerInterface::SendMove(enum Tetromino::Move move, int pieceId) {
    std::string const* moveCommandS = m_factory.CreateMoveMessage(move, pieceId);
    zmq::message_t moveCommand (moveCommandS->size());
    memcpy(static_cast<void*>(moveCommand.data()), moveCommandS->data(), 
                                moveCommandS->size());
    delete moveCommandS;
    m_command.send(moveCommand);

    zmq::message_t reply;
    int error = m_command.recv(&reply, ZMQ_NOBLOCK);
    if (error != -1) {
        std::string replyS (static_cast<char const*>(reply.data()), reply.size());
        return m_factory.ParseMoveReply(replyS);
    }
    else {
        return NULL;
    }
}
<commit_msg>Fixed infinite loop. #3<commit_after>#include <server/server_interface.hpp>
#include <iostream>

ServerInterface::ServerInterface(zmq::context_t& context, 
                                    std::string const& commandServer,
                                    std::string const& stateServer,
                                    std::string const& matchToken)
    : m_command(context, ZMQ_REQ), m_state(context, ZMQ_SUB),
        m_matchToken(matchToken)
{
    std::cout << "Connecting to command server " << commandServer << std::endl;
    m_command.connect(commandServer.c_str());
    std::cout << "Connecting to state server " << stateServer << std::endl;
    m_state.connect(stateServer.c_str());
}

ServerInterface::~ServerInterface() {
    std::cout << "Closing sockets." << std::endl;
    m_command.close();
    m_state.close();
}

bool ServerInterface::Initialize() {
    // Sends initial message to command server
    std::cout << "Initiating handshake... ";
    std::string const* requestS = m_factory.CreateInitMessage(m_matchToken);
    zmq::message_t request (requestS->size());
    memcpy(static_cast<void*>(request.data()), requestS->data(), requestS->size());
    delete requestS;
    m_command.send(request);

    // Subscribes to state server
    m_state.setsockopt(ZMQ_SUBSCRIBE, m_matchToken.c_str(), m_matchToken.size());

    zmq::message_t reply;
    m_command.recv(&reply);
    std::string replyS (static_cast<char const*>(reply.data()), reply.size());
    bool success = m_factory.ParseInitReply(replyS);
    if (success)
        std::cout << "Success!" << std::endl;
    else
        std::cout << "Failure." << std::endl;
    return success;
}

State const* ServerInterface::GetState() {
    zmq::message_t address;
    zmq::message_t state;
    int error = m_state.recv(&address, ZMQ_NOBLOCK);
    if (error != -1) {
        m_state.recv(&state);
        std::string stateS (static_cast<char const*>(state.data()), state.size());
        return m_factory.ParseStateMessage(stateS);
    }
    else {
        return NULL;
    }
}

bool ServerInterface::SendMove(enum Tetromino::Move move, int pieceId) {
    std::string const* moveCommandS = m_factory.CreateMoveMessage(move, pieceId);
    zmq::message_t moveCommand (moveCommandS->size());
    memcpy(static_cast<void*>(moveCommand.data()), moveCommandS->data(), 
                                moveCommandS->size());
    delete moveCommandS;
    m_command.send(moveCommand);

    zmq::message_t reply;
    m_command.recv(&reply);
    std::string replyS (static_cast<char const*>(reply.data()), reply.size());
    return m_factory.ParseMoveReply(replyS);
}

{'fraction_non_alphanumeric': 0.1449771689497717, 'fraction_numerical': 0.0005707762557077625, 'mean_word_length': 3.369908561928512, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6626998', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1575, 'n_words': 305}