[gupnp] Context: Add new convenience constructors



commit 98da362250dde377939d9d13110e2b5d6a98aee8
Author: Jens Georg <mail jensge org>
Date:   Sun Aug 7 19:14:04 2022 +0200

    Context: Add new convenience constructors
    
    Add new constructors that allow to set the new parameters directly,
    instead of forcing the use of g_object_new

 examples/get-volume.c        |  6 +++-
 examples/light-client.c      |  5 +++-
 examples/light-server.c      |  2 +-
 libgupnp/gupnp-context.c     | 70 ++++++++++++++++++++++++++++++++++++++++++--
 libgupnp/gupnp-context.h     | 18 ++++++++++--
 tests/test-context-manager.c | 12 ++++++--
 6 files changed, 103 insertions(+), 10 deletions(-)
---
diff --git a/examples/get-volume.c b/examples/get-volume.c
index 0413631..46c5f62 100644
--- a/examples/get-volume.c
+++ b/examples/get-volume.c
@@ -126,7 +126,11 @@ int main(int argc, char *argv[])
         GError *error = NULL;
         GMainLoop *loop = g_main_loop_new (NULL, FALSE);
 
-        GUPnPContext *context = gupnp_context_new ("wlp3s0", 0, &error);
+        GUPnPContext *context = gupnp_context_new_full ("wlp3s0",
+                                                        NULL,
+                                                        0,
+                                                        GSSDP_UDA_VERSION_1_0,
+                                                        &error);
 
         if (error != NULL) {
                 g_error ("%s", error->message);
diff --git a/examples/light-client.c b/examples/light-client.c
index 359bc3e..b3eae95 100644
--- a/examples/light-client.c
+++ b/examples/light-client.c
@@ -160,7 +160,10 @@ main (int argc, char **argv)
   }
 
   /* Create the UPnP context */
-  context = gupnp_context_new (NULL, 0, &error);
+  context = gupnp_context_new_for_address (NULL,
+                                           0,
+                                           GSSDP_UDA_VERSION_1_0,
+                                           &error);
   if (error) {
     g_printerr ("Error creating the GUPnP context: %s\n",
                error->message);
diff --git a/examples/light-server.c b/examples/light-server.c
index 246b4d6..88b3e27 100644
--- a/examples/light-server.c
+++ b/examples/light-server.c
@@ -149,7 +149,7 @@ main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
   }
 
   /* Create the UPnP context */
-  context = gupnp_context_new (NULL, 0, &error);
+  context = gupnp_context_new_for_address (NULL, 0, GSSDP_UDA_VERSION_1_0, &error);
   if (error) {
     g_printerr ("Error creating the GUPnP context: %s\n",
                error->message);
diff --git a/libgupnp/gupnp-context.c b/libgupnp/gupnp-context.c
index 7b39ed5..236c52d 100644
--- a/libgupnp/gupnp-context.c
+++ b/libgupnp/gupnp-context.c
@@ -596,9 +596,11 @@ _gupnp_context_get_server_uri (GUPnPContext *context)
  * @port: Port to run on, or 0 if you don't care what port is used.
  * @error: (inout)(optional)(nullable): A location to store a #GError, or %NULL
  *
- * Create a new #GUPnPContext with the specified @main_context, @iface and
+ * Create a new #GUPnPContext with the specified @iface and
  * @port.
  *
+ * Deprecated: 1.6. Use [ctor@GUPnP.Context.new_for_address] instead
+ *
  * Return value: A new #GUPnPContext object, or %NULL on an error
  **/
 GUPnPContext *
@@ -614,7 +616,71 @@ gupnp_context_new (const char   *iface,
                                NULL);
 }
 
-\
+/**
+ * gupnp_context_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 GUPnP context with address @addr on network interface @iface. If
+ * neither is specified, GUPnP will chose the address it deems most suitable.
+ *
+ * Since: 1.6.
+ *
+ * Return value: (nullable):  A new #GSSDPClient object or %NULL on error.
+ */
+GUPnPContext *
+gupnp_context_new_full (const char *iface,
+                       GInetAddress *addr,
+                       guint16 port,
+                       GSSDPUDAVersion uda_version,
+                       GError **error)
+{
+        return g_initable_new (GUPNP_TYPE_CONTEXT,
+                               NULL,
+                               error,
+                               "interface",
+                               iface,
+                               "address",
+                               addr,
+                               "port",
+                               port,
+                               "uda-version",
+                               uda_version,
+                               NULL);
+}
+
+/**
+ * gupnp_context_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 GUPnP context with address @addr. If none is specified, GUPnP
+ * will chose the address it deems most suitable.
+ *
+ * Since: 1.6.
+ *
+ * Return value: (nullable):  A new #GSSDPClient object or %NULL on error.
+ */
+GUPnPContext *
+gupnp_context_new_for_address (GInetAddress *addr,
+                                guint16 port,
+                                GSSDPUDAVersion uda_version,
+                                GError **error)
+{
+        return gupnp_context_new_full (NULL, addr, port, uda_version, error);
+}
+
 /**
  * gupnp_context_get_port:
  * @context: A #GUPnPContext
diff --git a/libgupnp/gupnp-context.h b/libgupnp/gupnp-context.h
index 79b3e30..383a563 100644
--- a/libgupnp/gupnp-context.h
+++ b/libgupnp/gupnp-context.h
@@ -41,10 +41,22 @@ struct _GUPnPContextClass {
         void (* _gupnp_reserved4) (void);
 };
 
+G_DEPRECATED_FOR(gupnp_context_new_for_address)
 GUPnPContext *
-gupnp_context_new                      (const char   *iface,
-                                        guint         port,
-                                        GError      **error);
+gupnp_context_new (const char *iface, guint port, GError **error);
+
+GUPnPContext *
+gupnp_context_new_for_address (GInetAddress *addr,
+                               guint16 port,
+                               GSSDPUDAVersion uda_version,
+                               GError **error);
+
+GUPnPContext *
+gupnp_context_new_full (const char *iface,
+                        GInetAddress *addr,
+                        guint16 port,
+                        GSSDPUDAVersion uda_version,
+                        GError **error);
 
 guint
 gupnp_context_get_port                 (GUPnPContext *context);
diff --git a/tests/test-context-manager.c b/tests/test-context-manager.c
index c10cf65..f35f6c0 100644
--- a/tests/test-context-manager.c
+++ b/tests/test-context-manager.c
@@ -34,11 +34,19 @@ test_context_manager_manage ()
 {
         GError *error = NULL;
 
-        GUPnPContext *ctx = gupnp_context_new ("lo", 0, &error);
+        GUPnPContext *ctx = gupnp_context_new_full ("lo",
+                                                    NULL,
+                                                    0,
+                                                    GSSDP_UDA_VERSION_1_0,
+                                                    &error);
         g_assert_no_error (error);
         g_assert_nonnull (ctx);
 
-        GUPnPContext *ctx2 = gupnp_context_new ("lo", 0, &error);
+        GUPnPContext *ctx2 = gupnp_context_new_full ("lo",
+                                                     NULL,
+                                                     0,
+                                                     GSSDP_UDA_VERSION_1_0,
+                                                     &error);
         g_assert_no_error (error);
         g_assert_nonnull (ctx2);
 


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