[gupnp] Add an abstract context manager base class



commit b218fcc86d8906a263be3ecee21ad5645b131ede
Author: Jens Georg <mail jensge org>
Date:   Fri Jun 24 11:02:51 2011 +0200

    Add an abstract context manager base class
    
    Make the Unix context manager derive from the simple context manager.
    Preparation for the windows context manager which shares a lot of code.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=658635

 libgupnp/Makefile.am                    |   10 +-
 libgupnp/gupnp-simple-context-manager.c |  217 +++++++++++++++++++++++++++++++
 libgupnp/gupnp-simple-context-manager.h |   78 +++++++++++
 libgupnp/gupnp-unix-context-manager.c   |  159 ++---------------------
 libgupnp/gupnp-unix-context-manager.h   |    8 +-
 5 files changed, 314 insertions(+), 158 deletions(-)
---
diff --git a/libgupnp/Makefile.am b/libgupnp/Makefile.am
index 0f05b3b..68d3c56 100644
--- a/libgupnp/Makefile.am
+++ b/libgupnp/Makefile.am
@@ -1,8 +1,10 @@
 LTVERSION = 4:0:0
 
+CONTEXT_MANAGER_IMPL = gupnp-unix-context-manager.c \
+		       gupnp-unix-context-manager.h
 if USE_NETWORK_MANAGER
-CONTEXT_MANAGER_IMPL = gupnp-network-manager.c  \
-		       gupnp-network-manager.h
+CONTEXT_MANAGER_IMPL += gupnp-network-manager.c  \
+		        gupnp-network-manager.h
 CONTEXT_MANAGER_CFLAGS = -DUSE_NETWORK_MANAGER
 else
 CONTEXT_MANAGER_IMPL =
@@ -78,8 +80,8 @@ libgupnp_1_0_la_SOURCES = $(introspection_sources)		\
 			  http-headers.h			\
 			  gupnp-context-private.h		\
 			  $(CONTEXT_MANAGER_IMPL) 		\
-			  gupnp-unix-context-manager.c  	\
-			  gupnp-unix-context-manager.h		\
+			  gupnp-simple-context-manager.h	\
+			  gupnp-simple-context-manager.c	\
 			  gupnp-device-info-private.h		\
 			  gupnp-error-private.h			\
 			  gupnp-resource-factory-private.h	\
