Fawkes API Fawkes Development Version

list_message.cpp

00001 
00002 /***************************************************************************
00003  *  plugin_list_messages.cpp - Fawkes Plugin List Message
00004  *
00005  *  Created: Sat Jun 02 01:25:48 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 <plugin/net/list_message.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 PluginListMessage <plugin/net/list_message.h>
00035  * Plugin list message.
00036  * A complex dynamic message with an arbitrary number of plugins. Uses
00037  * DynamicBuffer for the internal list of plugins and thus the buffer is
00038  * limited to 64 KB.
00039  *
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor. */
00044 PluginListMessage::PluginListMessage()
00045 {
00046   plugin_list = new DynamicBuffer(&(msg.plugin_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 PluginListMessage::PluginListMessage(unsigned int component_id,
00058                                      unsigned int msg_id,
00059                                      void *payload, size_t payload_size)
00060 {
00061   if ( component_id != FAWKES_CID_PLUGINMANAGER ) {
00062     throw TypeMismatchException("PluginListMessage: invalid component ID");
00063   }
00064   plugin_list_msg_t *tmsg = (plugin_list_msg_t *)payload;
00065   void *plugin_list_payload = (void *)((size_t)payload + sizeof(msg));
00066   plugin_list = new DynamicBuffer(&(tmsg->plugin_list), plugin_list_payload,
00067                                   payload_size - sizeof(msg));
00068 }
00069 
00070 
00071 /** Destructor. */
00072 PluginListMessage::~PluginListMessage()
00073 {
00074   delete plugin_list;
00075   if (_payload != NULL) {
00076     free(_payload);
00077     _payload = NULL;
00078     _payload_size = 0;
00079   }
00080 }
00081 
00082 
00083 /** Append plugin name.
00084  * @param plugin_name plugin name
00085  * @param len length in bytes to append (can be used for example to avoid
00086  * adding a file extension.
00087  */
00088 void
00089 PluginListMessage::append(const char *plugin_name, size_t len)
00090 {
00091   plugin_list->append(plugin_name, len);
00092 }
00093 
00094 
00095 void
00096 PluginListMessage::serialize()
00097 {
00098   _payload_size = sizeof(msg) + plugin_list->buffer_size();
00099   _payload = malloc(_payload_size);
00100   copy_payload(0, &msg, sizeof(msg));
00101   copy_payload(sizeof(msg), plugin_list->buffer(), plugin_list->buffer_size());
00102 }
00103 
00104 
00105 /** Reset iterator.
00106  * For incoming messages only.
00107  */
00108 void
00109 PluginListMessage::reset_iterator()
00110 {
00111   plugin_list->reset_iterator();
00112 }
00113 
00114 
00115 /** Check if more list elements are available.
00116  * For incoming messages only.
00117  * @return true if there are more elements available, false otherwise.
00118  */
00119 bool
00120 PluginListMessage::has_next()
00121 {
00122   return plugin_list->has_next();
00123 }
00124 
00125 
00126 /** Get next plugin from list.
00127  * @return next plugin from list. This string has been allocated via strndup, so
00128  * you have to free it yourself!
00129  */
00130 char *
00131 PluginListMessage::next()
00132 {
00133   size_t size;
00134   void *tmp = plugin_list->next(&size);
00135   return strndup((const char *)tmp, size);
00136 }
00137 
00138 } // end namespace fawkes
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends