Skip to content

Commit c9a7258

Browse files
authored
Fix API Subscription initialisation (#1354)
* Fix #1352,#1351 * Fix/Add details on client address
1 parent 532ac7e commit c9a7258

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

libsrc/api/JsonAPI.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ void JsonAPI::initialize()
9898
{
9999
// init API, REQUIRED!
100100
API::init();
101+
// Initialise jsonCB with current instance
102+
_jsonCB->setSubscriptionsTo(_hyperion);
101103

102104
// setup auth interface
103105
connect(this, &API::onPendingTokenRequest, this, &JsonAPI::newPendingTokenRequest);
@@ -129,6 +131,8 @@ void JsonAPI::handleMessage(const QString &messageString, const QString &httpAut
129131
{
130132
const QString ident = "JsonRpc@" + _peerAddress;
131133
QJsonObject message;
134+
//std::cout << "JsonAPI::handleMessage | [" << static_cast<int>(_hyperion->getInstanceIndex()) << "] Received: ["<< messageString.toStdString() << "]" << std::endl;
135+
132136
// parse the message
133137
if (!JsonUtils::parse(ident, messageString, message, _log))
134138
{

libsrc/api/JsonCB.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool JsonCB::subscribeFor(const QString& type, bool unsubscribe)
7373
if(type == "priorities-update")
7474
{
7575
if (unsubscribe)
76-
disconnect(_prioMuxer,0 ,0 ,0);
76+
disconnect(_prioMuxer, &PriorityMuxer::prioritiesChanged, this, &JsonCB::handlePriorityUpdate);
7777
else
7878
connect(_prioMuxer, &PriorityMuxer::prioritiesChanged, this, &JsonCB::handlePriorityUpdate, Qt::UniqueConnection);
7979
}
@@ -156,6 +156,8 @@ void JsonCB::resetSubscriptions()
156156

157157
void JsonCB::setSubscriptionsTo(Hyperion* hyperion)
158158
{
159+
//std::cout << "JsonCB::setSubscriptions for instance [" << static_cast<int>(hyperion->getInstanceIndex()) << "] " << std::endl;
160+
159161
// get current subs
160162
QStringList currSubs(getSubscribedCommands());
161163

@@ -179,11 +181,13 @@ void JsonCB::doCallback(const QString& cmd, const QVariant& data)
179181
QJsonObject obj;
180182
obj["command"] = cmd;
181183

182-
if(static_cast<QMetaType::Type>(data.type()) == QMetaType::QJsonArray)
184+
if(data.userType() == QMetaType::QJsonArray)
183185
obj["data"] = data.toJsonArray();
184186
else
185187
obj["data"] = data.toJsonObject();
186188

189+
//std::cout << "JsonCB::doCallback | [" << static_cast<int>(_hyperion->getInstanceIndex()) << "] Send: [" << QJsonDocument(obj).toJson(QJsonDocument::Compact).toStdString() << "]" << std::endl;
190+
187191
emit newCallback(obj);
188192
}
189193

libsrc/jsonserver/JsonClientConnection.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ void JsonClientConnection::disconnected()
5757
{
5858
emit connectionClosed();
5959
}
60+
61+
QHostAddress JsonClientConnection::getClientAddress()
62+
{
63+
return _socket->peerAddress();
64+
}

libsrc/jsonserver/JsonClientConnection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <QString>
55
#include <QByteArray>
66
#include <QJsonObject>
7+
#include <QHostAddress>
78

89
// util includes
910
#include <utils/Logger.h>
@@ -24,6 +25,7 @@ class JsonClientConnection : public QObject
2425
/// @param socket The Socket object for this connection
2526
///
2627
JsonClientConnection(QTcpSocket * socket, bool localConnection);
28+
QHostAddress getClientAddress();
2729

2830
signals:
2931
void connectionClosed();

libsrc/jsonserver/JsonServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void JsonServer::newConnection()
102102
{
103103
if(_netOrigin->accessAllowed(socket->peerAddress(), socket->localAddress()))
104104
{
105-
Debug(_log, "New connection from: %s ",socket->localAddress().toString().toStdString().c_str());
105+
Debug(_log, "New connection from: %s",QSTRING_CSTR(socket->peerAddress().toString()));
106106
JsonClientConnection * connection = new JsonClientConnection(socket, _netOrigin->isLocalAddress(socket->peerAddress(), socket->localAddress()));
107107
_openConnections.insert(connection);
108108

@@ -118,7 +118,7 @@ void JsonServer::newConnection()
118118
void JsonServer::closedConnection()
119119
{
120120
JsonClientConnection* connection = qobject_cast<JsonClientConnection*>(sender());
121-
Debug(_log, "Connection closed");
121+
Debug(_log, "Connection closed for %s", QSTRING_CSTR(connection->getClientAddress().toString()));
122122
_openConnections.remove(connection);
123123

124124
// schedule to delete the connection object

libsrc/webserver/WebSocketClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void WebSocketClient::getWsFrameHeader(WebSocketHeader* header)
212212
/// See http://tools.ietf.org/html/rfc6455#section-5.2 for more information
213213
void WebSocketClient::sendClose(int status, QString reason)
214214
{
215-
Debug(_log, "send close: %d %s", status, QSTRING_CSTR(reason));
215+
Debug(_log, "Send close to %s: %d %s", QSTRING_CSTR(_socket->peerAddress().toString()), status, QSTRING_CSTR(reason));
216216
ErrorIf(!reason.isEmpty(), _log, QSTRING_CSTR(reason));
217217
_receiveBuffer.clear();
218218
QByteArray sendBuffer;

0 commit comments

Comments
 (0)