diff --git a/libgupnp/gupnp-simple-context-manager.c b/libgupnp/gupnp-simple-context-manager.c
new file mode 100644
index 0000000..a28932a
--- /dev/null
+++ b/libgupnp/gupnp-simple-context-manager.c
@@ -0,0 +1,217 @@
+/*
+ * Copyright (C) 2009,2011 Nokia Corporation.
+ * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
+ *
+ * Author: Jens Georg <mail jensge org>
+ *         Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ *         Jorn Baayen <jorn openedhand com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/**
+ * SECTION:gupnp-simple-context-manager
+ * @short_description: Abstract implementation of basic #GUPnPContextManager.
+ *
+ */
+
+#include <config.h>
+#include <errno.h>
+#include <gio/gio.h>
+#include <libgssdp/gssdp-error.h>
+
+#include "gupnp-simple-context-manager.h"
+#include "gupnp-context.h"
+
+G_DEFINE_ABSTRACT_TYPE (GUPnPSimpleContextManager,
+                        gupnp_simple_context_manager,
+                        GUPNP_TYPE_CONTEXT_MANAGER);
+
+struct _GUPnPSimpleContextManagerPrivate {
+        GList *contexts; /* List of GUPnPContext instances */
+
+        GSource *idle_context_creation_src;
+};
+
+static GList*
+gupnp_simple_context_manager_get_interfaces (GUPnPSimpleContextManager *manager)
+{
+        GUPnPSimpleContextManagerClass *object_class =
+                              GUPNP_SIMPLE_CONTEXT_MANAGER_GET_CLASS (manager);
+
+        return object_class->get_interfaces (manager);
+}
+
+static void
+create_and_signal_context (const char                *interface,
+                           GUPnPSimpleContextManager *manager)
+{
+        GUPnPContext *context;
+        guint port;
+
+        GError *error;
+
+        g_object_get (manager,
+                      "port", &port,
+                      NULL);
+
+        error = NULL;
+        context = g_initable_new (GUPNP_TYPE_CONTEXT,
+                                  NULL,
+                                  &error,
+                                  "interface", interface,
+                                  "port", port,
+                                  NULL);
+        if (error != NULL) {
+                if (!(error->domain == GSSDP_ERROR &&
+                      error->code == GSSDP_ERROR_NO_IP_ADDRESS))
+                        g_warning
+                           ("Failed to create context for interface '%s': %s",
+                            interface,
+                            error->message);
+
+                g_error_free (error);
+
+                return;
+        }
+
+        g_signal_emit_by_name (manager,
+                               "context-available",
+                               context);
+
+        manager->priv->contexts = g_list_append (manager->priv->contexts,
+                                                 context);
+}
+
+/*
+ * Create a context for all network interfaces that are up.
+ */
+static gboolean
+create_contexts (gpointer data)
+{
+        GUPnPSimpleContextManager *manager = (GUPnPSimpleContextManager *) data;
+        GList *ifaces;
+
+        manager->priv->idle_context_creation_src = NULL;
+
+        if (manager->priv->contexts != NULL)
+               return FALSE;
+
+        ifaces = gupnp_simple_context_manager_get_interfaces (manager);
+        while (ifaces) {
+                create_and_signal_context ((char *) ifaces->data, manager);
+                g_free (ifaces->data);
+                ifaces = g_list_remove_link (ifaces, ifaces);
+        }
+
+        return FALSE;
+}
+
+static void
+destroy_contexts (GUPnPSimpleContextManager *manager)
+{
+        while (manager->priv->contexts) {
+                GUPnPContext *context;
+
+                context = GUPNP_CONTEXT (manager->priv->contexts->data);
+
+                g_signal_emit_by_name (manager,
+                                       "context-unavailable",
+                                       context);
+                g_object_unref (context);
+
+                manager->priv->contexts = g_list_delete_link
+                                        (manager->priv->contexts,
+                                         manager->priv->contexts);
+        }
+}
+
+static void
+schedule_contexts_creation (GUPnPSimpleContextManager *manager)
+{
+        manager->priv->idle_context_creation_src = NULL;
+
+        /* Create contexts in mainloop so that is happens after user has hooked
+         * to the "context-available" signal.
+         */
+        manager->priv->idle_context_creation_src = g_idle_source_new ();
+        g_source_attach (manager->priv->idle_context_creation_src,
+                         g_main_context_get_thread_default ());
+        g_source_set_callback (manager->priv->idle_context_creation_src,
+                               create_contexts,
+                               manager,
+                               NULL);
+        g_source_unref (manager->priv->idle_context_creation_src);
+}
+
+static void
+gupnp_simple_context_manager_init (GUPnPSimpleContextManager *manager)
+{
+        manager->priv =
+                G_TYPE_INSTANCE_GET_PRIVATE (manager,
+                                             GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER,
+                                             GUPnPSimpleContextManagerPrivate);
+}
+
+static void
+gupnp_simple_context_manager_constructed (GObject *object)
+{
+        GObjectClass *parent_class;
+        GUPnPSimpleContextManager *manager;
+
+        manager = GUPNP_SIMPLE_CONTEXT_MANAGER (object);
+        schedule_contexts_creation (manager);
+
+        /* Chain-up */
+        parent_class = G_OBJECT_CLASS (gupnp_simple_context_manager_parent_class);
+        if (parent_class->constructed != NULL)
+                parent_class->constructed (object);
+}
+
+static void
+gupnp_simple_context_manager_dispose (GObject *object)
+{
+        GUPnPSimpleContextManager *manager;
+        GObjectClass *object_class;
+
+        manager = GUPNP_SIMPLE_CONTEXT_MANAGER (object);
+
+        destroy_contexts (manager);
+
+        if (manager->priv->idle_context_creation_src) {
+                g_source_destroy (manager->priv->idle_context_creation_src);
+                manager->priv->idle_context_creation_src = NULL;
+        }
+
+
+        /* Call super */
+        object_class = G_OBJECT_CLASS (gupnp_simple_context_manager_parent_class);
+        object_class->dispose (object);
+}
+
+static void
+gupnp_simple_context_manager_class_init (GUPnPSimpleContextManagerClass *klass)
+{
+        GObjectClass *object_class;
+
+        object_class = G_OBJECT_CLASS (klass);
+
+        object_class->constructed  = gupnp_simple_context_manager_constructed;
+        object_class->dispose      = gupnp_simple_context_manager_dispose;
+
+        g_type_class_add_private (klass,
+                                  sizeof (GUPnPSimpleContextManagerPrivate));
+}
diff --git a/libgupnp/gupnp-simple-context-manager.h b/libgupnp/gupnp-simple-context-manager.h
new file mode 100644
index 0000000..c19aaaf
--- /dev/null
+++ b/libgupnp/gupnp-simple-context-manager.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2009,2011 Nokia Corporation.
+ * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
+ *
+ * Author: Jens Georg <mail jensge org>
+ *         Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ *         Jorn Baayen <jorn openedhand com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GUPNP_SIMPLE_CONTEXT_MANAGER_H__
+#define __GUPNP_SIMPLE_CONTEXT_MANAGER_H__
+
+#include "gupnp-context-manager.h"
+
+G_BEGIN_DECLS
+
+GType
+gupnp_simple_context_manager_get_type (void) G_GNUC_CONST;
+
+#define GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER \
+                (gupnp_simple_context_manager_get_type ())
+#define GUPNP_SIMPLE_CONTEXT_MANAGER(obj) \
+                (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+                 GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER, \
+                 GUPnPSimpleContextManager))
+#define GUPNP_SIMPLE_CONTEXT_MANAGER_CLASS(obj) \
+                (G_TYPE_CHECK_CLASS_CAST ((obj), \
+                 GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER, \
+                 GUPnPSimpleContextManagerClass))
+#define GUPNP_IS_SIMPLE_CONTEXT_MANAGER(obj) \
+                (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+                 GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER))
+#define GUPNP_IS_SIMPLE_CONTEXT_MANAGER_CLASS(obj) \
+                (G_TYPE_CHECK_CLASS_TYPE ((obj), \
+                 GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER))
+#define GUPNP_SIMPLE_CONTEXT_MANAGER_GET_CLASS(obj) \
+                (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+                 GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER, \
+                 GUPnPSimpleContextManagerClass))
+
+typedef struct _GUPnPSimpleContextManagerPrivate GUPnPSimpleContextManagerPrivate;
+
+typedef struct {
+        GUPnPContextManager parent;
+
+        GUPnPSimpleContextManagerPrivate *priv;
+} GUPnPSimpleContextManager;
+
+typedef struct {
+        GUPnPContextManagerClass parent_class;
+
+        /* vfuncs */
+        GList *(* get_interfaces) (GUPnPSimpleContextManager *context_manager);
+
+        /* future padding */
+        void (* _gupnp_reserved2) (void);
+        void (* _gupnp_reserved3) (void);
+        void (* _gupnp_reserved4) (void);
+} GUPnPSimpleContextManagerClass;
+
+G_END_DECLS
+
+#endif /* __GUPNP_SIMPLE_CONTEXT_MANAGER_H__ */
diff --git a/libgupnp/gupnp-unix-context-manager.c b/libgupnp/gupnp-unix-context-manager.c
index 835773e..095431c 100644
--- a/libgupnp/gupnp-unix-context-manager.c
+++ b/libgupnp/gupnp-unix-context-manager.c
@@ -51,72 +51,17 @@
 
 G_DEFINE_TYPE (GUPnPUnixContextManager,
                gupnp_unix_context_manager,
-               GUPNP_TYPE_CONTEXT_MANAGER);
-
-struct _GUPnPUnixContextManagerPrivate {
-        GList *contexts; /* List of GUPnPContext instances */
-
-        GSource *idle_context_creation_src;
-};
-
-static void
-create_and_signal_context (GUPnPUnixContextManager *manager,
-                           const char              *interface)
-{
-        GUPnPContext *context;
-        guint port;
-
-        GError *error;
-
-        g_object_get (manager,
-                      "port", &port,
-                      NULL);
-
-        error = NULL;
-        context = g_initable_new (GUPNP_TYPE_CONTEXT,
-                                  NULL,
-                                  &error,
-                                  "interface", interface,
-                                  "port", port,
-                                  NULL);
-        if (error != NULL) {
-                if (!(error->domain == GSSDP_ERROR &&
-                      error->code == GSSDP_ERROR_NO_IP_ADDRESS))
-                        g_warning
-                           ("Failed to create context for interface '%s': %s",
-                            interface,
-                            error->message);
-
-                g_error_free (error);
-
-                return;
-        }
-
-        g_signal_emit_by_name (manager,
-                               "context-available",
-                               context);
-
-        manager->priv->contexts = g_list_append (manager->priv->contexts,
-                                                 context);
-}
+               GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER);
 
 /*
  * Create a context for all network interfaces that are up.
  */
