cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
abstractfork.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2018 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#include "abstractfork.h"
6
7#include <iostream>
8
9#include <QFileSystemWatcher>
10#include <QLoggingCategory>
11#include <QTimer>
12
13Q_LOGGING_CATEGORY(C_SERVER_FORK, "cutelyst.server.fork", QtWarningMsg)
14
15AbstractFork::AbstractFork(QObject *parent)
16 : QObject(parent)
17{
18}
19
20void AbstractFork::setTouchReload(const QStringList &paths)
21{
22 m_touchReloadPaths = paths;
23}
24
25void AbstractFork::installTouchReload()
26{
27 if (!m_touchReloadPaths.isEmpty() && !m_touchReloadWatcher) {
28 m_touchReloadWatcher = new QFileSystemWatcher(this);
29 connect(m_touchReloadWatcher,
31 this,
32 &AbstractFork::fileChanged);
33 connect(m_touchReloadWatcher,
35 this,
36 &AbstractFork::directoryChanged);
37 const QStringList ret = m_touchReloadWatcher->addPaths(m_touchReloadPaths);
38 if (!ret.empty()) {
39 std::cerr << "Failed setup file watcher" << std::endl;
40 qCCritical(C_SERVER_FORK) << "unwatched files" << ret;
41 exit(1);
42 }
43
44 m_restartTimer = new QTimer(this);
45 connect(m_restartTimer, &QTimer::timeout, this, &AbstractFork::restart);
46 m_restartTimer->setInterval(std::chrono::seconds{1});
47 m_restartTimer->setSingleShot(true);
48 }
49}
50
51void AbstractFork::removeTouchReload()
52{
53 delete m_touchReloadWatcher;
54 m_touchReloadWatcher = nullptr;
55
56 delete m_restartTimer;
57}
58
59void AbstractFork::fileChanged(const QString &path)
60{
61 std::cout << "File changed restarting... " << qPrintable(path) << std::endl;
62 m_restartTimer->start();
63}
64
65void AbstractFork::directoryChanged(const QString &path)
66{
67 std::cout << "Directory changed restarting... " << qPrintable(path) << std::endl;
68 m_restartTimer->start();
69}
70
71#include "moc_abstractfork.cpp"
virtual void restart()=0
QStringList addPaths(const QStringList &paths)
void directoryChanged(const QString &path)
void fileChanged(const QString &path)
bool empty() const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setInterval(int msec)
void setSingleShot(bool singleShot)
void start()
void timeout()