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

[libvirt] [libvirt-glib 28/37] Add GVirConfigInterfaceNetwork



Make GVirConfigInterface an abstract base class, and move the
actual code in subclasses. This will make handling of the various
types of network interfaces more understandable and more logical
with respect to the corresponding XML.
---
 libvirt-gconfig/Makefile.am                        |    2 +
 .../libvirt-gconfig-interface-network.c            |  106 ++++++++++++++++++++
 .../libvirt-gconfig-interface-network.h            |   70 +++++++++++++
 libvirt-gconfig/libvirt-gconfig-interface.c        |   71 +-------------
 libvirt-gconfig/libvirt-gconfig-interface.h        |   18 ----
 libvirt-gconfig/libvirt-gconfig.h                  |    1 +
 libvirt-gconfig/libvirt-gconfig.sym                |   10 +-
 libvirt-gconfig/tests/test-domain-create.c         |    9 +-
 8 files changed, 189 insertions(+), 98 deletions(-)
 create mode 100644 libvirt-gconfig/libvirt-gconfig-interface-network.c
 create mode 100644 libvirt-gconfig/libvirt-gconfig-interface-network.h

diff --git a/libvirt-gconfig/Makefile.am b/libvirt-gconfig/Makefile.am
index 82063df..c74d123 100644
--- a/libvirt-gconfig/Makefile.am
+++ b/libvirt-gconfig/Makefile.am
@@ -18,6 +18,7 @@ GCONFIG_HEADER_FILES = \
 			libvirt-gconfig-domain-snapshot.h \
 			libvirt-gconfig-helpers.h \
 			libvirt-gconfig-interface.h \
+			libvirt-gconfig-interface-network.h \
 			libvirt-gconfig-network.h \
 			libvirt-gconfig-network-filter.h \
 			libvirt-gconfig-node-device.h \
@@ -40,6 +41,7 @@ GCONFIG_SOURCE_FILES = \
 			libvirt-gconfig-enum-types.h \
 			libvirt-gconfig-helpers.c \
 			libvirt-gconfig-interface.c \
+			libvirt-gconfig-interface-network.c \
 			libvirt-gconfig-network.c \
 			libvirt-gconfig-network-filter.c \
 			libvirt-gconfig-node-device.c \
