summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Contreras <felipe.contreras@gmail.com>2010-06-13 00:42:42 +0300
committerFelipe Contreras <felipe.contreras@gmail.com>2010-06-14 02:36:46 +0300
commit947b4cb1642862f6324262c8ed1693e58a7ca8c6 (patch)
treeb747a5729ef64196024f588fd9ec731805bfeefe
parentd90396fa5e40cc878b41e7777e92c6ad62b5aeec (diff)
Add pn_node
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
-rw-r--r--Makefile2
-rw-r--r--pn_node.c132
-rw-r--r--pn_node.h33
3 files changed, 166 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 850ea0d..9ff8bf1 100644
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,7 @@ QUIET_LINK = @echo ' LINK '$@;
QUIET_CLEAN = @echo ' CLEAN '$@;
endif
-test: test.o pn_core.o pn_session.o
+test: test.o pn_core.o pn_session.o pn_node.o
test: override CFLAGS += $(GIO_CFLAGS)
test: override LIBS += $(GIO_LIBS)
diff --git a/pn_node.c b/pn_node.c
new file mode 100644
index 0000000..ce9a786
--- /dev/null
+++ b/pn_node.c
@@ -0,0 +1,132 @@
+#include "pn_node.h"
+
+static void *parent_class;
+
+enum pn_node_status {
+ PN_NODE_STATUS_CLOSED,
+ PN_NODE_STATUS_CONNECTING,
+ PN_NODE_STATUS_OPEN,
+};
+
+struct pn_node_priv {
+ char *hostname;
+ int port;
+ enum pn_node_status status;
+};
+
+struct pn_node *
+pn_node_new(void)
+{
+ return g_object_new(PN_NODE_TYPE, NULL);
+}
+
+void
+pn_node_free(struct pn_node *node)
+{
+ if (!node)
+ return;
+ g_object_unref(node);
+}
+
+void
+pn_node_connect(struct pn_node *node,
+ const char *hostname,
+ int port)
+{
+ PN_NODE_GET_CLASS(node)->connect(node, hostname, port);
+}
+
+void
+pn_node_close(struct pn_node *node)
+{
+ PN_NODE_GET_CLASS(node)->close(node);
+}
+
+/* pn_node stuff */
+
+static void
+connect_impl(struct pn_node *node,
+ const char *hostname,
+ int port)
+{
+ struct pn_node_priv *priv = node->priv;
+
+ pn_node_close(node);
+
+ priv->hostname = g_strdup(hostname);
+ priv->port = port;
+
+ priv->status = PN_NODE_STATUS_CONNECTING;
+}
+
+static void
+close_impl(struct pn_node *node)
+{
+ struct pn_node_priv *priv = node->priv;
+
+ if (priv->status == PN_NODE_STATUS_CLOSED)
+ return;
+
+ priv->status = PN_NODE_STATUS_CLOSED;
+
+ g_free(priv->hostname);
+ priv->hostname = NULL;
+}
+
+/* GObject stuff */
+
+static void
+dispose(GObject *obj)
+{
+ pn_node_close(PN_NODE(obj));
+
+ G_OBJECT_CLASS(parent_class)->dispose(obj);
+}
+
+static void
+class_init(void *g_class,
+ void *class_data)
+{
+ struct pn_node_class *node_class = PN_NODE_CLASS(g_class);
+ GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
+
+ node_class->connect = &connect_impl;
+ node_class->close = &close_impl;
+
+ gobject_class->dispose = dispose;
+
+ parent_class = g_type_class_peek_parent(g_class);
+ g_type_class_add_private(g_class, sizeof(struct pn_node_priv));
+}
+
+static void
+instance_init(GTypeInstance *instance,
+ void *g_class)
+{
+ struct pn_node *self = PN_NODE(instance);
+ struct pn_node_priv *priv;
+
+ self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(instance, PN_NODE_TYPE, struct pn_node_priv);
+}
+
+GType
+pn_node_get_type(void)
+{
+ static gsize init_type;
+
+ if (g_once_init_enter(&init_type)) {
+ GType type;
+ GTypeInfo type_info = {
+ .class_size = sizeof(struct pn_node_class),
+ .class_init = class_init,
+ .instance_size = sizeof(struct pn_node),
+ .instance_init = instance_init,
+ };
+
+ type = g_type_register_static(G_TYPE_OBJECT, "PnNode", &type_info, 0);
+
+ g_once_init_leave(&init_type, type);
+ }
+
+ return init_type;
+}
diff --git a/pn_node.h b/pn_node.h
new file mode 100644
index 0000000..8c59d15
--- /dev/null
+++ b/pn_node.h
@@ -0,0 +1,33 @@
+#ifndef PN_NODE_H
+#define PN_NODE_H
+
+#include <glib-object.h>
+
+#include <stdbool.h>
+
+struct pn_node {
+ GObject parent;
+
+ struct pn_node_priv *priv;
+};
+
+struct pn_node_class {
+ GObjectClass parent_class;
+
+ void (*connect) (struct pn_node *node, const char *hostname, int port);
+ void (*close) (struct pn_node *node);
+};
+
+#define PN_NODE_TYPE (pn_node_get_type())
+#define PN_NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PN_NODE_TYPE, struct pn_node))
+#define PN_NODE_CLASS(c) (G_TYPE_CHECK_CLASS_CAST((c), PN_NODE_TYPE, struct pn_node_class))
+#define PN_NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PN_NODE_TYPE, struct pn_node_class))
+
+struct pn_node *pn_node_new(void);
+void pn_node_free(struct pn_node *node);
+void pn_node_connect(struct pn_node *node, const char *hostname, int port);
+void pn_node_close(struct pn_node *node);
+
+GType pn_node_get_type(void);
+
+#endif /* PN_NODE_H */