2 // Copyright (C) 2008 Francesco Salvestrini
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.
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.
27 #include "libs/misc/debug.h"
28 #include "libs/misc/environment.h"
29 #include "libs/conf/configuration.h"
31 #define PROGRAM_NAME "distribute"
36 << PROGRAM_NAME
<< " (" << PACKAGE_NAME
<< ") " << PACKAGE_VERSION
<< std::endl
38 << "Copyright (C) 2008 Francesco Salvestrini" << 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
51 << "Usage: " << PROGRAM_NAME
<< " [OPTION]... "<< 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
60 << "Report bugs to <" << PACKAGE_BUGREPORT
<< ">" << std::endl
;
63 void hint(const std::string
& message
)
65 BUG_ON(message
.size() == 0);
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
);
78 std::string conffile
= "";
80 int time_out
= DEFAULT_TIMEOUT
;
81 bool time_out_set
= false;
85 //int digit_optind = 0;
87 //int this_option_optind = optind ? optind : 1;
90 static struct option long_options
[] = {
91 { "config", 1, 0, 'c' },
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' },
101 c
= getopt_long(argc
, argv
, "c:t:j:dvVh",
102 long_options
, &option_index
);
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
);
121 hint("Wrong time-out value");
130 TR_CONFIG_LVL(TR_LVL_DEBUG
);
133 TR_CONFIG_LVL(TR_LVL_VERBOSE
);
142 hint("Unrecognized option");
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
=
161 std::string(PACKAGE_TARNAME
);
165 std::string(PROGRAM_NAME
);
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)
177 TR_DBG("Reading configuration file from '%s'\n",
180 Configuration::File config
;
181 std::ifstream
instream(conffile
.c_str());
187 if (config
.get
<int>(conf_timeout
, "time-out")) {
188 TR_DBG("Found 'max-mem' key, value '%d'\n",
191 // Check gathered configuration
193 TR_ERR("Wrong time-out value in "
194 "configuration file");
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());
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