diff --git a/libvirt-gconfig/libvirt-gconfig-interface-network.c b/libvirt-gconfig/libvirt-gconfig-interface-network.c
new file mode 100644
index 0000000..304712d
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-interface-network.c
@@ -0,0 +1,106 @@
+/*
+ * libvirt-gobject-config-interface_network-network.c: libvirt glib integration
+ *
+ * Copyright (C) 2008 Daniel P. Berrange
+ * Copyright (C) 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ *
+ * Author: Daniel P. Berrange <berrange redhat com>
+ * Author: Christophe Fergeau <cfergeau redhat com>
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include "libvirt-gconfig/libvirt-gconfig.h"
+#include "libvirt-gconfig/libvirt-gconfig-helpers-private.h"
+
+extern gboolean debugFlag;
+
+#define DEBUG(fmt, ...) do { if (G_UNLIKELY(debugFlag)) g_debug(fmt, ## __VA_ARGS__); } while (0)
+
+#define GVIR_CONFIG_INTERFACE_NETWORK_GET_PRIVATE(obj)                         \
+        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_CONFIG_INTERFACE_NETWORK, GVirConfigInterfaceNetworkPrivate))
+
+struct _GVirConfigInterfaceNetworkPrivate
+{
+    gboolean unused;
+};
+
+G_DEFINE_TYPE(GVirConfigInterfaceNetwork, gvir_config_interface_network, GVIR_TYPE_CONFIG_INTERFACE);
+
+
+static void gvir_config_interface_network_class_init(GVirConfigInterfaceNetworkClass *klass)
+{
+
+    g_type_class_add_private(klass, sizeof(GVirConfigInterfaceNetworkPrivate));
+}
+
+
+static void gvir_config_interface_network_init(GVirConfigInterfaceNetwork *conn)
+{
+    GVirConfigInterfaceNetworkPrivate *priv;
+
+    DEBUG("Init GVirConfigInterfaceNetwork=%p", conn);
+
+    priv = conn->priv = GVIR_CONFIG_INTERFACE_NETWORK_GET_PRIVATE(conn);
+
+    memset(priv, 0, sizeof(*priv));
+}
+
+
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new(void)
+{
+    xmlDocPtr doc;
+    xmlNodePtr node;
+
+    doc = xmlNewDoc((xmlChar *)"1.0");
+    node= xmlNewDocNode(doc, NULL, (xmlChar *)"interface", NULL);
+    xmlNewProp(doc->children, (xmlChar*)"type", (xmlChar*)"network");
+    xmlDocSetRootElement(doc, node);
+    return GVIR_CONFIG_INTERFACE_NETWORK(g_object_new(GVIR_TYPE_CONFIG_INTERFACE_NETWORK,
+                                         "node", node,
+                                         "schema", DATADIR "/libvirt/schemas/interface.rng",
+                                         NULL));
+}
+
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new_from_xml(const gchar *xml,
+                                                                       GError **error)
+{
+    xmlNodePtr node;
+
+    node = gvir_config_xml_parse(xml, "interface", error);
+    if ((error != NULL) && (*error != NULL))
+        return NULL;
+    xmlNewProp(node, (xmlChar*)"type", (xmlChar*)"network");
+    return GVIR_CONFIG_INTERFACE_NETWORK(g_object_new(GVIR_TYPE_CONFIG_INTERFACE_NETWORK,
+                                                      "node", node,
+                                                      "schema", DATADIR "/libvirt/schemas/interface.rng",
+                                                      NULL));
+}
+
+void gvir_config_interface_network_set_source(GVirConfigInterfaceNetwork *interface,
+                                              const char *source)
+{
+    xmlNodePtr source_node;
+
+    source_node = gvir_config_object_new_child(GVIR_CONFIG_OBJECT(interface),
+                                               "source", TRUE);
+    if (source_node == NULL)
+        return;
+    xmlNewProp(source_node, (xmlChar*)"network", (xmlChar*)source);
+}
diff --git a/libvirt-gconfig/libvirt-gconfig-interface-network.h b/libvirt-gconfig/libvirt-gconfig-interface-network.h
new file mode 100644
index 0000000..066709a
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-interface-network.h
@@ -0,0 +1,70 @@
+/*
+ * libvirt-gobject-config-interface-network.c: libvirt gobject integration
+ *
+ * Copyright (C) 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ *
+ * Author: Daniel P. Berrange <berrange redhat com>
+ * Author: Christophe Fergeau <cfergeau redhat com>
+ */
+
+#if !defined(__LIBVIRT_GCONFIG_H__) && !defined(LIBVIRT_GCONFIG_BUILD)
+#error "Only <libvirt-gconfig/libvirt-gconfig.h> can be included directly."
+#endif
+
+#ifndef __LIBVIRT_GCONFIG_INTERFACE_NETWORK_H__
+#define __LIBVIRT_GCONFIG_INTERFACE_NETWORK_H__
+
+G_BEGIN_DECLS
+
+#define GVIR_TYPE_CONFIG_INTERFACE_NETWORK            (gvir_config_interface_network_get_type ())
+#define GVIR_CONFIG_INTERFACE_NETWORK(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GVIR_TYPE_CONFIG_INTERFACE, GVirConfigInterfaceNetwork))
+#define GVIR_CONFIG_INTERFACE_NETWORK_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GVIR_TYPE_CONFIG_INTERFACE, GVirConfigInterfaceNetworkClass))
+#define GVIR_IS_CONFIG_INTERFACE_NETWORK(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GVIR_TYPE_CONFIG_INTERFACE))
+#define GVIR_IS_CONFIG_INTERFACE_NETWORK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GVIR_TYPE_CONFIG_INTERFACE))
+#define GVIR_CONFIG_INTERFACE_NETWORK_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GVIR_TYPE_CONFIG_INTERFACE, GVirConfigInterfaceNetworkClass))
+
+typedef struct _GVirConfigInterfaceNetwork GVirConfigInterfaceNetwork;
+typedef struct _GVirConfigInterfaceNetworkPrivate GVirConfigInterfaceNetworkPrivate;
+typedef struct _GVirConfigInterfaceNetworkClass GVirConfigInterfaceNetworkClass;
+
+struct _GVirConfigInterfaceNetwork
+{
+    GVirConfigObject parent;
+
+    GVirConfigInterfaceNetworkPrivate *priv;
+
+    /* Do not add fields to this struct */
+};
+
+struct _GVirConfigInterfaceNetworkClass
+{
+    GVirConfigObjectClass parent_class;
+
+    gpointer padding[20];
+};
+
+GType gvir_config_interface_network_get_type(void);
+
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new(void);
+GVirConfigInterfaceNetwork *gvir_config_interface_network_new_from_xml(const gchar *xml,
+                                                                       GError **error);
+
+void gvir_config_interface_network_set_source(GVirConfigInterfaceNetwork *interface,
+                                              const char *source);
+G_END_DECLS
+
+#endif /* __LIBVIRT_GCONFIG_INTERFACE_NETWORK_H__ */
diff --git a/libvirt-gconfig/libvirt-gconfig-interface.c b/libvirt-gconfig/libvirt-gconfig-interface.c
index 0993ab9..c449ba8 100644
--- a/libvirt-gconfig/libvirt-gconfig-interface.c
+++ b/libvirt-gconfig/libvirt-gconfig-interface.c
@@ -36,10 +36,10 @@ extern gboolean debugFlag;
 
 struct _GVirConfigInterfacePrivate
 {
-    GVirConfigInterfaceNetworkType type;
+    gboolean unused;
 };
 
