cleanup some things

This commit is contained in:
scatterflower 2020-08-23 21:30:38 -05:00
parent 0359628645
commit 0ab0fcd060
8 changed files with 44 additions and 40 deletions

View File

@ -13,7 +13,7 @@ class Advertiser : public QObject {
public:
Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port,
QString p_name, QString p_description);
QString p_name, QString p_description, QObject *parent = nullptr);
void contactMasterServer();
signals:

View File

@ -22,7 +22,6 @@ public:
AkashiMain(QWidget *parent = nullptr);
~AkashiMain();
QSettings config;
ConfigManager config_manager;
void generateDefaultConfig(bool backup_old);

View File

@ -10,14 +10,22 @@
class ConfigManager {
public:
ConfigManager(QSettings *);
ConfigManager();
bool initConfig();
void generateDefaultConfig(bool backup_old);
void updateConfig(int current_version);
bool loadServerSettings(QString *ms_ip, int *port, int *ws_port,
int *local_port, QString *name, QString *description,
bool *advertise_server);
struct server_settings {
QString ms_ip;
int port;
int ws_port;
int local_port;
QString name;
QString description;
bool advertise_server;
};
bool loadServerSettings(server_settings *settings);
private:
QSettings *config;

View File

@ -1,7 +1,9 @@
#include "include/advertiser.h"
Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port,
int p_local_port, QString p_name, QString p_description)
int p_local_port, QString p_name, QString p_description,
QObject *parent)
: QObject(parent)
{
ip = p_ip;
port = p_port;
@ -41,7 +43,7 @@ void Advertiser::socketConnected()
concat_ports = QString::number(local_port) + "&" + QString::number(ws_port);
AOPacket ao_packet("SCC", {concat_ports, name, description,
"akashi v" + QApplication::applicationVersion()});
"akashi v" + QApplication::applicationVersion()});
QByteArray data = ao_packet.toUtf8();
socket->write(data);

View File

@ -2,8 +2,7 @@
#include "ui_akashimain.h"
AkashiMain::AkashiMain(QWidget *parent)
: QMainWindow(parent), config("config.ini", QSettings::IniFormat),
config_manager(&config), ui(new Ui::AkashiMain)
: QMainWindow(parent), config_manager(), ui(new Ui::AkashiMain)
{
ui->setupUi(this);
qDebug("Main application started");
@ -11,27 +10,24 @@ AkashiMain::AkashiMain(QWidget *parent)
if (config_manager.initConfig()) {
// Config is sound, so proceed with starting the server
// Validate some of the config before passing it on
QString ms_ip, name, description;
int port, ws_port, local_port;
bool advertise_server;
bool config_valid = config_manager.loadServerSettings(
&ms_ip, &port, &ws_port, &local_port, &name, &description,
&advertise_server);
ConfigManager::server_settings settings;
bool config_valid = config_manager.loadServerSettings(&settings);
if (!config_valid) {
// TODO: send signal config invalid
config_manager.generateDefaultConfig(true);
}
else {
if (advertise_server) {
advertiser =
new Advertiser(ms_ip, port, ws_port, local_port, name, description);
if (settings.advertise_server) {
advertiser = new Advertiser(settings.ms_ip, settings.port,
settings.ws_port, settings.local_port,
settings.name, settings.description, this);
advertiser->contactMasterServer();
}
// TODO: start the server here
// TODO: send signal server starting.
server = new Server(port, ws_port);
server = new Server(settings.port, settings.ws_port);
server->start();
}
}

View File

@ -19,6 +19,6 @@ QString AOPacket::toString()
QByteArray AOPacket::toUtf8()
{
QString packet_string = toString();
return packet_string.toUtf8();
QString packet_string = toString();
return packet_string.toUtf8();
}

View File

@ -1,6 +1,9 @@
#include <include/config_manager.h>
ConfigManager::ConfigManager(QSettings *p_config) { config = p_config; }
ConfigManager::ConfigManager()
{
config = new QSettings("config.ini", QSettings::IniFormat);
}
// Validate and set up the config
bool ConfigManager::initConfig()
@ -106,23 +109,22 @@ void ConfigManager::updateConfig(int current_version)
}
// Validate and retriever settings related to advertising and the server
bool ConfigManager::loadServerSettings(QString *ms_ip, int *port, int *ws_port,
int *local_port, QString *name,
QString *description,
bool *advertise_server)
bool ConfigManager::loadServerSettings(server_settings *settings)
{
bool port_conversion_success;
bool ws_port_conversion_success;
bool local_port_conversion_success;
config->beginGroup("Options");
*ms_ip = config->value("ms_ip", "master.aceattorneyonline.com").toString();
*port = config->value("ms_port", "27016").toInt(&port_conversion_success);
*ws_port =
settings->ms_ip =
config->value("ms_ip", "master.aceattorneyonline.com").toString();
settings->port =
config->value("ms_port", "27016").toInt(&port_conversion_success);
settings->ws_port =
config->value("webao_port", "27017").toInt(&ws_port_conversion_success);
*local_port =
settings->local_port =
config->value("port", "27016").toInt(&local_port_conversion_success);
*name = config->value("server_name", "My First Server").toString();
*description =
settings->name = config->value("server_name", "My First Server").toString();
settings->description =
config->value("server_description", "This is my flashy new server")
.toString();
config->endGroup();
@ -132,12 +134,12 @@ bool ConfigManager::loadServerSettings(QString *ms_ip, int *port, int *ws_port,
}
else {
if (config->value("advertise", "true").toString() != "true")
*advertise_server = false;
settings->advertise_server = false;
else
*advertise_server = true;
settings->advertise_server = true;
if (config->value("webao_enable", "true").toString() != "true")
*ws_port = -1;
settings->ws_port = -1;
return true;
}

View File

@ -6,7 +6,4 @@ Server::Server(int p_port, int p_ws_port)
ws_port = p_ws_port;
}
void Server::start()
{
}
void Server::start() {}