1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#include "pn_cmd_node.h"
#include "pn_trans.h"
#include <string.h>
#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;
}
|