diff options
author | Felipe Contreras <felipe.contreras@gmail.com> | 2010-06-13 21:08:12 +0300 |
---|---|---|
committer | Felipe Contreras <felipe.contreras@gmail.com> | 2010-06-14 03:13:33 +0300 |
commit | 6cf15e26b7c0c88364e971908c5a63175efa89d5 (patch) | |
tree | af6284d8ae7a13e9cf88a92b45bcc8bdfa8c2c60 | |
parent | 7897d019f1a5a4844a2f656b005a57db87324b75 (diff) |
node: add error handling
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
-rw-r--r-- | pn_node.c | 31 | ||||
-rw-r--r-- | pn_node.h | 3 |
2 files changed, 32 insertions, 2 deletions
@@ -2,6 +2,8 @@ #include <gio/gio.h> +#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 @@ -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 */ |