-static gboolean
-create_contexts (gpointer data)
+static GList *
+gupnp_unix_context_manager_get_interfaces (GUPnPSimpleContextManager *manager)
 {
-        GUPnPUnixContextManager *manager = (GUPnPUnixContextManager *) data;
         struct ifaddrs *ifa_list, *ifa;
         GList *processed;
 
-        manager->priv->idle_context_creation_src = NULL;
-
-        if (manager->priv->contexts != NULL) {
-               return FALSE;
-        }
-
-
         if (getifaddrs (&ifa_list) != 0) {
                 g_warning ("Failed to retrieve list of network interfaces:%s\n",
                            strerror (errno));
@@ -137,111 +82,27 @@ create_contexts (gpointer data)
                         continue;
 
                 if (ifa->ifa_flags & IFF_UP)
-                        create_and_signal_context (manager, ifa->ifa_name);
-
-                processed = g_list_append (processed, ifa->ifa_name);
+                        processed = g_list_append (processed,
+                                                   g_strdup (ifa->ifa_name));
         }
 
-        g_list_free (processed);
         freeifaddrs (ifa_list);
 
-        return FALSE;
-}
-
-static void
-destroy_contexts (GUPnPUnixContextManager *manager)
-{
-        while (manager->priv->contexts) {
-                GUPnPContext *context;
-
-                context = GUPNP_CONTEXT (manager->priv->contexts->data);
-
-                g_signal_emit_by_name (manager,
-                                       "context-unavailable",
-                                       context);
-                g_object_unref (context);
-
-                manager->priv->contexts = g_list_delete_link
-                                        (manager->priv->contexts,
-                                         manager->priv->contexts);
-        }
-}
-
-static void
-schedule_contexts_creation (GUPnPUnixContextManager *manager)
-{
-        manager->priv->idle_context_creation_src = NULL;
-
-        /* Create contexts in mainloop so that is happens after user has hooked
-         * to the "context-available" signal.
-         */
-        manager->priv->idle_context_creation_src = g_idle_source_new ();
-        g_source_attach (manager->priv->idle_context_creation_src,
-                         g_main_context_get_thread_default ());
-        g_source_set_callback (manager->priv->idle_context_creation_src,
-                               create_contexts,
-                               manager,
-                               NULL);
-        g_source_unref (manager->priv->idle_context_creation_src);
+        return processed;
 }
 
 static void
 gupnp_unix_context_manager_init (GUPnPUnixContextManager *manager)
 {
-        manager->priv =
-                G_TYPE_INSTANCE_GET_PRIVATE (manager,
-                                             GUPNP_TYPE_UNIX_CONTEXT_MANAGER,
-                                             GUPnPUnixContextManagerPrivate);
-}
-
-static void
-gupnp_unix_context_manager_constructed (GObject *object)
-{
-        GObjectClass *parent_class;
-        GUPnPUnixContextManager *manager;
-
-        manager = GUPNP_UNIX_CONTEXT_MANAGER (object);
-        schedule_contexts_creation (manager);
-
-        /* Chain-up */
-        parent_class = G_OBJECT_CLASS (gupnp_unix_context_manager_parent_class);
-        if (parent_class->constructed != NULL) {
-                parent_class->constructed (object);
-        }
-}
-
-static void
-gupnp_unix_context_manager_dispose (GObject *object)
-{
-        GUPnPUnixContextManager *manager;
-        GObjectClass *object_class;
-
-        manager = GUPNP_UNIX_CONTEXT_MANAGER (object);
-
-        destroy_contexts (manager);
-
-        if (manager->priv->idle_context_creation_src) {
-                g_source_destroy (manager->priv->idle_context_creation_src);
-                manager->priv->idle_context_creation_src = NULL;
-        }
-
-
-        /* Call super */
-        object_class = G_OBJECT_CLASS (gupnp_unix_context_manager_parent_class);
-        object_class->dispose (object);
 }
 
 static void
 gupnp_unix_context_manager_class_init (GUPnPUnixContextManagerClass *klass)
 {
-        GObjectClass *object_class;
-
-        object_class = G_OBJECT_CLASS (klass);
-
-        object_class->constructed  = gupnp_unix_context_manager_constructed;
-        object_class->dispose      = gupnp_unix_context_manager_dispose;
+        GUPnPSimpleContextManagerClass *parent_class;
 
-        g_type_class_add_private (klass,
-                                  sizeof (GUPnPUnixContextManagerPrivate));
+        parent_class = GUPNP_SIMPLE_CONTEXT_MANAGER_CLASS (klass);
+        parent_class->get_interfaces =
+                                    gupnp_unix_context_manager_get_interfaces;
 }
 
