From db16e45e764bab36bfb6dd57b983ab381097de45 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 7 May 2017 00:56:12 +0800 Subject: [PATCH] [Browser] Add PingUpdater & PingHelper classes for detached ping --- apps/browser/CMakeLists.txt | 5 ++++ apps/browser/PingHelper.cpp | 58 ++++++++++++++++++++++++++++++++++++ apps/browser/PingHelper.hpp | 42 ++++++++++++++++++++++++++ apps/browser/PingUpdater.cpp | 50 +++++++++++++++++++++++++++++++ apps/browser/PingUpdater.hpp | 31 +++++++++++++++++++ apps/browser/Types.hpp | 15 ++++++++++ 6 files changed, 201 insertions(+) create mode 100644 apps/browser/PingHelper.cpp create mode 100644 apps/browser/PingHelper.hpp create mode 100644 apps/browser/PingUpdater.cpp create mode 100644 apps/browser/PingUpdater.hpp create mode 100644 apps/browser/Types.hpp diff --git a/apps/browser/CMakeLists.txt b/apps/browser/CMakeLists.txt index a605652c0..52bf0adf2 100644 --- a/apps/browser/CMakeLists.txt +++ b/apps/browser/CMakeLists.txt @@ -14,6 +14,8 @@ set(BROWSER netutils/HTTPNetwork.cpp netutils/Utils.cpp netutils/QueryClient.cpp + PingUpdater.cpp + PingHelper.cpp ${CMAKE_SOURCE_DIR}/files/tes3mp/browser.rc ) @@ -22,6 +24,8 @@ set(BROWSER_HEADER_MOC ServerModel.hpp ServerInfoDialog.hpp MySortFilterProxyModel.hpp + PingUpdater.hpp + PingHelper.hpp ) set(BROWSER_HEADER @@ -29,6 +33,7 @@ set(BROWSER_HEADER netutils/HTTPNetwork.hpp netutils/Utils.hpp netutils/QueryClient.hpp + Types.hpp ) source_group(browser FILES ${BROWSER} ${BROWSER_HEADER}) diff --git a/apps/browser/PingHelper.cpp b/apps/browser/PingHelper.cpp new file mode 100644 index 000000000..543c1128b --- /dev/null +++ b/apps/browser/PingHelper.cpp @@ -0,0 +1,58 @@ +// +// Created by koncord on 03.05.17. +// + +#include "PingHelper.hpp" +#include "ServerModel.hpp" +#include +#include "PingUpdater.hpp" + +void PingHelper::Add(int row, AddrPair addrPair) +{ + pingUpdater->addServer(row, addrPair); + if(!pingThread->isRunning()) + pingThread->start(); +} + +void PingHelper::Stop() +{ + emit pingUpdater->stop(); +} + +void PingHelper::SetModel(QAbstractTableModel *model) +{ + this->model = model; +} + +void PingHelper::update(int row, unsigned ping) +{ + model->setData(model->index(row, ServerData::PING), ping); +} + +PingHelper &PingHelper::Get() +{ + static PingHelper helper; + return helper; +} + +PingHelper::PingHelper() +{ + QObject(); + pingThread = new QThread; + pingUpdater = new PingUpdater; + pingUpdater->moveToThread(pingThread); + + connect(pingThread, SIGNAL(started()), pingUpdater, SLOT(process())); + connect(pingUpdater, SIGNAL(start()), pingThread, SLOT(start())); + connect(pingUpdater, SIGNAL(finished()), pingThread, SLOT(quit())); + connect(this, SIGNAL(stop()), pingUpdater, SLOT(stop())); + //connect(pingUpdater, SIGNAL(finished()), pingUpdater, SLOT(deleteLater())); + connect(pingUpdater, SIGNAL(updateModel(int, unsigned)), this, SLOT(update(int, unsigned))); + + +} + +PingHelper::~PingHelper() +{ + +} diff --git a/apps/browser/PingHelper.hpp b/apps/browser/PingHelper.hpp new file mode 100644 index 000000000..bf7d438f8 --- /dev/null +++ b/apps/browser/PingHelper.hpp @@ -0,0 +1,42 @@ +// +// Created by koncord on 03.05.17. +// + +#ifndef OPENMW_PINGHELPER_HPP +#define OPENMW_PINGHELPER_HPP + +#include +#include +#include +#include "Types.hpp" + +class PingUpdater; + +class PingHelper : public QObject +{ + Q_OBJECT +public: + + void Add(int row, AddrPair addrPair); + void Stop(); + void SetModel(QAbstractTableModel *model); + //void UpdateImmedialy(PingUpdater::AddrPair addrPair); + static PingHelper &Get(); +private: + PingHelper(); + ~PingHelper(); + + PingHelper(const PingHelper&) = delete; + PingHelper& operator=(const PingHelper&) = delete; +signals: + void stop(); +public slots: + void update(int row, unsigned ping); +private: + QThread *pingThread; + PingUpdater *pingUpdater; + QAbstractTableModel *model; +}; + + +#endif //OPENMW_PINGHELPER_HPP diff --git a/apps/browser/PingUpdater.cpp b/apps/browser/PingUpdater.cpp new file mode 100644 index 000000000..38389f59e --- /dev/null +++ b/apps/browser/PingUpdater.cpp @@ -0,0 +1,50 @@ +// +// Created by koncord on 02.05.17. +// + +#include "PingUpdater.hpp" +#include "netutils/Utils.hpp" +#include +#include +#include + +void PingUpdater::stop() +{ + servers.clear(); + run = false; +} + +void PingUpdater::addServer(int row, AddrPair addr) +{ + servers.push_back({row, addr}); + run = true; + emit start(); +} + +void PingUpdater::process() +{ + while(run) + { + if(servers.count() == 0) + { + QThread::msleep(1000); + if(servers.count() == 0) + { + qDebug() << "PingUpdater stopped due to inactivity"; + run = false; + continue; + } + } + + ServerRow server = servers.back(); + servers.pop_back(); + + unsigned ping = PingRakNetServer(server.second.first.toLatin1(), server.second.second); + + qDebug() << "Pong from" << server.second.first + "|" + QString::number(server.second.second) + << ":" << ping << "ms"; + + emit updateModel(server.first, ping); + } + emit finished(); +} diff --git a/apps/browser/PingUpdater.hpp b/apps/browser/PingUpdater.hpp new file mode 100644 index 000000000..8dd1111e3 --- /dev/null +++ b/apps/browser/PingUpdater.hpp @@ -0,0 +1,31 @@ +// +// Created by koncord on 02.05.17. +// + +#ifndef OPENMW_PINGUPDATER_HPP +#define OPENMW_PINGUPDATER_HPP + +#include +#include + +#include "Types.hpp" + +class PingUpdater : public QObject +{ + Q_OBJECT +public: + void addServer(int row, AddrPair addrPair); +public slots: + void stop(); + void process(); +signals: + void start(); + void updateModel(int row, unsigned ping); + void finished(); +private: + QVector servers; + bool run; +}; + + +#endif //OPENMW_PINGUPDATER_HPP diff --git a/apps/browser/Types.hpp b/apps/browser/Types.hpp new file mode 100644 index 000000000..b9d19f272 --- /dev/null +++ b/apps/browser/Types.hpp @@ -0,0 +1,15 @@ +// +// Created by koncord on 07.05.17. +// + +#ifndef OPENMW_TYPES_HPP +#define OPENMW_TYPES_HPP + +#include +#include + +typedef QPair AddrPair; +typedef QPair ServerRow; + + +#endif //OPENMW_TYPES_HPP