23 #include <netcomm/fawkes/client.h> 25 #include <plugins/openprs/mod_utils.h> 26 #include <config/netconf.h> 28 #include <oprs_f-pub.h> 32 extern "C" void finalize();
41 action_config_load(TermList terms)
44 ACTION_ASSERT_ARG_LENGTH(
"config-load", terms, 1);
45 ACTION_SET_AND_ASSERT_ARG_TYPE(
"config-load", prefix, terms, 1, STRING);
47 #if __cplusplus >= 201103L 48 std::unique_ptr<Configuration::ValueIterator> v(g_config->
search(prefix->u.string));
50 std::auto_ptr<Configuration::ValueIterator> v(g_config->
search(prefix->u.string));
53 TermList tl = sl_make_slist();
54 tl = build_term_list(tl, build_string(v->path()));
57 std::string type =
"";
58 std::string value = v->get_as_string();
61 tl = build_term_list(tl, build_id(declare_atom(
"UINT")));
63 TermList ll = sl_make_slist();
64 std::vector<unsigned int> uints = v->get_uints();
65 for (
size_t i = 0; i < uints.size(); ++i) {
66 ll = build_term_list(ll, build_long_long(uints[i]));
68 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
70 tl = build_term_list(tl, build_long_long(v->get_uint()));
72 }
else if (v->is_int()) {
73 tl = build_term_list(tl, build_id(declare_atom(
"INT")));
75 TermList ll = sl_make_slist();
76 std::vector<int> ints = v->get_ints();
77 for (
size_t i = 0; i < ints.size(); ++i) {
78 ll = build_term_list(ll, build_integer(ints[i]));
80 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
82 tl = build_term_list(tl, build_integer(v->get_int()));
84 }
else if (v->is_float()) {
85 tl = build_term_list(tl, build_id(declare_atom(
"FLOAT")));
87 TermList ll = sl_make_slist();
88 std::vector<float> floats = v->get_floats();
89 for (
size_t i = 0; i < floats.size(); ++i) {
90 ll = build_term_list(ll, build_float(floats[i]));
92 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
94 tl = build_term_list(tl, build_float(v->get_float()));
96 }
else if (v->is_bool()) {
97 tl = build_term_list(tl, build_id(declare_atom(
"BOOL")));
99 TermList ll = sl_make_slist();
100 std::vector<bool> bools = v->get_bools();
101 for (
size_t i = 0; i < bools.size(); ++i) {
102 ll = build_term_list(ll, bools[i] ? build_t() : build_nil());
104 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
106 tl = build_term_list(tl, v->get_bool() ? build_t() : build_nil());
108 }
else if (v->is_string()) {
109 tl = build_term_list(tl, build_id(declare_atom(
"STRING")));
111 TermList ll = sl_make_slist();
112 std::vector<std::string> strings = v->get_strings();
113 for (
size_t i = 0; i < strings.size(); ++i) {
114 ll = build_term_list(ll, build_string(strings[i].c_str()));
116 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
118 tl = build_term_list(tl, build_string(v->get_string().c_str()));
121 fprintf(stderr,
"Warn[config-load]: value at '%s' of unknown type '%s'",
122 v->path(), v->type());
125 add_external_fact((
char *)
"confval", tl);
128 TermList tl = sl_make_slist();
129 tl = build_term_list(tl, build_string(prefix->u.string));
130 add_external_fact((
char *)
"config-loaded", tl);
138 pred_string_prefix_p(TermList terms)
141 ACTION_ASSERT_B_ARG_LENGTH(
"string-prefix-p", terms, 2);
142 ACTION_SET_AND_ASSERT_B_ARG_TYPE(
"string-prefix-p", str, terms, 1, STRING);
143 ACTION_SET_AND_ASSERT_B_ARG_TYPE(
"string-prefix-p", prefix, terms, 2, STRING);
145 return (strncmp(str->u.string, prefix->u.string, strlen(prefix->u.string)) == 0);
150 func_string_remove_prefix(TermList terms)
153 ACTION_ASSERT_ARG_LENGTH(
"string-remove-prefix", terms, 2);
154 ACTION_SET_AND_ASSERT_ARG_TYPE(
"string-remove-prefix", str, terms, 1, STRING);
155 ACTION_SET_AND_ASSERT_ARG_TYPE(
"string-remove-prefix", prefix, terms, 2, STRING);
157 if (! pred_string_prefix_p(terms))
158 return build_string(str->u.string);
160 if (strlen(prefix->u.string) >= strlen(str->u.string))
161 return build_string(
"");
163 return build_string(&str->u.string[strlen(prefix->u.string)]);
171 printf(
"*** LOADING mod_config\n");
173 std::string fawkes_host;
174 unsigned short fawkes_port = 0;
175 get_fawkes_host_port(fawkes_host, fawkes_port);
177 printf(
"Connecting to Fawkes at %s:%u\n", fawkes_host.c_str(), fawkes_port);
184 fprintf(stderr,
"Error: cannot establish network connection: %s\n",
188 make_and_declare_action(
"config-load", action_config_load, 1);
189 make_and_declare_eval_pred(
"string-prefix-p", pred_string_prefix_p, 2, FALSE);
190 make_and_declare_eval_funct(
"string-remove-prefix", func_string_remove_prefix, 2);
191 add_user_end_kernel_hook(finalize);
198 printf(
"*** DESTROYING mod_config\n");
201 delete g_fnet_client;
202 g_fnet_client = NULL;
Simple Fawkes network client.
Fawkes library namespace.
void connect()
Connect to remote.
Base class for exceptions in Fawkes.
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
virtual void set_mirror_mode(bool mirror)
Enable or disable mirror mode.
ValueIterator * search(const char *path)
Iterator with search results.
Remote configuration via Fawkes net.