source: XMLIO_V2/external/include/Poco/Util/ServerApplication.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1//
2// ServerApplication.h
3//
4// $Id: //poco/1.3/Util/include/Poco/Util/ServerApplication.h#4 $
5//
6// Library: Util
7// Package: Application
8// Module:  ServerApplication
9//
10// Definition of the ServerApplication class.
11//
12// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13// and Contributors.
14//
15// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38
39#ifndef Util_ServerApplication_INCLUDED
40#define Util_ServerApplication_INCLUDED
41
42
43#include "Poco/Util/Util.h"
44#include "Poco/Util/Application.h"
45#include "Poco/Event.h"
46
47
48namespace Poco {
49namespace Util {
50
51
52class Util_API ServerApplication: public Application
53        /// A subclass of the Application class that is used
54        /// for implementing server applications.
55        ///
56        /// A ServerApplication allows for the application
57        /// to run as a Windows service or as a Unix daemon
58        /// without the need to add extra code.
59        ///
60        /// For a ServerApplication to work both from the command line
61        /// and as a daemon or service, a few rules must be met:
62        ///   - Subsystems must be registered in the constructor.
63        ///   - All non-trivial initializations must be made in the
64        ///     initialize() method.
65        ///   - At the end of the main() method, waitForTerminationRequest()
66        ///     should be called.
67        ///   - The main(argc, argv) function must look as follows:
68        ///
69        ///   int main(int argc, char** argv)
70        ///   {
71        ///       MyServerApplication app;
72        ///       return app.run(argc, argv);
73        ///   }
74        ///
75        /// The POCO_SERVER_MAIN macro can be used to implement main(argc, argv).
76        /// If POCO has been built with POCO_WIN32_UTF8, POCO_SERVER_MAIN supports
77        /// Unicode command line arguments.
78        ///
79        /// On Windows platforms, an application built on top of the
80        /// ServerApplication class can be run both from the command line
81        /// or as a service.
82        ///
83        /// To run an application as a Windows service, it must be registered
84        /// with the Windows Service Control Manager (SCM). To do this, the application
85        /// can be started from the command line, with the /registerService option
86        /// specified. This causes the application to register itself with the
87        /// SCM, and then exit. Similarly, an application registered as a service can
88        /// be unregistered, by specifying the /unregisterService option.
89        /// The file name of the application executable (excluding the .exe suffix)
90        /// is used as the service name. Additionally, a more user-friendly name can be
91        /// specified, using the /displayName option (e.g., /displayName="Demo Service").
92        ///
93        /// An application can determine whether it is running as a service by checking
94        /// for the "application.runAsService" configuration property.
95        ///
96        ///     if (config().getBool("application.runAsService", false))
97        ///     {
98        ///         // do service specific things
99        ///     }
100        ///
101        /// Note that the working directory for an application running as a service
102        /// is the Windows system directory (e.g., C:\Windows\system32). Take this
103        /// into account when working with relative filesystem paths. Also, services
104        /// run under a different user account, so an application that works when
105        /// started from the command line may fail to run as a service if it depends
106        /// on a certain environment (e.g., the PATH environment variable).
107        ///
108        /// An application registered as a Windows service can be started
109        /// with the NET START <name> command and stopped with the NET STOP <name>
110        /// command. Alternatively, the Services MMC applet can be used.
111        ///
112        /// On Unix platforms, an application built on top of the ServerApplication
113        /// class can be optionally run as a daemon by giving the --daemon
114        /// command line option. A daemon, when launched, immediately
115        /// forks off a background process that does the actual work. After launching
116        /// the background process, the foreground process exits.
117        ///
118        /// After the initialization is complete, but before entering the main() method,
119        /// the current working directory for the daemon process is changed to the root
120        /// directory ("/"), as it is common practice for daemon processes. Therefore, be
121        /// careful when working with files, as relative paths may not point to where
122        /// you expect them point to.
123        ///
124        /// An application can determine whether it is running as a daemon by checking
125        /// for the "application.runAsDaemon" configuration property.
126        ///
127        ///     if (config().getBool("application.runAsDaemon", false))
128        ///     {
129        ///         // do daemon specific things
130        ///     }
131        ///
132        /// When running as a daemon, specifying the --pidfile option (e.g.,
133        /// --pidfile=/var/run/sample.pid) may be useful to record the process ID of
134        /// the daemon in a file. The PID file will be removed when the daemon process
135        /// terminates (but not, if it crashes).
136{
137public:
138        ServerApplication();
139                /// Creates the ServerApplication.
140
141        ~ServerApplication();
142                /// Destroys the ServerApplication.
143               
144        bool isInteractive() const;
145                /// Returns true if the application runs from the command line.
146                /// Returns false if the application runs as a Unix daemon
147                /// or Windows service.
148
149        int run(int argc, char** argv);
150                /// Runs the application by performing additional initializations
151                /// and calling the main() method.
152
153#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
154        int run(int argc, wchar_t** argv);
155                /// Runs the application by performing additional initializations
156                /// and calling the main() method.
157                ///
158                /// This Windows-specific version of init is used for passing
159                /// Unicode command line arguments from wmain().
160#endif
161
162protected:
163        int run();
164        void waitForTerminationRequest();
165        void defineOptions(OptionSet& options);
166        void handleOption(const std::string& name, const std::string& value);
167        static void terminate();
168
169private:
170#if defined(POCO_OS_FAMILY_UNIX)
171        bool isDaemon(int argc, char** argv);
172        void beDaemon();
173#elif defined(POCO_OS_FAMILY_WINDOWS)
174        enum Action
175        {
176                SRV_RUN,
177                SRV_REGISTER,
178                SRV_UNREGISTER
179        };
180        static BOOL __stdcall ConsoleCtrlHandler(DWORD ctrlType);
181        static void __stdcall ServiceControlHandler(DWORD control);
182#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
183        static void __stdcall ServiceMain(DWORD argc, LPWSTR* argv);
184#else
185        static void __stdcall ServiceMain(DWORD argc, LPTSTR* argv);
186#endif
187
188        bool hasConsole();
189        bool isService();
190        void beService();
191        void registerService();
192        void unregisterService();
193       
194        Action      _action;
195        std::string _displayName;
196
197        static Poco::Event           _terminated;
198        static SERVICE_STATUS        _serviceStatus; 
199        static SERVICE_STATUS_HANDLE _serviceStatusHandle; 
200#endif
201};
202
203
204} } // namespace Poco::Util
205
206
207//
208// Macro to implement main()
209//
210#if defined(_WIN32) && defined(POCO_WIN32_UTF8)
211        #define POCO_SERVER_MAIN(App) \
212        int wmain(int argc, wchar_t** argv)     \
213        {                                                                       \
214                App app;                                                \
215                return app.run(argc, argv);             \
216        }
217#else
218        #define POCO_SERVER_MAIN(App) \
219        int main(int argc, char** argv)         \
220        {                                                                       \
221                App app;                                                \
222                return app.run(argc, argv);             \
223        }
224#endif
225
226
227#endif // Util_ServerApplication_INCLUDED
Note: See TracBrowser for help on using the repository browser.