From 72d88ac4e826723e8d5fd76b7de7ad5362821ccf Mon Sep 17 00:00:00 2001 From: R1tschY Date: Sun, 8 Sep 2019 19:01:39 +0200 Subject: [PATCH] Wake up app every 30s to keep TCP connections alive --- app/app.pro | 3 +- app/src/appdaemon.cpp | 56 ++++++++++++++++++++++++++ app/src/appdaemon.h | 8 ++++ lib/sailfishconnect/helper/cpphelper.h | 6 +++ rpm/harbour-sailfishconnect.spec | 1 + rpm/harbour-sailfishconnect.yaml | 1 + 6 files changed, 74 insertions(+), 1 deletion(-) diff --git a/app/app.pro b/app/app.pro index 9a109cb..727bee4 100644 --- a/app/app.pro +++ b/app/app.pro @@ -15,7 +15,8 @@ TARGET = harbour-sailfishconnect CONFIG += sailfishapp c++14 QT += network dbus feedback -PKGCONFIG += sailfishapp dbus-1 contextkit-statefs nemonotifications-qt5 +PKGCONFIG += \ + sailfishapp dbus-1 contextkit-statefs nemonotifications-qt5 keepalive DEFINES += \ QT_STATICPLUGIN \ QT_DEPRECATED_WARNINGS \ diff --git a/app/src/appdaemon.cpp b/app/src/appdaemon.cpp index b10a87a..b50e60e 100644 --- a/app/src/appdaemon.cpp +++ b/app/src/appdaemon.cpp @@ -74,6 +74,18 @@ AppDaemon::AppDaemon(QObject *parent) { notification_.setAppName(PRETTY_PACKAGE_NAME); notification_.setCategory("device"); + + connect(this, &Daemon::deviceAdded, + this, &AppDaemon::onDeviceAdded); + + connect( + &m_backgroundActivity, &BackgroundActivity::running, + this, &AppDaemon::onWakeUp); + m_backgroundActivity.setWakeupFrequency(BackgroundActivity::ThirtySeconds); + + for (auto* device : asConst(devicesList())) { + onDeviceAdded(device->id()); + } } void AppDaemon::askPairingConfirmation(Device *device) @@ -125,6 +137,50 @@ void AppDaemon::setQmlEngine(QQmlEngine *qmlEngine) } } +void AppDaemon::onDeviceAdded(const QString& id) +{ + Device* device = getDevice(id); + if (device == nullptr) + return; + + if (device->isTrusted() && device->isReachable()) { + m_connectedDevices.insert(device->id()); + } + + connect(device, &Device::reachableChanged, + this, &AppDaemon::onDeviceMayConnectedChanged); + connect(device, &Device::trustedChanged, + this, &AppDaemon::onDeviceMayConnectedChanged); +} + +void AppDaemon::onDeviceMayConnectedChanged() +{ + auto device = qobject_cast(sender()); + if (device->isTrusted() && device->isReachable()) { + m_connectedDevices.insert(device->id()); + } else { + m_connectedDevices.remove(device->id()); + } + + qCDebug(logger) + << "Device changed, got" + << m_connectedDevices.size() << "connected devices"; + m_backgroundActivity.setState( + m_connectedDevices.size() + ? BackgroundActivity::Waiting : BackgroundActivity::Stopped); +} + +void AppDaemon::onWakeUp() +{ +// qCDebug(logger) +// << QDateTime::currentDateTime().toString() << "Received wakeup, got" +// << m_connectedDevices.size() << "connected devices"; + + // immediately to go sleep, hope that is sufficient to keep connections + // alive + m_backgroundActivity.wait(); +} + AppDaemon *AppDaemon::instance() { return static_cast(Daemon::instance()); diff --git a/app/src/appdaemon.h b/app/src/appdaemon.h index eaf95bb..b146db3 100644 --- a/app/src/appdaemon.h +++ b/app/src/appdaemon.h @@ -20,8 +20,10 @@ #include +#include #include #include +#include class QQmlEngine; class QQmlImageProviderBase; @@ -47,8 +49,14 @@ class AppDaemon : public Daemon private: QEventLoopLocker m_eventLoopLock; + QSet m_connectedDevices; + BackgroundActivity m_backgroundActivity; Notification notification_; QQmlEngine* m_qmlEngine = nullptr; + + void onDeviceAdded(const QString &id); + void onDeviceMayConnectedChanged(); + void onWakeUp(); }; } // namespace SailfishConnect diff --git a/lib/sailfishconnect/helper/cpphelper.h b/lib/sailfishconnect/helper/cpphelper.h index eaa545a..6511b8c 100644 --- a/lib/sailfishconnect/helper/cpphelper.h +++ b/lib/sailfishconnect/helper/cpphelper.h @@ -23,6 +23,12 @@ namespace SailfishConnect { +template +constexpr const T& asConst(const T& t) noexcept +{ + return t; +} + template constexpr typename std::add_const::type& asConst(T& t) noexcept { diff --git a/rpm/harbour-sailfishconnect.spec b/rpm/harbour-sailfishconnect.spec index 5a05d4e..39bb970 100644 --- a/rpm/harbour-sailfishconnect.spec +++ b/rpm/harbour-sailfishconnect.spec @@ -30,6 +30,7 @@ BuildRequires: pkgconfig(Qt5Quick) BuildRequires: pkgconfig(Qt5Test) BuildRequires: pkgconfig(contextkit-statefs) BuildRequires: pkgconfig(nemonotifications-qt5) +BuildRequires: pkgconfig(keepalive) BuildRequires: cmake BuildRequires: python3-devel BuildRequires: desktop-file-utils diff --git a/rpm/harbour-sailfishconnect.yaml b/rpm/harbour-sailfishconnect.yaml index 647962d..3b3874f 100644 --- a/rpm/harbour-sailfishconnect.yaml +++ b/rpm/harbour-sailfishconnect.yaml @@ -30,6 +30,7 @@ PkgConfigBR: - Qt5Test - contextkit-statefs - nemonotifications-qt5 + - keepalive # Build dependencies without a pkgconfig setup can be listed here PkgBR: