QtService  1.1.0
A platform independent library to easily create system services and use some of their features
service.h
Go to the documentation of this file.
1 #ifndef QTSERVICE_SERVICE_H
2 #define QTSERVICE_SERVICE_H
3 
4 #include <functional>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/qcoreapplication.h>
8 #include <QtCore/qdir.h>
9 #include <QtCore/qscopedpointer.h>
10 #include <QtCore/qvector.h>
11 #include <QtCore/qhash.h>
12 #include <QtCore/qvariant.h>
13 
14 #include "QtService/qtservice_global.h"
15 #include "QtService/qtservice_helpertypes.h"
16 
17 #ifdef Q_OS_UNIX
18 # ifdef Q_OS_ANDROID
19 # define QT_SERVICE_POST_ENUM_DEPRECATED
20 # else
21 # define QT_SERVICE_POST_ENUM_DEPRECATED Q_DECL_DEPRECATED
22 # endif
23 # define QT_SERVICE_POST_USING_DEPRECATED Q_DECL_DEPRECATED
24 #else
25 # define QT_SERVICE_POST_ENUM_DEPRECATED
26 # define QT_SERVICE_POST_USING_DEPRECATED
27 #endif
28 
30 namespace QtService {
31 
32 class Terminal;
33 class TerminalClient;
34 class ServiceBackend;
35 class ServicePrivate;
37 class Q_SERVICE_EXPORT Service : public QObject
38 {
39  Q_OBJECT
40 
42  Q_PROPERTY(QString backend READ backend CONSTANT)
44  Q_PROPERTY(QDir runtimeDir READ runtimeDir CONSTANT)
45 
47  Q_PROPERTY(bool terminalActive READ isTerminalActive WRITE setTerminalActive NOTIFY terminalActiveChanged)
49  Q_PROPERTY(TerminalMode terminalMode READ terminalMode WRITE setTerminalMode NOTIFY terminalModeChanged)
51  Q_PROPERTY(bool globalTerminal READ isGlobalTerminal WRITE setGlobalTerminal NOTIFY globalTerminalChanged)
53  Q_PROPERTY(bool startWithTerminal READ startWithTerminal WRITE setStartWithTerminal NOTIFY startWithTerminalChanged)
54 
55 public:
61 
63 
64  //MAJOR compat remove
65  Synchronous QT_SERVICE_POST_ENUM_DEPRECATED = OperationCompleted,
66  Asynchronous QT_SERVICE_POST_ENUM_DEPRECATED = OperationPending
67  };
69  using CommandMode QT_SERVICE_POST_USING_DEPRECATED = CommandResult;//MAJOR compat remove
70  Q_ENUM(CommandResult)
71 
72 #ifdef Q_CC_MSVC
73 #pragma deprecated(CommandMode)
74 #pragma deprecated(Synchronous)
75 #pragma deprecated(Asynchronous)
76 #endif
77 
79  enum TerminalMode {
83  ReadWriteActive
84  };
85  Q_ENUM(TerminalMode)
86 
87 
88  explicit Service(int &argc, char **argv, int = QCoreApplication::ApplicationFlags);
89  ~Service() override;
90 
92  int exec();
93 
95  static Service *instance();
96 
98  Q_INVOKABLE QList<int> getSockets(const QByteArray &socketName);
100  Q_INVOKABLE int getSocket();
101 
103  QString backend() const;
105  QDir runtimeDir() const;
107  bool isTerminalActive() const;
109  TerminalMode terminalMode() const;
111  bool isGlobalTerminal() const;
113  bool startWithTerminal() const;
114 
115 public Q_SLOTS:
117  void quit();
119  void reload();
120 
122  void setTerminalActive(bool terminalActive);
124  void setTerminalMode(TerminalMode terminalMode);
126  void setGlobalTerminal(bool globalTerminal);
128  void setStartWithTerminal(bool startWithTerminal);
129 
130 Q_SIGNALS:
132  void started(bool success); //TODO doc should be direct connected
134  Q_DECL_DEPRECATED void started();
136  void stopped(int exitCode = EXIT_SUCCESS);
138  void reloaded(bool success);
140  Q_DECL_DEPRECATED void reloaded();
142  void paused(bool success);
144  Q_DECL_DEPRECATED void paused();
146  void resumed(bool success);
148  Q_DECL_DEPRECATED void resumed();
149 
151  void terminalActiveChanged(bool terminalActive, QPrivateSignal);
153  void terminalModeChanged(TerminalMode terminalMode, QPrivateSignal);
155  void globalTerminalChanged(bool globalTerminal, QPrivateSignal);
157  void startWithTerminalChanged(bool startWithTerminal, QPrivateSignal);
158 
159 protected Q_SLOTS:
161  virtual void terminalConnected(Terminal *terminal);
162 
163 protected:
165  virtual bool preStart();
166 
168  virtual CommandResult onStart() = 0;
170  virtual CommandResult onStop(int &exitCode);
172  virtual CommandResult onReload();
174  virtual CommandResult onPause();
176  virtual CommandResult onResume();
177 
179  virtual QVariant onCallback(const QByteArray &kind, const QVariantList &args);
180 
182  virtual bool verifyCommand(const QStringList &arguments);
183 
185  void addCallback(const QByteArray &kind, const std::function<QVariant(QVariantList)> &fn);
187  template <typename TFunction>
188  void addCallback(const QByteArray &kind, const TFunction &fn);
190  template <typename TClass, typename TReturn, typename... TArgs>
191  void addCallback(const QByteArray &kind, TReturn(TClass::*fn)(TArgs...), std::enable_if_t<std::is_base_of<QtService::Service, TClass>::value, void*> = nullptr);
192 
193 private:
194  friend class QtService::ServiceBackend;
195  friend class QtService::ServicePrivate;
196  friend class QtService::TerminalClient;
197 
199 };
200 
201 template<typename TFunction>
202 void Service::addCallback(const QByteArray &kind, const TFunction &fn)
203 {
204  addCallback(kind, __helpertypes::pack_function(fn));
205 }
206 
207 template<typename TClass, typename TReturn, typename... TArgs>
208 void Service::addCallback(const QByteArray &kind, TReturn (TClass::*fn)(TArgs...), std::enable_if_t<std::is_base_of<QtService::Service, TClass>::value, void*>)
209 {
210  auto self = static_cast<TClass*>(this);
211  addCallback(kind, [self, fn](TArgs... args) -> TReturn {
212  return (self->*fn)(args...);
213  });
214 }
215 
216 }
217 
219 #define qService QtService::Service::instance()
220 
222 #endif // QTSERVICE_SERVICE_H
CommandResult
Indicates whether a service command has finished or needs to run asynchronously.
Definition: service.h:57
TerminalMode
The modes a terminal can be in.
Definition: service.h:79
The terminal can only receive data from the service. Useful for machine-to-machine communication...
Definition: service.h:80
void addCallback(const QByteArray &kind, const std::function< QVariant(QVariantList)> &fn)
Adds a callback to be called by onCallback for the given kind.
The main interface to implement to create a service.
Definition: service.h:37
The command executed successfully, but the service should still exit. Only usable from onStart() ...
Definition: service.h:62
The primary namespace of the QtService library.
Represents a connection to a console terminal connected to the service.
Definition: terminal.h:15
The interface that needs to be implemented to provide the backend for the service engine...
The command failed synchronously. The system may exit the service afterwards.
Definition: service.h:60
The command was successfully completed synchronously.
Definition: service.h:58
The terminal can read and write as much as it wants. Useful for machine-to-machine communication...
Definition: service.h:82
The command is beeing proccessed asynchronously and the service will emit the corresponding signal on...
Definition: service.h:59
The terminal can only send data to the service. Useful for machine-to-machine communication.
Definition: service.h:81