Make bt_private_connection_create_notification_iterator() return a status code
[babeltrace.git] / plugins / ctf / fs-sink / writer.c
CommitLineData
bc506aa5
JD
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>
33b34c43 30#include <babeltrace/plugin/plugin-dev.h>
73d5c1ad 31#include <babeltrace/graph/connection.h>
b2e0c907
PP
32#include <babeltrace/graph/component.h>
33#include <babeltrace/graph/private-component.h>
34#include <babeltrace/graph/component-sink.h>
35#include <babeltrace/graph/private-component-sink.h>
36#include <babeltrace/graph/private-port.h>
37#include <babeltrace/graph/private-connection.h>
38#include <babeltrace/graph/notification.h>
39#include <babeltrace/graph/notification-iterator.h>
40#include <babeltrace/graph/notification-event.h>
41#include <babeltrace/graph/notification-packet.h>
f3168545 42#include <babeltrace/graph/notification-stream.h>
7d61fa8e 43#include <plugins-common.h>
bc506aa5
JD
44#include <stdio.h>
45#include <stdbool.h>
46#include <glib.h>
47#include "writer.h"
c70ed359 48#include <assert.h>
bc506aa5 49
a619fcb7 50gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data)
f3168545 51{
a619fcb7
JD
52 struct fs_writer *fs_writer = value;
53 struct writer_component *writer_component = user_data;
54
3b41eaba 55 fs_writer->trace_static = 1;
a619fcb7
JD
56 writer_close(writer_component, fs_writer);
57
f3168545
JD
58 return TRUE;
59}
60
bc506aa5
JD
61static
62void destroy_writer_component_data(struct writer_component *writer_component)
63{
c70ed359 64 bt_put(writer_component->input_iterator);
f3168545 65
f3168545 66 g_hash_table_foreach_remove(writer_component->trace_map,
a619fcb7 67 empty_trace_map, writer_component);
bc506aa5 68 g_hash_table_destroy(writer_component->trace_map);
f3168545 69
9057f037
JD
70 g_string_free(writer_component->base_path, true);
71 g_string_free(writer_component->trace_name_base, true);
bc506aa5
JD
72}
73
d8866baa
PP
74BT_HIDDEN
75void writer_component_finalize(struct bt_private_component *component)
bc506aa5
JD
76{
77 struct writer_component *writer_component = (struct writer_component *)
890882ef 78 bt_private_component_get_user_data(component);
bc506aa5
JD
79
80 destroy_writer_component_data(writer_component);
81 g_free(writer_component);
82}
83
bc506aa5 84static
f3168545 85void free_fs_writer(struct fs_writer *fs_writer)
bc506aa5 86{
f3168545
JD
87 bt_put(fs_writer->writer);
88 g_free(fs_writer);
bc506aa5
JD
89}
90
91static
92struct writer_component *create_writer_component(void)
93{
94 struct writer_component *writer_component;
95
96 writer_component = g_new0(struct writer_component, 1);
97 if (!writer_component) {
98 goto end;
99 }
100
101 writer_component->err = stderr;
102 writer_component->trace_id = 0;
9057f037
JD
103 writer_component->trace_name_base = g_string_new("trace");
104 if (!writer_component->trace_name_base) {
105 g_free(writer_component);
106 writer_component = NULL;
107 goto end;
108 }
bc506aa5
JD
109
110 /*
111 * Reader to writer corresponding structures.
112 */
113 writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
f3168545 114 g_direct_equal, NULL, (GDestroyNotify) free_fs_writer);
bc506aa5
JD
115
116end:
117 return writer_component;
118}
119
120static
121enum bt_component_status handle_notification(
122 struct writer_component *writer_component,
123 struct bt_notification *notification)
124{
125 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
126
127 if (!writer_component) {
128 ret = BT_COMPONENT_STATUS_ERROR;
129 goto end;
130 }
131
132 switch (bt_notification_get_type(notification)) {
ea0e619e 133 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
bc506aa5
JD
134 {
135 struct bt_ctf_packet *packet =
ea0e619e 136 bt_notification_packet_begin_get_packet(notification);
bc506aa5
JD
137
138 if (!packet) {
139 ret = BT_COMPONENT_STATUS_ERROR;
140 goto end;
141 }
142
143 ret = writer_new_packet(writer_component, packet);
144 bt_put(packet);
145 break;
146 }
147 case BT_NOTIFICATION_TYPE_PACKET_END:
148 {
149 struct bt_ctf_packet *packet =
ea0e619e 150 bt_notification_packet_end_get_packet(notification);
bc506aa5
JD
151
152 if (!packet) {
153 ret = BT_COMPONENT_STATUS_ERROR;
154 goto end;
155 }
156 ret = writer_close_packet(writer_component, packet);
157 bt_put(packet);
158 break;
159 }
160 case BT_NOTIFICATION_TYPE_EVENT:
161 {
162 struct bt_ctf_event *event = bt_notification_event_get_event(
163 notification);
164
165 if (!event) {
166 ret = BT_COMPONENT_STATUS_ERROR;
167 goto end;
168 }
bc506aa5
JD
169 ret = writer_output_event(writer_component, event);
170 bt_put(event);
171 if (ret != BT_COMPONENT_STATUS_OK) {
172 goto end;
173 }
174 break;
175 }
f384901f
JD
176 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
177 {
178 struct bt_ctf_stream *stream =
179 bt_notification_stream_begin_get_stream(notification);
180
181 if (!stream) {
182 ret = BT_COMPONENT_STATUS_ERROR;
183 goto end;
184 }
185 ret = writer_stream_begin(writer_component, stream);
186 bt_put(stream);
187 break;
188 }
bc506aa5 189 case BT_NOTIFICATION_TYPE_STREAM_END:
f3168545
JD
190 {
191 struct bt_ctf_stream *stream =
192 bt_notification_stream_end_get_stream(notification);
193
194 if (!stream) {
195 ret = BT_COMPONENT_STATUS_ERROR;
196 goto end;
197 }
198 ret = writer_stream_end(writer_component, stream);
199 bt_put(stream);
bc506aa5 200 break;
f3168545 201 }
bc506aa5
JD
202 default:
203 puts("Unhandled notification type");
204 }
205end:
206 return ret;
207}
208
d8866baa 209BT_HIDDEN
0d8b4d8e 210void writer_component_port_connected(
890882ef 211 struct bt_private_component *component,
8f4799f7
PP
212 struct bt_private_port *self_port,
213 struct bt_port *other_port)
c70ed359 214{
890882ef 215 struct bt_private_connection *connection;
c70ed359 216 struct writer_component *writer;
73d5c1ad 217 enum bt_connection_status conn_status;
93fc16a5 218 static const enum bt_notification_type notif_types[] = {
f3168545 219 BT_NOTIFICATION_TYPE_EVENT,
93fc16a5
JD
220 BT_NOTIFICATION_TYPE_PACKET_BEGIN,
221 BT_NOTIFICATION_TYPE_PACKET_END,
f384901f 222 BT_NOTIFICATION_TYPE_STREAM_BEGIN,
f3168545 223 BT_NOTIFICATION_TYPE_STREAM_END,
93fc16a5
JD
224 BT_NOTIFICATION_TYPE_SENTINEL,
225 };
c70ed359 226
890882ef 227 writer = bt_private_component_get_user_data(component);
c70ed359
JG
228 assert(writer);
229 assert(!writer->input_iterator);
890882ef 230 connection = bt_private_port_get_private_connection(self_port);
72b913fb 231 assert(connection);
73d5c1ad
PP
232 conn_status = bt_private_connection_create_notification_iterator(
233 connection, notif_types, &writer->input_iterator);
234 if (conn_status != BT_CONNECTION_STATUS_OK) {
0d8b4d8e 235 writer->error = true;
c70ed359 236 }
0d8b4d8e 237
72b913fb 238 bt_put(connection);
c70ed359
JG
239}
240
d8866baa
PP
241BT_HIDDEN
242enum bt_component_status writer_run(struct bt_private_component *component)
bc506aa5
JD
243{
244 enum bt_component_status ret;
245 struct bt_notification *notification = NULL;
246 struct bt_notification_iterator *it;
247 struct writer_component *writer_component =
890882ef 248 bt_private_component_get_user_data(component);
7cdc2bab 249 enum bt_notification_iterator_status it_ret;
bc506aa5 250
0d8b4d8e
PP
251 if (unlikely(writer_component->error)) {
252 ret = BT_COMPONENT_STATUS_ERROR;
253 goto end;
254 }
255
7cdc2bab
MD
256 it = writer_component->input_iterator;
257 assert(it);
258 it_ret = bt_notification_iterator_next(it);
bc506aa5 259
7cdc2bab
MD
260 switch (it_ret) {
261 case BT_NOTIFICATION_ITERATOR_STATUS_END:
262 ret = BT_COMPONENT_STATUS_END;
263 BT_PUT(writer_component->input_iterator);
264 goto end;
265 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
266 ret = BT_COMPONENT_STATUS_AGAIN;
267 goto end;
268 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
269 break;
270 default:
72b913fb 271 ret = BT_COMPONENT_STATUS_ERROR;
996a07a1
JG
272 goto end;
273 }
274
7cdc2bab
MD
275 notification = bt_notification_iterator_get_notification(it);
276 assert(notification);
bc506aa5
JD
277 ret = handle_notification(writer_component, notification);
278end:
bc506aa5
JD
279 bt_put(notification);
280 return ret;
281}
282
d8866baa 283BT_HIDDEN
bc506aa5 284enum bt_component_status writer_component_init(
890882ef 285 struct bt_private_component *component, struct bt_value *params,
7d61fa8e 286 UNUSED_VAR void *init_method_data)
bc506aa5
JD
287{
288 enum bt_component_status ret;
289 enum bt_value_status value_ret;
290 struct writer_component *writer_component = create_writer_component();
291 struct bt_value *value = NULL;
292 const char *path;
293
294 if (!writer_component) {
295 ret = BT_COMPONENT_STATUS_NOMEM;
296 goto end;
297 }
298
147337a3
PP
299 ret = bt_private_component_sink_add_input_private_port(component,
300 "in", NULL, NULL);
301 if (ret != BT_COMPONENT_STATUS_OK) {
b9d103be
PP
302 goto end;
303 }
304
bc506aa5
JD
305 value = bt_value_map_get(params, "path");
306 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
307 fprintf(writer_component->err,
308 "[error] output path parameter required\n");
309 ret = BT_COMPONENT_STATUS_INVALID;
310 goto error;
311 }
312
313 value_ret = bt_value_string_get(value, &path);
314 if (value_ret != BT_VALUE_STATUS_OK) {
315 ret = BT_COMPONENT_STATUS_INVALID;
316 goto error;
317 }
d00b90bf 318 bt_put(value);
bc506aa5 319
9057f037
JD
320 writer_component->base_path = g_string_new(path);
321 if (!writer_component) {
322 ret = BT_COMPONENT_STATUS_ERROR;
323 goto error;
324 }
bc506aa5 325
890882ef 326 ret = bt_private_component_set_user_data(component, writer_component);
bc506aa5
JD
327 if (ret != BT_COMPONENT_STATUS_OK) {
328 goto error;
329 }
330
bc506aa5
JD
331end:
332 return ret;
333error:
334 destroy_writer_component_data(writer_component);
56fa591a 335 g_free(writer_component);
bc506aa5
JD
336 return ret;
337}
This page took 0.043571 seconds and 4 git commands to generate.