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 <plugin/net/list_message.h>
25 
26 #include <netcomm/utils/dynamic_buffer.h>
27 #include <netcomm/fawkes/component_ids.h>
28 #include <core/exceptions/software.h>
29 #include <utils/misc/strndup.h>
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 
51 /** Message content constructor.
52  * This constructor is meant to be used with FawkesNetworkMessage::msgc().
53  * @param component_id component ID
54  * @param msg_id message ID
55  * @param payload message payload
56  * @param payload_size total payload size
57  */
58 PluginListMessage::PluginListMessage(unsigned int component_id,
59  unsigned int msg_id,
60  void *payload, size_t payload_size)
61 {
62  if ( component_id != FAWKES_CID_PLUGINMANAGER ) {
63  throw TypeMismatchException("PluginListMessage: invalid component ID");
64  }
65  plugin_list_msg_t *tmsg = (plugin_list_msg_t *)payload;
66  void *plugin_list_payload = (void *)((size_t)payload + sizeof(msg));
67  plugin_list = new DynamicBuffer(&(tmsg->plugin_list), plugin_list_payload,
68  payload_size - sizeof(msg));
69 }
70 
71 
72 /** Destructor. */
74 {
75  delete plugin_list;
76  if (_payload != NULL) {
77  free(_payload);
78  _payload = NULL;
79  _payload_size = 0;
80  }
81 }
82 
83 
84 /** Append plugin name.
85  * @param plugin_name plugin name
86  * @param len length in bytes to append (can be used for example to avoid
87  * adding a file extension.
88  */
89 void
90 PluginListMessage::append(const char *plugin_name, size_t len)
91 {
92  plugin_list->append(plugin_name, len);
93 }
94 
95 
96 void
98 {
99  _payload_size = sizeof(msg) + plugin_list->buffer_size();
100  _payload = malloc(_payload_size);
101  copy_payload(0, &msg, sizeof(msg));
102  copy_payload(sizeof(msg), plugin_list->buffer(), plugin_list->buffer_size());
103 }
104 
105 
106 /** Reset iterator.
107  * For incoming messages only.
108  */
109 void
111 {
112  plugin_list->reset_iterator();
113 }
114 
115 
116 /** Check if more list elements are available.
117  * For incoming messages only.
118  * @return true if there are more elements available, false otherwise.
119  */
120 bool
122 {
123  return plugin_list->has_next();
124 }
125 
126 
127 /** Get next plugin from list.
128  * @return next plugin from list. This string has been allocated via strndup, so
129  * you have to free it yourself!
130  */
131 char *
133 {
134  size_t size;
135  void *tmp = plugin_list->next(&size);
136  return strndup((const char *)tmp, size);
137 }
138 
139 } // 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:95
Plugin list message.
Definition: messages.h:94
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.