Skip to content

Commit

Permalink
Wake up app every 30s to keep TCP connections alive
Browse files Browse the repository at this point in the history
  • Loading branch information
R1tschY committed Sep 8, 2019
1 parent 9ee1100 commit 72d88ac
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/app.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
56 changes: 56 additions & 0 deletions app/src/appdaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Device*>(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<AppDaemon*>(Daemon::instance());
Expand Down
8 changes: 8 additions & 0 deletions app/src/appdaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

#include <sailfishconnect/daemon.h>

#include <QSet>
#include <QEventLoopLocker>
#include <notification.h>
#include <backgroundactivity.h>

class QQmlEngine;
class QQmlImageProviderBase;
Expand All @@ -47,8 +49,14 @@ class AppDaemon : public Daemon

private:
QEventLoopLocker m_eventLoopLock;
QSet<QString> m_connectedDevices;
BackgroundActivity m_backgroundActivity;
Notification notification_;
QQmlEngine* m_qmlEngine = nullptr;

void onDeviceAdded(const QString &id);
void onDeviceMayConnectedChanged();
void onWakeUp();
};

} // namespace SailfishConnect
Expand Down
6 changes: 6 additions & 0 deletions lib/sailfishconnect/helper/cpphelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

namespace SailfishConnect {

template<typename T>
constexpr const T& asConst(const T& t) noexcept
{
return t;
}

template<typename T>
constexpr typename std::add_const<T>::type& asConst(T& t) noexcept
{
Expand Down
1 change: 1 addition & 0 deletions rpm/harbour-sailfishconnect.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rpm/harbour-sailfishconnect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PkgConfigBR:
- Qt5Test
- contextkit-statefs
- nemonotifications-qt5
- keepalive

# Build dependencies without a pkgconfig setup can be listed here
PkgBR:
Expand Down

0 comments on commit 72d88ac

Please sign in to comment.