24 #include <config/net_handler.h> 25 #include <config/net_messages.h> 26 #include <config/net_list_content.h> 27 #include <logging/liblogger.h> 29 #include <netcomm/fawkes/component_ids.h> 30 #include <netcomm/fawkes/hub.h> 31 #include <config/config.h> 54 :
Thread(
"ConfigNetworkHandler",
Thread::OPMODE_WAITFORWAKEUP),
75 __inbound_queue.
clear();
84 ConfigNetworkHandler::send_inv_value(
unsigned int clid,
const char *path)
102 size_t data_size = 0;
104 num_values, data_size, (
void**)&values);
106 std::vector<unsigned int> c_values = i->
get_uints();
107 for (uint16_t i = 0; i < num_values; ++i) values[0] = c_values[0];
111 __hub->
send(clid, FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_UINT_VALUE, m, data_size);
114 "send_value: Value %s could not be sent",
118 }
else if ( i->
is_int() ) {
122 size_t data_size = 0;
124 num_values, data_size, (
void**)&values);
126 std::vector<int> c_values = i->
get_ints();
127 for (uint16_t i = 0; i < num_values; ++i) values[0] = c_values[0];
131 __hub->
send(clid, FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_INT_VALUE, m, data_size);
134 "send_value: Value %s could not be sent",
142 size_t data_size = 0;
144 num_values, data_size, (
void**)&values);
146 std::vector<bool> c_values = i->
get_bools();
147 for (uint16_t i = 0; i < num_values; ++i) values[0] = (c_values[0] ? 1 : 0);
151 __hub->
send(clid, FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_BOOL_VALUE, m, data_size);
154 "send_value: Value %s could not be sent",
162 size_t data_size = 0;
164 data_size, (
void**)&values);
166 std::vector<float> c_values = i->
get_floats();
167 for (uint16_t i = 0; i < num_values; ++i) values[0] = c_values[0];
171 __hub->
send(clid, FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_FLOAT_VALUE, m, data_size);
174 "send_value: Value %s could not be sent",
184 for (
unsigned int j = 0; j < s.size(); ++j) {
187 void *m = calloc(1, data_size);
190 strncpy(cd->
path, i->
path(), CONFIG_MSG_PATH_LENGTH);
195 for (
unsigned int i = 0; i < s.size(); ++i) {
199 strcpy(msg_string, s[i].c_str());
203 __hub->
send(clid, FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_STRING_VALUE, m, data_size);
208 void *m = calloc(1, data_size);
210 strncpy(cd->
path, i->
path(), CONFIG_MSG_PATH_LENGTH);
219 strcpy(msg_string, s.c_str());
221 __hub->
send(clid, FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_STRING_VALUE, m, data_size);
225 "send_value: Value %s could not be sent",
231 "send_value: unknown type of value %s",
241 while ( ! __inbound_queue.empty() ) {
246 if (msg->
msgid() == MSG_CONFIG_SUBSCRIBE) {
249 __subscribers.sort();
250 __subscribers.unique();
255 while ( i->
next() ) {
262 while ( i->
next() ) {
268 __hub->
send(msg->
clid(), FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_LIST, content);
271 }
else if (msg->
msgid() == MSG_CONFIG_ERASE_VALUE) {
274 char path[CONFIG_MSG_PATH_LENGTH + 1];
275 path[CONFIG_MSG_PATH_LENGTH] = 0;
276 strncpy(path, m->
cp.
path, CONFIG_MSG_PATH_LENGTH);
281 __config->
erase(path);
285 __hub->
send(msg->
clid(), FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_VALUE_ERASED,
289 send_inv_value(msg->
clid(),
"?");
290 e.
append(
"Failed to erase value");
295 }
else if ( (msg->
msgid() >= MSG_CONFIG_GET_BEGIN) &&
296 (msg->
msgid() <= MSG_CONFIG_GET_END) ) {
300 "CONFIG_GET_FLOAT: invalid payload size " 301 "(received %zu instead of %zu bytes",
305 char path[CONFIG_MSG_PATH_LENGTH + 1];
306 path[CONFIG_MSG_PATH_LENGTH] = 0;
307 strncpy(path, m->
cp.
path, CONFIG_MSG_PATH_LENGTH);
309 switch (msg->
msgid()) {
310 case MSG_CONFIG_GET_FLOAT:
311 case MSG_CONFIG_GET_UINT:
312 case MSG_CONFIG_GET_INT:
313 case MSG_CONFIG_GET_BOOL:
314 case MSG_CONFIG_GET_STRING:
315 case MSG_CONFIG_GET_VALUE:
319 send_value(msg->
clid(), i);
321 send_inv_value(msg->
clid(), path);
326 "get value: Value %s could not be found", path);
333 }
else if ( (msg->
msgid() >= MSG_CONFIG_SET_BEGIN) &&
334 (msg->
msgid() <= MSG_CONFIG_SET_END) ) {
336 bool success =
false;
338 char path[CONFIG_MSG_PATH_LENGTH + 1];
341 "inbound set: payload is too small" 342 "(%zu is less than %zu bytes",
344 send_inv_value(msg->
clid(),
"?");
347 path[CONFIG_MSG_PATH_LENGTH] = 0;
348 strncpy(path, cd->
path, CONFIG_MSG_PATH_LENGTH);
350 switch (msg->
msgid()) {
351 case MSG_CONFIG_SET_FLOAT:
352 case MSG_CONFIG_SET_DEFAULT_FLOAT:
357 for (
unsigned int i = 0; i < cd->
num_values; ++i) {
362 if ( msg->
msgid() == MSG_CONFIG_SET_FLOAT ) {
370 send_inv_value(msg->
clid(), path);
372 "set float: Value %s could not be set", path);
377 case MSG_CONFIG_SET_UINT:
378 case MSG_CONFIG_SET_DEFAULT_UINT:
382 std::vector<unsigned int> values(cd->
num_values);
383 for (
unsigned int i = 0; i < cd->
num_values; ++i) {
388 if ( msg->
msgid() == MSG_CONFIG_SET_UINT ) {
396 send_inv_value(msg->
clid(), path);
398 "set uint: Value %s could not be set", path);
403 case MSG_CONFIG_SET_INT:
404 case MSG_CONFIG_SET_DEFAULT_INT:
409 for (
unsigned int i = 0; i < cd->
num_values; ++i) {
414 if ( msg->
msgid() == MSG_CONFIG_SET_INT ) {
422 send_inv_value(msg->
clid(), path);
424 "set int: Value %s could not be set", path);
429 case MSG_CONFIG_SET_BOOL:
430 case MSG_CONFIG_SET_DEFAULT_BOOL:
435 for (
unsigned int i = 0; i < cd->
num_values; ++i) {
436 values[i] = (vs[i] != 0);
440 if ( msg->
msgid() == MSG_CONFIG_SET_INT ) {
441 __config->
set_bool(path, (*vs != 0));
448 send_inv_value(msg->
clid(), path);
450 "set bool: Value %s could not be set", path);
455 case MSG_CONFIG_SET_STRING:
456 case MSG_CONFIG_SET_DEFAULT_STRING:
462 std::vector<std::string> values(cd->
num_values);
463 for (
unsigned int i = 0; i < cd->
num_values; ++i) {
467 values[i] = std::string(msg_string, sv->
s_length);
473 std::string value = std::string(msg_string, sv->
s_length);
474 if ( msg->
msgid() == MSG_CONFIG_SET_INT ) {
482 send_inv_value(msg->
clid(), path);
484 "set string: Value %s could not be set", path);
495 send_value(msg->
clid(), i);
497 send_inv_value(msg->
clid(), path);
502 "get value: Value %s could not be found", path);
545 __subscribers.
lock();
546 if (find(__subscribers.begin(), __subscribers.end(), clid) != __subscribers.end()) {
548 "Client %u disconnected without closing the config, removing from list of subscribers",
550 __subscribers.remove(clid);
569 const char *path = v->
path();
571 __subscribers.
lock();
572 for (__sit = __subscribers.begin(); __sit != __subscribers.end(); ++__sit) {
574 send_value(*__sit, v);
577 "config_value_changed: Value for %s could not be sent " 578 "to client %u", path, *__sit);
595 __subscribers.
lock();
596 for (__sit = __subscribers.begin(); __sit != __subscribers.end(); ++__sit) {
599 prepare_msg<config_value_erased_msg_t>(path,
false);
600 __hub->
send(*__sit, FAWKES_CID_CONFIGMANAGER, MSG_CONFIG_VALUE_ERASED,
604 "configValueErased: Value for %s could not be sent " 605 "to client %u", path, *__sit);
void * payload() const
Get payload buffer.
virtual ValueIterator * iterator()=0
Iterator for all values.
void clear()
Clear the queue.
virtual void set_default_float(const char *path, float f)=0
Set new default value in configuration of type float.
virtual std::vector< bool > get_bools() const =0
Get list of values from configuration which is of type bool.
virtual void set_default_int(const char *path, int i)=0
Set new default value in configuration of type int.
void unref()
Decrement reference count and conditionally delete this instance.
void append(Configuration::ValueIterator *i)
Append from iterator.
virtual std::vector< std::string > get_strings() const =0
Get list of values from configuration which is of type string.
virtual void lock() const
Lock list.
virtual std::vector< float > get_floats() const =0
Get list of values from configuration which is of type float.
virtual bool is_bool() const =0
Check if current value is a bool.
Fawkes library namespace.
unsigned int clid() const
Get client ID.
virtual void config_value_changed(const Configuration::ValueIterator *v)
Called whenever a watched value has changed.
config_descriptor_t cp
value descriptor
~ConfigNetworkHandler()
Destructor.
virtual void add_handler(FawkesNetworkHandler *handler)=0
Add a message handler.
Interface for configuration change handling.
char path[CONFIG_MSG_PATH_LENGTH]
path to config value.
virtual void handle_network_message(FawkesNetworkMessage *msg)
Handle network message.
virtual void config_value_erased(const char *path)
Called whenever a value has been erased from the config.
Representation of a message that is sent over the network.
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 void set_int(const char *path, int i)=0
Set new value in configuration of type int.
virtual unsigned int get_uint() const =0
Get unsigned int value.
Thread class encapsulation of pthreads.
virtual size_t get_list_size() const =0
Get number of elements in list value.
virtual bool is_float() const =0
Check if current value is a float.
void push_back_locked(const Type &x)
Push element to list at back with lock protection.
virtual void set_bool(const char *path, bool b)=0
Set new value in configuration of type bool.
virtual bool is_int() const =0
Check if current value is a int.
virtual void set_floats(const char *path, std::vector< float > &f)=0
Set new value in configuration of type float.
virtual void set_strings(const char *path, std::vector< std::string > &s)=0
Set new value in configuration of type string.
virtual bool get_bool() const =0
Get bool value.
virtual void set_float(const char *path, float f)=0
Set new value in configuration of type float.
virtual int get_int() const =0
Get int value.
virtual void loop()
Process all network messages that have been received.
ConfigNetworkHandler(Configuration *config, FawkesNetworkHub *hub)
Constructor.
virtual bool is_string() const =0
Check if current value is a string.
void wakeup()
Wake up thread.
virtual void erase_default(const char *path)=0
Erase the given default value from the configuration.
virtual void erase(const char *path)=0
Erase the given value from the configuration.
virtual void set_default_bool(const char *path, bool b)=0
Set new default value in configuration of type bool.
virtual std::vector< unsigned int > get_uints() const =0
Get list of values from configuration which is of type unsigned int.
uint16_t is_default
1 if value is a default value, 0 otherwise, only for get response
Base class for exceptions in Fawkes.
virtual void send(FawkesNetworkMessage *msg)=0
Method to send a message to a specific client.
String value header indicating the string length.
virtual void set_default_uint(const char *path, unsigned int uint)=0
Set new default value in configuration of type unsigned int.
virtual void set_bools(const char *path, std::vector< bool > &b)=0
Set new value in configuration of type bool.
virtual void rem_change_handler(ConfigurationChangeHandler *h)
Remove a configuration change handler.
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)=0
Set new value in configuration of type unsigned int.
void ref()
Increment reference count.
virtual void unlock() const
Unlock list.
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 std::string get_string() const =0
Get string value.
Network handler abstract base class.
uint16_t num_values
Number of valus in list.
Generic configuration exception.
virtual const char * path() const =0
Path of value.
static void log_warn(const char *component, const char *format,...)
Log warning message.
virtual void client_disconnected(unsigned int clid)
Client disconnected.
void cancel()
Cancel a thread.
void pop_locked()
Pop element from queue with lock protection.
virtual void unlock()=0
Unlock the config.
virtual void client_connected(unsigned int clid)
Client connected.
unsigned short int msgid() const
Get message type ID.
Invalid value request message.
virtual ValueIterator * get_value(const char *path)=0
Get value from configuration.
void push_locked(const Type &x)
Push element to queue with lock protection.
Iterator interface to iterate over config values.
void join()
Join the thread.
virtual bool is_default() const =0
Check if current value was read from the default config.
uint16_t s_length
Length of following string.
virtual void add_change_handler(ConfigurationChangeHandler *h)
Add a configuration change handler.
config_descriptor_t cp
value descriptor
virtual void remove_handler(FawkesNetworkHandler *handler)=0
Remove a message handler.
MT * msg() const
Get correctly casted payload.
virtual void set_default_string(const char *path, std::string &s)=0
Set new default value in configuration of type string.
virtual void set_ints(const char *path, std::vector< int > &i)=0
Set new value in configuration of type int.
virtual void config_comment_changed(const Configuration::ValueIterator *v)
Called whenever a comment of a watched value has changed.
Interface for configuration handling.
virtual void set_uint(const char *path, unsigned int uint)=0
Set new value in configuration of type unsigned int.
virtual void set_string(const char *path, std::string &s)=0
Set new value in configuration of type string.
void append(const char *format,...)
Append messages to the message list.
virtual void lock()=0
Lock the config.
virtual void config_tag_changed(const char *new_location)
Tag changed.
void start(bool wait=true)
Call this method to start the thread.
size_t payload_size() const
Get payload size.