diff --git a/libgupnp/gupnp-unix-context-manager.h b/libgupnp/gupnp-unix-context-manager.h
index b122f2c..5fea247 100644
--- a/libgupnp/gupnp-unix-context-manager.h
+++ b/libgupnp/gupnp-unix-context-manager.h
@@ -24,7 +24,7 @@
 #ifndef __GUPNP_UNIX_CONTEXT_MANAGER_H__
 #define __GUPNP_UNIX_CONTEXT_MANAGER_H__
 
-#include "gupnp-context-manager.h"
+#include "gupnp-simple-context-manager.h"
 
 G_BEGIN_DECLS
 
@@ -55,13 +55,11 @@ gupnp_unix_context_manager_get_type (void) G_GNUC_CONST;
 typedef struct _GUPnPUnixContextManagerPrivate GUPnPUnixContextManagerPrivate;
 
 typedef struct {
-        GUPnPContextManager parent;
-
-        GUPnPUnixContextManagerPrivate *priv;
+        GUPnPSimpleContextManager parent;
 } GUPnPUnixContextManager;
 
 typedef struct {
-        GUPnPContextManagerClass parent_class;
+        GUPnPSimpleContextManagerClass parent_class;
 
         /* future padding */
         void (* _gupnp_reserved1) (void);



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