Share the ETCP monitor across desktop entry points

This commit is contained in:
evgeny committed 2026-10-27 02:49:39 +01:00
1 parent e9676e861e
commit 6beea150a5
8 files changed
+131 -50

No files matched your search

+44 -14
View File
@@ -16,6 +16,8 @@ extern "C" {
#include <QScrollBar>
#include <QHash>
#include <QApplication>
#include <QCloseEvent>
#include <QSignalBlocker>
#include <cstring>
#define CONN_LIST_ENTRY_SIZE 46
@@ -30,8 +32,18 @@ static void connMetricsCb(const uint8_t* data, int len) {
if (g_activeConnMonitor) g_activeConnMonitor->onConnMetrics(data, len);
}
ConnMonitorWindow::ConnMonitorWindow(QWidget* parent)
: QDialog(parent)
/* Один получатель bridge callbacks для всех меню/настроек, без указателей-владельцев в этих окнах. */
ConnMonitorWindow* ConnMonitorWindow::showMonitor() {
auto* window = g_activeConnMonitor;
if (!window) window = new ConnMonitorWindow;
if (window->isMinimized()) window->showNormal(); else window->show();
window->raise(); window->activateWindow();
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "connection monitor shown: window=%p", (void*)window);
return window;
}
ConnMonitorWindow::ConnMonitorWindow()
: QDialog(nullptr)
{
setWindowTitle(QCoreApplication::translate("ChatGui", "Connection Monitor"));
resize(900, 550);
@@ -89,7 +101,11 @@ ConnMonitorWindow::ConnMonitorWindow(QWidget* parent)
ConnMonitorWindow::~ConnMonitorWindow() {
m_timer->stop();
if (g_activeConnMonitor == this) g_activeConnMonitor = nullptr;
if (g_activeConnMonitor == this) {
g_activeConnMonitor = nullptr;
gui_bridge_set_conn_list_cb(nullptr); gui_bridge_set_conn_metrics_cb(nullptr);
}
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "connection monitor destroyed: window=%p", (void*)this);
}
void ConnMonitorWindow::showEvent(QShowEvent* event) {
@@ -102,6 +118,16 @@ void ConnMonitorWindow::hideEvent(QHideEvent* event) {
QDialog::hideEvent(event);
}
/* Снять singleton сразу при принятом close, не дожидаясь Qt::DeferredDelete. */
void ConnMonitorWindow::closeEvent(QCloseEvent* event) {
QDialog::closeEvent(event);
if (event->isAccepted() && g_activeConnMonitor == this) {
g_activeConnMonitor = nullptr;
gui_bridge_set_conn_list_cb(nullptr); gui_bridge_set_conn_metrics_cb(nullptr);
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "connection monitor closed: window=%p", (void*)this);
}
}
void ConnMonitorWindow::onTimerTick() {
if (m_details->textCursor().hasSelection()) return;
if (QApplication::mouseButtons()) return;
@@ -114,6 +140,7 @@ void ConnMonitorWindow::onConnSelected(QListWidgetItem* current, QListWidgetItem
(void)previous;
if (!current) { m_selectedNodeId = 0; m_details->clear(); return; }
m_selectedNodeId = (uint64_t)current->data(Qt::UserRole).toULongLong();
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "connection monitor selection: node=%016llx", (unsigned long long)m_selectedNodeId);
requestMetricsForSelected();
}
@@ -128,8 +155,11 @@ void ConnMonitorWindow::requestMetricsForSelected() {
}
void ConnMonitorWindow::onConnList(const uint8_t* data, int len) {
if (len < 2) return;
if (!data || len < 2) { DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "connection monitor: invalid list header bytes=%d", len); return; }
uint16_t count; memcpy(&count, data, 2);
if (count > 250 || len != 2 + count * CONN_LIST_ENTRY_SIZE) {
DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "connection monitor: invalid list count=%u bytes=%d", count, len); return;
}
const uint8_t* p = data + 2;
QHash<uint64_t, QListWidgetItem*> existing;
@@ -138,9 +168,9 @@ void ConnMonitorWindow::onConnList(const uint8_t* data, int len) {
existing[(uint64_t)item->data(Qt::UserRole).toULongLong()] = item;
}
m_connList->blockSignals(true);
QSignalBlocker blocker(m_connList);
for (uint16_t i = 0; i < count && (p + CONN_LIST_ENTRY_SIZE) <= (data + len); i++) {
for (uint16_t i = 0; i < count; i++) {
uint64_t nodeId; memcpy(&nodeId, p, 8); p += 8;
uint8_t flags = *p++;
uint8_t linkCount = *p++;
@@ -162,7 +192,7 @@ void ConnMonitorWindow::onConnList(const uint8_t* data, int len) {
else if (proxyClient) proxyRole = " C";
else if (proxyServer) proxyRole = " S";
QString nodeHex = QString("%1").arg(nodeId, 16, 16, QChar('0')).right(16).left(4);
QString nodeHex = QString("%1").arg(nodeId, 16, 16, QChar('0')).right(4);
QString rttStr = rtt > 0 ? QString::number(rtt / 10.0f, 'f', 1) : "-";
QString text = QString("[%1] %2 %3%4 %5L rtt=%6ms")
.arg(nodeHex)
@@ -188,16 +218,16 @@ void ConnMonitorWindow::onConnList(const uint8_t* data, int len) {
for (auto it = existing.begin(); it != existing.end(); ++it)
delete *it;
m_connList->blockSignals(false);
if (!m_selectedNodeId && m_connList->count() > 0) {
m_connList->setCurrentRow(0);
onConnSelected(m_connList->item(0), nullptr);
}
if (!m_connList->currentItem() && m_connList->count() > 0) m_connList->setCurrentRow(0);
auto* current = m_connList->currentItem();
const uint64_t selected = current ? current->data(Qt::UserRole).toULongLong() : 0;
blocker.unblock();
if (selected != m_selectedNodeId) onConnSelected(current, nullptr);
}
void ConnMonitorWindow::onConnMetrics(const uint8_t* data, int len) {
if (len <= 0) return;
if (!data || len <= 0) { DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "connection monitor: invalid metrics bytes=%d", len); return; }
if (!m_selectedNodeId) return;
int scrollVal = m_details->verticalScrollBar()->value();
m_details->setPlainText(QString::fromUtf8((const char*)data, len));
m_details->verticalScrollBar()->setValue(scrollVal);