From 006e1a930cce525dfc01eefedc8d346bd8bfae24 Mon Sep 17 00:00:00 2001 From: jhervoch Date: Thu, 30 Oct 2025 14:07:03 +0100 Subject: [PATCH] fix : leak when start a second server fix : msg when there is a file problem with llama_response.txt --- nclient.sh | 2 +- srcs/commands/Bot.cpp | 10 +++++++--- srcs/main.cpp | 7 +++---- srcs/server/Config.cpp | 1 - srcs/server/Server.cpp | 16 ++++++---------- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/nclient.sh b/nclient.sh index 0b9d9959..58aebede 100755 --- a/nclient.sh +++ b/nclient.sh @@ -33,5 +33,5 @@ echo "" fi # Garder la connexion ouverte pour interaction cat -} | nc localhost 9999 +} | nc -C localhost 9999 diff --git a/srcs/commands/Bot.cpp b/srcs/commands/Bot.cpp index fc60f6f6..a6f6f568 100644 --- a/srcs/commands/Bot.cpp +++ b/srcs/commands/Bot.cpp @@ -88,7 +88,7 @@ void remove_invalid_prompt_char(char& c) c = ' '; } -static void send_ollama_request(const std::string& prompt, std::string& response) +static bool send_ollama_request(const std::string& prompt, std::string& response) { std::string command = "curl -X POST -H \"Content-Type: application/json\" -v localhost:11434/api/generate -d '"; command += "{\"model\": \"gemma3:1b\",\"prompt\":\""; @@ -100,13 +100,16 @@ static void send_ollama_request(const std::string& prompt, std::string& response LOG_d_CMD(command); int code = ::system(command.c_str()); - if (code == -1) { + LOG_w_CMD(code); + if (code != 0) { LOG_E_SERVER("error sending API LLama request", command); + return false; } ::usleep(BOT_PROCESS_TIME_MS); std::ifstream file("llama_response.txt"); response.assign((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + return true; } bool Bot::_connect_to_server(Server& s, TcpSocket& so) @@ -194,7 +197,8 @@ void Bot::execute(Server& s, Client& c) LOG_DV_CMD(prompt); std::string response = "\"\""; - send_ollama_request(prompt, response); + if (!send_ollama_request(prompt, response)) + response = "\"Bot is under maintenance\""; if (!_connect_to_server(s, _socket)) return; diff --git a/srcs/main.cpp b/srcs/main.cpp index 91e5237c..07b1d718 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -10,15 +10,14 @@ int main(int ac, char** av) { int port = DEFAULT_PORT; - LOG_ERR.set_min_level(ERROR); // Seulement les erreurs dans ce log + LOG_ERR.set_min_level(ERROR); if (!Utils::check_args(ac, av, &port)) return 1; - Server newServer(port, av[2]); - setup_signal_handlers(); try { + Server newServer(port, av[2]); + setup_signal_handlers(); newServer.start(); } catch (std::exception& e) { - std::cout << e.what() << "\n"; LOG_ERR.error("Server error: " + std::string(e.what())); } return 0; diff --git a/srcs/server/Config.cpp b/srcs/server/Config.cpp index 8a7d9a7c..0bce1c2b 100644 --- a/srcs/server/Config.cpp +++ b/srcs/server/Config.cpp @@ -122,7 +122,6 @@ bool Config::_parse_config_file(const std::string& fileName) else if (currentSection == "irc.motd") _parse_motd(line); } - file.close(); return true; } diff --git a/srcs/server/Server.cpp b/srcs/server/Server.cpp index b2aee0cf..ec78d451 100644 --- a/srcs/server/Server.cpp +++ b/srcs/server/Server.cpp @@ -15,6 +15,7 @@ #include // hton*, ntoh*, inet_addr #include #include +#include #include #include #include @@ -35,13 +36,8 @@ Server::Server(const unsigned short port, const std::string& password) : _psswd(password), _name(ircConfig.get_name()), _port(port) { - try { - _serverSocket.tcp_bind(port); - _serverSocket.tcp_listen(); - } catch (std::exception& e) { - std::cout << e.what(); - exit(1); - } + _serverSocket.tcp_bind(port); + _serverSocket.tcp_listen(); LOG_SERVER.info("Server " + _name + " start at port :" + Utils::to_string(port)); std::cout << "\n"; @@ -317,9 +313,9 @@ bool Server::_handle_commands(int pfdIndex) while ((pos = client->get_read_buffer().find("\r\n")) != std::string::npos) { std::string line = client->get_read_buffer().substr(0, pos); client->get_read_buffer().erase(0, pos + 2); - if (line.empty()) { - continue; - } + if (line.empty()) { + continue; + } ICommand* cmd = _parse_command(*client, line); if (cmd) { cmd->execute(*this, *client);