Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

Commit 2c42584

Browse files
authored
Merge pull request #514 from YIO-Remote/feature/489_websocket-port
Configurable websocket port and using real hostname for zeroconf service publishing
2 parents b39b94f + 0076733 commit 2c42584

File tree

4 files changed

+61
-59
lines changed

4 files changed

+61
-59
lines changed

sources/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ int main(int argc, char* argv[]) {
202202
hwFactory->initialize();
203203

204204
// YIO API
205-
YioAPI* yioapi = new YioAPI(&engine);
205+
YioAPI* yioapi = YioAPI::getInstance();
206+
bool ok;
207+
int webSocketPort = qEnvironmentVariableIntValue("YIO_WEBSOCKET_PORT", &ok);
208+
yioapi->setWebSocketPort(ok ? webSocketPort : 946);
206209
engine.rootContext()->setContextProperty("api", yioapi);
207210

208211
// FACTORY RESET HANDLER

sources/yioapi.cpp

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
#include "yioapi.h"
2424

25+
#include <QHostInfo>
2526
#include <QJsonArray>
2627
#include <QJsonDocument>
2728
#include <QJsonObject>
28-
#include <QNetworkInterface>
2929
#include <QTimer>
3030

3131
#include "launcher.h"
@@ -39,48 +39,42 @@
3939
qCDebug(lcApi) << message << client->peerAddress().toString(); \
4040
}
4141

42-
YioAPI *YioAPI::s_instance = nullptr;
42+
YioAPI *YioAPI::getInstance() {
43+
static YioAPI api;
44+
return &api;
45+
}
4346