-G_DEFINE_TYPE(GVirConfigInterface, gvir_config_interface, GVIR_TYPE_CONFIG_DEVICE);
+G_DEFINE_ABSTRACT_TYPE(GVirConfigInterface, gvir_config_interface, GVIR_TYPE_CONFIG_DEVICE);
 
 
 static void gvir_config_interface_class_init(GVirConfigInterfaceClass *klass)
@@ -59,70 +59,3 @@ static void gvir_config_interface_init(GVirConfigInterface *conn)
 
     memset(priv, 0, sizeof(*priv));
 }
-
-
-GVirConfigInterface *gvir_config_interface_new(void)
-{
-    GVirConfigObject *object;
-
-    object = gvir_config_object_new(GVIR_TYPE_CONFIG_INTERFACE,
-                                    "interface",
-                                    DATADIR "/libvirt/schemas/interface.rng");
-    return GVIR_CONFIG_INTERFACE(object);
-}
-
-GVirConfigInterface *gvir_config_interface_new_from_xml(const gchar *xml,
-                                                        GError **error)
-{
-    GVirConfigObject *object;
-
-    object = gvir_config_object_new_from_xml(GVIR_TYPE_CONFIG_INTERFACE,
-                                             "interface",
-                                             DATADIR "/libvirt/schemas/interface.rng",
-                                             xml, error);
-    return GVIR_CONFIG_INTERFACE(object);
-}
-
-void gvir_config_interface_set_network_type(GVirConfigInterface *interface,
-                                            GVirConfigInterfaceNetworkType type)
-{
-    xmlNodePtr node;
-    const char *type_str;
-
-    node = gvir_config_object_get_xml_node(GVIR_CONFIG_OBJECT(interface));
-    if (node == NULL)
-        return;
-    type_str = gvir_config_genum_get_nick(GVIR_TYPE_CONFIG_INTERFACE_NETWORK_TYPE,
-                                          type);
-    if (type_str != NULL) {
-        xmlNewProp(node, (xmlChar*)"type", (xmlChar*)type_str);
-        interface->priv->type = type;
-    }
-}
-
-void gvir_config_interface_set_source(GVirConfigInterface *interface,
-                                      const char *source)
-{
-    xmlNodePtr source_node;
-    const char *attribute_name;
-
-    if (interface->priv->type != GVIR_CONFIG_INTERFACE_TYPE_NETWORK) {
-        /* I don't know what attribute name to use for 'dir' */
-        g_warning("set_source not implemented for non-'network' interface nodes");
-        return;
-    }
-
-    source_node = gvir_config_object_new_child(GVIR_CONFIG_OBJECT(interface),
-                                               "source", TRUE);
-    if (source_node == NULL)
-        return;
-
-    switch (interface->priv->type) {
-        case GVIR_CONFIG_INTERFACE_TYPE_NETWORK:
-            attribute_name = "network";
-            break;
-        default:
-            g_return_if_reached();
-    }
-    xmlNewProp(source_node, (xmlChar*)attribute_name, (xmlChar*)source);
-}
diff --git a/libvirt-gconfig/libvirt-gconfig-interface.h b/libvirt-gconfig/libvirt-gconfig-interface.h
index 8de3334..e14c713 100644
--- a/libvirt-gconfig/libvirt-gconfig-interface.h
+++ b/libvirt-gconfig/libvirt-gconfig-interface.h
@@ -56,26 +56,8 @@ struct _GVirConfigInterfaceClass
     gpointer padding[20];
 };
 
