-
Notifications
You must be signed in to change notification settings - Fork 60
Feature/add support for actions with arguments #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
2888209
607500a
cde44be
bb3f176
bc324f8
c5dc2cb
111a00c
c32928e
955a70a
671fbda
5023041
346124a
7c20655
0084c4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,7 +208,9 @@ gstd_action_create_default (GstdObject * object, const gchar * name, | |
| GstdAction *action = NULL; | ||
| GSignalQuery query; | ||
| guint action_id; | ||
| gint ret_sig = 0; | ||
| GValue ret_sig = G_VALUE_INIT; | ||
| GValue *args = NULL; | ||
| gchar **arg_list = NULL; | ||
|
|
||
| GST_INFO_OBJECT (action, "Action create"); | ||
|
|
||
|
|
@@ -222,19 +224,62 @@ gstd_action_create_default (GstdObject * object, const gchar * name, | |
| G_OBJECT_TYPE (action->target)); | ||
| g_signal_query (action_id, &query); | ||
|
|
||
| if (query.n_params > 0) { | ||
| GST_ERROR_OBJECT (action, "Only actions with no parameters are supported"); | ||
| ret = GSTD_BAD_VALUE; | ||
| goto out; | ||
| if (query.n_params > 0 && (!description || g_strcmp0(description, "(null)") == 0 || g_strcmp0(description, "") == 0)) { | ||
| return GSTD_NULL_ARGUMENT; | ||
| } else if (description) { | ||
| arg_list = g_strsplit (description, " ", query.n_params); | ||
| } | ||
|
|
||
| GST_INFO_OBJECT (action, "Emit to %s", GST_OBJECT_NAME (action->target)); | ||
| g_signal_emit_by_name (action->target, name, &ret_sig); | ||
| if (g_strv_length (arg_list) != query.n_params) { | ||
| return GSTD_NULL_ARGUMENT; | ||
| } | ||
|
|
||
| /* One additional value to store the instance as first value */ | ||
| args = g_new0 (GValue, query.n_params + 1); | ||
|
|
||
| g_value_init (&args[0], G_TYPE_OBJECT); | ||
| g_value_set_object (&args[0], action->target); | ||
|
|
||
| for (guint i = 0; i < query.n_params; i++) { | ||
| g_value_init (&args[i + 1], query.param_types[i]); | ||
|
|
||
| if (query.param_types[i] == G_TYPE_STRING) { | ||
| g_value_set_string (&args[i + 1], arg_list[i]); | ||
| } else if (query.param_types[i] == G_TYPE_INT) { | ||
| g_value_set_int (&args[i + 1], g_ascii_strtoll (arg_list[i], NULL, 10)); | ||
| } else if (query.param_types[i] == G_TYPE_UINT) { | ||
| g_value_set_uint (&args[i + 1], (guint) g_ascii_strtoull (arg_list[i], NULL, 10)); | ||
| } else if (query.param_types[i] == G_TYPE_UINT64) { | ||
| g_value_set_uint64 (&args[i + 1], (guint64) g_ascii_strtoull (arg_list[i], | ||
| NULL, 10)); | ||
| } else if (query.param_types[i] == G_TYPE_BOOLEAN) { | ||
| g_value_set_boolean (&args[i + 1], g_ascii_strcasecmp (arg_list[i], "true") == 0); | ||
| } else if (query.param_types[i] == G_TYPE_FLOAT) { | ||
| g_value_set_float (&args[i + 1], g_ascii_strtod (arg_list[i], NULL)); | ||
| } else if (query.param_types[i] == G_TYPE_DOUBLE) { | ||
| g_value_set_double (&args[i + 1], g_ascii_strtod (arg_list[i], NULL)); | ||
| } else { | ||
| ret = GSTD_BAD_COMMAND; | ||
| goto out; | ||
| } | ||
| } | ||
|
|
||
| if (ret_sig != 0) { | ||
| ret = GSTD_BAD_VALUE; | ||
| if (query.return_type != G_TYPE_NONE) { | ||
| g_value_init(&ret_sig, query.return_type); | ||
| g_signal_emitv (args, action_id, 0, &ret_sig); | ||
|
|
||
| /* The return value is not required */ | ||
|
|
||
| g_value_unset(&ret_sig); | ||
| } else { | ||
| g_signal_emitv (args, action_id, 0, NULL); | ||
| } | ||
|
|
||
| out: | ||
| for (guint i = 0; i <= query.n_params; i++) | ||
| g_value_unset (&args[i]); | ||
|
Comment on lines
+279
to
+280
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Braces |
||
| g_free (args); | ||
| g_strfreev (arg_list); | ||
|
|
||
| return ret; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| #include <string.h> | ||
| #include <gst/gst.h> | ||
| #include <libsoup/soup.h> | ||
| #include <json-glib/json-glib.h> | ||
|
|
||
| #include "gstd_http.h" | ||
| #include "gstd_parser.h" | ||
|
|
@@ -83,6 +84,7 @@ static GstdReturnCode do_put (SoupServer * server, SoupMessage * msg, | |
| static GstdReturnCode do_delete (SoupServer * server, SoupMessage * msg, | ||
| char *name, char **output, const char *path, GstdSession * session); | ||
| static void do_request (gpointer data_request, gpointer eval); | ||
| static void parse_json_body (SoupMessage *msg, gchar **out_name, gchar **out_desc); | ||
| static void server_callback (SoupServer * server, SoupMessage * msg, | ||
| const char *path, GHashTable * query, SoupClientContext * context, | ||
| gpointer data); | ||
|
|
@@ -311,9 +313,13 @@ do_request (gpointer data_request, gpointer eval) | |
| path = data_request_local->path; | ||
| query = data_request_local->query; | ||
|
|
||
| if (query != NULL) { | ||
| name = g_hash_table_lookup (query, "name"); | ||
| description_pipe = g_hash_table_lookup (query, "description"); | ||
|
Comment on lines
-314
to
-316
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you evaluated if this breaks something? It seems that everything is now encapsulated into a JSON. I wonder if this is going to break something on the HTTP side. Have you checked the examples?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I evaluated the examples using Postman and curl, with the current HTTP structure and using a JSON. For example: curl --location 'http://127.0.0.1:3000/pipelines' --header 'Content-Type: application/json' --data '{"name": "p1", "description": "videotestsrc name=vts ! identity name=id ! autovideosink"}'
curl --location --request POST 'http://127.0.0.1:3000/pipelines?name=p0&description=videotestsrc%20name%3Dvts%20!%20identity%20name%3Did%20!%20autovideosink' |
||
| parse_json_body (msg, &name, &description_pipe); | ||
|
|
||
| if (!name && query) { | ||
| name = g_strdup (g_hash_table_lookup (query, "name")); | ||
| } | ||
| if (!description_pipe && query) { | ||
| description_pipe = g_strdup (g_hash_table_lookup (query, "description")); | ||
DmongeN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (msg->method == SOUP_METHOD_GET) { | ||
|
|
@@ -327,6 +333,10 @@ do_request (gpointer data_request, gpointer eval) | |
| } else if (msg->method == SOUP_METHOD_OPTIONS) { | ||
| ret = GSTD_EOK; | ||
| } | ||
| g_free (name); | ||
| g_free (description_pipe); | ||
| name = NULL; | ||
| description_pipe = NULL; | ||
|
|
||
| description = gstd_return_code_to_string (ret); | ||
| response = | ||
|
|
@@ -356,6 +366,56 @@ do_request (gpointer data_request, gpointer eval) | |
| return; | ||
| } | ||
|
|
||
| static void | ||
| parse_json_body (SoupMessage *msg, gchar **out_name, gchar **out_desc) | ||
DmongeN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| const char *content_type = NULL; | ||
| JsonParser *parser = NULL; | ||
| JsonNode *root = NULL; | ||
| GError *err = NULL; | ||
|
|
||
| g_return_if_fail (msg); | ||
| g_return_if_fail (out_name); | ||
| g_return_if_fail (out_desc); | ||
|
|
||
| *out_name = NULL; | ||
| *out_desc = NULL; | ||
|
|
||
| soup_message_body_flatten (msg->request_body); | ||
| if (!msg->request_body || msg->request_body->length == 0) { | ||
| return; | ||
DmongeN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| content_type = soup_message_headers_get_content_type (msg->request_headers, NULL); | ||
| if (!content_type || !g_str_has_prefix (content_type, "application/json")) { | ||
| return; | ||
DmongeN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| parser = json_parser_new (); | ||
| if (!json_parser_load_from_data (parser, | ||
| msg->request_body->data, | ||
| msg->request_body->length, | ||
| &err)) { | ||
|
Comment on lines
+395
to
+398
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation is incorrect. You should use |
||
| g_clear_error (&err); | ||
| g_object_unref (parser); | ||
| return; | ||
| } | ||
|
|
||
| root = json_parser_get_root (parser); | ||
| if (JSON_NODE_HOLDS_OBJECT (root)) { | ||
| JsonObject *obj = json_node_get_object (root); | ||
| if (json_object_has_member (obj, "name")) { | ||
| const char *value = json_object_get_string_member (obj, "name"); | ||
| if (value) *out_name = g_strdup (value); | ||
| } | ||
| if (json_object_has_member (obj, "description")) { | ||
| const char *value = json_object_get_string_member (obj, "description"); | ||
| if (value) *out_desc = g_strdup (value); | ||
| } | ||
| } | ||
| g_object_unref (parser); | ||
| } | ||
|
|
||
| static void | ||
| server_callback (SoupServer * server, SoupMessage * msg, | ||
| const char *path, GHashTable * query, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should document this. That the actions ignore the return values