Make monitor work with large json data
This commit is contained in:
parent
150f1fef3f
commit
b21759b3bf
@ -64,7 +64,6 @@ TunnelProxyHandler::TunnelProxyHandler(QObject *parent) : JsonHandler(parent)
|
|||||||
returns.insert("timestamp", JsonTypes::basicTypeToString(JsonTypes::UInt));
|
returns.insert("timestamp", JsonTypes::basicTypeToString(JsonTypes::UInt));
|
||||||
setReturns("Ping", returns);
|
setReturns("Ping", returns);
|
||||||
|
|
||||||
|
|
||||||
// Client
|
// Client
|
||||||
params.clear(); returns.clear();
|
params.clear(); returns.clear();
|
||||||
setDescription("RegisterClient", "Register a new TunnelProxy client on TunnelProxy server with the given serverUuid. "
|
setDescription("RegisterClient", "Register a new TunnelProxy client on TunnelProxy server with the given serverUuid. "
|
||||||
|
|||||||
@ -56,7 +56,7 @@ bool MonitorServer::running() const
|
|||||||
|
|
||||||
void MonitorServer::sendMonitorData(QLocalSocket *clientConnection, const QVariantMap &dataMap)
|
void MonitorServer::sendMonitorData(QLocalSocket *clientConnection, const QVariantMap &dataMap)
|
||||||
{
|
{
|
||||||
QByteArray data = QJsonDocument::fromVariant(dataMap).toJson(QJsonDocument::Indented) + '\n';
|
QByteArray data = QJsonDocument::fromVariant(dataMap).toJson(QJsonDocument::Compact) + '\n';
|
||||||
qCDebug(dcMonitorServer()) << "Sending monitor data" << qUtf8Printable(data);
|
qCDebug(dcMonitorServer()) << "Sending monitor data" << qUtf8Printable(data);
|
||||||
clientConnection->write(data);
|
clientConnection->write(data);
|
||||||
clientConnection->flush();
|
clientConnection->flush();
|
||||||
|
|||||||
@ -42,6 +42,24 @@ MonitorClient::MonitorClient(const QString &serverName, bool jsonMode, QObject *
|
|||||||
connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(onErrorOccurred(QLocalSocket::LocalSocketError)));
|
connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(onErrorOccurred(QLocalSocket::LocalSocketError)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MonitorClient::processBufferData()
|
||||||
|
{
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(m_dataBuffer, &error);
|
||||||
|
if(error.error != QJsonParseError::NoError) {
|
||||||
|
qWarning() << "Failed to parse JSON data:" << error.errorString();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_jsonMode) {
|
||||||
|
qDebug() << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
||||||
|
emit dataReady(dataMap);
|
||||||
|
}
|
||||||
|
|
||||||
void MonitorClient::onConnected()
|
void MonitorClient::onConnected()
|
||||||
{
|
{
|
||||||
qDebug() << "Monitor connected to" << m_serverName;
|
qDebug() << "Monitor connected to" << m_serverName;
|
||||||
@ -56,23 +74,20 @@ void MonitorClient::onDisconnected()
|
|||||||
|
|
||||||
void MonitorClient::onReadyRead()
|
void MonitorClient::onReadyRead()
|
||||||
{
|
{
|
||||||
|
// Note: the server sends the data compact with '\n' at the end
|
||||||
QByteArray data = m_socket->readAll();
|
QByteArray data = m_socket->readAll();
|
||||||
|
|
||||||
QJsonParseError error;
|
int index = data.indexOf("}\n");
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
if (index < 0) {
|
||||||
|
// Append the entire data and continue
|
||||||
if(error.error != QJsonParseError::NoError) {
|
m_dataBuffer.append(data);
|
||||||
qWarning() << "Failed to parse JSON data" << data << ":" << error.errorString();
|
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
m_dataBuffer.append(data.left(index + 1));
|
||||||
|
processBufferData();
|
||||||
|
m_dataBuffer.clear();
|
||||||
|
m_dataBuffer.append(data.right(data.length() - index - 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_jsonMode) {
|
|
||||||
qDebug() << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
|
||||||
emit dataReady(dataMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MonitorClient::onErrorOccurred(QLocalSocket::LocalSocketError socketError)
|
void MonitorClient::onErrorOccurred(QLocalSocket::LocalSocketError socketError)
|
||||||
|
|||||||
@ -43,6 +43,9 @@ private:
|
|||||||
QString m_serverName;
|
QString m_serverName;
|
||||||
QLocalSocket *m_socket = nullptr;
|
QLocalSocket *m_socket = nullptr;
|
||||||
bool m_jsonMode = false;
|
bool m_jsonMode = false;
|
||||||
|
QByteArray m_dataBuffer;
|
||||||
|
|
||||||
|
void processBufferData();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void connected();
|
void connected();
|
||||||
|
|||||||
Reference in New Issue
Block a user