<commit_before>#include "rpcruntime_protcol.h"
#include "rpcruntime_decoded_function_call.h"
#include "rpcruntime_encoded_function_call.h"
#include <QDirIterator>
#include <assert.h>

RPCRuntimeProtocol::RPCRuntimeProtocol(RPCIODevice &device, std::chrono::steady_clock::duration timeout)
    : decoder{description}
    , encoder{description}
    , channel_codec{decoder}
    , device(&device)
    , device_timeout(timeout) {
    connection = QObject::connect(&device, &RPCIODevice::received, [&cc = channel_codec, this ](const QByteArray &data) {
        //qDebug() << "RPC-Protocol received" << data.size() << "bytes from device";
        try {
            cc.add_data(reinterpret_cast<const unsigned char *>(data.data()), data.size());
        } catch (const std::runtime_error &e) {
            // qDebug() << QString(e.what());
            emit console_message(RPCConsoleLevel::debug, QString(e.what()));
        }
    });
    assert(connection);
}

RPCRuntimeProtocol::~RPCRuntimeProtocol() {
    QObject::disconnect(connection);
}

RPCFunctionCallResult RPCRuntimeProtocol::call_and_wait(const RPCRuntimeEncodedFunctionCall &call) {
    return call_and_wait(call, device_timeout);
}

RPCFunctionCallResult RPCRuntimeProtocol::call_and_wait(const RPCRuntimeEncodedFunctionCall &call, std::chrono::steady_clock::duration timeout) {
    RPCFunctionCallResult result;
    result.decoded_function_call_reply = nullptr;
    int try_count = 0;
    std::chrono::steady_clock::duration duration;
    for (try_count = 0; try_count <= retries_per_transmission; try_count++) {
        if (try_count != 0) {
            emit console_message(
                RPCConsoleLevel::debug,
                QString(R"(Request for function "%1" timed out, retry %2)").arg(call.get_description()->get_function_name().c_str()).arg(try_count));
        }

        device->send(channel_codec.encode(call), call.encode());
        // Utility::thread_call(device,nullptr,
        //                      [ device = this->device, data = channel_codec.encode(call), display_data = call.encode() ] { device->send(data, display_data);
        //                      });
        auto start = std::chrono::high_resolution_clock::now();
        auto check_received = [this, &start, &timeout, &call, &result, try_count]() -> std::unique_ptr<RPCRuntimeDecodedFunctionCall> {
            device->waitReceived(timeout - (std::chrono::high_resolution_clock::now() - start), 1);
            if (channel_codec.transfer_complete()) { //found a reply
                auto transfer = channel_codec.pop_completed_transfer();
                auto &raw_data = transfer.get_raw_data();
                emit device->decoded_received(QByteArray(reinterpret_cast<const char *>(raw_data.data()), raw_data.size()));
                auto decoded_call = transfer.decode();
                if (decoded_call.get_id() == call.get_description()->get_reply_id()) { //found correct reply

                    return std::make_unique<RPCRuntimeDecodedFunctionCall>(std::move(decoded_call));

                } else { //found reply to something else, just gonna quietly ignore it
                    emit console_message(
                        RPCConsoleLevel::debug,
                        QString(R"(RPCProtocol::call_and_wait wrong answer. Expected answer to function "%1" but got answer to function "%2" instead.)")<commit_msg>Remembering xml file<commit_after>#include "rpcruntime_protcol.h"
#include "rpcruntime_decoded_function_call.h"
#include "rpcruntime_encoded_function_call.h"
#include <QDirIterator>
#include <assert.h>

RPCRuntimeProtocol::RPCRuntimeProtocol(RPCIODevice &device, std::chrono::steady_clock::duration timeout)
    : decoder{description}
    , encoder{description}
    , channel_codec{decoder}
    , device(&device)
    , device_timeout(timeout) {
    connection = QObject::connect(&device, &RPCIODevice::received, [&cc = channel_codec, this ](const QByteArray &data) {
        //qDebug() << "RPC-Protocol received" << data.size() << "bytes from device";
        try {
            cc.add_data(reinterpret_cast<const unsigned char *>(data.data()), data.size());
        } catch (const std::runtime_error &e) {
            // qDebug() << QString(e.what());
            emit console_message(RPCConsoleLevel::debug, QString(e.what()));
        }
    });
    assert(connection);
}

RPCRuntimeProtocol::~RPCRuntimeProtocol() {
    QObject::disconnect(connection);
}

RPCFunctionCallResult RPCRuntimeProtocol::call_and_wait(const RPCRuntimeEncodedFunctionCall &call) {
    return call_and_wait(call, device_timeout);
}

RPCFunctionCallResult RPCRuntimeProtocol::call_and_wait(const RPCRuntimeEncodedFunctionCall &call, std::chrono::steady_clock::duration timeout) {
    RPCFunctionCallResult result;
    result.decoded_function_call_reply = nullptr;
    int try_count = 0;
    std::chrono::steady_clock::duration duration;
    for (try_count = 0; try_count <= retries_per_transmission; try_count++) {
        if (try_count != 0) {
            emit console_message(
                RPCConsoleLevel::debug,
                QString(R"(Request for function "%1" timed out, retry %2)").arg(call.get_description()->get_function_name().c_str()).arg(try_count));
        }

        device->send(channel_codec.encode(call), call.encode());
		// Utility::thread_call(device,
        //                      [ device = this->device, data = channel_codec.encode(call), display_data = call.encode() ] { device->send(data, display_data);
        //                      });
        auto start = std::chrono::high_resolution_clock::now();
        auto check_received = [this, &start, &timeout, &call, &result, try_count]() -> std::unique_ptr<RPCRuntimeDecodedFunctionCall> {
            device->waitReceived(timeout - (std::chrono::high_resolution_clock::now() - start), 1);
            if (channel_codec.transfer_complete()) { //found a reply
                auto transfer = channel_codec.pop_completed_transfer();
                auto &raw_data = transfer.get_raw_data();
                emit device->decoded_received(QByteArray(reinterpret_cast<const char *>(raw_data.data()), raw_data.size()));
                auto decoded_call = transfer.decode();
                if (decoded_call.get_id() == call.get_description()->get_reply_id()) { //found correct reply

                    return std::make_unique<RPCRuntimeDecodedFunctionCall>(std::move(decoded_call));

                } else { //found reply to something else, just gonna quietly ignore it
                    emit console_message(
                        RPCConsoleLevel::debug,
                        QString(R"(RPCProtocol::call_and_wait wrong answer. Expected answer to function "%1" but got answer to function "%2" instead.)")
{'fraction_non_alphanumeric': 0.12879798428931377, 'fraction_numerical': 0.0023714243367422556, 'mean_word_length': 3.3173384516954574, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '5880865', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1882, 'n_words': 359}