summaryrefslogtreecommitdiff
path: root/pn_node.c
blob: 6109ebb4668c3287e810f85f58d6508fd6eca103 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "pn_node.h"
#include "pn_buffer.h"

#include <gio/gio.h>

#include "pn_log.h"

#include <string.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_buffer *input_buffer;
	struct pn_buffer *output_buffer;
};

GQuark
pn_node_error_quark(void)
{
	static GQuark quark;
	if (quark == 0)
		quark = g_quark_from_static_string("pecan-node-error-quark");
	return quark;
}

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);
}

void
pn_node_write(struct pn_node *node,
	      const void *buffer,
	      gsize count)
{
	PN_NODE_GET_CLASS(node)->write(node, buffer, count);
}

void
pn_node_parse(struct pn_node *node,
	      struct pn_buffer *buffer)
{
	PN_NODE_GET_CLASS(node)->parse(node, buffer);
}

/* pn_node stuff */

static void
parse_impl(struct pn_node *node,
	   struct pn_buffer *buffer)
{
}

static void
read_cb(GObject *source,
	GAsyncResult *result,
	gpointer user_data)
{
	struct pn_node *node = user_data;
	struct pn_node_priv *priv = node->priv;
	gssize size;
	GError *error = NULL;
	struct pn_buffer *b = priv->input_buffer;

	size = g_input_stream_read_finish(G_INPUT_STREAM(source),
					  result, &error);

	pn_debug(node, "size=%i", size);

	if (size == 0)
		error = g_error_new_literal(PN_NODE_ERROR, 0,
					    "Connection finished");

	if (error)
		goto nok;

	b->len += size;
	pn_node_parse(node, b);

	if (priv->status == PN_NODE_STATUS_OPEN) {
		g_input_stream_read_async(G_INPUT_STREAM(source), b->data, b->size,
					  G_PRIORITY_DEFAULT, NULL, read_cb, node);
	}
	else {
		g_object_unref(node);
	}
	return;

nok:
	pn_node_set_error(node, error);
	g_object_unref(node);
	return;
}

static void
write_cb(GObject *source,
	 GAsyncResult *result,
	 gpointer user_data)
{
	struct pn_node *node = user_data;
	struct pn_node_priv *priv = node->priv;
	gssize size;
	GError *error = NULL;
	struct pn_buffer *buffer;

	size = g_output_stream_write_finish(G_OUTPUT_STREAM(source),
					    result, &error);

	pn_debug(node, "size=%i", size);

	if (size == 0)
		error = g_error_new_literal(PN_NODE_ERROR, 0,
					    "Connection finished");

	if (error)
		goto nok;

	buffer = priv->output_buffer;

	buffer->offset += size;

	if (buffer->offset < buffer->len) {
		g_output_stream_write_async(G_OUTPUT_STREAM(source),
					    buffer->data + buffer->offset,
					    buffer->len - buffer->offset,
					    G_PRIORITY_DEFAULT,
					    NULL,
					    write_cb,
					    node);
	}
	else {
		pn_buffer_free(buffer);
		priv->output_buffer = NULL;

		g_object_unref(node);
	}
	return;

nok:
	pn_node_set_error(node, error);
	g_object_unref(node);
	return;
}

static void
write_impl(struct pn_node *node,
	   const void *buffer,
	   gsize count)
{
	struct pn_node_priv *priv = node->priv;
	GOutputStream *output;

	output = g_io_stream_get_output_stream(G_IO_STREAM(priv->socket_conn));
	priv->output_buffer = pn_buffer_new_use((void *)buffer, count);

	g_object_ref(node);
	g_output_stream_write_async(output, buffer, count,
				    G_PRIORITY_DEFAULT, NULL, write_cb, node);
}

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) {
		GInputStream *input;
		struct pn_buffer *b = priv->input_buffer;

		priv->status = PN_NODE_STATUS_OPEN;
		priv->socket_conn = socket_conn;

		input = g_io_stream_get_input_stream(G_IO_STREAM(socket_conn));

		g_object_ref(node);
		g_input_stream_read_async(input, b->data, b->size,
					  G_PRIORITY_DEFAULT, NULL,
					  read_cb, node);

		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
finalize(GObject *obj)
{
	struct pn_node_priv *priv = PN_NODE(obj)->priv;
	pn_buffer_free(priv->input_buffer);

	G_OBJECT_CLASS(parent_class)->finalize(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;
	node_class->write = &write_impl;
	node_class->parse = &parse_impl;

	gobject_class->dispose = dispose;
	gobject_class->finalize = finalize;

	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);
	priv->input_buffer = pn_buffer_new_and_alloc(0);
}

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;
}