| 1 | // |
|---|---|
| 2 | // SampleServer.cpp |
| 3 | // |
| 4 | // This sample demonstrates the ServerApplication class. |
| 5 | // |
| 6 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/Util/ServerApplication.h" |
| 14 | #include "Poco/Util/Option.h" |
| 15 | #include "Poco/Util/OptionSet.h" |
| 16 | #include "Poco/Util/HelpFormatter.h" |
| 17 | #include "Poco/Task.h" |
| 18 | #include "Poco/TaskManager.h" |
| 19 | #include "Poco/DateTimeFormatter.h" |
| 20 | #include <iostream> |
| 21 | |
| 22 | |
| 23 | using Poco::Util::Application; |
| 24 | using Poco::Util::ServerApplication; |
| 25 | using Poco::Util::Option; |
| 26 | using Poco::Util::OptionSet; |
| 27 | using Poco::Util::OptionCallback; |
| 28 | using Poco::Util::HelpFormatter; |
| 29 | using Poco::Task; |
| 30 | using Poco::TaskManager; |
| 31 | using Poco::DateTimeFormatter; |
| 32 | |
| 33 | |
| 34 | class SampleTask: public Task |
| 35 | { |
| 36 | public: |
| 37 | SampleTask(): Task("SampleTask") |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void runTask() |
| 42 | { |
| 43 | Application& app = Application::instance(); |
| 44 | while (!sleep(5000)) |
| 45 | { |
| 46 | Application::instance().logger().information("busy doing nothing... "+ DateTimeFormatter::format(app.uptime())); |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | class SampleServer: public ServerApplication |
| 53 | { |
| 54 | public: |
| 55 | SampleServer(): _helpRequested(false) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | ~SampleServer() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | void initialize(Application& self) |
| 65 | { |
| 66 | loadConfiguration(); // load default configuration files, if present |
| 67 | ServerApplication::initialize(self); |
| 68 | logger().information("starting up"); |
| 69 | } |
| 70 | |
| 71 | void uninitialize() |
| 72 | { |
| 73 | logger().information("shutting down"); |
| 74 | ServerApplication::uninitialize(); |
| 75 | } |
| 76 | |
| 77 | void defineOptions(OptionSet& options) |
| 78 | { |
| 79 | ServerApplication::defineOptions(options); |
| 80 | |
| 81 | options.addOption( |
| 82 | Option("help", "h", "display help information on command line arguments") |
| 83 | .required(false) |
| 84 | .repeatable(false) |
| 85 | .callback(OptionCallback<SampleServer>(this, &SampleServer::handleHelp))); |
| 86 | } |
| 87 | |
| 88 | void handleHelp(const std::string& name, const std::string& value) |
| 89 | { |
| 90 | _helpRequested = true; |
| 91 | displayHelp(); |
| 92 | stopOptionsProcessing(); |
| 93 | } |
| 94 | |
| 95 | void displayHelp() |
| 96 | { |
| 97 | HelpFormatter helpFormatter(options()); |
| 98 | helpFormatter.setCommand(commandName()); |
| 99 | helpFormatter.setUsage("OPTIONS"); |
| 100 | helpFormatter.setHeader("A sample server application that demonstrates some of the features of the Util::ServerApplication class."); |
| 101 | helpFormatter.format(std::cout); |
| 102 | } |
| 103 | |
| 104 | int main(const ArgVec& args) |
| 105 | { |
| 106 | if (!_helpRequested) |
| 107 | { |
| 108 | TaskManager tm; |
| 109 | tm.start(new SampleTask); |
| 110 | waitForTerminationRequest(); |
| 111 | tm.cancelAll(); |
| 112 | tm.joinAll(); |
| 113 | } |
| 114 | return Application::EXIT_OK; |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | bool _helpRequested; |
| 119 | }; |
| 120 | |
| 121 | |
| 122 | POCO_SERVER_MAIN(SampleServer) |
| 123 |