Fawkes API  Fawkes Development Version
list_message.cpp
1 
2 /***************************************************************************
3  * plugin_list_messages.cpp - Fawkes Plugin List Message
4  *
5  * Created: Sat Jun 02 01:25:48 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 <core/exceptions/software.h>
25 #include <netcomm/fawkes/component_ids.h>
26 #include <netcomm/utils/dynamic_buffer.h>
27 #include <plugin/net/list_message.h>
28 #include <utils/misc/strndup.h>
29 
30 #include <cstdlib>
31 #include <cstring>
32 
33 namespace fawkes {
34 
35 /** @class PluginListMessage <plugin/net/list_message.h>
36  * Plugin list message.
37  * A complex dynamic message with an arbitrary number of plugins. Uses
38  * DynamicBuffer for the internal list of plugins and thus the buffer is
39  * limited to 64 KB.
40  *
41  * @author Tim Niemueller
42  */
43 
44 /** Constructor. */
46 {
47  plugin_list = new DynamicBuffer(&(msg.plugin_list));
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 PluginListMessage::PluginListMessage(unsigned int component_id,
58  unsigned int msg_id,
59  void * payload,
60  size_t payload_size)
61 {
62  if (component_id != FAWKES_CID_PLUGINMANAGER) {
63  throw TypeMismatchException("PluginListMessage: invalid component ID");
64  }
66  void * plugin_list_payload = (void *)((size_t)payload + sizeof(msg));
67  plugin_list =
68  new DynamicBuffer(&(tmsg->plugin_list), plugin_list_payload, payload_size - sizeof(msg));
69 }
70 
71 /** Destructor. */
73 {
74  delete plugin_list;
75  if (_payload != NULL) {
76  free(_payload);
77  _payload = NULL;
78  _payload_size = 0;
79  }
80 }
81 
82 /** Append plugin name.
83  * @param plugin_name plugin name
84  * @param len length in bytes to append (can be used for example to avoid
85  * adding a file extension.
86  */
87 void
88 PluginListMessage::append(const char *plugin_name, size_t len)
89 {
90  plugin_list->append(plugin_name, len);
91 }
92 
93 void
95 {
96  _payload_size = sizeof(msg) + plugin_list->buffer_size();
97  _payload = malloc(_payload_size);
98  copy_payload(0, &msg, sizeof(msg));
99  copy_payload(sizeof(msg), plugin_list->buffer(), plugin_list->buffer_size());
100 }
101 
102 /** Reset iterator.
103  * For incoming messages only.
104  */
105 void
107 {
108  plugin_list->reset_iterator();
109 }
110 
111 /** Check if more list elements are available.
112  * For incoming messages only.
113  * @return true if there are more elements available, false otherwise.
114  */
115 bool
117 {
118  return plugin_list->has_next();
119 }
120 
121 /** Get next plugin from list.
122  * @return next plugin from list. This string has been allocated via strndup, so
123  * you have to free it yourself!
124  */
125 char *
127 {
128  size_t size;
129  void * tmp = plugin_list->next(&size);
130  return strndup((const char *)tmp, size);
131 }
132 
133 } // end namespace fawkes
void * _payload
Pointer to payload.
char * next()
Get next plugin from list.
Fawkes library namespace.
void * buffer()
Get pointer to buffer.
virtual ~PluginListMessage()
Destructor.
dynamic_list_t plugin_list
dynamically growing list of plugin names.
Definition: messages.h:101
Plugin list message.
Definition: messages.h:99
bool has_next()
Check if another element is available.
size_t buffer_size()
Get buffer size.
void reset_iterator()
Reset iterator.
PluginListMessage()
Constructor.
virtual void serialize()
Serialize message content.
virtual void * payload()
Return pointer to payload.
bool has_next()
Check if more list elements are available.
void copy_payload(size_t offset, const void *buf, size_t len)
Copy payload into payload buffer to a specified offset.
Dynamically growing buffer.
void append(const char *plugin_name, size_t len)
Append plugin name.
virtual size_t payload_size()
Return payload size.
void reset_iterator()
Reset iterator.
void append(const void *data, size_t data_size)
Append data.
void * next(size_t *size)
Get next buffer.