[gssdp: 7/7] Client: Add new convenience constructors




commit c8a54cd0b86f43c16ec8c6e4cd8f6da4066b5fdd
Author: Jens Georg <mail jensge org>
Date:   Fri Aug 5 18:42:53 2022 +0200

    Client: Add new convenience constructors

 examples/test-browser.c    |  6 +++-
 examples/test-publish.c    |  6 +++-
 libgssdp/gssdp-client.c    | 83 +++++++++++++++++++++++++++++++++++++++++++---
 libgssdp/gssdp-client.h    | 15 +++++++++
 libgssdp/gssdp-net-posix.c | 10 ++----
 tests/test-functional.c    | 43 ++++++++++++++++++++++++
 tests/test-util.c          | 38 +++++++--------------
 7 files changed, 161 insertions(+), 40 deletions(-)
---
diff --git a/examples/test-browser.c b/examples/test-browser.c
index bbabf9e..6de8c5b 100644
--- a/examples/test-browser.c
+++ b/examples/test-browser.c
@@ -46,7 +46,11 @@ main (G_GNUC_UNUSED int    argc,
         GMainLoop *main_loop;
 
         error = NULL;
-        client = gssdp_client_new (NULL, &error);
+        client = gssdp_client_new_full (NULL,
+                                        NULL,
+                                        0,
+                                        GSSDP_UDA_VERSION_1_0,
+                                        &error);
         if (error) {
                 g_printerr ("Error creating the GSSDP client: %s\n",
                             error->message);
diff --git a/examples/test-publish.c b/examples/test-publish.c
index 02072d5..faf58b2 100644
--- a/examples/test-publish.c
+++ b/examples/test-publish.c
@@ -21,7 +21,11 @@ main (G_GNUC_UNUSED int    argc,
         GMainLoop *main_loop;
 
         error = NULL;
-        client = gssdp_client_new (NULL, &error);
+        client = gssdp_client_new_full (NULL,
+                                        NULL,
+                                        0,
+                                        GSSDP_UDA_VERSION_1_0,
+                                        &error);
         if (error) {
                 g_printerr ("Error creating the GSSDP client: %s\n",
                             error->message);
diff --git a/libgssdp/gssdp-client.c b/libgssdp/gssdp-client.c
index 02f81c8..60aeb06 100644
--- a/libgssdp/gssdp-client.c
+++ b/libgssdp/gssdp-client.c
@@ -659,7 +659,7 @@ gssdp_client_class_init (GSSDPClientClass *klass)
          * used to determine the proper address.
          *
          * If not specified, will contain the currrent address family after
-         * the call to [method GLib Initable init]. Use %G_SOCKET_FAMILY_INVALID
+         * the call to [method Glib Initable init]. Use %G_SOCKET_FAMILY_INVALID
          * to specifiy using the default socket family (legacy IP)
          *
          * Since: 1.2.0
@@ -762,13 +762,17 @@ gssdp_client_class_init (GSSDPClientClass *klass)
 
 /**
  * gssdp_client_new:
- * @iface: (nullable): The name of the network interface, or %NULL for auto-detection.
+ * @iface: (nullable): The name of the network interface, or %NULL for
+ * auto-detection.
  * @error: (nullable): Location to store error, or %NULL
  *
  * Creates a GSSDP client on @iface. GSSDPClient will pick the address it finds
  * suitable for using.
  *
- * Using this utility function, the created client will be using UDA 1.0 and IPv4 only.
+ * Using this utility function, the created client will be using UDA 1.0 and
+ * IPv4 only.
+ *
+ * Deprecated: 1.6. Use [ctor@GSSDP.Client.new_for_address] instead.
  *
  * Return value: (nullable): A new #GSSDPClient object.
  **/
@@ -795,6 +799,8 @@ gssdp_client_new (const char *iface, GError **error)
  *
  * Using this utility function, the created client will be using UDA 1.0 and IPv4 only.
  *
+ * Deprecated: 1.6. Use [ctor@GSSDP.Client.new_for_address] instead.
+ *
  * Return value: (nullable):  A new #GSSDPClient object or %NULL on error.
  */
 GSSDPClient *
@@ -805,11 +811,78 @@ gssdp_client_new_with_port (const char *iface,
         return g_initable_new (GSSDP_TYPE_CLIENT,
                                NULL,
                                error,
-                               "interface", iface,
-                               "msearch-port", msearch_port,
+                               "interface",
+                               iface,
+                               "port",
+                               msearch_port,
                                NULL);
 }
 
+/**
+ * gssdp_client_new_full:
+ * @iface: (nullable): the name of a network interface
+ * @addr: (nullable): an IP address or %NULL for auto-detection. If you do not
+ * care about the address, but want to specify an address family, use
+ * [ctor@Glib.InetAddress.new_any] with the appropriate family instead.
+ * @port: The network port to use for M-SEARCH requests or 0 for
+ * random.
+ * @uda_version: The UDA version this client will adhere to
+ * @error: (allow-none): Location to store error, or %NULL.
+ *
+ * Creates a GSSDP client with address @addr. If none is specified, GSSDP
+ * will chose the address it deems most suitable.
+ *
+ * Since: 1.6.
+ *
+ * Return value: (nullable):  A new #GSSDPClient object or %NULL on error.
+ */
+GSSDPClient *
+gssdp_client_new_full (const char *iface,
+                       GInetAddress *addr,
+                       guint16 port,
+                       GSSDPUDAVersion uda_version,
+                       GError **error)
+{
+        return g_initable_new (GSSDP_TYPE_CLIENT,
+                               NULL,
+                               error,
+                               "interface",
+                               iface,
+                               "address",
+                               addr,
+                               "port",
+                               port,
+                               "uda-version",
+                               uda_version,
+                               NULL);
+}
+
+/**
+ * gssdp_client_new_for_address
+ * @addr: (nullable): an IP address or %NULL for auto-detection. If you do not
+ * care about the address, but want to specify an address family, use
+ * [ctor@Glib.InetAddress.new_any] with the appropriate family instead.
+ * @port: The network port to use for M-SEARCH requests or 0 for
+ * random.
+ * @uda_version: The UDA version this client will adhere to
+ * @error: (allow-none): Location to store error, or %NULL.
+ *
+ * Creates a GSSDP client with address @addr. If none is specified, GSSDP
+ * will chose the address it deems most suitable.
+ *
+ * Since: 1.6.
+ *
+ * Return value: (nullable):  A new #GSSDPClient object or %NULL on error.
+ */
+GSSDPClient *
+gssdp_client_new_for_address (GInetAddress *addr,
+                              guint16 port,
+                              GSSDPUDAVersion uda_version,
+                              GError **error)
+{
+        return gssdp_client_new_full (NULL, addr, port, uda_version, error);
+}
+
 /**
  * gssdp_client_set_server_id:(attributes org.gtk.Method.set_property=server-id):
  * @client: A #GSSDPClient
diff --git a/libgssdp/gssdp-client.h b/libgssdp/gssdp-client.h
index 9b2fb8c..0514b6d 100644
--- a/libgssdp/gssdp-client.h
+++ b/libgssdp/gssdp-client.h
@@ -47,15 +47,30 @@ struct _GSSDPClientClass {
         void (* _gssdp_reserved4) (void);
 };
 
+G_DEPRECATED_FOR(gssdp_client_new_for_address)
 GSSDPClient *
 gssdp_client_new              (const char   *iface,
                                GError      **error);
 
+G_DEPRECATED_FOR(gssdp_client_new_for_address)
 GSSDPClient *
 gssdp_client_new_with_port    (const char *iface,
                                guint16     msearch_port,
                                GError    **error);
 
+GSSDPClient *
+gssdp_client_new_for_address (GInetAddress *addr,
+                              guint16 port,
+                              GSSDPUDAVersion uda_version,
+                              GError **error);
+
+GSSDPClient *
+gssdp_client_new_full (const char *iface,
+                       GInetAddress *addr,
+                       guint16 port,
+                       GSSDPUDAVersion uda_version,
+                       GError **error);
+
 void
 gssdp_client_set_server_id    (GSSDPClient  *client,
                                const char   *server_id);
diff --git a/libgssdp/gssdp-net-posix.c b/libgssdp/gssdp-net-posix.c
index cd46014..be5a343 100644
--- a/libgssdp/gssdp-net-posix.c
+++ b/libgssdp/gssdp-net-posix.c
@@ -468,6 +468,9 @@ gssdp_net_get_host_ip (GSSDPNetworkDevice *device, GError **error)
          */
         if (device->host_addr) {
                 family = g_inet_address_get_family (device->host_addr);
+                // Address was solely added to select the address family
+                if (g_inet_address_get_is_any (device->host_addr))
+                        g_clear_object (&device->host_addr);
         }
 
         for (ifaceptr = up_ifaces;
@@ -491,15 +494,8 @@ gssdp_net_get_host_ip (GSSDPNetworkDevice *device, GError **error)
                 if (device->host_addr == NULL) {
                         switch (ifa->ifa_addr->sa_family) {
                         case AF_INET:
-                                /* legacy IP: Easy, just take the first
-                                 * address we can find */
-                                device->host_addr = g_object_ref (device_addr);
-                                break;
                         case AF_INET6:
-                                /* IP: Bit more complicated. We have to select a
-                                 * link-local or ULA address */
                                 device->host_addr = g_object_ref (device_addr);
-                                break;
                         default:
                                 /* We filtered this out in the list before */
                                 g_assert_not_reached ();
diff --git a/tests/test-functional.c b/tests/test-functional.c
index 9f43c6c..0349dbc 100644
--- a/tests/test-functional.c
+++ b/tests/test-functional.c
@@ -478,6 +478,47 @@ test_discovery_versioned_ignore_older (void)
         g_main_loop_unref (loop);
 }
 
+void
+test_client_creation ()
+{
+        GError *error = NULL;
+
+        GSSDPClient *client = NULL;
+        client = gssdp_client_new_for_address (NULL,
+                                               0,
+                                               GSSDP_UDA_VERSION_1_0,
+                                               &error);
+        g_assert_no_error (error);
+        g_assert_nonnull (client);
+
+        g_clear_object (&client);
+
+        GInetAddress *addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
+        client = gssdp_client_new_for_address (addr,
+                                               0,
+                                               GSSDP_UDA_VERSION_1_0,
+                                               &error);
+        g_assert_no_error (error);
+        g_assert_nonnull (client);
+        g_assert_cmpint (G_SOCKET_FAMILY_IPV4,
+                         ==,
+                         gssdp_client_get_family (client));
+        g_clear_object (&client);
+        g_clear_object (&addr);
+
+        addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV6);
+        client = gssdp_client_new_for_address (addr,
+                                               0,
+                                               GSSDP_UDA_VERSION_1_0,
+                                               &error);
+        g_assert_no_error (error);
+        g_assert_nonnull (client);
+        g_assert_cmpint (G_SOCKET_FAMILY_IPV6,
+                         ==,
+                         gssdp_client_get_family (client));
+        g_clear_object (&client);
+        g_clear_object (&addr);
+}
 
 int main(int argc, char *argv[])
 {
@@ -501,6 +542,8 @@ int main(int argc, char *argv[])
         g_test_add_func ("/functional/resource-group/discovery/versioned/ignore-older",
                          test_discovery_versioned_ignore_older);
 
+        g_test_add_func ("/functional/creation", test_client_creation);
+
         g_test_run ();
 
         return 0;
diff --git a/tests/test-util.c b/tests/test-util.c
index 44b32dd..ad0e0a1 100644
--- a/tests/test-util.c
+++ b/tests/test-util.c
@@ -45,33 +45,19 @@ unref_object (gpointer object)
 GSSDPClient *
 get_client (GError **outer_error)
 {
-        static gsize init_guard = 0;
-        static char *device = NULL;
+        GSSDPClient *client = NULL;
+        GError *error = NULL;
+        GInetAddress *lo =
+                g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
 
-        if (g_once_init_enter (&init_guard)) {
-                GSSDPClient *client = NULL;
-                GError *error = NULL;
+        client = gssdp_client_new_for_address (lo,
+                                               0,
+                                               GSSDP_UDA_VERSION_1_0,
+                                               &error);
 
-                g_debug ("Detecting network interface to use for tests...");
+        g_assert_no_error (error);
+        g_assert_nonnull (client);
+        g_clear_object (&lo);
 
-                client = gssdp_client_new ("lo", &error);
-                if (error == NULL) {
-                        g_debug ("Using lo");
-                        device = g_strdup ("lo");
-                        g_object_unref (client);
-                } else {
-                        g_clear_error(&error);
-                        client = gssdp_client_new ("lo0", &error);
-                        if (error == NULL) {
-                                g_debug ("Using lo0");
-                                device = g_strdup ("lo0");
-                                g_object_unref (client);
-                        } else {
-                                g_debug ("Using default interface, expect fails");
-                        }
-                }
-                g_once_init_leave (&init_guard, 1);
-        }
-
-        return gssdp_client_new (device, outer_error);
+        return client;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]