Fawkes API
Fawkes Development Version
|
23 #include <config/change_handler.h>
24 #include <config/netconf.h>
25 #include <config/sqlite.h>
26 #include <config/yaml.h>
27 #include <netcomm/fawkes/client.h>
28 #include <utils/system/argparser.h>
29 #include <utils/system/signal.h>
53 this->config = config;
59 handle_signal(
int signal)
66 config_tag_changed(
const char *new_tag)
68 printf(
"--> New tag loaded: %s\n", new_tag);
74 printf(
"%s %-55s| %-8s| %-14s\n",
88 config_value_erased(
const char *path)
90 printf(
" %-55s| %-8s| %-14s\n", path,
"",
"ERASED");
101 c->wait(FAWKES_CID_CONFIGMANAGER);
118 printf(
"D %-55s| %-8s| %-14s\n",
"Path",
"Type",
"Value");
120 "--------------------------------------------------------------------------------------\n");
130 printf(
"%s %-55s| %-8s| LIST (values below)\n",
135 std::vector<unsigned int> values = i->
get_uints();
136 for (
size_t j = 0; j < values.size(); ++j) {
137 printf(
" %-67s%-14u\n",
"", values[j]);
140 std::vector<int> values = i->
get_ints();
141 for (
size_t j = 0; j < values.size(); ++j) {
142 printf(
" %-67s%-14i\n",
"", values[j]);
145 std::vector<bool> values = i->
get_bools();
146 for (
size_t j = 0; j < values.size(); ++j) {
147 printf(
" %-67s%-14s\n",
"", values[j] ?
"true" :
"false");
151 for (
size_t j = 0; j < values.size(); ++j) {
152 printf(
" %-67s%-14f\n",
"", values[j]);
155 std::vector<std::string> values = i->
get_strings();
156 for (
size_t j = 0; j < values.size(); ++j) {
157 printf(
" %-67s%-14s\n",
"", values[j].c_str());
160 printf(
"%s %-55s| UNKNOWN LIST TYPE\n", (i->
is_default() ?
"*" :
" "), i->
path());
167 printf(
"%s %-55s| %-8s| %-14i\n",
173 printf(
"%s %-55s| %-8s| %-14s\n",
177 (i->
get_bool() ?
"true" :
"false"));
179 printf(
"%s %-55s| %-8s| %-14f\n",
185 printf(
"%s %-55s| %-8s| %-14s\n",
191 printf(
"%s %-55s| UNKNOWN TYPE\n", (i->
is_default() ?
"*" :
" "), i->
path());
198 printf(
"C %-55s: %s\n", i->
path(), comment.c_str());
222 printf(
"%s\n", v ?
"true" :
"false");
228 printf(
"%s\n", v.c_str());
234 printf(
"%-14i\n", i->
get_int());
236 printf(
"%-14s\n", (i->
get_bool() ?
"true" :
"false"));
246 print_usage(
const char *program_name)
248 std::cout <<
"Usage: " << program_name <<
" [options] <cmd>" << std::endl
249 <<
"where cmd is one of the following:" << std::endl
251 <<
" list" << std::endl
252 <<
" List all configuration items" << std::endl
254 <<
" watch" << std::endl
255 <<
" Watch configuration changes" << std::endl
257 <<
" get <path>" << std::endl
258 <<
" Get value for the given path" << std::endl
260 <<
" set <path> <value> [type]" << std::endl
261 <<
" Set value for the given path to the given type and value" << std::endl
262 <<
" where type is one of float/uint/int/bool/string. The type" << std::endl
263 <<
" is only necessary if you are creating a new value" << std::endl
265 <<
" set_default <path> <value> [type]" << std::endl
266 <<
" Set default value for the given path to the given type and value" << std::endl
267 <<
" where type is one of float/uint/int/bool/string. The type" << std::endl
268 <<
" is only necessary if you are creating a new value" << std::endl
270 <<
" set_comment <path> <comment>" << std::endl
271 <<
" Set comment for the given path to the given value. The value at" << std::endl
272 <<
" the given path must already exist in the host-specific configuration."
275 <<
" set_default_comment <path> <comment>" << std::endl
276 <<
" Set default comment for the given path to the given value. The value at"
278 <<
" the given path must already exist in the default configuration." << std::endl
280 <<
" erase <path>" << std::endl
281 <<
" Erase value for given path from config" << std::endl
282 <<
" erase_default <path>" << std::endl
283 <<
" Erase default value for given path from config" << std::endl
285 <<
"and options is none, one or more of the following:" << std::endl
287 <<
" -c Show comments (only available with list and watch cmd)" << std::endl
288 <<
" -a Show all values, even double if default and host-specific" << std::endl
289 <<
" values exist (only available with list and -r)" << std::endl
290 <<
" -q Quiet. Only show important output, suitable for parsing." << std::endl
291 <<
" (not supported for all commands yet) " << std::endl
293 <<
"You may use one of the following options where to retrieve the config from."
295 <<
"The default is '-r localhost'.\n"
297 <<
" -r host[:port] Remote host (and optionally port) to connect to" << std::endl
298 <<
" -f file Config file (relative to CONFDIR) to load" << std::endl
307 main(
int argc,
char **argv)
311 if (argp.has_arg(
"h")) {
312 print_usage(argv[0]);
316 std::string host =
"localhost";
317 unsigned short int port = 1910;
318 if (argp.has_arg(
"r")) {
319 argp.parse_hostport(
"r", host, port);
322 std::string config_file;
323 if (argp.has_arg(
"f")) {
324 config_file = argp.arg(
"f");
328 if (argp.has_arg(
"q")) {
338 if (config_file.empty()) {
343 printf(
"Could not connect to host: %s\n", host.c_str());
350 if (fnmatch(
"*.sql", config_file.c_str(), FNM_PATHNAME) == 0) {
356 config->
load(config_file.c_str());
359 const std::vector<const char *> &args = argp.items();
361 if (args.size() == 0) {
363 printf(
"Not enough args\n\n");
364 print_usage(argv[0]);
365 }
else if (strcmp(
"get", args[0]) == 0) {
366 if (args.size() == 2) {
368 printf(
"Requesting value %s\n", args[1]);
380 printf(
"No such value found!\n");
388 printf(
"You must supply path argument\n");
390 }
else if ((strcmp(
"set", args[0]) == 0) || (strcmp(
"set_default", args[0]) == 0)) {
391 bool set_def = (strcmp(
"set_default", args[0]) == 0);
392 if (args.size() >= 3) {
394 printf(
"Requesting old value for %s\n", args[1]);
401 printf(
"Value does not currently exist in configuration.\n");
404 std::string desired_type =
"";
405 if (args.size() == 4) {
407 desired_type = args[3];
410 if ((desired_type ==
"") && !i->
valid()) {
411 printf(
"Please specify type\n");
413 }
else if ((desired_type !=
"") && (i->
valid() && (desired_type != i->
type()))) {
414 printf(
"The given type '%s' contradicts with type '%s' in config. "
415 "Erase before setting with new type.\n",
416 desired_type.c_str(),
421 desired_type = i->
type();
423 if (desired_type ==
"float") {
425 float f = strtod(args[2], &endptr);
426 if (endptr[0] != 0) {
427 printf(
"ERROR: '%s' is not a float\n", args[2]);
435 }
else if ((desired_type ==
"unsigned int") || (desired_type ==
"uint")) {
437 long int li = strtol(args[2], &endptr, 10);
438 if ((endptr[0] != 0) || (li < 0)) {
439 printf(
"ERROR: '%s' is not an unsigned int\n", args[2]);
447 }
else if (desired_type ==
"int") {
449 long int li = strtol(args[2], &endptr, 10);
450 if (endptr[0] != 0) {
451 printf(
"ERROR: '%s' is not an int\n", args[2]);
459 }
else if (desired_type ==
"bool") {
462 if (strcasecmp(
"true", args[2]) == 0) {
465 }
else if (strcasecmp(
"false", args[2]) == 0) {
469 printf(
"ERROR: '%s' is not a boolean.\n", args[2]);
478 }
else if (desired_type ==
"string") {
485 printf(
"Invalid type: %s\n", desired_type.c_str());
495 printf(
"ERROR: value does not exist\n");
500 printf(
"Usage: %s set <path> <value> [type]\n", argp.program_name());
502 }
else if ((strcmp(
"set_comment", args[0]) == 0)
503 || (strcmp(
"set_default_comment", args[0]) == 0)) {
504 bool set_def = (strcmp(
"set_default_comment", args[0]) == 0);
505 if (args.size() >= 3) {
515 printf(
"Usage: %s set_(default_)comment <path> <value>\n", argp.program_name());
517 }
else if ((strcmp(
"erase", args[0]) == 0) || (strcmp(
"erase_default", args[0]) == 0)) {
518 bool erase_def = (strcmp(
"erase_default", args[0]) == 0);
519 if (args.size() == 2) {
520 printf(
"Erasing %svalue %s\n", (erase_def ?
"default " :
""), args[1]);
528 printf(
"No such value found!\n");
535 config->
erase(args[1]);
539 printf(
"Failed to erase %s (default vs. non-default?)\n", args[1]);
541 printf(
"Successfully erased %s\n", args[1]);
547 printf(
"You must supply path argument\n");
549 }
else if (strcmp(
"watch", args[0]) == 0) {
562 print_line(i, argp.has_arg(
"c"));
567 "------------------------------------------------------------------------------------\n");
568 printf(
"Modifications since watching:\n");
570 "------------------------------------------------------------------------------------\n");
573 }
else if (strcmp(
"list", args[0]) == 0) {
576 printf(
"Transmitting config from host... ");
588 bool show_comments = argp.has_arg(
"c");
589 if (argp.has_arg(
"a") && netconf) {
590 printf(
"DEFAULT ENTRIES\n");
593 print_line(i, show_comments);
596 printf(
"HOST-SPECIFIC ENTRIES\n");
599 print_line(i, show_comments);
605 print_line(i, show_comments);
613 printf(
"Cleaning up... ");
virtual bool valid() const =0
ValueIterator * iterator_hostspecific()
Iterator for all host-specific values.
virtual void set_string(const char *path, std::string &s)=0
virtual void set_default_comment(const char *path, const char *comment)=0
virtual ValueIterator * iterator()=0
virtual bool get_bool() const =0
virtual void set_mirror_mode(bool mirror)
Enable or disable mirror mode.
virtual std::vector< unsigned int > get_uints() const =0
virtual void set_default_float(const char *path, float f)=0
void connect()
Connect to remote.
virtual bool is_string() const =0
virtual std::string get_as_string() const =0
virtual void load(const char *file_path)=0
virtual void set_default_int(const char *path, int i)=0
virtual std::string get_string() const =0
ValueIterator * iterator_default()
Iterator for all default values.
virtual void erase_default(const char *path)=0
virtual bool is_list() const =0
virtual std::vector< bool > get_bools() const =0
void disconnect()
Disconnect socket.
virtual bool is_bool() const =0
virtual void set_int(const char *path, int i)=0
virtual void set_default_bool(const char *path, bool b)=0
virtual bool is_default() const =0
virtual void set_float(const char *path, float f)=0
virtual void set_bool(const char *path, bool b)=0
virtual void erase(const char *path)=0
virtual std::vector< int > get_ints() const =0
virtual bool is_float() const =0
void print_trace()
Prints trace to stderr.
virtual void set_default_uint(const char *path, unsigned int uint)=0
virtual void rem_change_handler(ConfigurationChangeHandler *h)
Remove a configuration change handler.
virtual bool is_int() const =0
virtual void set_default_string(const char *path, std::string &s)=0
virtual std::string get_comment() const =0
virtual void set_comment(const char *path, const char *comment)=0
virtual int get_int() const =0
virtual const char * type() const =0
virtual std::vector< float > get_floats() const =0
virtual std::vector< std::string > get_strings() const =0
virtual void add_change_handler(ConfigurationChangeHandler *h)
Add a configuration change handler.
virtual const char * path() const =0
virtual void set_uint(const char *path, unsigned int uint)=0
virtual ValueIterator * get_value(const char *path)=0
virtual unsigned int get_uint() const =0
virtual bool is_uint() const =0
virtual float get_float() const =0