网页设计教学网站,百度热搜榜单,电影网站权重怎么做,免费域名注册网站源码【C】郭老二博文之#xff1a;C目录
1、Poco::Util::Application 应用框架
1.1 应用程序基本功能
Poco::Util::Application是POCO实现的的应用程序框架#xff0c;支持功能如下#xff1a;
命令行参数处理配置文件初始化和关机日志
1.2 命令行程序和守护进程
POCO支持…【C】郭老二博文之C目录
1、Poco::Util::Application 应用框架
1.1 应用程序基本功能
Poco::Util::Application是POCO实现的的应用程序框架支持功能如下
命令行参数处理配置文件初始化和关机日志
1.2 命令行程序和守护进程
POCO支持两种应用
命令行应用程序从命令行启动POCO支持命令行参数处理服务器应用程序通常作为守护进程运行比如Linux守护进程、Windows服务
2、Poco::Util::Subsystem 子系统
2.1 说明
Poco::Util::Application继承自Poco::Util::Subsystem。 POCO中将不同的子系统组成一个应用程序。子系统以模块化的方式扩展应用。
子系统抽象出统一的初始化和关闭等步骤。当一个应用程序被初始化时它所有注册的子系统也被初始化。当一个应用程序关闭时它所有注册的子系统也会关闭。
2.2 使用
一个子系统都是从Poco::Util::Subsystem继承而来下面列出几个常用的接口
const char* name()返回子系统的名称void initialize(Application app)初始化子系统操作void uninitialize(Application app)关闭子系统时的操作void reinitialize(Application app)重新配置子系统(可选;默认实现调用uninitialize()然后调用initialize())void defineOptions(OptionSet options)子系统自定义命令行参数
3、命令行程序
命令行应用程序是通过创建Poco::Util::Application的子类来实现的。有几个虚成员函数需要重写
void initialize(Application self)void reinitialize()void uninitialize()void defineOptions()int main(std::vectorstd::string args)
注意上面的main是Poco::Util::Application的成员函数应用程序的主要逻辑写在这里。 返回值是枚举ExitCode
enum ExitCode
{EXIT_OK 0, 成功终止EXIT_USAGE 64, 命令行使用错误EXIT_DATAERR 65, 数据格式错误EXIT_NOINPUT 66, 无法打开输入EXIT_NOUSER 67, 地址错误EXIT_NOHOST 68, 主机名是未知EXIT_UNAVAILABLE 69, 服务不可用EXIT_SOFTWARE 70, 内部软件错误EXIT_OSERR 71, 系统错误 (e.g., cant fork)EXIT_OSFILE 72, 关键操作系统文件丢失EXIT_CANTCREAT 73, 不能创建(用户)输出文件EXIT_IOERR 74, 输入/输出错误EXIT_TEMPFAIL 75, 临时错误可以尝试重新运行EXIT_PROTOCOL 76, 协议中的远程错误EXIT_NOPERM 77, 没有权限EXIT_CONFIG 78 配置错误
};4、Poco::Util::ServerApplication 服务类程序
想要实现一个服务类程序需要从Poco::Util::ServerApplication继承 Poco::Util::ServerApplication本身继承自Poco::Util::Application
服务类应用程序可以从命令行运行比如Linux守护进程、Windows服务。 通常服务类应用程序在后台线程中工作。因此main()成员函数将启动线程然后等待外部请求来终止应用程序(参见waitForTerminationRequest())。
5、配置文件
5.1 说明
默认情况下会创建两个配置:
可写的MapConfiguration只读的PRIO_APPLICATIONSystemConfiguration, PRIO_SYSTEM
5.2 用法
void MyApplication::initialize(Application self)
{loadConfiguration(); // 加载默认配置文件Application::initialize(self);
}6、命令行参数
6.1 说明
应用程序可以定义和处理命令行参数(选项)。命令行参数格式随系统不同而不同
Windows/option or /optionvalueLinux-o, -ovalue, --option or --option:value
6.2 使用
应用程序的选项通过重写虚函数 void defineOptions(OptionSet options) 来实现 “定义选项类”OptionSet 用来处理选项比如OptionSet::addOption()函数可以添加选项示例如下
void defineOptions(OptionSet options)
{Application::defineOptions(options);options.addOption(Option(help, h, display help information on command line arguments).required(false).repeatable(false).callback(OptionCallbackSampleApp(this, SampleApp::handleHelp)));options.addOption(Option(config-file, f, load configuration data from a file).required(false).repeatable(true).argument(file).callback(OptionCallbackSampleApp(this, SampleApp::handleConfig)));options.addOption(Option(bind, b, bind option value to test.property).required(false).argument(value).validator(new IntValidator(0, 100)).binding(test.property));
}运行时打印
-h, --help display help information on command line arguments
-ffile, --config-filefile load configuration data from a file
-bvalue, --bindvalue bind option value to test.property每个选项Option有如下内容
一个全名一个用字符表示的缩写短名字一段描述一个参数名argument是否必须required是否可重复repeatable它可以在命令行中被多次给出回调函数callback
6.3 验证选项参数
通过为选项指定一个Validator对象可以自动验证选项参数。
IntValidator 检查参数是否为一定范围内的整数。RegExpValidator 验证参数是否匹配给定的正则表达式。
比如上例中的.validator(new IntValidator(0, 100))
6.4 显示帮助信息
Poco::Util::HelpFormatter类可用于显示命令行选项帮助信息。 当用户请求帮助信息时应取消所有进一步的命令行处理(特别是强制执行所需选项)。这可以通过调用stopOptionsProcessing()来完成。
void displayHelp()
{HelpFormatter helpFormatter(options());helpFormatter.setCommand(commandName());helpFormatter.setUsage(OPTIONS);helpFormatter.setHeader(Poco::Util::Application类的示例);helpFormatter.format(std::cout);
}void handleHelp(const std::string name, const std::string value)
{_helpRequested true;displayHelp();stopOptionsProcessing();
}7、Windows 服务
Windows服务需要注册。 Poco::Util::ServerApplication可以实现注册的步骤。
注册在命令行启动时指定选项 /registerService取消注册指定 /unregisterService 选项来取消设置服务名称通过选项 /displayName 来指定
8、Linux 守护进程
在Linux平台上继承Poco::Util::ServerApplication后可以通过命令行参数“–daemon”来实现作为守护进程运行。 一个守护进程当启动时会立即从执行实际工作的后台进程中分离出来。启动后台进程后前台进程退出。 初始化完成后在进入main()方法之前守护进程的当前工作目录将更改为根目录(“/”)这是守护进程的常见做法。
应用程序可以通过检查application.runasdaemon配置属性来确定它是否作为守护进程运行。 与Windows服务一样在配置文件中使用相对路径时要小心因为守护进程的当前工作目录是根目录。
#include Poco/Util/ServerApplication.h
#include Poco/Util/Option.h
#include Poco/Util/OptionSet.h
#include Poco/Util/HelpFormatter.h
#include Poco/Task.h
#include Poco/TaskManager.h
#include Poco/DateTimeFormatter.h
#include iostreamusing Poco::Util::Application;
using Poco::Util::ServerApplication;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::OptionCallback;
using Poco::Util::HelpFormatter;
using Poco::Task;
using Poco::TaskManager;
using Poco::DateTimeFormatter;class SampleTask: public Task
{
public:SampleTask(): Task(SampleTask){}void runTask(){Application app Application::instance();while (!sleep(5000)){Application::instance().logger().information(busy doing nothing... DateTimeFormatter::format(app.uptime()));}}
};class SampleServer: public ServerApplication
{
public:SampleServer(): _helpRequested(false){}~SampleServer(){}protected:void initialize(Application self){loadConfiguration(); // load default configuration files, if presentServerApplication::initialize(self);logger().information(starting up);}void uninitialize(){logger().information(shutting down);ServerApplication::uninitialize();}void defineOptions(OptionSet options){ServerApplication::defineOptions(options);options.addOption(Option(help, h, display help information on command line arguments).required(false).repeatable(false).callback(OptionCallbackSampleServer(this, SampleServer::handleHelp)));}void handleHelp(const std::string name, const std::string value){_helpRequested true;displayHelp();stopOptionsProcessing();}void displayHelp(){HelpFormatter helpFormatter(options());helpFormatter.setCommand(commandName());helpFormatter.setUsage(OPTIONS);helpFormatter.setHeader(A sample server application that demonstrates some of the features of the Util::ServerApplication class.);helpFormatter.format(std::cout);}int main(const ArgVec args){if (!_helpRequested){TaskManager tm;tm.start(new SampleTask);waitForTerminationRequest();tm.cancelAll();tm.joinAll();}return Application::EXIT_OK;}
private:bool _helpRequested;
};POCO_SERVER_MAIN(SampleServer)9、一个简单的示例
#include Poco/Util/Application.h
#include Poco/Util/Option.h
#include Poco/Util/OptionSet.h
#include Poco/Util/HelpFormatter.h
#include Poco/Util/AbstractConfiguration.h
#include Poco/AutoPtr.h
#include iostream
#include sstreamusing Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using Poco::Util::AbstractConfiguration;
using Poco::Util::OptionCallback;
using Poco::AutoPtr;class SampleApp: public Application/// This sample demonstrates some of the features of the Util::Application class,/// such as configuration file handling and command line arguments processing.////// Try SampleApp --help (on Unix platforms) or SampleApp /help (elsewhere) for/// more information.
{
public:SampleApp(): _helpRequested(false){}protected:void initialize(Application self){loadConfiguration(); // load default configuration files, if presentApplication::initialize(self);// add your own initialization code here}void uninitialize(){// add your own uninitialization code hereApplication::uninitialize();}void reinitialize(Application self){Application::reinitialize(self);// add your own reinitialization code here}void defineOptions(OptionSet options){Application::defineOptions(options);options.addOption(Option(help, h, display help information on command line arguments).required(false).repeatable(false).callback(OptionCallbackSampleApp(this, SampleApp::handleHelp)));options.addOption(Option(define, D, define a configuration property).required(false).repeatable(true).argument(namevalue).callback(OptionCallbackSampleApp(this, SampleApp::handleDefine)));options.addOption(Option(config-file, f, load configuration data from a file).required(false).repeatable(true).argument(file).callback(OptionCallbackSampleApp(this, SampleApp::handleConfig)));options.addOption(Option(bind, b, bind option value to test.property).required(false).repeatable(false).argument(value).binding(test.property));}void handleHelp(const std::string name, const std::string value){_helpRequested true;displayHelp();stopOptionsProcessing();}void handleDefine(const std::string name, const std::string value){defineProperty(value);}void handleConfig(const std::string name, const std::string value){loadConfiguration(value);}void displayHelp(){HelpFormatter helpFormatter(options());helpFormatter.setCommand(commandName());helpFormatter.setUsage(OPTIONS);helpFormatter.setHeader(A sample application that demonstrates some of the features of the Poco::Util::Application class.);helpFormatter.format(std::cout);}void defineProperty(const std::string def){std::string name;std::string value;std::string::size_type pos def.find();if (pos ! std::string::npos){name.assign(def, 0, pos);value.assign(def, pos 1, def.length() - pos);}else name def;config().setString(name, value);}int main(const ArgVec args){if (!_helpRequested){logger().information(Command line:);std::ostringstream ostr;for (ArgVec::const_iterator it argv().begin(); it ! argv().end(); it){ostr *it ;}logger().information(ostr.str());logger().information(Arguments to main():);for (ArgVec::const_iterator it args.begin(); it ! args.end(); it){logger().information(*it);}logger().information(Application properties:);printProperties();}return Application::EXIT_OK;}void printProperties(const std::string base){AbstractConfiguration::Keys keys;config().keys(base, keys);if (keys.empty()){if (config().hasProperty(base)){std::string msg;msg.append(base);msg.append( );msg.append(config().getString(base));logger().information(msg);}}else{for (AbstractConfiguration::Keys::const_iterator it keys.begin(); it ! keys.end(); it){std::string fullKey base;if (!fullKey.empty()) fullKey .;fullKey.append(*it);printProperties(fullKey);}}}private:bool _helpRequested;
};打印帮助信息
$ ./SampleApp -h
usage: SampleApp OPTIONS
A sample application that demonstrates some of the features of the Poco::Util::Application class.-h, --help display help information on command line arguments
-Dnamevalue, --definenamevalue define a configuration property
-ffile, --config-filefile load configuration data from a file
-bvalue, --bindvalue bind option value to test.property运行后的打印信息
$ ./SampleApp
[Information] Command line:
[Information] ./SampleApp
[Information] Arguments to main():
[Information] Application properties:
[Information] application.argc 1
[Information] application.argv[0] ./SampleApp
[Information] application.baseName SampleApp
[Information] application.cacheDir /home/laoer/.cache/SampleApp/
[Information] application.configDir /home/laoer/git/poco/Util/samples/SampleApp/
[Information] application.dataDir /home/laoer/.local/share/SampleApp/
[Information] application.dir /home/laoer/git/poco/Util/samples/SampleApp/bin/Linux/x86_64/
[Information] application.name SampleApp
[Information] application.path /home/laoer/git/poco/Util/samples/SampleApp/bin/Linux/x86_64/SampleApp
[Information] application.tempDir /home/laoer/.local/tmp/SampleApp/
[Information] logging.channels.c1.class ConsoleChannel
[Information] logging.channels.c1.formatter f1
[Information] logging.formatters.f1.class PatternFormatter
[Information] logging.formatters.f1.pattern [%p] %t
[Information] logging.loggers.app.channel c1
[Information] logging.loggers.app.name Application
[Information] logging.loggers.root.channel.class ConsoleChannel
[Information] system.osName Linux
[Information] system.osVersion 6.2.0-37-generic
[Information] system.osArchitecture x86_64
[Information] system.nodeName laoer
[Information] system.nodeId
[Information] system.currentDir /home/laoer/git/poco/Util/samples/SampleApp/bin/Linux/x86_64/
[Information] system.homeDir /home/laoer/
[Information] system.configHomeDir /home/laoer/.config/
[Information] system.cacheHomeDir /home/laoer/.cache/
[Information] system.dataHomeDir /home/laoer/.local/share/
[Information] system.tempHomeDir /home/laoer/.local/tmp/
[Information] system.tempDir /tmp/
[Information] system.configDir /etc/
[Information] system.dateTime 2023-12-10T14:50:02Z
[Information] system.pid 4919