Fixed configuration related code
[distributed.git] / src / client / distribute.cxx
blob70e540fbffd00dca02a42a936a38afb5daa20a00
1 //
2 // Copyright (C) 2008 Francesco Salvestrini
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <string>
24 #include <iostream>
26 #include "libs/misc/debug.h"
27 #include "libs/misc/environment.h"
28 #include "libs/conf/configuration.h"
30 #define PROGRAM_NAME "distribute"
32 void version(void)
34 std::cout
35 << PROGRAM_NAME << " (" << PACKAGE_NAME << ") " << PACKAGE_VERSION << std::endl
36 << std::endl
37 << "Copyright (C) 2008 Francesco Salvestrini" << std::endl
38 << std::endl
39 << "This is free software. You may redistribute copies of it under the terms of" << std::endl
40 << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>." << std::endl
41 << "There is NO WARRANTY, to the extent permitted by law." << std::endl;
45 #define DEFAULT_TIMEOUT 10
47 void help(void)
49 std::cout
50 << "Usage: " << PROGRAM_NAME << " [OPTION]... "<< std::endl
51 << std::endl
52 << "Options: " << std::endl
53 << " -t, --time-out=TIME time-out in secs (default " << DEFAULT_TIMEOUT << ")" << std::endl
54 << " -j, --job=FILE use FILE as job to distribute" << std::endl
55 << " -d, --debug enable debugging traces" << std::endl
56 << " -h, --help print this help, then exit" << std::endl
57 << " -V, --version print version number, then exit" << std::endl
58 << std::endl
59 << "Report bugs to <" << PACKAGE_BUGREPORT << ">" << std::endl;
62 void hint(const std::string & message)
64 BUG_ON(message.size() == 0);
66 std::cout
67 << message << std::endl
68 << "Try `" << PROGRAM_NAME << " -h' for more information." << std::endl;
71 int main(int argc, char * argv[])
73 TR_CONFIG_LVL(TR_LVL_DEFAULT);
74 TR_CONFIG_PFX(PROGRAM_NAME);
76 try {
77 std::string conffile = "";
79 int time_out = DEFAULT_TIMEOUT;
80 bool time_out_set = false;
81 std::string job = "";
83 int c;
84 //int digit_optind = 0;
85 while (1) {
86 //int this_option_optind = optind ? optind : 1;
87 int option_index = 0;
89 static struct option long_options[] = {
90 { "config", 1, 0, 'c' },
91 { "job", 1, 0, 'j' },
92 { "time-out", 1, 0, 't' },
93 { "debug", 0, 0, 'd' },
94 { "version", 0, 0, 'V' },
95 { "help", 0, 0, 'h' },
96 { 0, 0, 0, 0 }
99 c = getopt_long(argc, argv, "c:t:j:dVh",
100 long_options, &option_index);
101 if (c == -1) {
102 break;
105 switch (c) {
106 case 'c':
107 conffile = optarg;
108 break;
109 case 't':
111 // XXX FIXME:
112 // The current solution is ugly.
113 // Add modifiers (s, m, h) to
114 // this option in order to use
115 // different time granularity.
117 time_out = atoi(optarg);
118 if (time_out < 0) {
119 hint("Wrong time-out value");
120 return 1;
122 time_out_set = true;
123 break;
124 case 'j':
125 job = optarg;
126 break;
127 case 'd':
128 TR_CONFIG_LVL(TR_LVL_DEBUG);
129 break;
130 case 'V':
131 version();
132 return 0;
133 case 'h':
134 help();
135 return 0;
136 case '?':
137 hint("Unrecognized option");
138 return 1;
140 default:
141 BUG();
142 return 1;
146 // Options related checks
147 BUG_ON(time_out < 0);
149 // Build configuration file path
150 if (conffile.size() == 0) {
151 std::string homedir = Environment::get("HOME");
152 std::string confdir =
153 homedir +
154 std::string("/") +
155 std::string(".") +
156 std::string(PACKAGE_TARNAME);
157 conffile =
158 confdir +
159 std::string("/") +
160 std::string(PROGRAM_NAME);
161 } else {
162 TR_DBG("Configuration file overridden\n");
165 BUG_ON(conffile.size() == 0);
167 TR_DBG("Initial (configuration file) values:\n");
168 TR_DBG(" Time out: '%d'\n", time_out);
170 // Read configuration file (if available)
171 try {
172 TR_DBG("Reading configuration file from '%s'\n",
173 conffile.c_str());
175 Configuration::File config;
176 std::ifstream instream(conffile.c_str());
178 instream >> config;
180 int conf_timeout;
182 if (config.get<int>(conf_timeout, "time-out")) {
183 TR_DBG("Found 'max-mem' key, value '%d'\n",
184 conf_timeout);
186 // Check gathered configuration
187 if (time_out < 0) {
188 TR_ERR("Wrong time-out value in "
189 "configuration file");
190 return 1;
193 if (!time_out_set) {
194 TR_DBG("Updating 'time-out' key\n");
196 // Configuration value not specified
197 // in command line ...
198 time_out = conf_timeout;
201 } catch (std::exception & e) {
202 TR_ERR("%s\n", e.what());
203 } catch (...) {
204 BUG();
207 // Options related checks
208 BUG_ON(time_out < 0);
210 TR_DBG("Final (configuration file) values:\n");
211 TR_DBG(" Time out: '%d'\n", time_out);
213 // Dump (acquired and derived) infos
214 TR_DBG("Final values:\n");
215 TR_DBG(" Time out: '%d'\n", time_out);
216 TR_DBG(" Job: '%s'\n", job.c_str());
217 TR_DBG(" Configuration file: '%s'\n", conffile.c_str());
219 // Assert what is needed to be asserted
220 BUG_ON(time_out < 0);
222 // Do the supposed work
223 MISSING_CODE();
224 } catch (...) {
225 BUG();
228 return 0;