#include "pn_cmd_node.h" #include "pn_trans.h" #include #include "pn_log.h" static void *parent_class; struct pn_cmd_node_priv { unsigned counter; }; struct pn_cmd_node * pn_cmd_node_new(void) { return g_object_new(PN_CMD_NODE_TYPE, NULL); } void pn_cmd_node_free(struct pn_cmd_node *conn) { if (!conn) return; g_object_unref(conn); } static inline void show_cmd(bool incoming, const char *str, size_t len) { const char *mark = incoming ? "<-" : "->"; pn_info(NULL, "%s %.*s", mark, len, str); } static void send_valist(struct pn_cmd_node *self, const char *command, const char *format, va_list args) { struct pn_cmd_node_priv *priv = self->priv; struct pn_trans *trans; trans = pn_trans_new(); trans->id = ++priv->counter; trans->command = g_strdup(command); trans->params = g_strdup_vprintf(format, args); { char *data; size_t len; data = pn_trans_to_string(trans); len = strlen(data); show_cmd(false, data, len - 2); g_free(data); } pn_trans_free(trans); } void pn_cmd_node_send(struct pn_cmd_node *self, const char *command, const char *format, ...) { va_list args; va_start(args, format); send_valist(self, command, format, args); va_end(args); } /* GObject stuff */ static void class_init(void *g_class, void *class_data) { parent_class = g_type_class_peek_parent(g_class); g_type_class_add_private(g_class, sizeof(struct pn_cmd_node_priv)); } static void instance_init(GTypeInstance *instance, void *g_class) { struct pn_cmd_node *self = PN_CMD_NODE(instance); struct pn_cmd_node_priv *priv = self->priv; self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(instance, PN_CMD_NODE_TYPE, struct pn_cmd_node_priv); } GType pn_cmd_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_cmd_node_class), .class_init = class_init, .instance_size = sizeof(struct pn_cmd_node), .instance_init = instance_init, }; type = g_type_register_static(PN_NODE_TYPE, "PnCmdNode", &type_info, 0); g_once_init_leave(&init_type, type); } return init_type; }