diff --git a/debian/guhd.1 b/debian/guhd.1 index 5e6ca2da..d3506fe4 100644 --- a/debian/guhd.1 +++ b/debian/guhd.1 @@ -1,6 +1,6 @@ .\" Manpage for guhd. .\" Contact simon.stuerz@guh.guru to correct errors or typos. -.TH man 1 "December 2015" "1.3" "guhd man page" +.TH man 1 "January 2016" "1.4" "guhd man page" .SH NAME guhd \- An open source IoT (Internet of Things) server .SH SYNOPSIS @@ -102,8 +102,9 @@ How to report bugs: .SH AUTHOR Written by Simon Stürz and Michael Zanetti .SH COPYRIGHT -Copyright \(co 2013-2015 ARGE guh. +Copyright \(co 2014-2016 guh GmbH. .br + License GPLv2: GNU GPL version 2 . .br This is free software: you are free to change and redistribute it. diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 13c2e7cf..27718cab 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -238,7 +238,7 @@ DeviceManager::DeviceManager(QObject *parent) : /*! Destructor of the DeviceManager. Each loaded \l{DevicePlugin} will be deleted. */ DeviceManager::~DeviceManager() { - qCDebug(dcDeviceManager) << "Shutting down DeviceManager"; + qCDebug(dcApplication) << "Shutting down device manager"; foreach (DevicePlugin *plugin, m_devicePlugins) { delete plugin; } diff --git a/server/guhapplication.cpp b/server/guhapplication.cpp new file mode 100644 index 00000000..b196723d --- /dev/null +++ b/server/guhapplication.cpp @@ -0,0 +1,73 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2016 Simon Stuerz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "guhapplication.h" +#include "loggingcategories.h" +#include "guhcore.h" + +#include "unistd.h" +#include "signal.h" + +namespace guhserver { + +static void catchUnixSignals(const std::vector& quitSignals, const std::vector& ignoreSignals = std::vector()) { + + auto handler = [](int sig) ->void { + switch (sig) { + case SIGQUIT: + qCDebug(dcApplication) << "Cought SIGQUIT quit signal..."; + break; + case SIGINT: + qCDebug(dcApplication) << "Cought SIGINT quit signal..."; + break; + case SIGTERM: + qCDebug(dcApplication) << "Cought SIGTERM quit signal..."; + break; + case SIGHUP: + qCDebug(dcApplication) << "Cought SIGHUP quit signal..."; + break; + default: + break; + } + + qCDebug(dcApplication) << "====================================="; + qCDebug(dcApplication) << "Shutting down guh daemon"; + qCDebug(dcApplication) << "====================================="; + + GuhCore::instance()->destroy(); + GuhApplication::quit(); + }; + + // all these signals will be ignored. + for (int sig : ignoreSignals) + signal(sig, SIG_IGN); + + for (int sig : quitSignals) + signal(sig, handler); +} + + +GuhApplication::GuhApplication(int &argc, char **argv) : + QCoreApplication(argc, argv) +{ + catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP}); +} + +} diff --git a/server/guhapplication.h b/server/guhapplication.h new file mode 100644 index 00000000..4735149e --- /dev/null +++ b/server/guhapplication.h @@ -0,0 +1,37 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2016 Simon Stuerz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef GUHAPPLICATION_H +#define GUHAPPLICATION_H + +#include +#include +#include + +namespace guhserver { + +class GuhApplication : public QCoreApplication +{ +public: + GuhApplication(int &argc, char **argv); +}; + +} +#endif // GUHAPPLICATION_H diff --git a/server/guhcore.cpp b/server/guhcore.cpp index 68c5d85e..e47d4b74 100644 --- a/server/guhcore.cpp +++ b/server/guhcore.cpp @@ -29,13 +29,6 @@ instantiate, set up and connect all the other components. */ -/*! \enum guhserver::GuhCore::RunningMode - \value RunningModeApplication - Guh runns as application. - \value RunningModeService - Guh is started as service (daemon). -*/ - /*! \fn void guhserver::GuhCore::eventTriggered(const Event &event); This signal is emitted when an \a event happend. */ @@ -129,28 +122,17 @@ GuhCore *GuhCore::instance() GuhCore::~GuhCore() { m_logger->logSystemEvent(false); - qCDebug(dcApplication) << "Shutting down. Bye."; } /*! Destroyes the \l{GuhCore} instance. */ void GuhCore::destroy() { - delete s_instance; + if (s_instance) + delete s_instance; + s_instance = 0; } -/*! Returns the runningMode of this instance. */ -GuhCore::RunningMode GuhCore::runningMode() const -{ - return m_runningMode; -} - -/*! Set the \a runningMode of this instance. */ -void GuhCore::setRunningMode(const RunningMode &runningMode) -{ - m_runningMode = runningMode; -} - /*! Calls the metheod DeviceManager::plugins(). * \sa DeviceManager::plugins(), */ QList GuhCore::plugins() const @@ -557,6 +539,7 @@ RuleEngine *GuhCore::ruleEngine() const GuhCore::GuhCore(QObject *parent) : QObject(parent) { + qCDebug(dcApplication) << "Creating Log Engine"; m_logger = new LogEngine(this); qCDebug(dcApplication) << "Creating Device Manager"; @@ -565,7 +548,6 @@ GuhCore::GuhCore(QObject *parent) : qCDebug(dcApplication) << "Creating Rule Engine"; m_ruleEngine = new RuleEngine(this); - qCDebug(dcApplication) << "Creating Server Manager"; m_serverManager = new ServerManager(this); diff --git a/server/guhcore.h b/server/guhcore.h index b1f06aa3..0171cc72 100644 --- a/server/guhcore.h +++ b/server/guhcore.h @@ -46,20 +46,11 @@ class GuhCore : public QObject { Q_OBJECT public: - enum RunningMode { - RunningModeApplication, - RunningModeService - }; - static GuhCore* instance(); ~GuhCore(); - // Used for testing void destroy(); - RunningMode runningMode() const; - void setRunningMode(const RunningMode &runningMode); - QList plugins() const; DeviceManager::DeviceError setPluginConfig(const PluginId &pluginId, const ParamList ¶ms); @@ -126,7 +117,6 @@ private: explicit GuhCore(QObject *parent = 0); static GuhCore *s_instance; - RunningMode m_runningMode; ServerManager *m_serverManager; DeviceManager *m_deviceManager; diff --git a/server/guhservice.cpp b/server/guhservice.cpp index abcbb79e..c8ba18bb 100644 --- a/server/guhservice.cpp +++ b/server/guhservice.cpp @@ -62,14 +62,7 @@ void GuhService::start() qCDebug(dcApplication) << "====================================="; qCDebug(dcApplication) << "guhd" << GUH_VERSION_STRING << "started as daemon."; qCDebug(dcApplication) << "====================================="; - GuhCore::instance()->setRunningMode(GuhCore::RunningModeService); -} - -void GuhService::stop() -{ - qCDebug(dcApplication) << "====================================="; - qCDebug(dcApplication) << "Shutting down guh daemon"; - qCDebug(dcApplication) << "====================================="; + GuhCore::instance(); } } diff --git a/server/guhservice.h b/server/guhservice.h index 2903e737..8b7b8e6d 100644 --- a/server/guhservice.h +++ b/server/guhservice.h @@ -37,7 +37,6 @@ public: protected: void start(); - void stop(); }; } diff --git a/server/logging/logengine.cpp b/server/logging/logengine.cpp index a0dbd0ba..808f5a96 100644 --- a/server/logging/logengine.cpp +++ b/server/logging/logengine.cpp @@ -145,6 +145,12 @@ LogEngine::LogEngine(QObject *parent): initDB(); } +LogEngine::~LogEngine() +{ + qCDebug(dcApplication) << "Shutting down log engine"; + m_db.close(); +} + /*! Returns the list of \l{LogEntry}{LogEntries} of the database matching the given \a filter. \sa LogEntry, LogFilter diff --git a/server/logging/logengine.h b/server/logging/logengine.h index ea6663fa..d06c9ba3 100644 --- a/server/logging/logengine.h +++ b/server/logging/logengine.h @@ -38,6 +38,7 @@ class LogEngine: public QObject Q_OBJECT public: LogEngine(QObject *parent = 0); + ~LogEngine(); QList logEntries(const LogFilter &filter = LogFilter()) const; diff --git a/server/main.cpp b/server/main.cpp index 9ede6fa1..fce576ac 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -36,6 +36,7 @@ #include "guhcore.h" #include "guhservice.h" #include "guhsettings.h" +#include "guhapplication.h" #include "loggingcategories.h" static QHash s_loggingFilters; @@ -87,12 +88,11 @@ static void consoleLogHandler(QtMsgType type, const QMessageLogContext& context, logFile.close(); } - int main(int argc, char *argv[]) { qInstallMessageHandler(consoleLogHandler); - QCoreApplication application(argc, argv); + GuhApplication application(argc, argv); application.setOrganizationName("guh"); application.setApplicationName("guhd"); application.setApplicationVersion(GUH_VERSION_STRING); @@ -121,13 +121,13 @@ int main(int argc, char *argv[]) parser.addHelpOption(); parser.addVersionOption(); QString applicationDescription = QString("\nguh ( /[guːh]/ ) is an open source IoT (Internet of Things) server, \n" - "which allows to control a lot of different devices from many different. \n" + "which allows to control a lot of different devices from many different \n" "manufacturers. With the powerful rule engine you are able to connect any \n" "device available in the system and create individual scenes and behaviors \n" "for your environment.\n\n" - "guhd %1 (C) 2014-2015 guh\n" + "guhd %1 %2 2014-2016 guh GmbH\n" "Released under the GNU GENERAL PUBLIC LICENSE Version 2.\n\n" - "API version: %2\n").arg(GUH_VERSION_STRING).arg(JSON_PROTOCOL_VERSION); + "API version: %3\n").arg(GUH_VERSION_STRING).arg(QChar(0xA9)).arg(JSON_PROTOCOL_VERSION); parser.setApplicationDescription(applicationDescription); @@ -142,6 +142,7 @@ int main(int argc, char *argv[]) foreach (const QString &filterName, sortedFilterList) { debugDescription += "\n- " + filterName + " (" + (s_loggingFilters.value(filterName) ? "yes" : "no") + ")"; } + // create sorted plugin loggingFiler list QStringList sortedPluginList = QStringList(loggingFiltersPlugins.keys()); sortedPluginList.sort(); @@ -195,7 +196,7 @@ int main(int argc, char *argv[]) } // create core instance - GuhCore::instance()->setRunningMode(GuhCore::RunningModeApplication); + GuhCore::instance(); return application.exec(); } diff --git a/server/qtservice/qtservice.h b/server/qtservice/qtservice.h index 86c4d3da..05e3f2b6 100644 --- a/server/qtservice/qtservice.h +++ b/server/qtservice/qtservice.h @@ -168,7 +168,6 @@ public: ~QtService() { } - protected: Application *application() const { return app; } diff --git a/server/ruleengine.cpp b/server/ruleengine.cpp index 2b94a8ad..f7428149 100644 --- a/server/ruleengine.cpp +++ b/server/ruleengine.cpp @@ -213,6 +213,11 @@ RuleEngine::RuleEngine(QObject *parent) : } } +RuleEngine::~RuleEngine() +{ + qCDebug(dcApplication) << "Shutting down rule engine"; +} + /*! Ask the Engine to evaluate all the rules for the given \a event. This will search all the \l{Rule}{Rules} triggered by the given \a event and evaluate their states in the system. It will return a diff --git a/server/ruleengine.h b/server/ruleengine.h index 746172b9..b971e814 100644 --- a/server/ruleengine.h +++ b/server/ruleengine.h @@ -64,6 +64,7 @@ public: }; explicit RuleEngine(QObject *parent = 0); + ~RuleEngine(); QList evaluateEvent(const Event &event); diff --git a/server/server.pro b/server/server.pro index 506ba31b..c8144221 100644 --- a/server/server.pro +++ b/server/server.pro @@ -16,7 +16,8 @@ include(server.pri) include(qtservice/qtservice.pri) SOURCES += main.cpp \ - guhservice.cpp + guhservice.cpp \ + guhapplication.cpp boblight { xcompile { @@ -28,4 +29,5 @@ boblight { } HEADERS += \ - guhservice.h + guhservice.h \ + guhapplication.h diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp index 07e3dfab..0824b31f 100644 --- a/server/tcpserver.cpp +++ b/server/tcpserver.cpp @@ -87,6 +87,8 @@ TcpServer::TcpServer(QObject *parent) : /*! Destructor of this \l{TcpServer}. */ TcpServer::~TcpServer() { + qCDebug(dcApplication) << "Shutting down TCP server"; + stopServer(); } /*! Sending \a data to a list of \a clients.*/ @@ -309,7 +311,6 @@ bool TcpServer::stopServer() { // Listen on all Networkinterfaces foreach (QTcpServer *s, m_serverList) { - qCDebug(dcConnection) << "Closing Tcp server on" << s->serverAddress().toString() << s->serverPort(); QUuid uuid = m_serverList.key(s); s->close(); m_serverList.take(uuid)->deleteLater(); diff --git a/server/transportinterface.cpp b/server/transportinterface.cpp index 013d950c..4e763d51 100644 --- a/server/transportinterface.cpp +++ b/server/transportinterface.cpp @@ -167,5 +167,4 @@ void TransportInterface::validateMessage(const QUuid &clientId, const QByteArray emit dataAvailable(clientId, targetNamespace, method, message); } - } diff --git a/server/webserver.cpp b/server/webserver.cpp index b53e3b9c..fb5ed88c 100644 --- a/server/webserver.cpp +++ b/server/webserver.cpp @@ -131,6 +131,7 @@ WebServer::WebServer(const QSslConfiguration &sslConfiguration, QObject *parent) /*! Destructor of this \l{WebServer}. */ WebServer::~WebServer() { + qCDebug(dcApplication) << "Shutting down webserver"; this->close(); } diff --git a/server/websocketserver.cpp b/server/websocketserver.cpp index c76e6e81..4a718819 100644 --- a/server/websocketserver.cpp +++ b/server/websocketserver.cpp @@ -85,6 +85,7 @@ WebSocketServer::WebSocketServer(const QSslConfiguration &sslConfiguration, QObj /*! Destructor of this \l{WebSocketServer}. */ WebSocketServer::~WebSocketServer() { + qCDebug(dcApplication) << "Shutting down websocket server"; stopServer(); } @@ -217,7 +218,6 @@ bool WebSocketServer::startServer() */ bool WebSocketServer::stopServer() { - qCDebug(dcConnection) << "Stopping websocket server"; m_server->close(); delete m_server; m_server = 0;