aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/survey/respond.c
blob: fcb067b04949932b33f1d7728a19b0f7caf2aa27 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <stdlib.h>
#include <string.h>

#include "core/nng_impl.h"

// Respondent protocol.  The RESPONDENT protocol is the "replier" side of
// the surveyor pattern.  This is useful for building service discovery, or
// voting algorithsm, for example.

typedef struct resp_pipe resp_pipe;
typedef struct resp_sock resp_sock;

static void resp_recv_cb(void *);
static void resp_putq_cb(void *);
static void resp_getq_cb(void *);
static void resp_send_cb(void *);
static void resp_sock_getq_cb(void *);
static void resp_pipe_fini(void *);

// A resp_sock is our per-socket protocol private structure.
struct resp_sock {
	nni_sock *  nsock;
	nni_msgq *  urq;
	nni_msgq *  uwq;
	int         raw;
	int         ttl;
	nni_idhash *pipes;
	char *      btrace;
	size_t      btrace_len;
	nni_aio *   aio_getq;
	nni_mtx     mtx;
};

// A resp_pipe is our per-pipe protocol private structure.
struct resp_pipe {
	nni_pipe * npipe;
	resp_sock *psock;
	uint32_t   id;
	nni_msgq * sendq;
	nni_aio *  aio_getq;
	nni_aio *  aio_putq;
	nni_aio *  aio_send;
	nni_aio *  aio_recv;
};

static void
resp_sock_fini(void *arg)
{
	resp_sock *s = arg;

	nni_aio_stop(s->aio_getq);
	nni_aio_fini(s->aio_getq);
	nni_idhash_fini(s->pipes);
	if (s->btrace != NULL) {
		nni_free(s->btrace, s->btrace_len);
	}
	nni_mtx_fini(&s->mtx);
	NNI_FREE_STRUCT(s);
}

static int
resp_sock_init(void **sp, nni_sock *nsock)
{
	resp_sock *s;
	int        rv;

	if ((s = NNI_ALLOC_STRUCT(s)) == NULL) {
		return (NNG_ENOMEM);
	}
	if (((rv = nni_idhash_init(&s->pipes)) != 0) ||
	    ((rv = nni_aio_init(&s->aio_getq, resp_sock_getq_cb, s)) != 0)) {
		resp_sock_fini(s);
		return (rv);
	}

	s->ttl        = 8; // Per RFC
	s->nsock      = nsock;
	s->raw        = 0;
	s->btrace     = NULL;
	s->btrace_len = 0;
	s->urq        = nni_sock_recvq(nsock);
	s->uwq        = nni_sock_sendq(nsock);

	nni_mtx_init(&s->mtx);

	*sp = s;
	nni_sock_senderr(nsock, NNG_ESTATE);
	return (0);
}

static void
resp_sock_open(void *arg)
{
	resp_sock *s = arg;

	nni_msgq_aio_get(s->uwq, s->aio_getq);
}

static void
resp_sock_close(void *arg)
{
	resp_sock *s = arg;

	nni_aio_cancel(s->aio_getq, NNG_ECLOSED);
}

static void
resp_pipe_fini(void *arg)
{
	resp_pipe *p = arg;

	nni_aio_fini(p->aio_putq);
	nni_aio_fini(p->aio_getq);
	nni_aio_fini(p->aio_send);
	nni_aio_fini(p->aio_recv);
	nni_msgq_fini(p->sendq);
	NNI_FREE_STRUCT(p);
}

static int
resp_pipe_init(void **pp, nni_pipe *npipe, void *s)
{
	resp_pipe *p;
	int        rv;

	if ((p = NNI_ALLOC_STRUCT(p)) == NULL) {
		return (NNG_ENOMEM);
	}
	if (((rv = nni_msgq_init(&p->sendq, 2)) != 0) ||
	    ((rv = nni_aio_init(&p->aio_putq, resp_putq_cb, p)) != 0) ||
	    ((rv = nni_aio_init(&p->aio_recv, resp_recv_cb, p)) != 0) ||
	    ((rv = nni_aio_init(&p->aio_getq, resp_getq_cb, p)) != 0) ||
	    ((rv = nni_aio_init(&p->aio_send, resp_send_cb, p)) != 0)) {
		resp_pipe_fini(p);
		return (rv);
	}

	p->npipe = npipe;
	p->psock = s;
	*pp      = p;
	return (0);
}

static int
resp_pipe_start(void *arg)
{
	resp_pipe *p = arg;
	resp_sock *s = p->psock;
	int        rv;

	p->id = nni_pipe_id(p->npipe);

	nni_mtx_lock(&s->mtx);
	rv = nni_idhash_insert(s->pipes, p->id, p);
	nni_mtx_unlock(&s->mtx);
	if (rv != 0) {
		return (rv);
	}

	nni_pipe_recv(p->npipe, p->aio_recv);
	nni_msgq_aio_get(p->sendq, p->aio_getq);

	return (rv);
}

