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