Skip to content

Commit 2e4942a

Browse files
committed
Streamline Callbacks
1 parent 5d5c2bf commit 2e4942a

File tree

8 files changed

+79
-40
lines changed

8 files changed

+79
-40
lines changed

include/api/JsonAPI.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class JsonAPI : public API
5050
///
5151
void initialize();
5252

53+
QSharedPointer<JsonCallbacks> JsonAPI::getCallBack() const;
54+
5355
public slots:
5456

5557
private slots:
@@ -82,7 +84,7 @@ private slots:
8284
///
8385
/// Signal emits with the reply message provided with handleMessage()
8486
///
85-
void callbackMessage(QJsonObject);
87+
void callbackReady(QJsonObject);
8688

8789
///
8890
/// Signal emits whenever a JSON-message should be forwarded

include/api/JsonCallbacks.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class JsonCallbacks : public QObject
9393
/// @brief Emits whenever a new json mesage callback is ready to send
9494
/// @param The JsonObject message
9595
///
96-
void newCallback(QJsonObject);
96+
void callbackReady(QJsonObject);
9797

9898
private slots:
9999
///
@@ -182,6 +182,8 @@ private slots:
182182

183183
/// construct callback msg
184184
void doCallback(Subscription::Type cmd, const QVariant& data);
185+
void doCallback(Subscription::Type cmd, const QJsonArray& data);
186+
void doCallback(Subscription::Type cmd, const QJsonObject& data);
185187

186188
Logger *_log;
187189
Hyperion* _hyperion;

libsrc/api/JsonAPI.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ JsonAPI::JsonAPI(QString peerAddress, Logger *log, bool localConnection, QObject
8585
_jsonCB = QSharedPointer<JsonCallbacks>(new JsonCallbacks( _log, _peerAddress, parent));
8686
}
8787

88+
QSharedPointer<JsonCallbacks> JsonAPI::getCallBack() const
89+
{
90+
return _jsonCB;
91+
}
92+
8893
void JsonAPI::initialize()
8994
{
9095
// init API, REQUIRED!
@@ -97,9 +102,6 @@ void JsonAPI::initialize()
97102
// listen for killed instances
98103
connect(_instanceManager, &HyperionIManager::instanceStateChanged, this, &JsonAPI::handleInstanceStateChange);
99104

100-
// pipe callbacks from subscriptions to parent
101-
connect(_jsonCB.data(), &JsonCallbacks::newCallback, this, &JsonAPI::callbackMessage);
102-
103105
// notify hyperion about a jsonMessageForward
104106
if (_hyperion != nullptr)
105107
{
@@ -1537,7 +1539,7 @@ void JsonAPI::sendSuccessReply(const JsonApiCommand& cmd)
15371539

15381540
void JsonAPI::sendSuccessReply(const QString &command, int tan, InstanceCmd::Type isInstanceCmd)
15391541
{
1540-
emit callbackMessage(getBasicCommandReply(true, command, tan , isInstanceCmd));
1542+
emit callbackReady(getBasicCommandReply(true, command, tan , isInstanceCmd));
15411543
}
15421544

15431545
void JsonAPI::sendSuccessDataReply(const QJsonValue &infoData, const JsonApiCommand& cmd)
@@ -1572,7 +1574,7 @@ void JsonAPI::sendSuccessDataReplyWithError(const QJsonValue &infoData, const QS
15721574
reply["errorData"] = errorsArray;
15731575
}
15741576

1575-
emit callbackMessage(reply);
1577+
emit callbackReady(reply);
15761578
}
15771579

15781580
void JsonAPI::sendErrorReply(const QString &error, const JsonApiCommand& cmd)
@@ -1601,7 +1603,7 @@ void JsonAPI::sendErrorReply(const QString &error, const QStringList& errorDetai
16011603
reply["errorData"] = errorsArray;
16021604
}
16031605

1604-
emit callbackMessage(reply);
1606+
emit callbackReady(reply);
16051607
}
16061608

16071609
void JsonAPI::sendNewRequest(const QJsonValue &infoData, const JsonApiCommand& cmd)
@@ -1621,7 +1623,7 @@ void JsonAPI::sendNewRequest(const QJsonValue &infoData, const QString &command,
16211623

16221624
request["info"] = infoData;
16231625

1624-
emit callbackMessage(request);
1626+
emit callbackReady(request);
16251627
}
16261628

