Fawkes API  Fawkes Development Version
net_list_content.cpp
1 
2 /***************************************************************************
3  * config_list_content.cpp - Fawkes Config List Message Content
4  *
5  * Created: Sat Dec 08 23:38:10 2007
6  * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <config/net_list_content.h>
25 
26 #include <netcomm/utils/dynamic_buffer.h>
27 #include <netcomm/fawkes/component_ids.h>
28 #include <core/exceptions/software.h>
29 #include <cstdlib>
30 #include <cstring>
31 
32 namespace fawkes {
33 
34 /** @class ConfigListContent <config/net_list_content.h>
35  * Config list content.
36  * A complex dynamic message with an arbitrary number of config entities. Uses
37  * DynamicBuffer for the internal list of plugins and thus the buffer is
38  * limited to 4 GB in total.
39  *
40  * @author Tim Niemueller
41  */
42 
43 /** Constructor. */
45 {
46  config_list = new DynamicBuffer(&(msg.config_list));
47 }
48 
49 
50 /** Message content constructor.
51  * This constructor is meant to be used with FawkesNetworkMessage::msgc().
52  * @param component_id component ID
53  * @param msg_id message ID
54  * @param payload message payload
55  * @param payload_size total payload size
56  */
57 ConfigListContent::ConfigListContent(unsigned int component_id,
58  unsigned int msg_id,
59  void *payload, size_t payload_size)
60 {
61  if ( component_id != FAWKES_CID_CONFIGMANAGER ) {
62  throw TypeMismatchException("ConfigListContent: invalid component ID");
63  }
64  config_list_msg_t *tmsg = (config_list_msg_t *)payload;
65  void *config_list_payload = (void *)((size_t)payload + sizeof(msg));
66  config_list = new DynamicBuffer(&(tmsg->config_list), config_list_payload,
67  payload_size - sizeof(msg));
68 }
69 
70 
71 /** Destructor. */
73 {
74  delete config_list;
75  if (_payload != NULL) {
76  free(_payload);
77  _payload = NULL;
78  _payload_size = 0;
79  }
80 }
81 
82 
83 /** Append from iterator.
84  * Appends the value the iterator points to.
85  * @param i iterator
86  */
87 void
89 {
90  if ( i->is_float() ) {
91  append_float(i->path(), i->get_float(), i->is_default());
92  } else if ( i->is_int() ) {
93  append_int(i->path(), i->get_int(), i->is_default());
94  } else if ( i->is_uint() ) {
95  append_uint(i->path(), i->get_uint(), i->is_default());
96  } else if ( i->is_bool() ) {
97  append_bool(i->path(), i->get_bool(), i->is_default());
98  } else if ( i->is_string() ) {
99  append_string(i->path(), i->get_string().c_str(), i->is_default());
100  } else {
101  throw TypeMismatchException("Invalid type of config iterator value");
102  }
103 
104  std::string comment = i->get_comment();
105  if (comment != "") {
106  append_comment(i->path(), comment.c_str(), i->is_default());
107  }
108 }
109 
110 /** Append float value.
111  * @param path of value
112  * @param f float value
113  * @param def_val true if this is a default value, false otherwise
114  */
115 void
116 ConfigListContent::append_float(const char *path, float f, bool def_val)
117 {
119  memset(&cle, 0, sizeof(cle));
120  strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
121  cle.header.type = MSG_CONFIG_FLOAT_VALUE;
122  cle.header.cp.is_default = (def_val ? 1 : 0);
123  cle.f = f;
124  config_list->append(&cle, sizeof(cle));
125 }
126 
127 
128 /** Append integer value.
129  * @param path of value
130  * @param i integer value
131  * @param def_val true if this is a default value, false otherwise
132  */
133 void
134 ConfigListContent::append_int(const char *path, int i, bool def_val)
135 {
137  memset(&cle, 0, sizeof(cle));
138  strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
139  cle.header.type = MSG_CONFIG_INT_VALUE;
140  cle.header.cp.is_default = (def_val ? 1 : 0);
141  cle.i = i;
142  config_list->append(&cle, sizeof(cle));
143 }
144 
145 
146 /** Append unsigned integer value.
147  * @param path of value
148  * @param u unsigned integer value
149  * @param def_val true if this is a default value, false otherwise
150  */
151 void
152 ConfigListContent::append_uint(const char *path, unsigned int u, bool def_val)
153 {
155  memset(&cle, 0, sizeof(cle));
156  strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
157  cle.header.type = MSG_CONFIG_UINT_VALUE;
158  cle.header.cp.is_default = (def_val ? 1 : 0);
159  cle.u = u;
160  config_list->append(&cle, sizeof(cle));
161 }
162 
163 
164 /** Append boolean value.
165  * @param path of value
166  * @param b boolean value
167  * @param def_val true if this is a default value, false otherwise
168  */
169 void
170 ConfigListContent::append_bool(const char *path, bool b, bool def_val)
171 {
173  memset(&cle, 0, sizeof(cle));
174  strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
175  cle.header.type = MSG_CONFIG_BOOL_VALUE;
176  cle.header.cp.is_default = (def_val ? 1 : 0);
177  cle.b = b;
178  config_list->append(&cle, sizeof(cle));
179 }
180 
181 
182 /** Append string value.
183  * @param path of value
184  * @param s string value
185  * @param def_val true if this is a default value, false otherwise
186  */
187 void
188 ConfigListContent::append_string(const char *path, const char *s, bool def_val)
189 {
190  size_t s_length = strlen(s);
191  size_t sl = sizeof(config_list_string_entity_t) + s_length;
193  strncpy(cle->header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
194  cle->header.type = MSG_CONFIG_STRING_VALUE;
195  cle->header.cp.is_default = (def_val ? 1 : 0);
196  cle->s_length = s_length;
197  strcpy(cle->s, s);
198  config_list->append(cle, sl);
199  free(cle);
200 }
201 
202 
203 /** Append comment.
204  * @param path of value
205  * @param s comment
206  * @param def_val true if this is a default value, false otherwise
207  */
208 void
209 ConfigListContent::append_comment(const char *path, const char *s, bool def_val)
210 {
211  size_t s_length = strlen(s);
212  size_t sl = sizeof(config_list_string_entity_t) + s_length;
214  strncpy(cle->header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
215  cle->header.type = MSG_CONFIG_COMMENT_VALUE;
216  cle->header.cp.is_default = (def_val ? 1 : 0);
217  cle->s_length = s_length;
218  strcpy(cle->s, s);
219  config_list->append(cle, sl);
220  free(cle);
221 }
222 
223 
224 void
226 {
227  _payload_size = sizeof(msg) + config_list->buffer_size();
228  _payload = malloc(_payload_size);
229  copy_payload(0, &msg, sizeof(msg));
230  copy_payload(sizeof(msg), config_list->buffer(), config_list->buffer_size());
231 }
232 
233 
234 /** Reset iterator.
235  * For incoming messages only.
236  */
237 void
239 {
240  config_list->reset_iterator();
241 }
242 
243 
244 /** Check if more list elements are available.
245  * For incoming messages only.
246  * @return true if there are more elements available, false otherwise.
247  */
248 bool
250 {
251  return config_list->has_next();
252 }
253 
254 
255 /** Get next plugin from list.
256  * @param size upon return contains the size of the returned data element.
257  * @return next config entitiy from the list. The value is only of the type of
258  * the header. Check the message type and the size and cast the message to the correct
259  * entity.
260  */
263 {
264  void *tmp = config_list->next(size);
265  return (config_list_entity_header_t *)tmp;
266 }
267 
268 } // end namespace fawkes
void * _payload
Pointer to payload.
virtual bool is_float() const =0
Check if current value is a float.
uint32_t is_default
1 if value is a default value, 0 otherwise, only for get response
Definition: net_messages.h:94
void append(Configuration::ValueIterator *i)
Append from iterator.
Config list float entity.
Definition: net_messages.h:178
virtual ~ConfigListContent()
Destructor.
virtual bool is_bool() const =0
Check if current value is a bool.
void append_float(const char *path, float f, bool def_val=false)
Append float value.
Fawkes library namespace.
config_list_entity_header_t * next(size_t *size)
Get next plugin from list.
void * buffer()
Get pointer to buffer.
char path[CONFIG_MSG_PATH_LENGTH]
path to config value.
Definition: net_messages.h:93
virtual std::string get_string() const =0
Get string value.
config_list_entity_header_t header
config entity header
Definition: net_messages.h:197
virtual bool is_string() const =0
Check if current value is a string.
config_list_entity_header_t header
config entity header
Definition: net_messages.h:179
virtual bool is_uint() const =0
Check if current value is a unsigned int.
virtual float get_float() const =0
Get float value.
Config list int entity.
Definition: net_messages.h:190
virtual const char * path() const =0
Path of value.
void append_bool(const char *path, bool b, bool def_val=false)
Append boolean value.
dynamic_list_t config_list
DynamicBuffer for list.
Definition: net_messages.h:167
bool has_next()
Check if another element is available.
size_t buffer_size()
Get buffer size.
Config list bool entity.
Definition: net_messages.h:196
Config list unsigned int entity.
Definition: net_messages.h:184
config_list_entity_header_t header
config entity header
Definition: net_messages.h:191
uint32_t type
type of entity, uses MSG_CONFIG_*_VALUE message IDs
Definition: net_messages.h:173
virtual void serialize()
Serialize message content.
bool has_next()
Check if more list elements are available.
virtual unsigned int get_uint() const =0
Get unsigned int value.
void append_uint(const char *path, unsigned int u, bool def_val=false)
Append unsigned integer value.
config_list_entity_header_t header
config entity header
Definition: net_messages.h:210
virtual bool is_int() const =0
Check if current value is a int.
virtual std::string get_comment() const =0
Get comment of value.
config_list_entity_header_t header
config entity header
Definition: net_messages.h:185
virtual void * payload()
Return pointer to payload.
virtual bool get_bool() const =0
Get bool value.
void append_comment(const char *path, const char *s, bool def_val=false)
Append comment.
Config list message.
Definition: net_messages.h:166
uint16_t s_length
Length of following comment string.
Definition: net_messages.h:211
config_descriptor_t cp
Config descriptor.
Definition: net_messages.h:172
uint16_t s_length
length of following string value
Definition: net_messages.h:204
void copy_payload(size_t offset, const void *buf, size_t len)
Copy payload into payload buffer to a specified offset.
Dynamically growing buffer.
virtual size_t payload_size()
Return payload size.
Config list entity header.
Definition: net_messages.h:171
void append_int(const char *path, int i, bool def_val=false)
Append integer value.
Iterator interface to iterate over config values.
Definition: config.h:68
void reset_iterator()
Reset iterator.
Config list comment entity.
Definition: net_messages.h:209
int32_t b
0 is false, everything else is true
Definition: net_messages.h:198
char s[2]
Comment value, 0-terminated.
Definition: net_messages.h:212
void append(const void *data, size_t data_size)
Append data.
char s[2]
string value, 0-terminated
Definition: net_messages.h:205
void append_string(const char *path, const char *s, bool def_val=false)
Append string value.
virtual bool is_default() const =0
Check if current value was read from the default config.
Config list string entity.
Definition: net_messages.h:202
config_list_entity_header_t header
config entity header
Definition: net_messages.h:203
void * next(size_t *size)
Get next buffer.
void reset_iterator()
Reset iterator.
virtual int get_int() const =0
Get int value.