<commit_before>#include "Server.h"
#include "Logger.h"
#include "common.h"
#include "fmitcp.pb.h"

using namespace fmitcp;

void serverOnConnect(lw_server s, lw_client c) {
  Server * server = (Server*)lw_server_tag(s);
  server->clientConnected(c);
  lw_fdstream_nagle(c,lw_false);
}
void serverOnData(lw_server s, lw_client client, const char* data, size_t size) {
  Server * server = (Server*)lw_server_tag(s);
  server->clientData(client,data,size);
}
void serverOnDisconnect(lw_server s, lw_client c) {
  Server * server = (Server*)lw_server_tag(s);
  server->clientDisconnected(c);
}
void serverOnError(lw_server s, lw_error error) {
  Server * server = (Server*)lw_server_tag(s);
  server->error(s,error);
}

/*!
 * Callback function for FMILibrary. Logs the FMILibrary operations.
 */
void jmCallbacksLogger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message) {
  printf("[module = %s][log level = %s] %s\n", module, jm_log_level_to_string(log_level), message);fflush(NULL);
}

void Server::error(lw_server s, lw_error error) {
  string err = lw_error_tostring(error);
  onError(err);
}

Server::Server(string fmuPath, bool debugLogging, jm_log_level_enu_t logLevel, EventPump *pump) {
  m_fmuParsed = true;
  m_fmuPath = fmuPath;
  m_debugLogging = debugLogging;
  m_logLevel = logLevel;
  init(pump);
}

Server::Server(string fmuPath, bool debugLogging, jm_log_level_enu_t logLevel, EventPump *pump, const Logger &logger) {
  m_fmuParsed = true;
  m_fmuPath = fmuPath;
  m_debugLogging = debugLogging;
  m_logLevel = logLevel;
  m_logger = logger;
  init(pump);
}

Server::~Server() {
  lw_server_delete(m_server);
}

void Server::init(EventPump * pump) {
  m_pump = pump;
  m_server = lw_server_new(pump->getPump());
  m_sendDummyResponses = false;
  // Parse FMU
  // JM callbacks
  m_jmCallbacks.malloc = malloc;
  m_jmCallbacks.calloc = calloc;
  m_jmCallbacks.realloc = realloc;
  m_jmCallbacks.free = free;
  m_jmCallbacks.logger = jmCallbacksLogger;
  m_jmCallbacks.log_level = m_logLevel;
  m_jmCallbacks.context = 0;
  // working directory
  char* dir = fmi_import_mk_temp_dir(&m_jmCallbacks, NULL, "fmitcp_");
  m_workingDir = dir; // convert to std::string<commit_msg>made it possible to fake FMU with fmupath equal to dummy<commit_after>#include "Server.h"
#include "Logger.h"
#include "common.h"
#include "fmitcp.pb.h"

using namespace fmitcp;

void serverOnConnect(lw_server s, lw_client c) {
  Server * server = (Server*)lw_server_tag(s);
  server->clientConnected(c);
  lw_fdstream_nagle(c,lw_false);
}
void serverOnData(lw_server s, lw_client client, const char* data, size_t size) {
  Server * server = (Server*)lw_server_tag(s);
  server->clientData(client,data,size);
}
void serverOnDisconnect(lw_server s, lw_client c) {
  Server * server = (Server*)lw_server_tag(s);
  server->clientDisconnected(c);
}
void serverOnError(lw_server s, lw_error error) {
  Server * server = (Server*)lw_server_tag(s);
  server->error(s,error);
}

/*!
 * Callback function for FMILibrary. Logs the FMILibrary operations.
 */
void jmCallbacksLogger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message) {
  printf("[module = %s][log level = %s] %s\n", module, jm_log_level_to_string(log_level), message);fflush(NULL);
}

void Server::error(lw_server s, lw_error error) {
  string err = lw_error_tostring(error);
  onError(err);
}

Server::Server(string fmuPath, bool debugLogging, jm_log_level_enu_t logLevel, EventPump *pump) {
  m_fmuParsed = true;
  m_fmuPath = fmuPath;
  m_debugLogging = debugLogging;
  m_logLevel = logLevel;
  init(pump);
}

Server::Server(string fmuPath, bool debugLogging, jm_log_level_enu_t logLevel, EventPump *pump, const Logger &logger) {
  m_fmuParsed = true;
  m_fmuPath = fmuPath;
  m_debugLogging = debugLogging;
  m_logLevel = logLevel;
  m_logger = logger;
  init(pump);
}

Server::~Server() {
  lw_server_delete(m_server);
}

void Server::init(EventPump * pump) {
  m_pump = pump;
  m_server = lw_server_new(pump->getPump());

  if(m_fmuPath == "dummy"){
    m_sendDummyResponses = true;
    m_fmuParsed = true;
    return;
  }

  // Parse FMU
  // JM callbacks
  m_jmCallbacks.malloc = malloc;
  m_jmCallbacks.calloc = calloc;
  m_jmCallbacks.realloc = realloc;
  m_jmCallbacks.free = free;
  m_jmCallbacks.logger = jmCallbacksLogger;
  m_jmCallbacks.log_level = m_logLevel;
  m_jmCallbacks.context = 0;
  // working directory
  char* dir = fmi_import_mk_temp_dir(&m_jmCallbacks, NULL, "fmitcp_");
  m_workingDir = dir; // convert to std::string
{'fraction_non_alphanumeric': 0.12225636523266023, 'fraction_numerical': 0.0004389815627743635, 'mean_word_length': 5.320388349514563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1520782', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1790, 'n_words': 348}