16271629
void JsonAPI::sendNoAuthorization(const JsonApiCommand& cmd)

libsrc/api/JsonCallbacks.cpp

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -282,22 +282,43 @@ QStringList JsonCallbacks::getSubscribedCommands() const
282282
}
283283

284284
void JsonCallbacks::doCallback(Subscription::Type cmd, const QVariant& data)
285+
{
286+
if (data.userType() == QMetaType::QJsonArray)
287+
{
288+
doCallback(cmd, data.toJsonArray());
289+
}
290+
else
291+
{
292+
doCallback(cmd, data.toJsonObject());
293+
}
294+
}
295+
296+
void JsonCallbacks::doCallback(Subscription::Type cmd, const QJsonArray& data)
285297
{
286298
QJsonObject obj;
287299
obj["command"] = Subscription::toString(cmd);
288300

289301
if (Subscription::isInstanceSpecific(cmd))
290302
{
291-
obj["instance"] = _hyperion->getInstanceIndex();
303+
obj.insert("instance", _hyperion->getInstanceIndex());
292304
}
305+
obj.insert("data", data);
293306

294-
if (data.userType() == QMetaType::QJsonArray) {
295-
obj["data"] = data.toJsonArray();
296-
} else {
297-
obj["data"] = data.toJsonObject();
307+
emit callbackReady(obj);
308+
}
309+
310+
void JsonCallbacks::doCallback(Subscription::Type cmd, const QJsonObject& data)
311+
{
312+
QJsonObject obj;
313+
obj["command"] = Subscription::toString(cmd);
314+
315+
if (Subscription::isInstanceSpecific(cmd))
316+
{
317+
obj.insert("instance", _hyperion->getInstanceIndex());
298318
}
319+
obj.insert("data", data);
299320

300-
emit newCallback(obj);
321+
emit callbackReady(obj);
301322
}
302323

303324
void JsonCallbacks::handleComponentState(hyperion::Components comp, bool state)
@@ -306,7 +327,7 @@ void JsonCallbacks::handleComponentState(hyperion::Components comp, bool state)
306327
data["name"] = componentToIdString(comp);
307328
data["enabled"] = state;
308329

309-
doCallback(Subscription::ComponentsUpdate, QVariant(data));
330+
doCallback(Subscription::ComponentsUpdate, data);
310331
}
311332

312333
void JsonCallbacks::handlePriorityUpdate(int currentPriority, const PriorityMuxer::InputsMap& activeInputs)
@@ -315,15 +336,15 @@ void JsonCallbacks::handlePriorityUpdate(int currentPriority, const PriorityMuxe
315336
data["priorities"] = JsonInfo::getPrioritiestInfo(currentPriority, activeInputs);
316337
data["priorities_autoselect"] = _hyperion->sourceAutoSelectEnabled();
317338

318-
doCallback(Subscription::PrioritiesUpdate, QVariant(data));
339+
doCallback(Subscription::PrioritiesUpdate, data);
319340
}
320341

321342
void JsonCallbacks::handleImageToLedsMappingChange(int mappingType)
322343
{
323344
QJsonObject data;
324345
data["imageToLedMappingType"] = ImageProcessor::mappingTypeToStr(mappingType);
325346

326-
doCallback(Subscription::ImageToLedMappingUpdate, QVariant(data));
347+
doCallback(Subscription::ImageToLedMappingUpdate, data);
327348
}
328349

329350
void JsonCallbacks::handleAdjustmentChange()
@@ -335,37 +356,37 @@ void JsonCallbacks::handleVideoModeChange(VideoMode mode)
335356
{
336357
QJsonObject data;
337358
data["videomode"] = QString(videoMode2String(mode));
338-
doCallback(Subscription::VideomodeUpdate, QVariant(data));
359+
doCallback(Subscription::VideomodeUpdate, data);
339360
}
340361

341362
#if defined(ENABLE_EFFECTENGINE)
342363
void JsonCallbacks::handleEffectListChange()
343364
{
344365
QJsonObject effects;
345366
effects["effects"] = JsonInfo::getEffects(_hyperion);
346-
doCallback(Subscription::EffectsUpdate, QVariant(effects));
367+
doCallback(Subscription::EffectsUpdate, effects);
347368
}
348369
#endif
349370

