net_list_content.cpp

00001 
00002 /***************************************************************************
00003  *  config_list_content.cpp - Fawkes Config List Message Content
00004  *
00005  *  Created: Sat Dec 08 23:38:10 2007
00006  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <config/net_list_content.h>
00025 
00026 #include <netcomm/utils/dynamic_buffer.h>
00027 #include <netcomm/fawkes/component_ids.h>
00028 #include <core/exceptions/software.h>
00029 #include <cstdlib>
00030 #include <cstring>
00031 
00032 namespace fawkes {
00033 
00034 /** @class ConfigListContent <config/net_list_content.h>
00035  * Config list content.
00036  * A complex dynamic message with an arbitrary number of config entities. Uses
00037  * DynamicBuffer for the internal list of plugins and thus the buffer is
00038  * limited to 4 GB in total.
00039  *
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor. */
00044 ConfigListContent::ConfigListContent()
00045 {
00046   config_list = new DynamicBuffer(&(msg.config_list));
00047 }
00048 
00049 
00050 /** Message content constructor.
00051  * This constructor is meant to be used with FawkesNetworkMessage::msgc().
00052  * @param component_id component ID
00053  * @param msg_id message ID
00054  * @param payload message payload
00055  * @param payload_size total payload size
00056  */
00057 ConfigListContent::ConfigListContent(unsigned int component_id,
00058                                      unsigned int msg_id,
00059                                      void *payload, size_t payload_size)
00060 {
00061   if ( component_id != FAWKES_CID_CONFIGMANAGER ) {
00062     throw TypeMismatchException("ConfigListContent: invalid component ID");
00063   }
00064   config_list_msg_t *tmsg = (config_list_msg_t *)payload;
00065   void *config_list_payload = (void *)((size_t)payload + sizeof(msg));
00066   config_list = new DynamicBuffer(&(tmsg->config_list), config_list_payload,
00067                                   payload_size - sizeof(msg));
00068 }
00069 
00070 
00071 /** Destructor. */
00072 ConfigListContent::~ConfigListContent()
00073 {
00074   delete config_list;
00075   if (_payload != NULL) {
00076     free(_payload);
00077     _payload = NULL;
00078     _payload_size = 0;
00079   }
00080 }
00081 
00082 
00083 /** Append from iterator.
00084  * Appends the value the iterator points to.
00085  * @param i iterator
00086  */
00087 void
00088 ConfigListContent::append(Configuration::ValueIterator *i)
00089 {
00090   if ( i->is_float() ) {
00091     append_float(i->path(), i->get_float(), i->is_default());
00092   } else if ( i->is_int() ) {
00093     append_int(i->path(), i->get_int(), i->is_default());
00094   } else if ( i->is_uint() ) {
00095     append_uint(i->path(), i->get_uint(), i->is_default());
00096   } else if ( i->is_bool() ) {
00097     append_bool(i->path(), i->get_bool(), i->is_default());
00098   } else if ( i->is_string() ) {
00099     append_string(i->path(), i->get_string().c_str(), i->is_default());
00100   } else {
00101     throw TypeMismatchException("Invalid type of config iterator value");
00102   }
00103 
00104   std::string comment = i->get_comment();
00105   if (comment != "") {
00106     append_comment(i->path(), comment.c_str(), i->is_default());
00107   }
00108 }
00109 
00110 /** Append float value.
00111  * @param path of value
00112  * @param f float value
00113  * @param def_val true if this is a default value, false otherwise
00114  */
00115 void
00116 ConfigListContent::append_float(const char *path, float f, bool def_val)
00117 {
00118   config_list_float_entity_t cle;
00119   memset(&cle, 0, sizeof(cle));
00120   strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00121   cle.header.type = MSG_CONFIG_FLOAT_VALUE;
00122   cle.header.cp.is_default = (def_val ? 1 : 0);
00123   cle.f = f;
00124   config_list->append(&cle, sizeof(cle));
00125 }
00126 
00127 
00128 /** Append integer value.
00129  * @param path of value
00130  * @param i integer value
00131  * @param def_val true if this is a default value, false otherwise
00132  */
00133 void
00134 ConfigListContent::append_int(const char *path, int i, bool def_val)
00135 {
00136   config_list_int_entity_t cle;
00137   memset(&cle, 0, sizeof(cle));
00138   strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00139   cle.header.type = MSG_CONFIG_INT_VALUE;
00140   cle.header.cp.is_default = (def_val ? 1 : 0);
00141   cle.i = i;
00142   config_list->append(&cle, sizeof(cle));
00143 }
00144 
00145 
00146 /** Append unsigned integer value.
00147  * @param path of value
00148  * @param u unsigned integer value
00149  * @param def_val true if this is a default value, false otherwise
00150  */
00151 void
00152 ConfigListContent::append_uint(const char *path, unsigned int u, bool def_val)
00153 {
00154   config_list_uint_entity_t cle;
00155   memset(&cle, 0, sizeof(cle));
00156   strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00157   cle.header.type = MSG_CONFIG_UINT_VALUE;
00158   cle.header.cp.is_default = (def_val ? 1 : 0);
00159   cle.u = u;
00160   config_list->append(&cle, sizeof(cle));
00161 }
00162 
00163 
00164 /** Append boolean value.
00165  * @param path of value
00166  * @param b boolean value
00167  * @param def_val true if this is a default value, false otherwise
00168  */
00169 void
00170 ConfigListContent::append_bool(const char *path, bool b, bool def_val)
00171 {
00172   config_list_bool_entity_t cle;
00173   memset(&cle, 0, sizeof(cle));
00174   strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00175   cle.header.type = MSG_CONFIG_BOOL_VALUE;
00176   cle.header.cp.is_default = (def_val ? 1 : 0);
00177   cle.b = b;
00178   config_list->append(&cle, sizeof(cle));
00179 }
00180 
00181 
00182 /** Append string value.
00183  * @param path of value
00184  * @param s string value
00185  * @param def_val true if this is a default value, false otherwise
00186  */
00187 void
00188 ConfigListContent::append_string(const char *path, const char *s, bool def_val)
00189 {
00190   size_t s_length = strlen(s);
00191   size_t sl = sizeof(config_list_string_entity_t) + s_length;
00192   config_list_string_entity_t *cle = (config_list_string_entity_t *)calloc(1, sl);
00193   strncpy(cle->header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00194   cle->header.type = MSG_CONFIG_STRING_VALUE;
00195   cle->header.cp.is_default = (def_val ? 1 : 0);
00196   cle->s_length = s_length;
00197   strcpy(cle->s, s);
00198   config_list->append(cle, sl);
00199   free(cle);
00200 }
00201 
00202 
00203 /** Append comment.
00204  * @param path of value
00205  * @param s comment
00206  * @param def_val true if this is a default value, false otherwise
00207  */
00208 void
00209 ConfigListContent::append_comment(const char *path, const char *s, bool def_val)
00210 {
00211   size_t s_length = strlen(s);
00212   size_t sl = sizeof(config_list_string_entity_t) + s_length;
00213   config_list_comment_entity_t *cle = (config_list_comment_entity_t *)calloc(1, sl);
00214   strncpy(cle->header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00215   cle->header.type = MSG_CONFIG_COMMENT_VALUE;
00216   cle->header.cp.is_default = (def_val ? 1 : 0);
00217   cle->s_length = s_length;
00218   strcpy(cle->s, s);
00219   config_list->append(cle, sl);
00220   free(cle);
00221 }
00222 
00223 
00224 void
00225 ConfigListContent::serialize()
00226 {
00227   _payload_size = sizeof(msg) + config_list->buffer_size();
00228   _payload = malloc(_payload_size);
00229   copy_payload(0, &msg, sizeof(msg));
00230   copy_payload(sizeof(msg), config_list->buffer(), config_list->buffer_size());
00231 }
00232 
00233 
00234 /** Reset iterator.
00235  * For incoming messages only.
00236  */
00237 void
00238 ConfigListContent::reset_iterator()
00239 {
00240   config_list->reset_iterator();
00241 }
00242 
00243 
00244 /** Check if more list elements are available.
00245  * For incoming messages only.
00246  * @return true if there are more elements available, false otherwise.
00247  */
00248 bool
00249 ConfigListContent::has_next()
00250 {
00251   return config_list->has_next();
00252 }
00253 
00254 
00255 /** Get next plugin from list.
00256  * @param size upon return contains the size of the returned data element.
00257  * @return next config entitiy from the list. The value is only of the type of
00258  * the header. Check the message type and the size and cast the message to the correct
00259  * entity.
00260  */
00261 config_list_entity_header_t *
00262 ConfigListContent::next(size_t *size)
00263 {
00264   void *tmp = config_list->next(size);
00265   return (config_list_entity_header_t *)tmp;
00266 }
00267 
00268 } // end namespace fawkes

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1