Use struct to transport config

+ Fix debug message on success
This commit is contained in:
Salanto 2021-07-11 20:15:04 +02:00
parent 910560ec13
commit 13cd901cfc
5 changed files with 57 additions and 36 deletions

View File

@ -21,6 +21,16 @@
#include <QtNetwork>
#include <QObject>
//Don't question this. It needs to be here for some reason.
struct advertiser_config {
QString name;
QString description;
int port;
int ws_port;
int players;
QUrl masterserver;
bool debug;
};
/**
* @brief Represents the advertiser of the server. Sends current server information to masterserver.
@ -54,15 +64,10 @@ public slots:
void msRequestFinished(QNetworkReply *reply);
/**
* @brief setAdvertiserSettings Configures the values being advertised to masterserver.
* @param f_name Servername.
* @param f_description Serverdescription.
* @param f_port Client port.
* @param f_ws_port Optional Websocket proxy port.
* @param f_players Maximum amount of clients.
* @param f_master_url URL of the advertisement target.
* @brief Sets the values being advertised to masterserver.
* @param config Configuration struct for the advertiser. Always includes ALL settings.
*/
void setAdvertiserSettings(QString f_name, QString f_description, int f_port, int f_ws_port, int f_players, QUrl f_master_url, bool f_debug);
void setAdvertiserSettings(advertiser_config config);
private:

View File

@ -138,6 +138,11 @@ class Server : public QObject {
*/
int getCharID(QString char_name);
/**
* @brief Creates an HTTP advertiser config struct and emits it using server::reloadHTTPRequest.
*/
void reloadHTTPAdvertiserConfig();
/**
* @brief The collection of all currently connected clients.
*/
@ -230,14 +235,9 @@ class Server : public QObject {
/**
* @brief Sends all necessary info for the new advertiser.
* @param f_name Servername.
* @param f_description Serverdescription.
* @param f_port Client port.
* @param f_ws_port Optional Websocket proxy port.
* @param f_players Maximum amount of clients.
* @param f_master_url URL of the advertisement target.
* @param Struct that contains all configuration for the advertiser
*/
void reloadHTTPRequest(QString f_name, QString f_description, int f_port, int f_ws_port, int f_players, QUrl f_master_url, bool f_debug);
void reloadHTTPRequest(struct advertiser_config config);
/**
* @brief Sends a modcall webhook request, emitted by AOClient::pktModcall.

View File

@ -412,9 +412,7 @@ void AOClient::cmdReload(int argc, QStringList argv)
{
ConfigManager::reloadSettings();
emit server->reloadRequest(ConfigManager::serverName(), ConfigManager::serverDescription());
emit server->reloadHTTPRequest(ConfigManager::serverName(),ConfigManager::serverDescription(),ConfigManager::serverPort(),
ConfigManager::webaoPort(),ConfigManager::maxPlayers(),ConfigManager::advertiserHTTPIP(),
ConfigManager::advertiserHTTPDebug());
server->reloadHTTPAdvertiserConfig();
sendServerMessage("Reloaded configurations");
}

View File

@ -48,28 +48,35 @@ void HTTPAdvertiser::msAdvertiseServer()
void HTTPAdvertiser::msRequestFinished(QNetworkReply *reply)
{
if (m_debug) {
QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
if (json.isNull()) {
qCritical().noquote() << "Invalid JSON response from" << reply->url();
reply->deleteLater();
return;
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
qDebug().noquote() << "Succesfully advertised server.";
}
else {
QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
if (json.isNull()) {
qCritical().noquote() << "Invalid JSON response from" << reply->url();
reply->deleteLater();
return;
}
qDebug().noquote() << "Got valid response from" << reply->url();
qDebug() << json;
qDebug().noquote() << "Got valid response from" << reply->url();
qDebug() << json;
}
}
reply->deleteLater();
}
void HTTPAdvertiser::setAdvertiserSettings(QString f_name, QString f_description, int f_port, int f_ws_port, int f_players, QUrl f_master_url, bool f_debug)
void HTTPAdvertiser::setAdvertiserSettings(advertiser_config config)
{
m_name = f_name;
m_description = f_description;
m_port = f_port;
m_ws_port = f_ws_port;
m_players = f_players;
m_masterserver = f_master_url;
m_debug = f_debug;
m_name = config.name;
m_description = config.description;
m_port = config.port;
m_ws_port = config.ws_port;
m_players = config.players;
m_masterserver = config.masterserver;
m_debug = config.debug;
msAdvertiseServer();
}

View File

@ -66,9 +66,7 @@ void Server::start()
httpAdvertiser, &HTTPAdvertiser::msAdvertiseServer);
connect(this, &Server::reloadHTTPRequest,
httpAdvertiser, &HTTPAdvertiser::setAdvertiserSettings);
emit reloadHTTPRequest(ConfigManager::serverName(),ConfigManager::serverDescription(),ConfigManager::serverPort(),ConfigManager::webaoPort(),
ConfigManager::maxPlayers(),ConfigManager::advertiserHTTPIP(),ConfigManager::advertiserHTTPDebug());
reloadHTTPAdvertiserConfig();
httpAdvertiserTimer->start(300000);
}
@ -257,6 +255,19 @@ int Server::getCharID(QString char_name)
return -1; // character does not exist
}
void Server::reloadHTTPAdvertiserConfig()
{
advertiser_config config;
config.name = ConfigManager::serverName();
config.description = ConfigManager::serverDescription();
config.port = ConfigManager::serverPort();
config.ws_port = ConfigManager::webaoPort();
config.players = ConfigManager::maxPlayers();
config.masterserver = ConfigManager::advertiserHTTPIP();
config.debug = ConfigManager::advertiserHTTPDebug();
emit reloadHTTPRequest(config);
}
void Server::allowMessage()
{
can_send_ic_messages = true;