Fixed compilation issues
[distributed.git] / src / client / distribute.cxx
blob7d766e7cfbfad6071ea04c3a0dfbf369a94a630b
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 <string>
23 #include <iostream>
25 #include "getopt.h"
27 #include "libs/misc/debug.h"
28 #include "libs/misc/environment.h"
29 #include "libs/conf/configuration.h"
31 #define PROGRAM_NAME "distribute"
33 void version(void)
35 std::cout
36 << PROGRAM_NAME << " (" << PACKAGE_NAME << ") " << PACKAGE_VERSION << std::endl
37 << std::endl
38 << "Copyright (C) 2008 Francesco Salvestrini" << std::endl
39 << std::endl
40 << "This is free software. You may redistribute copies of it under the terms of" << std::endl
41 << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>." << std::endl
42 << "There is NO WARRANTY, to the extent permitted by law." << std::endl;
46 #define DEFAULT_TIMEOUT 10
48 void help(void)
50 std::cout
51 << "Usage: " << PROGRAM_NAME << " [OPTION]... "<< std::endl
52 << std::endl
53 << "Options: " << std::endl
54 << " -t, --time-out=TIME time-out in secs (default " << DEFAULT_TIMEOUT << ")" << std::endl
55 << " -j, --job=FILE use FILE as job to distribute" << std::endl
56 << " -d, --debug enable debugging traces" << std::endl
57 << " -h, --help print this help, then exit" << std::endl
58 << " -V, --version print version number, then exit" << std::endl
59 << std::endl
60 << "Report bugs to <" << PACKAGE_BUGREPORT << ">" << std::endl;
63 void hint(const std::string & message)
65 BUG_ON(message.size() == 0);
67 std::cout
68 << message << std::endl
69 << "Try `" << PROGRAM_NAME << " -h' for more information." << std::endl;
72 int main(int argc, char * argv[])
74 TR_CONFIG_LVL(TR_LVL_DEFAULT);
75 TR_CONFIG_PFX(PROGRAM_NAME);
77 try {
78 std::string conffile = "";
80 int time_out = DEFAULT_TIMEOUT;
81 bool time_out_set = false;
82 std::string job = "";
84 int c;
85 //int digit_optind = 0;
86 while (1) {
87 //int this_option_optind = optind ? optind : 1;
88 int option_index = 0;
90 static struct option long_options[] = {
91 { "config", 1, 0, 'c' },
92 { "job", 1, 0, 'j' },
93 { "time-out", 1, 0, 't' },
94 { "debug", 0, 0, 'd' },
95 { "verbose", 0, 0, 'v' },
96 { "version", 0, 0, 'V' },
97 { "help", 0, 0, 'h' },
98 { 0, 0, 0, 0 }
101 c = getopt_long(argc, argv, "c:t:j:dvVh",
102 long_options, &option_index);
103 if (c == -1) {
104 break;
107 switch (c) {
108 case 'c':
109 conffile = optarg;
110 break;
111 case 't':
113 // XXX FIXME:
114 // The current solution is ugly.
115 // Add modifiers (s, m, h) to
116 // this option in order to use
117 // different time granularity.
119 time_out = atoi(optarg);
120 if (time_out < 0) {
121 hint("Wrong time-out value");
122 return 1;
124 time_out_set = true;
125 break;
126 case 'j':
127 job = optarg;
128 break;
129 case 'd':
130 TR_CONFIG_LVL(TR_LVL_DEBUG);
131 break;
132 case 'v':
133 TR_CONFIG_LVL(TR_LVL_VERBOSE);
134 break;
135 case 'V':
136 version();
137 return 0;
138 case 'h':
139 help();
140 return 0;
141 case '?':
142 hint("Unrecognized option");
143 return 1;
145 default:
146 BUG();
147 return 1;
151 // Options related checks
152 BUG_ON(time_out < 0);
154 // Build configuration file path
155 if (conffile.size() == 0) {
156 std::string homedir = Environment::get("HOME");
157 std::string confdir =
158 homedir +
159 std::string("/") +
160 std::string(".") +
161 std::string(PACKAGE_TARNAME);
162 conffile =
163 confdir +
164 std::string("/") +
165 std::string(PROGRAM_NAME);
166 } else {
167 TR_DBG("Configuration file overridden\n");
170 BUG_ON(conffile.size() == 0);
172 TR_DBG("Initial (configuration file) values:\n");
173 TR_DBG(" Time out: '%d'\n", time_out);
175 // Read configuration file (if available)
176 try {
177 TR_DBG("Reading configuration file from '%s'\n",
178 conffile.c_str());
180 Configuration::File config;
181 std::ifstream instream(conffile.c_str());
183 instream >> config;
185 int conf_timeout;
187 if (config.get<int>(conf_timeout, "time-out")) {
188 TR_DBG("Found 'max-mem' key, value '%d'\n",
189 conf_timeout);
191 // Check gathered configuration
192 if (time_out < 0) {
193 TR_ERR("Wrong time-out value in "
194 "configuration file");
195 return 1;
198 if (!time_out_set) {
199 TR_DBG("Updating 'time-out' key\n");
201 // Configuration value not specified
202 // in command line ...
203 time_out = conf_timeout;
206 } catch (std::exception & e) {
207 TR_ERR("%s\n", e.what());
208 } catch (...) {
209 BUG();
212 // Options related checks
213 BUG_ON(time_out < 0);
215 TR_DBG("Final (configuration file) values:\n");
216 TR_DBG(" Time out: '%d'\n", time_out);
218 // Dump (acquired and derived) infos
219 TR_DBG("Final values:\n");
220 TR_DBG(" Time out: '%d'\n", time_out);
221 TR_DBG(" Job: '%s'\n", job.c_str());
222 TR_DBG(" Configuration file: '%s'\n", conffile.c_str());
224 // Assert what is needed to be asserted
225 BUG_ON(time_out < 0);
227 // Do the supposed work
228 MISSING_CODE();
229 } catch (...) {
230 BUG();
233 return 0;