Some code fixes done to multicast service classes.
[tourist.git] / client / Tourist.cpp
blobedd8499425e5ff9b4446a7b4935f3ff1f7be1e7b
1 // Peer test
3 #include "Poco/Util/ServerApplication.h"
4 #include "Poco/Task.h"
5 #include "Poco/TaskManager.h"
6 #include "Poco/Util/Option.h"
7 #include "Poco/Util/OptionSet.h"
8 #include "Poco/Util/HelpFormatter.h"
9 #include "Poco/Util/AbstractConfiguration.h"
10 #include "Poco/Util/IntValidator.h"
11 #include "Poco/AutoPtr.h"
12 #include "Poco/Thread.h"
13 #include <ctime>
14 #include <sys/time.h>
15 #include <iostream>
16 #include <string>
18 #include "Tourist/App/Application.h"
19 #include "Tourist/Util/Config.h"
22 //#include "Poco/FileChannel.h"
24 using Poco::Thread;
25 using Poco::Util::Application;
27 //using Poco::FileChannel;
28 using Poco::Util::ServerApplication;
29 using Poco::Task;
30 using Poco::TaskManager;
31 using Poco::Util::Option;
32 using Poco::Util::OptionSet;
33 using Poco::Util::HelpFormatter;
34 using Poco::Util::AbstractConfiguration;
35 using Poco::Util::OptionCallback;
36 using Poco::AutoPtr;
38 class TouristClientTask: public Task {
39 public:
40 TouristClientTask(): Task("TouristClientTask") {
43 void runTask()
45 fprintf(stdout, "Tourist sequence initiating!!\n");
46 Application &app = Application::instance();
48 struct timeval tv;
49 struct timezone tz;
50 //To ensure that we get fairly random Id on
51 // same host.
52 if (gettimeofday(&tv, &tz)==-1)
53 srand ( time(NULL) );
54 else
55 srand (tv.tv_usec); // Should be more random than time()
57 std::string logfile = app.config().getString("logfile");
58 std::string propfile = app.config().getString("propfile");
60 printf("Node properties file %s\n", propfile.c_str());
61 printf("Logging properties file %s\n", logfile.c_str());
63 Tourist::Util::Config config(logfile, propfile);
64 Tourist::App::Application app1(config);
66 if(app1.init() != 0)
68 fprintf(stderr, "Failed to initialize the application, check log for detail!");
69 return;
72 Thread t;
73 t.start(app1);
75 while(1)
76 Thread::sleep(500);
80 class TouristClient: public ServerApplication {
81 public:
82 TouristClient(): _helpRequested(false) {}
84 private:
85 bool _helpRequested;
87 protected:
88 void initialize(Application& self) {
89 loadConfiguration(); // load default configuration files, if present
90 Application::initialize(self);
91 // add your own initialization code here
94 void uninitialize() {
95 // add your own uninitialization code here
96 Application::uninitialize();
99 void reinitialize(Application& self) {
100 Application::reinitialize(self);
101 // add your own reinitialization code here
104 void defineOptions(OptionSet& options) {
105 Application::defineOptions(options);
107 options.addOption(
108 Option("help", "h", "display help information on command line arguments")
109 .required(false)
110 .repeatable(false)
111 .callback(OptionCallback<TouristClient>(this, &TouristClient::handleHelp)));
113 /*options.addOption(
114 Option("config-file", "f", "load configuration data from a file")
115 .required(false)
116 .repeatable(true)
117 .argument("FILE")
118 .callback(OptionCallback<TouristClient>(this, &TouristClient::handleConfig)));*/
120 options.addOption(
121 Option("properties", "l", "lists all configuration properties")
122 .required(false)
123 .repeatable(false)
124 .binding("application.listprops"));
126 options.addOption(
127 Option("propfile", "p", "Tourist node properties file")
128 .required(false)
129 .repeatable(false)
130 .argument("FILE")
131 .binding("propfile"));
133 options.addOption(
134 Option("logfile", "o", "logging configuration")
135 .required(false)
136 .repeatable(false)
137 .argument("FILE")
138 .binding("logfile"));
142 void handleHelp(const std::string& name, const std::string& value) {
143 _helpRequested = true;
144 displayHelp();
145 stopOptionsProcessing();
148 void handleConfig(const std::string& name, const std::string& value) {
149 std::cout<<"Name : "<<name<<" value: " <<value<<std::endl;
150 loadConfiguration(value);
153 void displayHelp() {
154 HelpFormatter helpFormatter(options());
155 helpFormatter.setCommand(commandName());
156 helpFormatter.setUsage("OPTIONS");
157 helpFormatter.setHeader("A test application for the tourist libraries");
158 helpFormatter.format(std::cout);
161 void printProperties(const std::string& base) {
162 AbstractConfiguration::Keys keys;
163 config().keys(base, keys);
164 if (keys.empty()) {
165 if (config().hasProperty(base)) {
166 std::string msg;
167 msg.append(base);
168 msg.append(" = ");
169 msg.append(config().getString(base));
170 logger().information(msg);
172 } else {
173 for (AbstractConfiguration::Keys::const_iterator it = keys.begin();
174 it != keys.end(); ++it) {
175 std::string fullKey = base;
176 if (!fullKey.empty()) fullKey += '.';
177 fullKey.append(*it);
178 printProperties(fullKey);
183 int main(const std::vector<std::string>& args) {
184 if (config().hasProperty("application.listprops") && !_helpRequested){
185 printProperties("");
186 return Application::EXIT_OK;
189 if (_helpRequested) return Application::EXIT_OK;
191 TaskManager tm;
192 tm.start(new TouristClientTask);
193 waitForTerminationRequest();
194 tm.cancelAll();
195 tm.joinAll();
196 return Application::EXIT_OK;
200 int main(int arg, char** argv)
202 TouristClient tourist;
203 return tourist.run(arg, argv);