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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#include "pn_node.h"
#include <gio/gio.h>
#include "pn_log.h"
static void *parent_class;
enum pn_node_status {
PN_NODE_STATUS_CLOSED,
PN_NODE_STATUS_CONNECTING,
PN_NODE_STATUS_OPEN,
};
struct pn_node_priv {
char *hostname;
int port;
enum pn_node_status status;
GSocketConnection *socket_conn;
GError *error;
};
struct pn_node *
pn_node_new(void)
{
return g_object_new(PN_NODE_TYPE, NULL);
}
void
pn_node_free(struct pn_node *node)
{
if (!node)
return;
g_object_unref(node);
}
void
pn_node_connect(struct pn_node *node,
const char *hostname,
int port)
{
PN_NODE_GET_CLASS(node)->connect(node, hostname, port);
}
void
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
connect_cb(GObject *source,
GAsyncResult *res,
gpointer user_data)
{
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, &error);
g_object_ref(node);
if (socket_conn) {
priv->status = PN_NODE_STATUS_OPEN;
priv->socket_conn = socket_conn;
struct pn_node_class *class;
class = g_type_class_peek(PN_NODE_TYPE);
g_signal_emit(G_OBJECT(node), class->open_sig, 0, node);
}
else {
pn_node_set_error(node, error);
}
g_object_unref(node);
}
static void
connect_impl(struct pn_node *node,
const char *hostname,
int port)
{
struct pn_node_priv *priv = node->priv;
pn_node_close(node);
priv->hostname = g_strdup(hostname);
priv->port = port;
priv->status = PN_NODE_STATUS_CONNECTING;
{
GSocketClient *client;
client = g_socket_client_new();
g_socket_client_connect_to_host_async(client, hostname, port,
NULL, connect_cb, node);
g_object_unref(client);
}
}
static void
close_impl(struct pn_node *node)
{
struct pn_node_priv *priv = node->priv;
if (priv->status == PN_NODE_STATUS_CLOSED)
return;
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;
}
g_free(priv->hostname);
priv->hostname = NULL;
}
/* GObject stuff */
static void
dispose(GObject *obj)
{
pn_node_close(PN_NODE(obj));
G_OBJECT_CLASS(parent_class)->dispose(obj);
}
static void
class_init(void *g_class,
void *class_data)
{
struct pn_node_class *node_class = PN_NODE_CLASS(g_class);
GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
node_class->connect = &connect_impl;
node_class->close = &close_impl;
gobject_class->dispose = dispose;
parent_class = g_type_class_peek_parent(g_class);
g_type_class_add_private(g_class, sizeof(struct pn_node_priv));
node_class->open_sig = g_signal_new("open", G_TYPE_FROM_CLASS(gobject_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
instance_init(GTypeInstance *instance,
void *g_class)
{
struct pn_node *self = PN_NODE(instance);
struct pn_node_priv *priv;
self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(instance, PN_NODE_TYPE, struct pn_node_priv);
}
GType
pn_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_node_class),
.class_init = class_init,
.instance_size = sizeof(struct pn_node),
.instance_init = instance_init,
};
type = g_type_register_static(G_TYPE_OBJECT, "PnNode", &type_info, 0);
g_once_init_leave(&init_type, type);
}
return init_type;
}
|