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.
26 #include "libs/misc/debug.h"
27 #include "libs/misc/environment.h"
28 #include "libs/conf/configuration.h"
30 #define PROGRAM_NAME "distribute"
35 << PROGRAM_NAME
<< " (" << PACKAGE_NAME
<< ") " << PACKAGE_VERSION
<< std::endl
37 << "Copyright (C) 2008 Francesco Salvestrini" << 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
50 << "Usage: " << PROGRAM_NAME
<< " [OPTION]... "<< 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
59 << "Report bugs to <" << PACKAGE_BUGREPORT
<< ">" << std::endl
;
62 void hint(const std::string
& message
)
64 BUG_ON(message
.size() == 0);
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
);
77 std::string conffile
= "";
79 int time_out
= DEFAULT_TIMEOUT
;
80 bool time_out_set
= false;
84 //int digit_optind = 0;
86 //int this_option_optind = optind ? optind : 1;
89 static struct option long_options
[] = {
90 { "config", 1, 0, 'c' },
92 { "time-out", 1, 0, 't' },
93 { "debug", 0, 0, 'd' },
94 { "version", 0, 0, 'V' },
95 { "help", 0, 0, 'h' },
99 c
= getopt_long(argc
, argv
, "c:t:j:dVh",
100 long_options
, &option_index
);
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
);
119 hint("Wrong time-out value");
128 TR_CONFIG_LVL(TR_LVL_DEBUG
);
137 hint("Unrecognized option");
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
=
156 std::string(PACKAGE_TARNAME
);
160 std::string(PROGRAM_NAME
);
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)
172 TR_DBG("Reading configuration file from '%s'\n",
175 Configuration::File config
;
176 std::ifstream
instream(conffile
.c_str());
182 if (config
.get
<int>(conf_timeout
, "time-out")) {
183 TR_DBG("Found 'max-mem' key, value '%d'\n",
186 // Check gathered configuration
188 TR_ERR("Wrong time-out value in "
189 "configuration file");
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());
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