350371
void JsonCallbacks::handleSettingsChange(settings::type type, const QJsonDocument& data)
351372
{
352-
QJsonObject dat;
373+
QJsonObject obj;
353374
if(data.isObject()) {
354-
dat[typeToString(type)] = data.object();
375+
obj[typeToString(type)] = data.object();
355376
} else {
356-
dat[typeToString(type)] = data.array();
377+
obj[typeToString(type)] = data.array();
357378
}
358379

359-
doCallback(Subscription::SettingsUpdate, QVariant(dat));
380+
doCallback(Subscription::SettingsUpdate, obj);
360381
}
361382

362383
void JsonCallbacks::handleLedsConfigChange(settings::type type, const QJsonDocument& data)
363384
{
364385
if(type == settings::LEDS)
365386
{
366-
QJsonObject dat;
367-
dat[typeToString(type)] = data.array();
368-
doCallback(Subscription::LedsUpdate, QVariant(dat));
387+
QJsonObject obj;
388+
obj[typeToString(type)] = data.array();
389+
doCallback(Subscription::LedsUpdate, obj);
369390
}
370391
}
371392

@@ -385,21 +406,24 @@ void JsonCallbacks::handleTokenChange(const QVector<AuthManager::AuthDefinition>
385406
sub["last_use"] = entry.lastUse;
386407
arr.push_back(sub);
387408
}
388-
doCallback(Subscription::TokenUpdate, QVariant(arr));
409+
doCallback(Subscription::TokenUpdate, arr);
389410
}
390411

391412
void JsonCallbacks::handleLedColorUpdate(const std::vector<ColorRgb> &ledColors)
392413
{
393414
QJsonObject result;
394415
QJsonArray leds;
395416

396-
for (const auto &color : ledColors)
417+
// Avoid copying by appending RGB values directly
418+
for (const auto& color : ledColors)
397419
{
398-
leds << QJsonValue(color.red) << QJsonValue(color.green) << QJsonValue(color.blue);
420+
leds.append(QJsonValue(color.red));
421+
leds.append(QJsonValue(color.green));
422+
leds.append(QJsonValue(color.blue));
399423
}
400424
result["leds"] = leds;
401425

402-
doCallback(Subscription::LedColorsUpdate, QVariant(result));
426+
doCallback(Subscription::LedColorsUpdate, result);
403427
}
404428

405429
void JsonCallbacks::handleImageUpdate(const Image<ColorRgb> &image)
@@ -413,7 +437,7 @@ void JsonCallbacks::handleImageUpdate(const Image<ColorRgb> &image)
413437
QJsonObject result;
414438
result["image"] = "data:image/jpg;base64," + QString(byteArray.toBase64());
415439

416-
doCallback(Subscription::ImageUpdate, QVariant(result));
440+
doCallback(Subscription::ImageUpdate, result);
417441
}
418442

419443
void JsonCallbacks::handleLogMessageUpdate(const Logger::T_LOG_MESSAGE &msg)
@@ -445,7 +469,7 @@ void JsonCallbacks::handleLogMessageUpdate(const Logger::T_LOG_MESSAGE &msg)
445469
}
446470
result.insert("messages", messageArray);
447471

448-
doCallback(Subscription::LogMsgUpdate, QVariant(result));
472+
doCallback(Subscription::LogMsgUpdate, result);
449473
}
450474

451475
void JsonCallbacks::handleEventUpdate(const Event &event)
@@ -454,6 +478,6 @@ void JsonCallbacks::handleEventUpdate(const Event &event)
454478

455479
result["event"] = eventToString(event);
456480

457-
doCallback(Subscription::EventUpdate, QVariant(result));
481+
doCallback(Subscription::EventUpdate, result);
458482
}
459483

libsrc/jsonserver/JsonClientConnection.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// project includes
22
#include "JsonClientConnection.h"
33
#include <api/JsonAPI.h>
4+
#include <api/JsonCallbacks.h>
45

