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!
API::init();
// Initialise jsonCB with current instance
_jsonCB->setSubscriptionsTo(_hyperion);
// setup auth interface
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;
QJsonObject message;
//std::cout << "JsonAPI::handleMessage | [" << static_cast<int>(_hyperion->getInstanceIndex()) << "] Received: ["<< messageString.toStdString() << "]" << std::endl;
// parse the message
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 (unsubscribe)
disconnect(_prioMuxer,0 ,0 ,0);
disconnect(_prioMuxer, &PriorityMuxer::prioritiesChanged, this, &JsonCB::handlePriorityUpdate);
else
connect(_prioMuxer, &PriorityMuxer::prioritiesChanged, this, &JsonCB::handlePriorityUpdate, Qt::UniqueConnection);
}
@ -156,6 +156,8 @@ void JsonCB::resetSubscriptions()
void JsonCB::setSubscriptionsTo(Hyperion* hyperion)
{
//std::cout << "JsonCB::setSubscriptions for instance [" << static_cast<int>(hyperion->getInstanceIndex()) << "] " << std::endl;
// get current subs
QStringList currSubs(getSubscribedCommands());
@ -179,11 +181,13 @@ void JsonCB::doCallback(const QString& cmd, const QVariant& data)
QJsonObject obj;
obj["command"] = cmd;
if(static_cast<QMetaType::Type>(data.type()) == QMetaType::QJsonArray)
if(data.userType() == QMetaType::QJsonArray)
obj["data"] = data.toJsonArray();
else
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);
}

View File

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

View File

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

View File

@ -102,7 +102,7 @@ void JsonServer::newConnection()
{
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()));
_openConnections.insert(connection);
@ -118,7 +118,7 @@ void JsonServer::newConnection()
void JsonServer::closedConnection()
{
JsonClientConnection* connection = qobject_cast<JsonClientConnection*>(sender());
Debug(_log, "Connection closed");
Debug(_log, "Connection closed for %s", QSTRING_CSTR(connection->getClientAddress().toString()));
_openConnections.remove(connection);
// 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
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));
_receiveBuffer.clear();
QByteArray sendBuffer;