diff options
author | Jens Georg <mail@jensge.org> | 2022-12-07 22:00:06 +0100 |
---|---|---|
committer | Jens Georg <mail@jensge.org> | 2022-12-07 22:00:06 +0100 |
commit | 298d3f77decc31ec573d2efdbe53ad0547cd46e0 (patch) | |
tree | 908e0332140a53d84ffb9d5d872b96d6643ac8f1 /examples | |
parent | 863f2c0bf2ea1a65906852481313aa7224c92536 (diff) | |
download | gupnp-298d3f77decc31ec573d2efdbe53ad0547cd46e0.tar.gz |
examples: Add ContextManager spawn tool
Mainly useful for debugging context managers
Diffstat (limited to 'examples')
-rw-r--r-- | examples/meson.build | 2 | ||||
-rw-r--r-- | examples/test-context-manager.c | 75 |
2 files changed, 76 insertions, 1 deletions
diff --git a/examples/meson.build b/examples/meson.build index eb32dfd..a9460e0 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -17,7 +17,7 @@ executable( dependencies: gupnp ) -foreach program : ['browsing', 'proxy', 'server', 'introspection', 'white-list'] +foreach program : ['browsing', 'proxy', 'server', 'introspection', 'white-list', 'context-manager'] executable( 'test-' + program, sources : 'test-@0@.c'.format(program), diff --git a/examples/test-context-manager.c b/examples/test-context-manager.c new file mode 100644 index 0000000..560b452 --- /dev/null +++ b/examples/test-context-manager.c @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: MIT +// SPDX-FileCopyrightText: 2022 Jens Georg <mail@jensge.org> + +#include <glib-unix.h> +#include <libgupnp/gupnp.h> + +typedef struct { + GMainLoop *loop; + GList *contexts; +} Data; + +static void +on_context_available (GUPnPContextManager *context_manager, + GUPnPContext *context, + gpointer user_data) +{ + Data *data = user_data; + + g_print ("New context: %s %s\n", + gssdp_client_get_interface (GSSDP_CLIENT (context)), + gssdp_client_get_host_ip (GSSDP_CLIENT (context))); + data->contexts = + g_list_prepend (data->contexts, g_object_ref (context)); +} + +static void +on_context_unavailable (GUPnPContextManager *context_manager, + GUPnPContext *context, + gpointer user_data) +{ + Data *data = user_data; + + g_print ("Context unavailable: %s %s\n", + gssdp_client_get_interface (GSSDP_CLIENT (context)), + gssdp_client_get_host_ip (GSSDP_CLIENT (context))); + + GList *item = g_list_find (data->contexts, context); + if (item != NULL) { + g_object_unref (G_OBJECT (item->data)); + data->contexts = g_list_delete_link (data->contexts, item); + } +} + +int +main (int argc, char *argv[]) +{ + Data data = { NULL, NULL }; + + g_autoptr (GMainLoop) loop = g_main_loop_new (NULL, FALSE); + data.loop = loop; + + g_autoptr (GUPnPContextManager) mgr = + gupnp_context_manager_create_full (GSSDP_UDA_VERSION_1_0, + G_SOCKET_FAMILY_INVALID, + 0); + + g_signal_connect (mgr, + "context-available", + G_CALLBACK (on_context_available), + &data); + g_signal_connect (mgr, + "context-unavailable", + G_CALLBACK (on_context_unavailable), + &data); + + //g_timeout_add_seconds (5, (GSourceFunc) g_main_loop_quit, loop); + g_unix_signal_add (SIGINT, (GSourceFunc) g_main_loop_quit, loop); + + g_main_loop_run (loop); + + g_signal_handlers_disconnect_by_data (mgr, &data); + + g_list_free_full (data.contexts, g_object_unref); + return 0; +} |