libcommonc++  0.7
Service.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Service_hxx
24 #define __ccxx_Service_hxx
25 
26 #include <commonc++/Common.h++>
29 
30 namespace ccxx {
31 
78 {
79  public:
80 
92  Service(int argc, char** argv, const String& version,
93  const String& compileDate = "", const String& compileTime = "");
94 
96  virtual ~Service();
97 
98  protected:
99 
107  inline void setDescription(const String& desc)
108  { _desc = desc; }
109 
117  inline void setWorkingDir(const String& dir)
118  { _workingDir = dir; }
119 
120  /*
121  */
122  virtual bool processOption(char opt, const String& longOpt,
123  const String& arg = String::null);
124 
136  void detach();
137 
144  virtual void run() = 0;
145 
152  void testShutdown();
153 
162  virtual void shutdown() = 0;
163 
165  inline bool isDebug() const
166  { return(_debug); }
167 
168  private:
169 
170 #ifdef CCXX_OS_WINDOWS
171  static void _serviceMain(int argc, char** argv);
172  void _serviceInit();
173  static void _controlHandler(DWORD code);
174  void _serviceStart();
175  void _serviceStop();
176  void _serviceInstall();
177  void _serviceUninstall();
178  bool _waitForStatus(SC_HANDLE handle, DWORD pending, DWORD final);
179 
180  SERVICE_STATUS_HANDLE _handle;
181  SERVICE_STATUS _status;
182  UINT _oldErrorMode;
183 #endif
184 
185 #ifdef CCXX_OS_POSIX
186  static void _sighandler(int sig);
187 #endif
188 
189  String _name;
190  String _displayName;
191  String _desc;
192  bool _shutdown;
193  bool _debug;
194  String _pidFile;
195 
196 #ifdef CCXX_OS_WINDOWS
197  enum Action { None, Start, Stop, Install, Uninstall } _action;
198 #endif
199 
200  String _workingDir;
201  static Service *_instance;
202 };
203 
204 } // namespace ccxx
205 
206 #endif // __ccxx_Service_hxx
void setDescription(const String &desc)
Set the service description.
Definition: Service.h++:107
void setWorkingDir(const String &dir)
On POSIX systems, set the working directory for the service daemon process.
Definition: Service.h++:117
bool isDebug() const
Test if debug (foreground) mode is enabled.
Definition: Service.h++:165
Basic functionality for a system service, such as a daemon on UNIX or an "NT Service" on Windows...
Definition: Service.h++:77
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
Base functionality for an application, including command-line argument parsing and a shutdown handler...
Definition: Application.h++:42
static const String null
The null string.
Definition: String.h++:1128
Definition: AllocationMap.c++:25