Allow a component to remove a port and any user to disconnect one
[babeltrace.git] / plugins / writer / writer.c
1 /*
2 * writer.c
3 *
4 * Babeltrace CTF Writer Output Plugin
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/plugin/plugin-dev.h>
31 #include <babeltrace/component/component.h>
32 #include <babeltrace/component/component-sink.h>
33 #include <babeltrace/component/port.h>
34 #include <babeltrace/component/connection.h>
35 #include <babeltrace/component/notification/notification.h>
36 #include <babeltrace/component/notification/iterator.h>
37 #include <babeltrace/component/notification/event.h>
38 #include <babeltrace/component/notification/packet.h>
39 #include <plugins-common.h>
40 #include <stdio.h>
41 #include <stdbool.h>
42 #include <glib.h>
43 #include "writer.h"
44 #include <assert.h>
45
46 static
47 void destroy_writer_component_data(struct writer_component *writer_component)
48 {
49 bt_put(writer_component->input_iterator);
50 g_hash_table_destroy(writer_component->stream_map);
51 g_hash_table_destroy(writer_component->stream_class_map);
52 g_hash_table_destroy(writer_component->trace_map);
53 g_string_free(writer_component->base_path, true);
54 g_string_free(writer_component->trace_name_base, true);
55 }
56
57 static
58 void destroy_writer_component(struct bt_component *component)
59 {
60 struct writer_component *writer_component = (struct writer_component *)
61 bt_component_get_private_data(component);
62
63 destroy_writer_component_data(writer_component);
64 g_free(writer_component);
65 }
66
67 static
68 void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class)
69 {
70 BT_PUT(writer_stream_class);
71 g_free(writer_stream_class);
72 }
73
74 static
75 void unref_stream(struct bt_ctf_stream_class *writer_stream)
76 {
77 BT_PUT(writer_stream);
78 g_free(writer_stream);
79 }
80
81 static
82 void unref_trace(struct bt_ctf_writer *writer)
83 {
84 BT_PUT(writer);
85 g_free(writer);
86 }
87
88 static
89 struct writer_component *create_writer_component(void)
90 {
91 struct writer_component *writer_component;
92
93 writer_component = g_new0(struct writer_component, 1);
94 if (!writer_component) {
95 goto end;
96 }
97
98 writer_component->err = stderr;
99 writer_component->trace_id = 0;
100 writer_component->trace_name_base = g_string_new("trace");
101 if (!writer_component->trace_name_base) {
102 g_free(writer_component);
103 writer_component = NULL;
104 goto end;
105 }
106
107 /*
108 * Reader to writer corresponding structures.
109 */
110 writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
111 g_direct_equal, NULL, (GDestroyNotify) unref_trace);
112 writer_component->stream_class_map = g_hash_table_new_full(g_direct_hash,
113 g_direct_equal, NULL, (GDestroyNotify) unref_stream_class);
114 writer_component->stream_map = g_hash_table_new_full(g_direct_hash,
115 g_direct_equal, NULL, (GDestroyNotify) unref_stream);
116
117 end:
118 return writer_component;
119 }
120
121 static
122 enum bt_component_status handle_notification(
123 struct writer_component *writer_component,
124 struct bt_notification *notification)
125 {
126 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
127
128 if (!writer_component) {
129 ret = BT_COMPONENT_STATUS_ERROR;
130 goto end;
131 }
132
133 switch (bt_notification_get_type(notification)) {
134 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
135 {
136 struct bt_ctf_packet *packet =
137 bt_notification_packet_begin_get_packet(notification);
138
139 if (!packet) {
140 ret = BT_COMPONENT_STATUS_ERROR;
141 goto end;
142 }
143
144 ret = writer_new_packet(writer_component, packet);
145 bt_put(packet);
146 break;
147 }
148 case BT_NOTIFICATION_TYPE_PACKET_END:
149 {
150 struct bt_ctf_packet *packet =
151 bt_notification_packet_end_get_packet(notification);
152
153 if (!packet) {
154 ret = BT_COMPONENT_STATUS_ERROR;
155 goto end;
156 }
157 ret = writer_close_packet(writer_component, packet);
158 bt_put(packet);
159 break;
160 }
161 case BT_NOTIFICATION_TYPE_EVENT:
162 {
163 struct bt_ctf_event *event = bt_notification_event_get_event(
164 notification);
165
166 if (!event) {
167 ret = BT_COMPONENT_STATUS_ERROR;
168 goto end;
169 }
170 ret = BT_COMPONENT_STATUS_OK;
171 ret = writer_output_event(writer_component, event);
172 bt_put(event);
173 if (ret != BT_COMPONENT_STATUS_OK) {
174 goto end;
175 }
176 break;
177 }
178 case BT_NOTIFICATION_TYPE_STREAM_END:
179 break;
180 default:
181 puts("Unhandled notification type");
182 }
183 end:
184 return ret;
185 }
186
187 static
188 enum bt_component_status writer_component_accept_port_connection(
189 struct bt_component *component, struct bt_port *self_port)
190 {
191 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
192 struct bt_connection *connection;
193 struct writer_component *writer;
194
195 writer = bt_component_get_private_data(component);
196 assert(writer);
197 assert(!writer->input_iterator);
198 connection = bt_port_get_connection(self_port);
199 assert(connection);
200 writer->input_iterator = bt_connection_create_notification_iterator(
201 connection);
202
203 if (!writer->input_iterator) {
204 ret = BT_COMPONENT_STATUS_ERROR;
205 }
206 bt_put(connection);
207 return ret;
208 }
209
210 static
211 enum bt_component_status run(struct bt_component *component)
212 {
213 enum bt_component_status ret;
214 enum bt_notification_iterator_status it_status;
215 struct bt_notification *notification = NULL;
216 struct bt_notification_iterator *it;
217 struct writer_component *writer_component =
218 bt_component_get_private_data(component);
219
220 it = writer_component->input_iterator;
221 assert(it);
222
223 notification = bt_notification_iterator_get_notification(it);
224 if (!notification) {
225 ret = BT_COMPONENT_STATUS_ERROR;
226 goto end;
227 }
228
229 it_status = bt_notification_iterator_next(it);
230 if (it_status != BT_COMPONENT_STATUS_OK) {
231 ret = BT_COMPONENT_STATUS_ERROR;
232 goto end;
233 }
234
235 ret = handle_notification(writer_component, notification);
236 end:
237 bt_put(it);
238 bt_put(notification);
239 return ret;
240 }
241
242 static
243 enum bt_component_status writer_component_init(
244 struct bt_component *component, struct bt_value *params,
245 UNUSED_VAR void *init_method_data)
246 {
247 enum bt_component_status ret;
248 enum bt_value_status value_ret;
249 struct writer_component *writer_component = create_writer_component();
250 struct bt_value *value = NULL;
251 const char *path;
252
253 if (!writer_component) {
254 ret = BT_COMPONENT_STATUS_NOMEM;
255 goto end;
256 }
257
258 value = bt_value_map_get(params, "path");
259 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
260 fprintf(writer_component->err,
261 "[error] output path parameter required\n");
262 ret = BT_COMPONENT_STATUS_INVALID;
263 goto error;
264 }
265
266 value_ret = bt_value_string_get(value, &path);
267 if (value_ret != BT_VALUE_STATUS_OK) {
268 ret = BT_COMPONENT_STATUS_INVALID;
269 goto error;
270 }
271
272 writer_component->base_path = g_string_new(path);
273 if (!writer_component) {
274 ret = BT_COMPONENT_STATUS_ERROR;
275 goto error;
276 }
277
278 ret = bt_component_set_private_data(component, writer_component);
279 if (ret != BT_COMPONENT_STATUS_OK) {
280 goto error;
281 }
282
283 end:
284 return ret;
285 error:
286 destroy_writer_component_data(writer_component);
287 g_free(writer_component);
288 return ret;
289 }
290
291 /* Initialize plug-in entry points. */
292 BT_PLUGIN(writer);
293 BT_PLUGIN_DESCRIPTION("Babeltrace CTF-Writer output plug-in.");
294 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
295 BT_PLUGIN_LICENSE("MIT");
296 BT_PLUGIN_SINK_COMPONENT_CLASS(writer, run);
297 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(writer, writer_component_init);
298 BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(writer,
299 writer_component_accept_port_connection);
300 BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(writer, destroy_writer_component);
301 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(writer, "Formats CTF-IR to CTF.");
This page took 0.046106 seconds and 4 git commands to generate.