Make monitor work with large json data
parent
150f1fef3f
commit
b21759b3bf
|
|
@ -64,7 +64,6 @@ TunnelProxyHandler::TunnelProxyHandler(QObject *parent) : JsonHandler(parent)
|
|||
returns.insert("timestamp", JsonTypes::basicTypeToString(JsonTypes::UInt));
|
||||
setReturns("Ping", returns);
|
||||
|
||||
|
||||
// Client
|
||||
params.clear(); returns.clear();
|
||||
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)
|
||||
{
|
||||
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);
|
||||
clientConnection->write(data);
|
||||
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)));
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
qDebug() << "Monitor connected to" << m_serverName;
|
||||
|
|
@ -56,23 +74,20 @@ void MonitorClient::onDisconnected()
|
|||
|
||||
void MonitorClient::onReadyRead()
|
||||
{
|
||||
// Note: the server sends the data compact with '\n' at the end
|
||||
QByteArray data = m_socket->readAll();
|
||||
|
||||
QJsonParseError error;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
||||
|
||||
if(error.error != QJsonParseError::NoError) {
|
||||
qWarning() << "Failed to parse JSON data" << data << ":" << error.errorString();
|
||||
int index = data.indexOf("}\n");
|
||||
if (index < 0) {
|
||||
// Append the entire data and continue
|
||||
m_dataBuffer.append(data);
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -43,6 +43,9 @@ private:
|
|||
QString m_serverName;
|
||||
QLocalSocket *m_socket = nullptr;
|
||||
bool m_jsonMode = false;
|
||||
QByteArray m_dataBuffer;
|
||||
|
||||
void processBufferData();
|
||||
|
||||
signals:
|
||||
void connected();
|
||||
|
|
|
|||
Loading…
Reference in New Issue