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