[farsight2/master] Store proxies in structures to be able to remember actions

Olivier Crête olivier.crete at collabora.co.uk
Tue Dec 23 15:24:43 PST 2008


---
 fs-upnp-simple-igd.c |   77 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/fs-upnp-simple-igd.c b/fs-upnp-simple-igd.c
index 4dbb7e3..aea9879 100644
--- a/fs-upnp-simple-igd.c
+++ b/fs-upnp-simple-igd.c
@@ -33,7 +33,7 @@ struct _FsUpnpSimpleIgdPrivate
   GUPnPContext *gupnp_context;
   GUPnPControlPoint *cp;
 
-  GPtrArray *service_proxies;
+  GArray *service_proxies;
 
   gulong avail_handler;
   gulong unavail_handler;
@@ -43,6 +43,17 @@ struct _FsUpnpSimpleIgdPrivate
   gboolean gathering;
 };
 
+struct Proxy {
+  GUPnPServiceProxy *proxy;
+  GArray *actions;
+};
+
+struct Action {
+  GUPnPServiceProxyAction *action;
+  GSource *timeout_source;
+};
+
+
 /* signals */
 enum
 {
@@ -80,6 +91,8 @@ static void fs_upnp_simple_igd_set_property (GObject *object, guint prop_id,
 static void fs_upnp_simple_igd_gather_proxy (FsUpnpSimpleIgd *self,
     GUPnPServiceProxy *proxy);
 
+static void cleanup_proxy (struct Proxy *prox);
+
 static void
 fs_upnp_simple_igd_class_init (FsUpnpSimpleIgdClass *klass)
 {
@@ -144,7 +157,7 @@ fs_upnp_simple_igd_init (FsUpnpSimpleIgd *self)
 
   self->priv->request_timeout = 5;
 
-  self->priv->service_proxies = g_ptr_array_new ();
+  self->priv->service_proxies = g_array_new (TRUE, TRUE, sizeof(struct Proxy));
 }
 
 static void
@@ -160,9 +173,12 @@ fs_upnp_simple_igd_dispose (GObject *object)
     g_signal_handler_disconnect (self->priv->cp, self->priv->unavail_handler);
   self->priv->unavail_handler = 0;
 
-  while (self->priv->service_proxies->len)
-    g_object_unref ( G_OBJECT (
-            g_ptr_array_remove_index_fast (self->priv->service_proxies, 0)));
+  while(self->priv->service_proxies->len)
+  {
+    cleanup_proxy (
+        &g_array_index (self->priv->service_proxies, struct Proxy, 0));
+    g_array_remove_index_fast (self->priv->service_proxies, 0);
+  }
 
   if (self->priv->cp)
     g_object_unref (self->priv->cp);
@@ -176,13 +192,33 @@ fs_upnp_simple_igd_dispose (GObject *object)
 }
 
 static void
+cleanup_proxy (struct Proxy *prox)
+{
+  guint i;
+
+  for (i=0; i < prox->actions->len; i++)
+  {
+    struct Action *action = &g_array_index (prox->actions, struct Action, i);
+
+    if (action->timeout_source)
+      g_source_destroy (action->timeout_source);
+
+    gupnp_service_proxy_cancel_action (prox->proxy, action->action);
+  }
+
+  g_array_free (prox->actions, TRUE);
+
+  g_object_unref (prox->proxy);
+}
+
+static void
 fs_upnp_simple_igd_finalize (GObject *object)
 {
   FsUpnpSimpleIgd *self = FS_UPNP_SIMPLE_IGD_CAST (object);
 
   g_main_context_unref (self->priv->main_context);
 
-  g_ptr_array_free (self->priv->service_proxies, TRUE);
+  g_array_free (self->priv->service_proxies, TRUE);
 
   G_OBJECT_CLASS (fs_upnp_simple_igd_parent_class)->finalize (object);
 }
@@ -225,7 +261,12 @@ _cp_service_avail (GUPnPControlPoint *cp,
     GUPnPServiceProxy *proxy,
     FsUpnpSimpleIgd *self)
 {
-  g_ptr_array_add (self->priv->service_proxies, g_object_ref (proxy));
+  struct Proxy prox;
+
+  prox.proxy = g_object_ref (proxy);
+  prox.actions = g_array_new (TRUE, TRUE, sizeof (struct Action));
+
+  g_array_append_val(self->priv->service_proxies, prox);
 
   if (self->priv->gathering)
     fs_upnp_simple_igd_gather_proxy (self, proxy);
@@ -237,8 +278,20 @@ _cp_service_unavail (GUPnPControlPoint *cp,
     GUPnPServiceProxy *proxy,
     FsUpnpSimpleIgd *self)
 {
-  if (g_ptr_array_remove_fast(self->priv->service_proxies, proxy))
-    g_object_unref (proxy);
+  guint i;
+
+  for (i=0; i < self->priv->service_proxies->len; i++)
+  {
+    struct Proxy *prox =
+      &g_array_index (self->priv->service_proxies, struct Proxy, i);
+
+    if (prox->proxy == proxy)
+    {
+      g_array_remove_index_fast (self->priv->service_proxies, i);
+      g_object_unref (proxy);
+      break;
+    }
+  }
 }
 
 
@@ -296,9 +349,9 @@ fs_upnp_simple_igd_gather (FsUpnpSimpleIgd *self, gboolean gather)
 
     for (i = 0; i < self->priv->service_proxies->len; i++)
     {
-      GUPnPServiceProxy *proxy =
-        g_ptr_array_index(self->priv->service_proxies, i);
-      fs_upnp_simple_igd_gather_proxy (self, proxy);
+      struct Proxy *prox =
+        &g_array_index(self->priv->service_proxies, struct Proxy, i);
+      fs_upnp_simple_igd_gather_proxy (self, prox->proxy);
     }
   }
 }
-- 
1.5.6.5




More information about the farsight-commits mailing list