From 6cf15e26b7c0c88364e971908c5a63175efa89d5 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Sun, 13 Jun 2010 21:08:12 +0300 Subject: node: add error handling Signed-off-by: Felipe Contreras --- pn_node.c | 31 +++++++++++++++++++++++++++++-- pn_node.h | 3 +++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/pn_node.c b/pn_node.c index 6d7f7ac..61c0050 100644 --- a/pn_node.c +++ b/pn_node.c @@ -2,6 +2,8 @@ #include +#include "pn_log.h" + static void *parent_class; enum pn_node_status { @@ -15,6 +17,7 @@ struct pn_node_priv { int port; enum pn_node_status status; GSocketConnection *socket_conn; + GError *error; }; struct pn_node * @@ -45,6 +48,14 @@ pn_node_close(struct pn_node *node) PN_NODE_GET_CLASS(node)->close(node); } +void +pn_node_set_error(struct pn_node *node, + GError *error) +{ + node->priv->error = error; + pn_node_close(node); +} + /* pn_node stuff */ static void @@ -55,8 +66,9 @@ connect_cb(GObject *source, struct pn_node *node = user_data; struct pn_node_priv *priv = node->priv; GSocketConnection *socket_conn; + GError *error = NULL; - socket_conn = g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source), res, NULL); + socket_conn = g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source), res, &error); g_object_ref(node); @@ -69,7 +81,7 @@ connect_cb(GObject *source, g_signal_emit(G_OBJECT(node), class->open_sig, 0, node); } else { - pn_node_close(node); + pn_node_set_error(node, error); } g_object_unref(node); @@ -108,6 +120,17 @@ close_impl(struct pn_node *node) priv->status = PN_NODE_STATUS_CLOSED; + if (priv->error) { + struct pn_node_class *class; + class = g_type_class_peek(PN_NODE_TYPE); + g_signal_emit(G_OBJECT(node), class->error_sig, 0, &priv->error, node); + + if (priv->error) { + pn_warn(node, "unhandled error: %s", priv->error->message); + g_clear_error(&priv->error); + } + } + if (priv->socket_conn) { g_object_unref(priv->socket_conn); priv->socket_conn = NULL; @@ -146,6 +169,10 @@ class_init(void *g_class, G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); + node_class->error_sig = g_signal_new("error", G_TYPE_FROM_CLASS(gobject_class), + G_SIGNAL_RUN_FIRST, 0, NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); } static void diff --git a/pn_node.h b/pn_node.h index 10c88ef..c243fd9 100644 --- a/pn_node.h +++ b/pn_node.h @@ -18,6 +18,7 @@ struct pn_node_class { void (*close) (struct pn_node *node); guint open_sig; + guint error_sig; }; #define PN_NODE_TYPE (pn_node_get_type()) @@ -30,6 +31,8 @@ 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); +void pn_node_set_error(struct pn_node *node, GError *error); + GType pn_node_get_type(void); #endif /* PN_NODE_H */ -- cgit v1.2.3