Füge Unterstützung für das Neuladen von Räumen aus der Datenbank hinzu

- Implementiere die Methode `reloadRooms` in den Klassen `Server` und `SSLServer`, um Räume aus der Datenbank neu zu laden.
- Ergänze einen Signalhandler in `main.cpp`, um das Neuladen der Räume bei Empfang von SIGUSR1 zu ermöglichen.
- Füge die Methode `getRooms` in der `Database`-Klasse hinzu, um Räume aus der Datenbank abzurufen.
- Aktualisiere die Header-Dateien entsprechend, um die neuen Methoden zu berücksichtigen.
This commit is contained in:
Torsten Schulz (local)
2025-09-04 16:43:33 +02:00
parent a199352f61
commit 08d6a0c93b
7 changed files with 97 additions and 0 deletions

View File

@@ -307,6 +307,19 @@ namespace Yc {
return true;
}
void Server::reloadRooms() {
std::cout << "[YourChat] Reloading rooms from database..." << std::endl;
// Clear existing rooms
_rooms.clear();
// Reload from database
Json::Value emptyRoomList;
createRooms(emptyRoomList);
std::cout << "[YourChat] Reloaded " << _rooms.size() << " rooms from database" << std::endl;
}
void Server::createRooms(Json::Value roomList) {
auto self = shared_from_this();
bool created = false;

View File

@@ -18,6 +18,7 @@ namespace Yc {
~Server();
void run();
void stop();
void reloadRooms();
std::vector<std::string> roomList();
Json::Value jsonRoomList();
bool roomAllowed(std::string roomName, std::string userName, std::string password);

View File

@@ -435,6 +435,32 @@ void SSLServer::createRooms() {
}
}
void SSLServer::reloadRooms() {
std::cout << "[YourChat] Reloading rooms from database..." << std::endl;
// Clear existing rooms
_rooms.clear();
// Try to load rooms from database first
try {
Json::Value dbRooms = _database->getRooms();
if (dbRooms.isArray() && dbRooms.size() > 0) {
for (const auto& room : dbRooms) {
auto newRoom = std::make_shared<ChatRoom>(nullptr, room);
_rooms.push_back(newRoom);
}
std::cout << "[YourChat] Loaded " << _rooms.size() << " rooms from database" << std::endl;
return;
}
} catch (const std::exception& e) {
std::cerr << "[YourChat] Failed to load rooms from database: " << e.what() << std::endl;
}
// Fallback to config or default rooms
createRooms();
std::cout << "[YourChat] Fallback to config/default rooms: " << _rooms.size() << " rooms loaded" << std::endl;
}
std::vector<std::string> SSLServer::roomList() {
std::vector<std::string> list;
for (const auto &room: _rooms) {

View File

@@ -39,6 +39,7 @@ public:
void run();
void stop();
void createRooms();
void reloadRooms();
// Room management
std::vector<std::string> roomList();

View File

@@ -1,6 +1,8 @@
#include "database.h"
#include <pqxx/pqxx>
#include <stdexcept>
#include <iostream>
#include <json/json.h>
namespace Yc {
namespace Lib {
@@ -40,5 +42,43 @@ pqxx::result Database::exec(const std::string& query)
return r;
}
Json::Value Database::getRooms()
{
Json::Value rooms = Json::arrayValue;
try {
std::string query = R"(
SELECT r.id, r.title, r.password_hash, r.room_type_id, r.is_public, r.owner_id, r.min_age, r.max_age, r.created_at, r.updated_at, rt.tr as room_type
FROM chat.room r
LEFT JOIN chat.room_type rt ON r.room_type_id = rt.id
ORDER BY r.title
)";
auto result = exec(query);
for (const auto& row : result) {
Json::Value room;
room["id"] = row["id"].as<int>();
room["name"] = row["title"].c_str();
room["password"] = row["password_hash"].is_null() ? "" : row["password_hash"].c_str();
room["type"] = row["room_type_id"].is_null() ? 0 : row["room_type_id"].as<int>();
room["is_public"] = row["is_public"].as<bool>();
room["owner_id"] = row["owner_id"].is_null() ? 0 : row["owner_id"].as<int>();
room["min_age"] = row["min_age"].is_null() ? 0 : row["min_age"].as<int>();
room["max_age"] = row["max_age"].is_null() ? 0 : row["max_age"].as<int>();
room["created_at"] = row["created_at"].c_str();
room["updated_at"] = row["updated_at"].c_str();
room["room_type"] = row["room_type"].is_null() ? "" : row["room_type"].c_str();
room["allowed"] = Json::arrayValue;
room["roundlength"] = 60;
rooms.append(room);
}
} catch (const std::exception& e) {
std::cerr << "[YourChat] Database error in getRooms: " << e.what() << std::endl;
}
return rooms;
}
} // namespace Lib
} // namespace Yc

View File

@@ -13,6 +13,7 @@ public:
Database(std::shared_ptr<Config> config);
~Database() = default;
pqxx::result exec(const std::string& query);
Json::Value getRooms();
private:
std::unique_ptr<pqxx::connection> _connection;
};

View File

@@ -13,6 +13,7 @@
std::atomic<bool> g_running{true};
std::shared_ptr<Yc::Lib::SSLServer> g_sslServer = nullptr;
std::shared_ptr<Yc::Lib::Server> g_server = nullptr;
std::shared_ptr<Yc::Lib::Database> g_database = nullptr;
// Signal handler for graceful shutdown
void signalHandler(int signal) {
@@ -27,14 +28,28 @@ void signalHandler(int signal) {
}
}
// Signal handler for room reload
void reloadHandler(int signal) {
std::cout << "\n[YourChat] Received SIGUSR1, reloading rooms from database..." << std::endl;
if (g_sslServer) {
g_sslServer->reloadRooms();
}
if (g_server) {
g_server->reloadRooms();
}
}
// main function
int main(int, char **) {
// Set up signal handlers
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler);
std::signal(SIGUSR1, reloadHandler); // Room reload signal
auto config = std::make_shared<Yc::Lib::Config>();
auto database = std::make_shared<Yc::Lib::Database>(config);
g_database = database; // Store global reference for reload
// Check if SSL is enabled
bool sslEnabled = config->value("server", "ssl_enabled").asBool();