Fix API Subscription initialisation (#1354)

* Fix #1352,#1351

* Fix/Add details on client address
This commit is contained in:
LordGrey 2021-10-16 13:55:23 +02:00 committed by GitHub
parent 532ac7e330
commit c9a7258160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 5 deletions

View File

@ -98,6 +98,8 @@ void JsonAPI::initialize()
{ {
// init API, REQUIRED! // init API, REQUIRED!
API::init(); API::init();
// Initialise jsonCB with current instance
_jsonCB->setSubscriptionsTo(_hyperion);
// setup auth interface // setup auth interface
connect(this, &API::onPendingTokenRequest, this, &JsonAPI::newPendingTokenRequest); connect(this, &API::onPendingTokenRequest, this, &JsonAPI::newPendingTokenRequest);
@ -129,6 +131,8 @@ void JsonAPI::handleMessage(const QString &messageString, const QString &httpAut
{ {
const QString ident = "JsonRpc@" + _peerAddress; const QString ident = "JsonRpc@" + _peerAddress;
QJsonObject message; QJsonObject message;
//std::cout << "JsonAPI::handleMessage | [" << static_cast<int>(_hyperion->getInstanceIndex()) << "] Received: ["<< messageString.toStdString() << "]" << std::endl;
// parse the message // parse the message
if (!JsonUtils::parse(ident, messageString, message, _log)) if (!JsonUtils::parse(ident, messageString, message, _log))
{ {

View File

@ -73,7 +73,7 @@ bool JsonCB::subscribeFor(const QString& type, bool unsubscribe)
if(type == "priorities-update") if(type == "priorities-update")
{ {
if (unsubscribe) if (unsubscribe)
disconnect(_prioMuxer,0 ,0 ,0); disconnect(_prioMuxer, &PriorityMuxer::prioritiesChanged, this, &JsonCB::handlePriorityUpdate);
else else
connect(_prioMuxer, &PriorityMuxer::prioritiesChanged, this, &JsonCB::handlePriorityUpdate, Qt::UniqueConnection); connect(_prioMuxer, &PriorityMuxer::prioritiesChanged, this, &JsonCB::handlePriorityUpdate, Qt::UniqueConnection);
} }
@ -156,6 +156,8 @@ void JsonCB::resetSubscriptions()
void JsonCB::setSubscriptionsTo(Hyperion* hyperion) void JsonCB::setSubscriptionsTo(Hyperion* hyperion)
{ {
//std::cout << "JsonCB::setSubscriptions for instance [" << static_cast<int>(hyperion->getInstanceIndex()) << "] " << std::endl;
// get current subs // get current subs
QStringList currSubs(getSubscribedCommands()); QStringList currSubs(getSubscribedCommands());
@ -179,11 +181,13 @@ void JsonCB::doCallback(const QString& cmd, const QVariant& data)
QJsonObject obj; QJsonObject obj;
obj["command"] = cmd; obj["command"] = cmd;
if(static_cast<QMetaType::Type>(data.type()) == QMetaType::QJsonArray) if(data.userType() == QMetaType::QJsonArray)
obj["data"] = data.toJsonArray(); obj["data"] = data.toJsonArray();
else else
obj["data"] = data.toJsonObject(); obj["data"] = data.toJsonObject();
//std::cout << "JsonCB::doCallback | [" << static_cast<int>(_hyperion->getInstanceIndex()) << "] Send: [" << QJsonDocument(obj).toJson(QJsonDocument::Compact).toStdString() << "]" << std::endl;
emit newCallback(obj); emit newCallback(obj);
} }

View File

@ -57,3 +57,8 @@ void JsonClientConnection::disconnected()
{ {
emit connectionClosed(); emit connectionClosed();
} }
QHostAddress JsonClientConnection::getClientAddress()
{
return _socket->peerAddress();
}

View File

@ -4,6 +4,7 @@
#include <QString> #include <QString>
#include <QByteArray> #include <QByteArray>
#include <QJsonObject> #include <QJsonObject>
#include <QHostAddress>
// util includes // util includes
#include <utils/Logger.h> #include <utils/Logger.h>
@ -24,6 +25,7 @@ public:
/// @param socket The Socket object for this connection /// @param socket The Socket object for this connection
/// ///
JsonClientConnection(QTcpSocket * socket, bool localConnection); JsonClientConnection(QTcpSocket * socket, bool localConnection);
QHostAddress getClientAddress();
signals: signals:
void connectionClosed(); void connectionClosed();

View File

@ -102,7 +102,7 @@ void JsonServer::newConnection()
{ {
if(_netOrigin->accessAllowed(socket->peerAddress(), socket->localAddress())) if(_netOrigin->accessAllowed(socket->peerAddress(), socket->localAddress()))
{ {
Debug(_log, "New connection from: %s ",socket->localAddress().toString().toStdString().c_str()); Debug(_log, "New connection from: %s",QSTRING_CSTR(socket->peerAddress().toString()));
JsonClientConnection * connection = new JsonClientConnection(socket, _netOrigin->isLocalAddress(socket->peerAddress(), socket->localAddress())); JsonClientConnection * connection = new JsonClientConnection(socket, _netOrigin->isLocalAddress(socket->peerAddress(), socket->localAddress()));
_openConnections.insert(connection); _openConnections.insert(connection);
@ -118,7 +118,7 @@ void JsonServer::newConnection()
void JsonServer::closedConnection() void JsonServer::closedConnection()
{ {
JsonClientConnection* connection = qobject_cast<JsonClientConnection*>(sender()); JsonClientConnection* connection = qobject_cast<JsonClientConnection*>(sender());
Debug(_log, "Connection closed"); Debug(_log, "Connection closed for %s", QSTRING_CSTR(connection->getClientAddress().toString()));
_openConnections.remove(connection); _openConnections.remove(connection);
// schedule to delete the connection object // schedule to delete the connection object

View File

@ -212,7 +212,7 @@ void WebSocketClient::getWsFrameHeader(WebSocketHeader* header)
/// See http://tools.ietf.org/html/rfc6455#section-5.2 for more information /// See http://tools.ietf.org/html/rfc6455#section-5.2 for more information
void WebSocketClient::sendClose(int status, QString reason) void WebSocketClient::sendClose(int status, QString reason)
{ {
Debug(_log, "send close: %d %s", status, QSTRING_CSTR(reason)); Debug(_log, "Send close to %s: %d %s", QSTRING_CSTR(_socket->peerAddress().toString()), status, QSTRING_CSTR(reason));
ErrorIf(!reason.isEmpty(), _log, QSTRING_CSTR(reason)); ErrorIf(!reason.isEmpty(), _log, QSTRING_CSTR(reason));
_receiveBuffer.clear(); _receiveBuffer.clear();
QByteArray sendBuffer; QByteArray sendBuffer;