-typedef enum {
-    GVIR_CONFIG_INTERFACE_TYPE_NETWORK,
-    GVIR_CONFIG_INTERFACE_TYPE_BRIDGE,
-    GVIR_CONFIG_INTERFACE_TYPE_USER,
-    GVIR_CONFIG_INTERFACE_TYPE_ETHERNET,
-    GVIR_CONFIG_INTERFACE_TYPE_DIRECT,
-    GVIR_CONFIG_INTERFACE_TYPE_MCAST,
-    GVIR_CONFIG_INTERFACE_TYPE_SERVER
-} GVirConfigInterfaceNetworkType;
-
 GType gvir_config_interface_get_type(void);
 
-GVirConfigInterface *gvir_config_interface_new(void);
-GVirConfigInterface *gvir_config_interface_new_from_xml(const gchar *xml,
-                                                        GError **error);
-void gvir_config_interface_set_network_type(GVirConfigInterface *interface,
-                                            GVirConfigInterfaceNetworkType type);
-
-void gvir_config_interface_set_source(GVirConfigInterface *interface,
-                                      const char *source);
 G_END_DECLS
 
 #endif /* __LIBVIRT_GCONFIG_INTERFACE_H__ */
diff --git a/libvirt-gconfig/libvirt-gconfig.h b/libvirt-gconfig/libvirt-gconfig.h
index 01a1af3..c0deae9 100644
--- a/libvirt-gconfig/libvirt-gconfig.h
+++ b/libvirt-gconfig/libvirt-gconfig.h
@@ -35,6 +35,7 @@
 #include <libvirt-gconfig/libvirt-gconfig-enum-types.h>
 #include <libvirt-gconfig/libvirt-gconfig-helpers.h>
 #include <libvirt-gconfig/libvirt-gconfig-interface.h>
+#include <libvirt-gconfig/libvirt-gconfig-interface-network.h>
 #include <libvirt-gconfig/libvirt-gconfig-network.h>
 #include <libvirt-gconfig/libvirt-gconfig-network-filter.h>
 #include <libvirt-gconfig/libvirt-gconfig-node-device.h>
diff --git a/libvirt-gconfig/libvirt-gconfig.sym b/libvirt-gconfig/libvirt-gconfig.sym
index 886690e..b89b9d2 100644
--- a/libvirt-gconfig/libvirt-gconfig.sym
+++ b/libvirt-gconfig/libvirt-gconfig.sym
@@ -49,11 +49,11 @@ LIBVIRT_GOBJECT_0.0.1 {
 	gvir_config_domain_snapshot_new_from_xml;
 
 	gvir_config_interface_get_type;
-	gvir_config_interface_network_type_get_type;
-	gvir_config_interface_new;
-	gvir_config_interface_new_from_xml;
-	gvir_config_interface_set_network_type;
-	gvir_config_interface_set_source;
+
+	gvir_config_interface_network_get_type;
+	gvir_config_interface_network_new;
+	gvir_config_interface_network_new_from_xml;
+	gvir_config_interface_network_set_source;
 
 	gvir_config_network_get_type;
 	gvir_config_network_new;
diff --git a/libvirt-gconfig/tests/test-domain-create.c b/libvirt-gconfig/tests/test-domain-create.c
index 3f3f973..fa949f7 100644
--- a/libvirt-gconfig/tests/test-domain-create.c
+++ b/libvirt-gconfig/tests/test-domain-create.c
@@ -98,13 +98,10 @@ int main(void)
     devices = g_list_append(devices, disk);
 
     /* network interface node */
-    GVirConfigInterface *interface;
+    GVirConfigInterfaceNetwork *interface;
 
-
-    interface = gvir_config_interface_new();
-    gvir_config_interface_set_network_type(interface,
-                                           GVIR_CONFIG_INTERFACE_TYPE_NETWORK);
-    gvir_config_interface_set_source(interface, "default");
+    interface = gvir_config_interface_network_new();
+    gvir_config_interface_network_set_source(interface, "default");
     devices = g_list_append(devices, interface);
 
     gvir_config_domain_set_devices(domain, devices);
-- 
1.7.7


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