23 #include <netcomm/fawkes/client.h> 24 #include <config/netconf.h> 25 #include <config/change_handler.h> 26 #include <utils/system/argparser.h> 27 #include <utils/system/signal.h> 51 this->config = config;
64 printf(
"--> New tag loaded: %s\n", new_tag);
69 printf(
"%s %-55s| %-8s| %-14s\n", v->
is_default() ?
"*" :
" ",
75 printf(
"%s %s: %s\n", v->
is_default() ?
"C" :
"c",
81 printf(
" %-55s| %-8s| %-14s\n", path,
"",
"ERASED");
92 c->wait(FAWKES_CID_CONFIGMANAGER);
108 printf(
"D %-55s| %-8s| %-14s\n",
"Path",
"Type",
"Value");
109 printf(
"--------------------------------------------------------------------------------------\n");
119 printf(
"%s %-55s| %-8s| LIST (values below)\n", (i->
is_default() ?
"*" :
" "),
122 std::vector<unsigned int> values = i->
get_uints();
123 for (
size_t j = 0; j < values.size(); ++j) {
124 printf(
" %-67s%-14u\n",
"", values[j]);
126 }
else if ( i->
is_int() ) {
127 std::vector<int> values = i->
get_ints();
128 for (
size_t j = 0; j < values.size(); ++j) {
129 printf(
" %-67s%-14i\n",
"", values[j]);
132 std::vector<bool> values = i->
get_bools();
133 for (
size_t j = 0; j < values.size(); ++j) {
134 printf(
" %-67s%-14s\n",
"", values[j] ?
"true" :
"false");
138 for (
size_t j = 0; j < values.size(); ++j) {
139 printf(
" %-67s%-14f\n",
"", values[j]);
142 std::vector<std::string> values = i->
get_strings();
143 for (
size_t j = 0; j < values.size(); ++j) {
144 printf(
" %-67s%-14s\n",
"", values[j].c_str());
147 printf(
"%s %-55s| UNKNOWN LIST TYPE\n", (i->
is_default() ?
"*" :
" "), i->
path());
152 }
else if ( i->
is_int() ) {
161 printf(
"%s %-55s| UNKNOWN TYPE\n", (i->
is_default() ?
"*" :
" "), i->
path());
168 printf(
"C %-55s: %s\n", i->
path(), comment.c_str());
184 printf(
"%-14s\n",
"LIST");
188 }
else if ( i->
is_int() ) {
189 printf(
"%-14i\n", i->
get_int());
191 printf(
"%-14s\n", (i->
get_bool() ?
"true" :
"false"));
202 print_usage(
const char *program_name)
204 std::cout <<
"Usage: " << program_name <<
" [options] <cmd>" << std::endl
205 <<
"where cmd is one of the following:" << std::endl << std::endl
206 <<
" list" << std::endl
207 <<
" List all configuration items" << std::endl << std::endl
208 <<
" watch" << std::endl
209 <<
" Watch configuration changes" << std::endl << std::endl
210 <<
" get <path>" << std::endl
211 <<
" Get value for the given path" << std::endl << std::endl
212 <<
" set <path> <value> [type]" << std::endl
213 <<
" Set value for the given path to the given type and value" << std::endl
214 <<
" where type is one of float/uint/int/bool/string. The type" << std::endl
215 <<
" is only necessary if you are creating a new value" << std::endl << std::endl
216 <<
" set_default <path> <value> [type]" << std::endl
217 <<
" Set default value for the given path to the given type and value" << std::endl
218 <<
" where type is one of float/uint/int/bool/string. The type" << std::endl
219 <<
" is only necessary if you are creating a new value" << std::endl << std::endl
220 <<
" set_comment <path> <comment>" << std::endl
221 <<
" Set comment for the given path to the given value. The value at" << std::endl
222 <<
" the given path must already exist in the host-specific configuration." << std::endl << std::endl
223 <<
" set_default_comment <path> <comment>" << std::endl
224 <<
" Set default comment for the given path to the given value. The value at" << std::endl
225 <<
" the given path must already exist in the default configuration." << std::endl << std::endl
226 <<
" erase <path>" << std::endl
227 <<
" Erase value for given path from config" << std::endl
228 <<
" erase_default <path>" << std::endl
229 <<
" Erase default value for given path from config" << std::endl << std::endl
230 <<
"and options is none, one or more of the following:" << std::endl << std::endl
231 <<
" -c Show comments (only available with list and watch cmd)" << std::endl
232 <<
" -a Show all values, even double if default and host-specific " << std::endl
233 <<
" values exist (only available with list)" << std::endl
234 <<
" -q Quiet. Only show important output, suitable for parsing. " << std::endl
235 <<
" (not supported for all commands yet) " << std::endl
236 <<
" -r host[:port] Remote host (and optionally port) to connect to\n" << std::endl
245 main(
int argc,
char **argv)
250 print_usage(argv[0]);
254 std::string host =
"localhost";
255 unsigned short int port = 1910;
271 printf(
"Could not connect to host: %s\n", host.c_str());
277 const std::vector< const char* > & args = argp.
items();
279 if ( args.size() == 0) {
281 printf(
"Not enough args\n\n");
282 print_usage(argv[0]);
283 }
else if (strcmp(
"get", args[0]) == 0) {
284 if (args.size() == 2) {
286 printf(
"Requesting value %s\n", args[1]);
297 printf(
"No such value found!\n");
302 printf(
"You must supply path argument\n");
304 }
else if ((strcmp(
"set", args[0]) == 0) || (strcmp(
"set_default", args[0]) == 0)) {
305 bool set_def = (strcmp(
"set_default", args[0]) == 0);
306 if (args.size() >= 3) {
308 printf(
"Requesting old value for %s\n", args[1]);
315 printf(
"Value does not currently exist in configuration.\n");
318 std::string desired_type =
"";
319 if (args.size() == 4) {
321 desired_type = args[3];
324 if ( (desired_type ==
"") && ! i->
valid()) {
325 printf(
"Please specify type\n");
327 }
else if ( (desired_type !=
"") && (i->
valid() && (desired_type != i->
type())) ) {
328 printf(
"The given type '%s' contradicts with type '%s' in config. " 329 "Erase before setting with new type.\n", desired_type.c_str(), i->
type());
332 if ( i->
valid() ) desired_type = i->
type();
334 if ( desired_type ==
"float" ) {
336 float f = strtod(args[2], &endptr);
337 if ( endptr[0] != 0 ) {
338 printf(
"ERROR: '%s' is not a float\n", args[2]);
346 }
else if ( (desired_type ==
"unsigned int") || (desired_type ==
"uint") ) {
348 long int li = strtol(args[2], &endptr, 10);
349 if ( (endptr[0] != 0) || (li < 0) ) {
350 printf(
"ERROR: '%s' is not an unsigned int\n", args[2]);
358 }
else if ( desired_type ==
"int" ) {
360 long int li = strtol(args[2], &endptr, 10);
361 if ( endptr[0] != 0 ) {
362 printf(
"ERROR: '%s' is not an int\n", args[2]);
370 }
else if ( desired_type ==
"bool" ) {
373 if ( strcasecmp(
"true", args[2]) == 0 ) {
376 }
else if ( strcasecmp(
"false", args[2]) == 0 ) {
380 printf(
"ERROR: '%s' is not a boolean.\n", args[2]);
389 }
else if ( desired_type ==
"string" ) {
396 printf(
"Invalid type: %s\n", desired_type.c_str());
406 printf(
"ERROR: value does not exist\n");
412 printf(
"Usage: %s set <path> <value> [type]\n", argp.
program_name());
414 }
else if ((strcmp(
"set_comment", args[0]) == 0) ||
415 (strcmp(
"set_default_comment", args[0]) == 0)) {
416 bool set_def = (strcmp(
"set_default_comment", args[0]) == 0);
417 if (args.size() >= 3) {
427 printf(
"Usage: %s set_(default_)comment <path> <value>\n", argp.
program_name());
429 }
else if ((strcmp(
"erase", args[0]) == 0) || (strcmp(
"erase_default", args[0]) == 0)) {
430 bool erase_def = (strcmp(
"erase_default", args[0]) == 0);
431 if (args.size() == 2) {
432 printf(
"Erasing %svalue %s\n", (erase_def ?
"default " :
""), args[1]);
440 printf(
"No such value found!\n");
447 netconf->
erase(args[1]);
451 printf(
"Failed to erase %s (default vs. non-default?)\n", args[1]);
453 printf(
"Successfully erased %s\n", args[1]);
459 printf(
"You must supply path argument\n");
461 }
else if (strcmp(
"watch", args[0]) == 0) {
471 while ( i->
next() ) {
472 print_line(i, argp.
has_arg(
"c"));
476 printf(
"------------------------------------------------------------------------------------\n");
477 printf(
"Modifications since watching:\n");
478 printf(
"------------------------------------------------------------------------------------\n");
481 }
else if (strcmp(
"list", args[0]) == 0) {
482 printf(
"Transmitting config from host... ");
493 bool show_comments = argp.
has_arg(
"c");
495 printf(
"DEFAULT ENTRIES\n");
497 while ( i->
next() ) {
498 print_line(i, show_comments);
501 printf(
"HOST-SPECIFIC ENTRIES\n");
503 while ( i->
next() ) {
504 print_line(i, show_comments);
509 while ( i->
next() ) {
510 print_line(i, show_comments);
518 printf(
"Cleaning up... ");
virtual std::string get_comment() const =0
Get comment of value.
const char * program_name() const
Get name of program.
bool parse_hostport(const char *argn, char **host, unsigned short int *port)
Parse host:port string.
Simple Fawkes network client.
virtual std::vector< bool > get_bools() const =0
Get list of values from configuration which is of type bool.
const std::vector< const char *> & items() const
Get non-option items.
virtual std::vector< std::string > get_strings() const =0
Get list of values from configuration which is of type string.
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
virtual std::vector< float > get_floats() const =0
Get list of values from configuration which is of type float.
virtual const char * type() const =0
Type of value.
virtual bool is_bool() const =0
Check if current value is a bool.
Fawkes library namespace.
void disconnect()
Disconnect socket.
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
Interface for configuration change handling.
void lock()
Lock the config.
void connect()
Connect to remote.
ValueIterator * iterator()
Iterator for all values.
Interface for signal handling.
Parse command line arguments.
virtual bool next()=0
Check if there is another element and advance to this if possible.
virtual std::vector< int > get_ints() const =0
Get list of values from configuration which is of type int.
virtual float get_float() const =0
Get float value.
virtual unsigned int get_uint() const =0
Get unsigned int value.
void unlock()
Unlock the config.
virtual bool is_float() const =0
Check if current value is a float.
virtual bool is_int() const =0
Check if current value is a int.
ValueIterator * iterator_default()
Iterator for all default values.
virtual bool get_bool() const =0
Get bool value.
virtual int get_int() const =0
Get int value.
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
virtual std::string get_as_string() const =0
Get value as string.
virtual bool is_string() const =0
Check if current value is a string.
virtual std::vector< unsigned int > get_uints() const =0
Get list of values from configuration which is of type unsigned int.
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
Base class for exceptions in Fawkes.
virtual void rem_change_handler(ConfigurationChangeHandler *h)
Remove a configuration change handler.
virtual bool is_uint() const =0
Check if current value is a unsigned int.
virtual bool is_list() const =0
Check if a value is a list.
virtual void erase(const char *path)
Erase the given value from the configuration.
virtual std::string get_string() const =0
Get string value.
ValueIterator * iterator_hostspecific()
Iterator for all host-specific values.
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.
virtual void set_mirror_mode(bool mirror)
Enable or disable mirror mode.
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
virtual const char * path() const =0
Path of value.
virtual bool valid() const =0
Check if the current element is valid.
void print_trace()
Prints trace to stderr.
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.
Iterator interface to iterate over config values.
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
virtual bool is_default() const =0
Check if current value was read from the default config.
virtual void add_change_handler(ConfigurationChangeHandler *h)
Add a configuration change handler.
bool has_arg(const char *argn)
Check if argument has been supplied.
virtual void set_default_comment(const char *path, std::string &comment)
Set new default comment for existing default configuration value.
Interface for configuration handling.
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
Remote configuration via Fawkes net.
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
virtual void erase_default(const char *path)
Erase the given default value from the configuration.