static void
resp_pipe_stop(void *arg)
{
	resp_pipe *p = arg;
	resp_sock *s = p->psock;

	nni_msgq_close(p->sendq);
	nni_aio_stop(p->aio_putq);
	nni_aio_stop(p->aio_getq);
	nni_aio_stop(p->aio_send);
	nni_aio_stop(p->aio_recv);

	if (p->id != 0) {
		nni_mtx_lock(&s->mtx);
		nni_idhash_remove(s->pipes, p->id);
		nni_mtx_unlock(&s->mtx);
		p->id = 0;
	}
}

// resp_sock_send watches for messages from the upper write queue,
// extracts the destination pipe, and forwards it to the appropriate
// destination pipe via a separate queue.  This prevents a single bad
// or slow pipe from gumming up the works for the entire socket.s

void
resp_sock_getq_cb(void *arg)
{
	resp_sock *s = arg;
	nni_msg *  msg;
	uint32_t   id;
	resp_pipe *p;
	int        rv;

	if (nni_aio_result(s->aio_getq) != 0) {
		return;
	}
	msg = nni_aio_get_msg(s->aio_getq);
	nni_aio_set_msg(s->aio_getq, NULL);

	// We yank the outgoing pipe id from the header
	if (nni_msg_header_len(msg) < 4) {
		nni_msg_free(msg);
		// We can't really close down the socket, so just keep going.
		nni_msgq_aio_get(s->uwq, s->aio_getq);
		return;
	}
	id = nni_msg_header_trim_u32(msg);

	nni_mtx_lock(&s->mtx);
	if ((rv = nni_idhash_find(s->pipes, id, (void **) &p)) != 0) {
		// Destination pipe not present.
		nni_msg_free(msg);
	} else {
		// Non-blocking put.
		if (nni_msgq_tryput(p->sendq, msg) != 0) {
			nni_msg_free(msg);
		}
	}
	nni_msgq_aio_get(s->uwq, s->aio_getq);
	nni_mtx_unlock(&s->mtx);
}

void
resp_getq_cb(void *arg)
{
	resp_pipe *p = arg;

	if (nni_aio_result(p->aio_getq) != 0) {
		nni_pipe_stop(p->npipe);
		return;
	}

	nni_aio_set_msg(p->aio_send, nni_aio_get_msg(p->aio_getq));
	nni_aio_set_msg(p->aio_getq, NULL);

	nni_pipe_send(p->npipe, p->aio_send);
}

void
resp_send_cb(void *arg)
{
	resp_pipe *p = arg;

	if (nni_aio_result(p->aio_send) != 0) {
		nni_msg_free(nni_aio_get_msg(p->aio_send));
		nni_aio_set_msg(p->aio_send, NULL);
		nni_pipe_stop(p->npipe);
		return;
	}

	nni_msgq_aio_get(p->sendq, p->aio_getq);
}

static void
resp_recv_cb(void *arg)
{
	resp_pipe *p = arg;
	resp_sock *s = p->psock;
	nni_msgq * urq;
	nni_msg *  msg;
	int        hops;
	int        rv;

	if (nni_aio_result(p->aio_recv) != 0) {
		goto error;
	}

	urq = nni_sock_recvq(s->nsock);

	msg = nni_aio_get_msg(p->aio_recv);
	nni_aio_set_msg(p->aio_recv, NULL);
	nni_msg_set_pipe(msg, p->id);

	// Store the pipe id in the header, first thing.
	if (nni_msg_header_append_u32(msg, p->id) != 0) {
		nni_msg_free(msg);
		goto error;
	}

	// Move backtrace from body to header
	hops = 0;
	for (;;) {
		int      end = 0;
		uint8_t *body;

		if (hops >= s->ttl) {
			nni_msg_free(msg);
			goto error;
		}
		if (nni_msg_len(msg) < 4) {
			nni_msg_free(msg);
			goto error;
		}
		body = nni_msg_body(msg);
		end  = (body[0] & 0x80) ? 1 : 0;
		rv   = nni_msg_header_append(msg, body, 4);
		if (rv != 0) {
			nni_msg_free(msg);
			goto error;
		}
		nni_msg_trim(msg, 4);
		if (end) {
			break;
		}
	}

	// Now send it up.
	nni_aio_set_msg(p->aio_putq, msg);
	nni_msgq_aio_put(urq, p->aio_putq);
	return;

error:
	nni_pipe_stop(p->npipe);
}

