diff --git a/akashi.pro b/akashi.pro index a75c453..14c0246 100644 --- a/akashi.pro +++ b/akashi.pro @@ -1,4 +1,4 @@ -QT += core gui +QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets diff --git a/include/advertiser.h b/include/advertiser.h index 196b624..f20f295 100644 --- a/include/advertiser.h +++ b/include/advertiser.h @@ -1,9 +1,26 @@ #ifndef MASTER_H #define MASTER_H -class Advertiser{ +#include + +#include +#include +#include + +class Advertiser : public QObject{ public: - Advertiser(); + Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description); + void contactMasterServer(); + +private: + QString ip; + int port; + int ws_port; + int local_port; + QString name; + QString description; + + void readData(); }; #endif // MASTER_H diff --git a/include/akashimain.h b/include/akashimain.h index 71f1dd4..1d56f2a 100644 --- a/include/akashimain.h +++ b/include/akashimain.h @@ -3,6 +3,8 @@ #define CONFIG_VERSION 1 +#include + #include #include #include diff --git a/include/packet_manager.h b/include/packet_manager.h new file mode 100644 index 0000000..02ec333 --- /dev/null +++ b/include/packet_manager.h @@ -0,0 +1,12 @@ +#ifndef PACKET_MANAGER_H +#define PACKET_MANAGER_H + +#include +#include + +class PacketManager{ +public: + static QString buildPacket(QString header, QStringList contents); +}; + +#endif // PACKET_MANAGER_H diff --git a/src/advertiser.cpp b/src/advertiser.cpp index 816df22..2b17ca5 100644 --- a/src/advertiser.cpp +++ b/src/advertiser.cpp @@ -1,5 +1,35 @@ #include "include/advertiser.h" -Advertiser::Advertiser(){ - +Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description){ + ip = p_ip; + port = p_port; + ws_port = p_ws_port; + local_port = p_local_port; + name = p_name; + description = p_description; +} + +void Advertiser::contactMasterServer() { + QString concat_ports; + if(ws_port == -1) + concat_ports = QString::number(local_port); + else + concat_ports = QString::number(local_port) + "&" + QString::number(ws_port); + + QString ao_packet = PacketManager::buildPacket("SCC", {concat_ports, name, description, "akashi v" + QApplication::applicationVersion()}); + QByteArray data = ao_packet.toUtf8(); + + QTcpSocket socket(this); + connect(&socket, SIGNAL(readyRead()), SLOT(readData())); + + socket.connectToHost(ip, port); + if(socket.waitForConnected()){ + socket.write(data); + qDebug() << "Advertisement sent to master server"; + } +} + +void Advertiser::readData() { + // The master server should never really send data back to us + // But we handle it anyways, just in case this ever ends up being implemented } diff --git a/src/akashimain.cpp b/src/akashimain.cpp index 02e30c6..65fa55e 100644 --- a/src/akashimain.cpp +++ b/src/akashimain.cpp @@ -13,7 +13,25 @@ AkashiMain::AkashiMain(QWidget *parent) // Config is sound, so proceed with starting the server // TODO: start the server here // TODO: send signal server starting - + bool port_conversion_success; + bool ws_port_conversion_success; + bool local_port_conversion_success; + config.beginGroup("Options"); + QString ms_ip = config.value("ms_ip", "master.aceattorneyonline.com").toString(); + int port = config.value("ms_port", "27016").toInt(&port_conversion_success); + int ws_port = config.value("webao_port", "27017").toInt(&ws_port_conversion_success); + int local_port = config.value("port", "27016").toInt(&local_port_conversion_success); + QString name = config.value("server_name", "My First Server").toString(); + QString description = config.value("server_description", "This is my flashy new server").toString(); + config.endGroup(); + if(!port_conversion_success || !ws_port_conversion_success || !local_port_conversion_success) { + // TODO: send signal invalid conf due to bad port number + } else { + if(config.value("advertise", "true").toString() != "true") + ws_port = -1; + Advertiser advertiser(ms_ip, port, ws_port, local_port, name, description); + advertiser.contactMasterServer(); + } } } diff --git a/src/packet_manager.cpp b/src/packet_manager.cpp new file mode 100644 index 0000000..370c878 --- /dev/null +++ b/src/packet_manager.cpp @@ -0,0 +1,12 @@ +#include + +QString PacketManager::buildPacket(QString header, QStringList contents) +{ + QString ao_packet = header; + for(int i = 0; i < contents.length(); i++){ + ao_packet += "#" + contents[i]; + } + ao_packet += "#%"; + + return ao_packet; +}