56
// qt inc
67
#include <QTcpSocket>
@@ -17,8 +18,9 @@ JsonClientConnection::JsonClientConnection(QTcpSocket *socket, bool localConnect
1718
// create a new instance of JsonAPI
1819
_jsonAPI = new JsonAPI(socket->peerAddress().toString(), _log, localConnection, this);
1920
// get the callback messages from JsonAPI and send it to the client
20-
connect(_jsonAPI, &JsonAPI::callbackMessage, this , &JsonClientConnection::sendMessage);
21+
connect(_jsonAPI, &JsonAPI::callbackReady, this , &JsonClientConnection::sendMessage);
2122
connect(_jsonAPI, &JsonAPI::forceClose, this , [&](){ _socket->close(); } );
23+
connect(_jsonAPI->getCallBack().get(), &JsonCallbacks::callbackReady, this, &JsonClientConnection::sendMessage);
2224

2325
_jsonAPI->initialize();
2426
}
@@ -47,7 +49,8 @@ void JsonClientConnection::readRequest()
4749
qint64 JsonClientConnection::sendMessage(QJsonObject message)
4850
{
4951
QJsonDocument writer(message);
50-
QByteArray data = writer.toJson(QJsonDocument::Compact) + "\n";
52+
QByteArray data = writer.toJson(QJsonDocument::Compact);
53+
data.append('\n');
5154

5255
if (!_socket || (_socket->state() != QAbstractSocket::ConnectedState)) return 0;
5356
return _socket->write(data.data(), data.size());

libsrc/webserver/WebJsonRpc.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "QtHttpClientWrapper.h"
66

77
#include <api/JsonAPI.h>
8+
#include <api/JsonCallbacks.h>
89

910
WebJsonRpc::WebJsonRpc(QtHttpRequest* request, QtHttpServer* server, bool localConnection, QtHttpClientWrapper* parent)
1011
: QObject(parent)
@@ -14,8 +15,10 @@ WebJsonRpc::WebJsonRpc(QtHttpRequest* request, QtHttpServer* server, bool localC
1415
{
1516
const QString client = request->getClientInfo().clientAddress.toString();
1617
_jsonAPI = new JsonAPI(client, _log, localConnection, this, true);
17-
connect(_jsonAPI, &JsonAPI::callbackMessage, this, &WebJsonRpc::handleCallback);
18+
connect(_jsonAPI, &JsonAPI::callbackReady, this, &WebJsonRpc::sendCallbackMessage);
1819
connect(_jsonAPI, &JsonAPI::forceClose, [&]() { _wrapper->closeConnection(); _stopHandle = true; });
20+
connect(_jsonAPI->getCallBack().get(), &JsonCallbacks::callbackReady, this, &WebJsonRpc::sendCallbackMessage);
21+
1922
_jsonAPI->initialize();
2023
}
2124

@@ -31,7 +34,7 @@ void WebJsonRpc::handleMessage(QtHttpRequest* request)
3134
}
3235
}
3336

34-
void WebJsonRpc::handleCallback(QJsonObject obj)
37+
void WebJsonRpc::sendCallbackMessage(QJsonObject obj)
3538
{
3639
// guard against wrong callbacks; TODO: Remove when JSONAPI is more solid
3740
if(!_unlocked) return;

libsrc/webserver/WebJsonRpc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ class WebJsonRpc : public QObject {
2626
bool _unlocked = false;
2727

2828
private slots:
29-
void handleCallback(QJsonObject obj);
29+
void sendCallbackMessage(QJsonObject obj);
3030
};

libsrc/webserver/WebSocketClient.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <hyperion/Hyperion.h>
66
#include <api/JsonAPI.h>
7+
#include <api/JsonCallbacks.h>
78

89
#include <QTcpSocket>
910
#include <QtEndian>
@@ -24,9 +25,11 @@ WebSocketClient::WebSocketClient(QtHttpRequest* request, QTcpSocket* sock, bool
2425

2526
// Json processor
2627
_jsonAPI.reset(new JsonAPI(client, _log, localConnection, this));
27-
connect(_jsonAPI.get(), &JsonAPI::callbackMessage, this, &WebSocketClient::sendMessage);
28+
connect(_jsonAPI.get(), &JsonAPI::callbackReady, this, &WebSocketClient::sendMessage);
2829
connect(_jsonAPI.get(), &JsonAPI::forceClose, this,[this]() { this->sendClose(CLOSECODE::NORMAL); });
2930

31+
connect(_jsonAPI->getCallBack().get(), &JsonCallbacks::callbackReady, this, &WebSocketClient::sendMessage);
32+
3033
connect(this, &WebSocketClient::handleMessage, _jsonAPI.get(), &JsonAPI::handleMessage);
3134

3235
Debug(_log, "New connection from %s", QSTRING_CSTR(client));

0 commit comments

Comments
 (0)