Commit | Line | Data |
---|---|---|
c7eee084 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 | ||
55 | bt_object_init(query_exec, bt_query_executor_destroy); | |
56 | BT_LOGD("Created query executor: addr=%p", query_exec); | |
57 | ||
58 | end: | |
59 | return query_exec; | |
60 | } | |
61 | ||
62 | enum bt_query_status bt_query_executor_query( | |
63 | struct bt_query_executor *query_exec, | |
64 | struct bt_component_class *component_class, | |
65 | const char *object, struct bt_value *params, | |
66 | struct bt_value **user_result) | |
67 | { | |
90157d89 | 68 | struct bt_component_class_query_method_return ret = { |
c7eee084 PP |
69 | .result = NULL, |
70 | .status = BT_QUERY_STATUS_OK, | |
71 | }; | |
72 | ||
73 | if (!query_exec) { | |
74 | BT_LOGW_STR("Invalid parameter: query executor is NULL."); | |
75 | ret.status = BT_QUERY_STATUS_INVALID; | |
76 | goto end; | |
77 | } | |
78 | ||
79 | if (query_exec->canceled) { | |
80 | BT_LOGW_STR("Invalid parameter: query executor is canceled."); | |
81 | ret.status = BT_QUERY_STATUS_EXECUTOR_CANCELED; | |
82 | goto end; | |
83 | } | |
84 | ||
85 | if (!component_class) { | |
86 | BT_LOGW_STR("Invalid parameter: component class is NULL."); | |
87 | ret.status = BT_QUERY_STATUS_INVALID; | |
88 | goto end; | |
89 | } | |
90 | ||
91 | if (!object) { | |
92 | BT_LOGW_STR("Invalid parameter: object string is NULL."); | |
93 | ret.status = BT_QUERY_STATUS_INVALID; | |
94 | goto end; | |
95 | } | |
96 | ||
97 | if (!params) { | |
98 | params = bt_value_null; | |
99 | } | |
100 | ||
101 | if (!component_class->methods.query) { | |
102 | /* Not an error: nothing to query */ | |
103 | BT_LOGD("Component class has no registered query method: " | |
104 | "addr=%p, name=\"%s\", type=%s", | |
105 | component_class, | |
106 | bt_component_class_get_name(component_class), | |
107 | bt_component_class_type_string(component_class->type)); | |
108 | ret.status = BT_QUERY_STATUS_ERROR; | |
109 | goto end; | |
110 | } | |
111 | ||
112 | BT_LOGD("Calling user's query method: " | |
113 | "query-exec-addr=%p, comp-class-addr=%p, " | |
114 | "comp-class-name=\"%s\", comp-class-type=%s, " | |
115 | "object=\"%s\", params-addr=%p", | |
116 | query_exec, component_class, | |
117 | bt_component_class_get_name(component_class), | |
118 | bt_component_class_type_string(component_class->type), | |
119 | object, params); | |
120 | ret = component_class->methods.query(component_class, query_exec, | |
121 | object, params); | |
122 | BT_LOGD("User method returned: status=%s, result-addr=%p", | |
123 | bt_query_status_string(ret.status), ret.result); | |
124 | if (query_exec->canceled) { | |
125 | BT_PUT(ret.result); | |
126 | ret.status = BT_QUERY_STATUS_EXECUTOR_CANCELED; | |
127 | goto end; | |
128 | } else { | |
129 | if (ret.status == BT_QUERY_STATUS_EXECUTOR_CANCELED) { | |
130 | /* | |
131 | * The user cannot decide that the executor is | |
132 | * canceled if it's not. | |
133 | */ | |
134 | BT_PUT(ret.result); | |
135 | ret.status = BT_QUERY_STATUS_ERROR; | |
136 | goto end; | |
137 | } | |
138 | } | |
139 | ||
140 | switch (ret.status) { | |
141 | case BT_QUERY_STATUS_INVALID: | |
142 | /* | |
143 | * This is reserved for invalid parameters passed to | |
144 | * this function. | |
145 | */ | |
146 | BT_PUT(ret.result); | |
147 | ret.status = BT_QUERY_STATUS_ERROR; | |
148 | break; | |
149 | case BT_QUERY_STATUS_OK: | |
150 | if (!ret.result) { | |
151 | ret.result = bt_value_null; | |
152 | } | |
153 | break; | |
154 | default: | |
155 | if (ret.result) { | |
156 | BT_LOGW("User method did not return BT_QUERY_STATUS_OK, but result is not NULL: " | |
157 | "status=%s, result-addr=%p", | |
158 | bt_query_status_string(ret.status), ret.result); | |
159 | BT_PUT(ret.result); | |
160 | } | |
161 | } | |
162 | ||
163 | end: | |
164 | if (user_result) { | |
165 | *user_result = ret.result; | |
166 | ret.result = NULL; | |
167 | } | |
168 | ||
169 | bt_put(ret.result); | |
170 | return ret.status; | |
171 | } | |
172 | ||
173 | enum bt_query_status bt_query_executor_cancel( | |
174 | struct bt_query_executor *query_exec) | |
175 | { | |
176 | enum bt_query_status ret = BT_QUERY_STATUS_OK; | |
177 | ||
178 | if (!query_exec) { | |
179 | BT_LOGW_STR("Invalid parameter: query executor is NULL."); | |
180 | ret = BT_QUERY_STATUS_INVALID; | |
181 | goto end; | |
182 | } | |
183 | ||
184 | query_exec->canceled = BT_TRUE; | |
185 | BT_LOGV("Canceled query executor: addr=%p", query_exec); | |
186 | ||
187 | end: | |
188 | return ret; | |
189 | } | |
190 | ||
191 | bt_bool bt_query_executor_is_canceled(struct bt_query_executor *query_exec) | |
192 | { | |
193 | bt_bool canceled = BT_FALSE; | |
194 | ||
195 | if (!query_exec) { | |
196 | BT_LOGW_STR("Invalid parameter: query executor is NULL."); | |
197 | goto end; | |
198 | } | |
199 | ||
200 | canceled = query_exec->canceled; | |
201 | ||
202 | end: | |
203 | return canceled; | |
204 | } |