Commit | Line | Data |
---|---|---|
1286dcbb PP |
1 | /* |
2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_TAG "QUERY-EXECUTOR" | |
24 | #include <babeltrace/lib-logging-internal.h> | |
25 | ||
26 | #include <babeltrace/graph/query-executor.h> | |
27 | #include <babeltrace/graph/query-executor-internal.h> | |
28 | #include <babeltrace/graph/component-class.h> | |
29 | #include <babeltrace/graph/component-class-internal.h> | |
30 | #include <babeltrace/values.h> | |
31 | #include <babeltrace/object-internal.h> | |
32 | #include <babeltrace/compiler-internal.h> | |
33 | ||
34 | static | |
35 | void bt_query_executor_destroy(struct bt_object *obj) | |
36 | { | |
37 | struct bt_query_executor *query_exec = | |
38 | container_of(obj, struct bt_query_executor, base); | |
39 | ||
40 | BT_LOGD("Destroying port: addr=%p", query_exec); | |
41 | g_free(query_exec); | |
42 | } | |
43 | ||
44 | struct bt_query_executor *bt_query_executor_create(void) | |
45 | { | |
46 | struct bt_query_executor *query_exec; | |
47 | ||
48 | BT_LOGD_STR("Creating query executor."); | |
49 | query_exec = g_new0(struct bt_query_executor, 1); | |
50 | if (!query_exec) { | |
51 | BT_LOGE_STR("Failed to allocate one query executor."); | |
52 | goto end; | |
53 | } | |
54 | ||
1d7bf349 PP |
55 | bt_object_init_shared(&query_exec->base, |
56 | bt_query_executor_destroy); | |
1286dcbb PP |
57 | BT_LOGD("Created query executor: addr=%p", query_exec); |
58 | ||
59 | end: | |
60 | return query_exec; | |
61 | } | |
62 | ||
63 | enum bt_query_status bt_query_executor_query( | |
64 | struct bt_query_executor *query_exec, | |
65 | struct bt_component_class *component_class, | |
66 | const char *object, struct bt_value *params, | |
67 | struct bt_value **user_result) | |
68 | { | |
fe7265b5 | 69 | struct bt_component_class_query_method_return ret = { |
1286dcbb PP |
70 | .result = NULL, |
71 | .status = BT_QUERY_STATUS_OK, | |
72 | }; | |
73 | ||
74 | if (!query_exec) { | |
75 | BT_LOGW_STR("Invalid parameter: query executor is NULL."); | |
76 | ret.status = BT_QUERY_STATUS_INVALID; | |
77 | goto end; | |
78 | } | |
79 | ||
80 | if (query_exec->canceled) { | |
81 | BT_LOGW_STR("Invalid parameter: query executor is canceled."); | |
82 | ret.status = BT_QUERY_STATUS_EXECUTOR_CANCELED; | |
83 | goto end; | |
84 | } | |
85 | ||
86 | if (!component_class) { | |
87 | BT_LOGW_STR("Invalid parameter: component class is NULL."); | |
88 | ret.status = BT_QUERY_STATUS_INVALID; | |
89 | goto end; | |
90 | } | |
91 | ||
92 | if (!object) { | |
93 | BT_LOGW_STR("Invalid parameter: object string is NULL."); | |
94 | ret.status = BT_QUERY_STATUS_INVALID; | |
95 | goto end; | |
96 | } | |
97 | ||
98 | if (!params) { | |
99 | params = bt_value_null; | |
100 | } | |
101 | ||
102 | if (!component_class->methods.query) { | |
103 | /* Not an error: nothing to query */ | |
104 | BT_LOGD("Component class has no registered query method: " | |
105 | "addr=%p, name=\"%s\", type=%s", | |
106 | component_class, | |
107 | bt_component_class_get_name(component_class), | |
108 | bt_component_class_type_string(component_class->type)); | |
109 | ret.status = BT_QUERY_STATUS_ERROR; | |
110 | goto end; | |
111 | } | |
112 | ||
113 | BT_LOGD("Calling user's query method: " | |
114 | "query-exec-addr=%p, comp-class-addr=%p, " | |
115 | "comp-class-name=\"%s\", comp-class-type=%s, " | |
116 | "object=\"%s\", params-addr=%p", | |
117 | query_exec, component_class, | |
118 | bt_component_class_get_name(component_class), | |
119 | bt_component_class_type_string(component_class->type), | |
120 | object, params); | |
121 | ret = component_class->methods.query(component_class, query_exec, | |
122 | object, params); | |
123 | BT_LOGD("User method returned: status=%s, result-addr=%p", | |
124 | bt_query_status_string(ret.status), ret.result); | |
125 | if (query_exec->canceled) { | |
8138bfe1 | 126 | BT_OBJECT_PUT_REF_AND_RESET(ret.result); |
1286dcbb PP |
127 | ret.status = BT_QUERY_STATUS_EXECUTOR_CANCELED; |
128 | goto end; | |
129 | } else { | |
130 | if (ret.status == BT_QUERY_STATUS_EXECUTOR_CANCELED) { | |
131 | /* | |
132 | * The user cannot decide that the executor is | |
133 | * canceled if it's not. | |
134 | */ | |
8138bfe1 | 135 | BT_OBJECT_PUT_REF_AND_RESET(ret.result); |
1286dcbb PP |
136 | ret.status = BT_QUERY_STATUS_ERROR; |
137 | goto end; | |
138 | } | |
139 | } | |
140 | ||
141 | switch (ret.status) { | |
142 | case BT_QUERY_STATUS_INVALID: | |
143 | /* | |
144 | * This is reserved for invalid parameters passed to | |
145 | * this function. | |
146 | */ | |
8138bfe1 | 147 | BT_OBJECT_PUT_REF_AND_RESET(ret.result); |
1286dcbb PP |
148 | ret.status = BT_QUERY_STATUS_ERROR; |
149 | break; | |
150 | case BT_QUERY_STATUS_OK: | |
151 | if (!ret.result) { | |
152 | ret.result = bt_value_null; | |
153 | } | |
154 | break; | |
155 | default: | |
156 | if (ret.result) { | |
157 | BT_LOGW("User method did not return BT_QUERY_STATUS_OK, but result is not NULL: " | |
158 | "status=%s, result-addr=%p", | |
159 | bt_query_status_string(ret.status), ret.result); | |
8138bfe1 | 160 | BT_OBJECT_PUT_REF_AND_RESET(ret.result); |
1286dcbb PP |
161 | } |
162 | } | |
163 | ||
164 | end: | |
165 | if (user_result) { | |
166 | *user_result = ret.result; | |
167 | ret.result = NULL; | |
168 | } | |
169 | ||
8138bfe1 | 170 | bt_object_put_ref(ret.result); |
1286dcbb PP |
171 | return ret.status; |
172 | } | |
173 | ||
174 | enum bt_query_status bt_query_executor_cancel( | |
175 | struct bt_query_executor *query_exec) | |
176 | { | |
177 | enum bt_query_status ret = BT_QUERY_STATUS_OK; | |
178 | ||
179 | if (!query_exec) { | |
180 | BT_LOGW_STR("Invalid parameter: query executor is NULL."); | |
181 | ret = BT_QUERY_STATUS_INVALID; | |
182 | goto end; | |
183 | } | |
184 | ||
185 | query_exec->canceled = BT_TRUE; | |
186 | BT_LOGV("Canceled query executor: addr=%p", query_exec); | |
187 | ||
188 | end: | |
189 | return ret; | |
190 | } | |
191 | ||
192 | bt_bool bt_query_executor_is_canceled(struct bt_query_executor *query_exec) | |
193 | { | |
194 | bt_bool canceled = BT_FALSE; | |
195 | ||
196 | if (!query_exec) { | |
197 | BT_LOGW_STR("Invalid parameter: query executor is NULL."); | |
198 | goto end; | |
199 | } | |
200 | ||
201 | canceled = query_exec->canceled; | |
202 | ||
203 | end: | |
204 | return canceled; | |
205 | } |