summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Contreras <felipe.contreras@gmail.com>2010-06-14 02:56:02 +0300
committerFelipe Contreras <felipe.contreras@gmail.com>2010-06-14 03:57:54 +0300
commite7fec91e6abbd4fb29aa6aa42b8bc67ec361ad0a (patch)
tree4127fd30a58bc5e7a806a07dd12ee355707ed4eb
parent54ff389121af56e2e6b01a9e266a15613b94ddba (diff)
Add transaction callback support
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
-rw-r--r--pn_cmd_node.c13
-rw-r--r--pn_cmd_node.h3
-rw-r--r--pn_ns.c2
-rw-r--r--pn_trans.h5
4 files changed, 20 insertions, 3 deletions
diff --git a/pn_cmd_node.c b/pn_cmd_node.c
index 327f19c..991d6c8 100644
--- a/pn_cmd_node.c
+++ b/pn_cmd_node.c
@@ -39,6 +39,7 @@ show_cmd(bool incoming,
static void
send_valist(struct pn_cmd_node *self,
+ pn_trans_cb_t cb,
const char *command,
const char *format,
va_list args)
@@ -51,6 +52,7 @@ send_valist(struct pn_cmd_node *self,
trans->id = ++priv->counter;
trans->command = g_strdup(command);
trans->params = g_strdup_vprintf(format, args);
+ trans->cb = cb;
g_hash_table_insert(priv->transactions, GINT_TO_POINTER(trans->id), trans);
@@ -69,13 +71,14 @@ send_valist(struct pn_cmd_node *self,
void
pn_cmd_node_send(struct pn_cmd_node *self,
+ pn_trans_cb_t cb,
const char *command,
const char *format,
...)
{
va_list args;
va_start(args, format);
- send_valist(self, command, format, args);
+ send_valist(self, cb, command, format, args);
va_end(args);
}
@@ -89,13 +92,19 @@ got_command(struct pn_cmd_node *self,
struct pn_cmd_node_priv *priv = self->priv;
struct pn_cmd *cmd;
struct pn_trans *trans;
+ pn_trans_cb_t cb = NULL;
show_cmd(true, string, length);
cmd = pn_cmd_new_from_string(string, length);
if (cmd->tr_id)
trans = g_hash_table_lookup(priv->transactions, GINT_TO_POINTER(cmd->tr_id));
- pn_warn(self, "unhandled command: %.*s", length, string);
+ if (trans)
+ cb = trans->cb;
+ if (cb)
+ cb(self, cmd);
+ else
+ pn_warn(self, "unhandled command: %.*s", length, string);
pn_cmd_free(cmd);
}
diff --git a/pn_cmd_node.h b/pn_cmd_node.h
index f43c7ff..ae2dc5a 100644
--- a/pn_cmd_node.h
+++ b/pn_cmd_node.h
@@ -16,10 +16,13 @@ struct pn_cmd_node_class {
#define PN_CMD_NODE_TYPE (pn_cmd_node_get_type())
#define PN_CMD_NODE(obj)(G_TYPE_CHECK_INSTANCE_CAST((obj), PN_CMD_NODE_TYPE, struct pn_cmd_node))
+#include "pn_trans.h"
+
struct pn_cmd_node *pn_cmd_node_new(void);
void pn_cmd_node_free(struct pn_cmd_node *node);
void pn_cmd_node_send(struct pn_cmd_node *node,
+ pn_trans_cb_t cb,
const char *command,
const char *format, ...);
diff --git a/pn_ns.c b/pn_ns.c
index e881b2c..0bdc954 100644
--- a/pn_ns.c
+++ b/pn_ns.c
@@ -33,7 +33,7 @@ pn_ns_free(struct pn_ns *ns)
static void
open_cb(struct pn_ns *self)
{
- pn_cmd_node_send(PN_CMD_NODE(self), "VER", "MSNP12");
+ pn_cmd_node_send(PN_CMD_NODE(self), NULL, "VER", "MSNP12");
}
/* GObject stuff */
diff --git a/pn_trans.h b/pn_trans.h
index 397efb8..8b3aa1e 100644
--- a/pn_trans.h
+++ b/pn_trans.h
@@ -1,10 +1,15 @@
#ifndef PN_TRANS_H
#define PN_TRANS_H
+struct pn_cmd;
+
+typedef void (*pn_trans_cb_t)(void *node, struct pn_cmd *cmd);
+
struct pn_trans {
int id;
char *command;
char *params;
+ pn_trans_cb_t cb;
};
struct pn_trans *pn_trans_new(void);