static void
resp_putq_cb(void *arg)
{
	resp_pipe *p = arg;

	if (nni_aio_result(p->aio_putq) != 0) {
		nni_msg_free(nni_aio_get_msg(p->aio_putq));
		nni_aio_set_msg(p->aio_putq, NULL);
		nni_pipe_stop(p->npipe);
	}

	nni_pipe_recv(p->npipe, p->aio_recv);
}

static int
resp_sock_setopt_raw(void *arg, const void *buf, size_t sz)
{
	resp_sock *s = arg;
	int        rv;

	if ((rv = nni_setopt_int(&s->raw, buf, sz, 0, 1)) == 0) {
		nni_sock_senderr(s->nsock, s->raw ? 0 : NNG_ESTATE);
	}
	return (rv);
}

static int
resp_sock_getopt_raw(void *arg, void *buf, size_t *szp)
{
	resp_sock *s = arg;
	return (nni_getopt_int(s->raw, buf, szp));
}

static int
resp_sock_setopt_maxttl(void *arg, const void *buf, size_t sz)
{
	resp_sock *s = arg;
	return (nni_setopt_int(&s->ttl, buf, sz, 1, 255));
}

static int
resp_sock_getopt_maxttl(void *arg, void *buf, size_t *szp)
{
	resp_sock *s = arg;
	return (nni_getopt_int(s->ttl, buf, szp));
}

static nni_msg *
resp_sock_sfilter(void *arg, nni_msg *msg)
{
	resp_sock *s = arg;

	if (s->raw) {
		return (msg);
	}

	// Cannot send again until a receive is done...
	nni_sock_senderr(s->nsock, NNG_ESTATE);

	// If we have a stored backtrace, append it to the header...
	// if we don't have a backtrace, discard the message.
	if (s->btrace == NULL) {
		nni_msg_free(msg);
		return (NULL);
	}

	// drop anything else in the header...
	nni_msg_header_clear(msg);

	if (nni_msg_header_append(msg, s->btrace, s->btrace_len) != 0) {
		nni_free(s->btrace, s->btrace_len);
		s->btrace     = NULL;
		s->btrace_len = 0;
		nni_msg_free(msg);
		return (NULL);
	}

	nni_free(s->btrace, s->btrace_len);
	s->btrace     = NULL;
	s->btrace_len = 0;
	return (msg);
}

static nni_msg *
resp_sock_rfilter(void *arg, nni_msg *msg)
{
	resp_sock *s = arg;
	char *     header;
	size_t     len;

	if (s->raw) {
		return (msg);
	}

	nni_sock_senderr(s->nsock, 0);
	len    = nni_msg_header_len(msg);
	header = nni_msg_header(msg);
	if (s->btrace != NULL) {
		nni_free(s->btrace, s->btrace_len);
		s->btrace     = NULL;
		s->btrace_len = 0;
	}
	if ((s->btrace = nni_alloc(len)) == NULL) {
		nni_msg_free(msg);
		return (NULL);
	}
	s->btrace_len = len;
	memcpy(s->btrace, header, len);
	nni_msg_header_clear(msg);
	return (msg);
}

static nni_proto_pipe_ops resp_pipe_ops = {
	.pipe_init  = resp_pipe_init,
	.pipe_fini  = resp_pipe_fini,
	.pipe_start = resp_pipe_start,
	.pipe_stop  = resp_pipe_stop,
};

static nni_proto_sock_option resp_sock_options[] = {
	{
	    .pso_name   = NNG_OPT_RAW,
	    .pso_getopt = resp_sock_getopt_raw,
	    .pso_setopt = resp_sock_setopt_raw,
	},
	{
	    .pso_name   = NNG_OPT_MAXTTL,
	    .pso_getopt = resp_sock_getopt_maxttl,
	    .pso_setopt = resp_sock_setopt_maxttl,
	},
	// terminate list
	{ NULL, NULL, NULL },
};

static nni_proto_sock_ops resp_sock_ops = {
	.sock_init    = resp_sock_init,
	.sock_fini    = resp_sock_fini,
	.sock_open    = resp_sock_open,
	.sock_close   = resp_sock_close,
	.sock_options = resp_sock_options,
	.sock_rfilter = resp_sock_rfilter,
	.sock_sfilter = resp_sock_sfilter,
};

static nni_proto resp_proto = {
	.proto_version  = NNI_PROTOCOL_VERSION,
	.proto_self     = { NNG_PROTO_RESPONDENT_V0, "respondent" },
	.proto_peer     = { NNG_PROTO_SURVEYOR_V0, "surveyor" },
	.proto_flags    = NNI_PROTO_FLAG_SNDRCV,
	.proto_sock_ops = &resp_sock_ops,
	.proto_pipe_ops = &resp_pipe_ops,
};

int
nng_respondent0_open(nng_socket *sidp)
{
	return (nni_proto_open(sidp, &resp_proto));
}