Skip to content

Commit 006e1a9

Browse files
author
jhervoch
committed
fix : leak when start a second server
fix : msg when there is a file problem with llama_response.txt
1 parent 4ee0fde commit 006e1a9

5 files changed

Lines changed: 17 additions & 19 deletions

File tree

nclient.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ echo ""
3333
fi
3434
# Garder la connexion ouverte pour interaction
3535
cat
36-
} | nc localhost 9999
36+
} | nc -C localhost 9999
3737

srcs/commands/Bot.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void remove_invalid_prompt_char(char& c)
8888
c = ' ';
8989
}
9090

91-
static void send_ollama_request(const std::string& prompt, std::string& response)
91+
static bool send_ollama_request(const std::string& prompt, std::string& response)
9292
{
9393
std::string command = "curl -X POST -H \"Content-Type: application/json\" -v localhost:11434/api/generate -d '";
9494
command += "{\"model\": \"gemma3:1b\",\"prompt\":\"";
@@ -100,13 +100,16 @@ static void send_ollama_request(const std::string& prompt, std::string& response
100100
LOG_d_CMD(command);
101101

102102
int code = ::system(command.c_str());
103-
if (code == -1) {
103+
LOG_w_CMD(code);
104+
if (code != 0) {
104105
LOG_E_SERVER("error sending API LLama request", command);
106+
return false;
105107
}
106108

107109
::usleep(BOT_PROCESS_TIME_MS);
108110
std::ifstream file("llama_response.txt");
109111
response.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
112+
return true;
110113
}
111114

112115
bool Bot::_connect_to_server(Server& s, TcpSocket& so)
@@ -194,7 +197,8 @@ void Bot::execute(Server& s, Client& c)
194197
LOG_DV_CMD(prompt);
195198

196199
std::string response = "\"\"";
197-
send_ollama_request(prompt, response);
200+
if (!send_ollama_request(prompt, response))
201+
response = "\"Bot is under maintenance\"";
198202

199203
if (!_connect_to_server(s, _socket))
200204
return;

srcs/main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ int main(int ac, char** av)
1010
{
1111
int port = DEFAULT_PORT;
1212

13-
LOG_ERR.set_min_level(ERROR); // Seulement les erreurs dans ce log
13+
LOG_ERR.set_min_level(ERROR);
1414
if (!Utils::check_args(ac, av, &port))
1515
return 1;
16-
Server newServer(port, av[2]);
17-
setup_signal_handlers();
1816
try {
17+
Server newServer(port, av[2]);
18+
setup_signal_handlers();
1919
newServer.start();
2020
} catch (std::exception& e) {
21-
std::cout << e.what() << "\n";
2221
LOG_ERR.error("Server error: " + std::string(e.what()));
2322
}
2423
return 0;

srcs/server/Config.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ bool Config::_parse_config_file(const std::string& fileName)
122122
else if (currentSection == "irc.motd")
123123
_parse_motd(line);
124124
}
125-
126125
file.close();
127126
return true;
128127
}

srcs/server/Server.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <arpa/inet.h> // hton*, ntoh*, inet_addr
1616
#include <csignal>
1717
#include <cstddef>
18+
#include <cstdlib>
1819
#include <cstring>
1920
#include <exception>
2021
#include <fcntl.h>
@@ -35,13 +36,8 @@
3536
Server::Server(const unsigned short port, const std::string& password) :
3637
_psswd(password), _name(ircConfig.get_name()), _port(port)
3738
{
38-
try {
39-
_serverSocket.tcp_bind(port);
40-
_serverSocket.tcp_listen();
41-
} catch (std::exception& e) {
42-
std::cout << e.what();
43-
exit(1);
44-
}
39+
_serverSocket.tcp_bind(port);
40+
_serverSocket.tcp_listen();
4541
LOG_SERVER.info("Server " + _name + " start at port :" + Utils::to_string(port));
4642
std::cout << "\n";
4743

@@ -317,9 +313,9 @@ bool Server::_handle_commands(int pfdIndex)
317313
while ((pos = client->get_read_buffer().find("\r\n")) != std::string::npos) {
318314
std::string line = client->get_read_buffer().substr(0, pos);
319315
client->get_read_buffer().erase(0, pos + 2);
320-
if (line.empty()) {
321-
continue;
322-
}
316+
if (line.empty()) {
317+
continue;
318+
}
323319
ICommand* cmd = _parse_command(*client, line);
324320
if (cmd) {
325321
cmd->execute(*this, *client);

0 commit comments

Comments
 (0)