44-
YioAPI::YioAPI(QQmlApplicationEngine *engine) : m_engine(engine) {
45-
s_instance = this;
47+
YioAPI::YioAPI() : m_port(946), m_server(nullptr), m_context(nullptr) {
4648
m_entities = Entities::getInstance();
4749
m_integrations = Integrations::getInstance();
4850
m_config = Config::getInstance();
4951
}
5052

5153
YioAPI::~YioAPI() {
52-
s_instance = nullptr;
54+
if (m_server) {
55+
delete m_server;
56+
}
5357
}
5458

5559
void YioAPI::start() {
5660
m_server = new QWebSocketServer(QStringLiteral("YIO API"), QWebSocketServer::NonSecureMode, this);
5761

58-
// start websocket server on port 946(YIO)
59-
if (m_server->listen(QHostAddress::Any, 946)) {
62+
// start websocket server
63+
if (m_server->listen(QHostAddress::Any, m_port)) {
6064
connect(m_server, &QWebSocketServer::newConnection, this, &YioAPI::onNewConnection);
6165
connect(m_server, &QWebSocketServer::closed, this, &YioAPI::onClosed);
6266

6367
m_running = true;
6468
emit runningChanged();
6569
}
6670

67-
QString macAddr;
68-
for (QNetworkInterface interface : QNetworkInterface::allInterfaces()) {
69-
if (!(interface.flags() & QNetworkInterface::IsLoopBack)) {
70-
macAddr = interface.hardwareAddress();
71-
break;
72-
}
73-
}
74-
75-
macAddr.replace(":", "");
76-
m_hostname = "";
77-
m_hostname.append("YIO-Remote-").append(macAddr);
78-
qCDebug(lcApi) << "NAME" << m_hostname;
71+
m_hostname = QHostInfo::localHostName().split(QLatin1Char('.')).at(0);
72+
qCDebug(lcApi) << "YIO api published as ZeroConf service name:" << m_hostname;
7973
emit hostnameChanged();
8074

81-
m_zeroConf.startServicePublish(m_hostname.toUtf8(), "_yio-remote._tcp", "local", 946);
75+
m_zeroConf.startServicePublish(m_hostname.toUtf8(), "_yio-remote._tcp", "local", m_port);
8276

83-
qCDebug(lcApi) << "YIO api started";
77+
qCDebug(lcApi) << "YIO api started on port" << m_port;
8478
}
8579

8680
void YioAPI::stop() {
@@ -89,6 +83,7 @@ void YioAPI::stop() {
8983
m_running = false;
9084
m_zeroConf.stopServicePublish();
9185
emit runningChanged();
86+
qCDebug(lcApi) << "YIO api stopped";
9287
}
9388

9489
void YioAPI::sendMessage(QString message) {
@@ -852,7 +847,7 @@ void YioAPI::apiSystemSubscribeToEvents(QWebSocket *client, const int &id) {
852847
// add client to the subscribed client list
853848
m_subscribed_clients.append(client);
854849

855-
// connect to signals
850+
// connect to signals. FIXME handle multiple clients and perform proper cleanup
856851
m_context = new QObject(this);
857852

858853
QObject::connect(m_config, &Config::configChanged, m_context, [=]() { subscribeOnSignalEvent("config_changed"); });
@@ -882,8 +877,11 @@ void YioAPI::apiSystemUnsubscribeFromEvents(QWebSocket *client, const int &id) {
882877
return;
883878
}
884879

885-
QObject::disconnect(m_context);
886-
m_context->deleteLater();
880+
// FIXME handle multiple clients and perform proper cleanup
881+
if (m_context) {
882+
QObject::disconnect(m_context);
883+
m_context->deleteLater();
884+
}
887885

888886
for (int i = 0; i < m_subscribed_clients.length(); i++) {
889887
if (m_subscribed_clients[i] == client) {

sources/yioapi.h

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class YioAPI : public YioAPIInterface {
3838
Q_INTERFACES(YioAPIInterface)
3939

4040
public:
41+
static YioAPI* getInstance();
42+
4143
Q_PROPERTY(bool running READ running NOTIFY runningChanged) // returns the state of the API
4244
Q_PROPERTY(QString hostname READ hostname NOTIFY hostnameChanged) // returns the hostname of the remote
4345

@@ -51,6 +53,9 @@ class YioAPI : public YioAPIInterface {
5153
*/
5254
Q_INVOKABLE void sendMessage(QString message) override;
5355

56+
void setWebSocketPort(quint16 port) { m_port = port; }
57+
quint16 webSocketPort() { return m_port; }
58+
5459
// CONFIG MANIPULATION METHODS
5560
QVariantMap getConfig() override;
5661
bool setConfig(QVariantMap config);
@@ -67,11 +72,6 @@ class YioAPI : public YioAPIInterface {
6772
void discoverNetworkServices() override;
6873
Q_INVOKABLE void discoverNetworkServices(QString mdns) override;
6974

70-
explicit YioAPI(QQmlApplicationEngine* engine = nullptr);
71-
~YioAPI() override;
72-
73-
static YioAPI* getInstance() { return s_instance; }
74-
7575
signals:
7676
// void closed();
7777
// void messageReceived(QVariantMap message);
@@ -87,35 +87,10 @@ class YioAPI : public YioAPIInterface {
8787
void onClientDisconnected();
8888

8989
private:
90-
QWebSocketServer* m_server;
91-
QMap<QWebSocket*, bool> m_clients; // websocket client, true if authentication was successful
92-
93-
QList<QWebSocket*> m_subscribed_clients;
94-
QObject* m_context;
95-
void subscribeOnSignalEvent(const QString& event);
96-
97-
bool m_running = false;
98-
99-
static YioAPI* s_instance;
100-
QQmlApplicationEngine* m_engine;
101-
102-
QString m_token = "0"; // "c82b5fd6bea6fc3faf9a30bb864a9ee2"
103-
// QCryptographicHash::hash(m_token.toLocal8Bit(), QCryptographicHash::Sha512);
104-
QByteArray m_hash =
105-
"{U\xC0<$\xF7\n\xA7PA\xC3=\xBEk\xF5\xC1\xCA\x8B\t\x91\xA0\x9Et\xBA"
106-
"E\xE9\xA0)\xE4\x07^E\x04\x17Xg\xE4)\x04\xB7\xD4\x9D,\x19%\xD7\xA1\xDC\x84U\x83\xA2\xAA\x1D\xD7:\xBE\xF6"
107-
"1\xFA\x90\xED\x16\xBB";
108-
QString m_hostname;
109-
110-
QZeroConf m_zeroConf;
111-
QZeroConf* m_zeroConfBrowser;
90+
YioAPI();
91+
~YioAPI() override;
11292

113-
QStringList m_discoverableServices;
114-
QString m_prevIp;
115-
116-
Entities* m_entities;
117-
Integrations* m_integrations;
118-
Config* m_config;
93+
void subscribeOnSignalEvent(const QString& event);
11994

12095
// API CALLS
12196
void apiSendResponse(QWebSocket* client, const int& id, const bool& success, QVariantMap response);
@@ -168,4 +143,30 @@ class YioAPI : public YioAPIInterface {
168143
void apiSettingsSetDarkMode(QWebSocket* client, const int& id, const QVariantMap& map);
169144

170145
void apiLoggerControl(QWebSocket* client, const int& id, const QVariantMap& map);
146+
147+
private:
148+
quint16 m_port;
149+
bool m_running = false;
150+
QWebSocketServer* m_server;
151+
QMap<QWebSocket*, bool> m_clients; // websocket client, true if authentication was successful
152+
153+
QList<QWebSocket*> m_subscribed_clients;
154+
QObject* m_context;
155+
156+
QString m_token = "0"; // "c82b5fd6bea6fc3faf9a30bb864a9ee2"
157+
// QCryptographicHash::hash(m_token.toLocal8Bit(), QCryptographicHash::Sha512);
158+
QByteArray m_hash =
159+
"{U\xC0<$\xF7\n\xA7PA\xC3=\xBEk\xF5\xC1\xCA\x8B\t\x91\xA0\x9Et\xBA"
160+
"E\xE9\xA0)\xE4\x07^E\x04\x17Xg\xE4)\x04\xB7\xD4\x9D,\x19%\xD7\xA1\xDC\x84U\x83\xA2\xAA\x1D\xD7:\xBE\xF6"
161+
"1\xFA\x90\xED\x16\xBB";
162+
QString m_hostname;
163+
164+
QZeroConf m_zeroConf;
165+
166+
QStringList m_discoverableServices;
167+
QString m_prevIp;
168+
169+
Entities* m_entities;
170+
Integrations* m_integrations;
171+
Config* m_config;
171172
};

translations/en_US.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ to set up YIO remote</source>
721721
<context>
722722
<name>QGuiApplication</name>
723723
<message>
724-
<location filename="../sources/main.cpp" line="216"/>
724+
<location filename="../sources/main.cpp" line="217"/>
725725
<source>Factory reset failed.
726726
%1</source>
727727
<translation type="unfinished"></translation>

0 commit comments

Comments
 (0)