aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/pubsub/pub.c
blob: e3b37f1aa6d0aeb6bf6fd14e0d5cf65ab1e56ef6 (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
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// 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"

// Publish protocol.  The PUB protocol simply sends messages out, as
// a broadcast.  It has nothing more sophisticated because it does not
// perform sender-side filtering.  Its best effort delivery, so anything
// that can't receive the message won't get one.

typedef struct nni_pub_pipe	nni_pub_pipe;
typedef struct nni_pub_sock	nni_pub_sock;

static void nni_pub_pipe_recv_cb(void *);
static void nni_pub_pipe_send_cb(void *);
static void nni_pub_pipe_getq_cb(void *);
static void nni_pub_sock_getq_cb(void *);
static void nni_pub_sock_fini(void *);
static void nni_pub_pipe_fini(void *);

// An nni_pub_sock is our per-socket protocol private structure.
struct nni_pub_sock {
	nni_sock *	sock;
	nni_msgq *	uwq;
	int		raw;
	nni_aio		aio_getq;
	nni_list	pipes;
	nni_mtx		mtx;
};

// An nni_pub_pipe is our per-pipe protocol private structure.
struct nni_pub_pipe {
	nni_pipe *	pipe;
	nni_pub_sock *	pub;
	nni_msgq *	sendq;
	nni_aio		aio_getq;
	nni_aio		aio_send;
	nni_aio		aio_recv;
	nni_list_node	node;
};

static int
nni_pub_sock_init(void **pubp, nni_sock *sock)
{
	nni_pub_sock *pub;
	int rv;

	if ((pub = NNI_ALLOC_STRUCT(pub)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_mtx_init(&pub->mtx)) != 0) {
		nni_pub_sock_fini(pub);
		return (rv);
	}
	rv = nni_aio_init(&pub->aio_getq, nni_pub_sock_getq_cb, pub);
	if (rv != 0) {
		nni_pub_sock_fini(pub);
		return (rv);
	}
	pub->sock = sock;
	pub->raw = 0;
	NNI_LIST_INIT(&pub->pipes, nni_pub_pipe, node);

	pub->uwq = nni_sock_sendq(sock);

	*pubp = pub;
	nni_sock_recverr(sock, NNG_ENOTSUP);
	return (0);
}


static void
nni_pub_sock_fini(void *arg)
{
	nni_pub_sock *pub = arg;

	nni_aio_fini(&pub->aio_getq);
	nni_mtx_fini(&pub->mtx);
	NNI_FREE_STRUCT(pub);
}


static void
nni_pub_sock_open(void *arg)
{
	nni_pub_sock *pub = arg;

	nni_msgq_aio_get(pub->uwq, &pub->aio_getq);
}


static void
nni_pub_pipe_fini(void *arg)
{
	nni_pub_pipe *pp = arg;

	nni_msgq_fini(pp->sendq);
	nni_aio_fini(&pp->aio_getq);
	nni_aio_fini(&pp->aio_send);
	nni_aio_fini(&pp->aio_recv);
	NNI_FREE_STRUCT(pp);
}


static int
nni_pub_pipe_init(void **ppp, nni_pipe *pipe, void *psock)
{
	nni_pub_pipe *pp;
	int rv;

	if ((pp = NNI_ALLOC_STRUCT(pp)) == NULL) {
		return (NNG_ENOMEM);
	}
	// XXX: consider making this depth tunable
	if ((rv = nni_msgq_init(&pp->sendq, 16)) != 0) {
		goto fail;
	}

	rv = nni_aio_init(&pp->aio_getq, nni_pub_pipe_getq_cb, pp);
	if (rv != 0) {
		goto fail;
	}

	rv = nni_aio_init(&pp->aio_send, nni_pub_pipe_send_cb, pp);
	if (rv != 0) {
		goto fail;
	}

	rv = nni_aio_init(&pp->aio_recv, nni_pub_pipe_recv_cb, pp);
	if (rv != 0) {
		goto fail;
	}
	pp->pipe = pipe;
	pp->pub = psock;
	*ppp = pp;
	return (0);

fail:
	nni_pub_pipe_fini(pp);
	return (rv);
}


static int
nni_pub_pipe_start(void *arg)
{
	nni_pub_pipe *pp = arg;
	nni_pub_sock *pub = pp->pub;

	if (nni_pipe_peer(pp->pipe) != NNG_PROTO_SUB) {
		return (NNG_EPROTO);
	}
	nni_mtx_lock(&pub->mtx);
	nni_list_append(&pub->pipes, pp);
	nni_mtx_unlock(&pub->mtx);

	// Start the receiver and the queue reader.
	nni_pipe_recv(pp->pipe, &pp->aio_recv);
	nni_msgq_aio_get(pp->sendq, &pp->aio_getq);

	return (0);
}


static void
nni_pub_pipe_stop(void *arg)
{
	nni_pub_pipe *pp = arg;
	nni_pub_sock *pub = pp->pub;

	nni_aio_stop(&pp->aio_getq);
	nni_aio_stop(&pp->aio_send);
	nni_aio_stop(&pp->aio_recv);
	nni_msgq_close(pp->sendq);

	nni_mtx_lock(&pub->mtx);
	if (nni_list_active(&pub->pipes, pp)) {
		nni_list_remove(&pub->pipes, pp);
	}
	nni_mtx_unlock(&pub->mtx);
}


static void
nni_pub_sock_getq_cb(void *arg)
{
	nni_pub_sock *pub = arg;
	nni_msgq *uwq = pub->uwq;
	nni_msg *msg, *dup;

	nni_pub_pipe *pp;
	nni_pub_pipe *last;
	int rv;

	if (nni_aio_result(&pub->aio_getq) != 0) {
		return;
	}

	msg = pub->aio_getq.a_msg;
	pub->aio_getq.a_msg = NULL;

	nni_mtx_lock(&pub->mtx);
	last = nni_list_last(&pub->pipes);
	NNI_LIST_FOREACH (&pub->pipes, pp) {
		if (pp != last) {
			rv = nni_msg_dup(&dup, msg);
			if (rv != 0) {
				continue;
			}
		} else {
			dup = msg;
		}
		if ((rv = nni_msgq_tryput(pp->sendq, dup)) != 0) {
			nni_msg_free(dup);
		}
	}
	nni_mtx_unlock(&pub->mtx);

	if (last == NULL) {
		nni_msg_free(msg);
	}

	nni_msgq_aio_get(uwq, &pub->aio_getq);
}


static void
nni_pub_pipe_recv_cb(void *arg)
{
	nni_pub_pipe *pp = arg;

	if (nni_aio_result(&pp->aio_recv) != 0) {
		nni_pipe_stop(pp->pipe);
		return;
	}

	nni_msg_free(pp->aio_recv.a_msg);
	pp->aio_recv.a_msg = NULL;
	nni_pipe_recv(pp->pipe, &pp->aio_recv);
}


static void
nni_pub_pipe_getq_cb(void *arg)
{
	nni_pub_pipe *pp = arg;

	if (nni_aio_result(&pp->aio_getq) != 0) {
		nni_pipe_stop(pp->pipe);
		return;
	}

	pp->aio_send.a_msg = pp->aio_getq.a_msg;
	pp->aio_getq.a_msg = NULL;

	nni_pipe_send(pp->pipe, &pp->aio_send);
}


static void
nni_pub_pipe_send_cb(void *arg)
{
	nni_pub_pipe *pp = arg;

	if (nni_aio_result(&pp->aio_send) != 0) {
		nni_msg_free(pp->aio_send.a_msg);
		pp->aio_send.a_msg = NULL;
		nni_pipe_stop(pp->pipe);
		return;
	}

	pp->aio_send.a_msg = NULL;
	nni_msgq_aio_get(pp->sendq, &pp->aio_getq);
}


static int
nni_pub_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
{
	nni_pub_sock *pub = arg;
	int rv;

	switch (opt) {
	case NNG_OPT_RAW:
		rv = nni_setopt_int(&pub->raw, buf, sz, 0, 1);
		break;
	default:
		rv = NNG_ENOTSUP;
	}
	return (rv);
}


static int
nni_pub_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
{
	nni_pub_sock *pub = arg;
	int rv;

	switch (opt) {
	case NNG_OPT_RAW:
		rv = nni_getopt_int(&pub->raw, buf, szp);
		break;
	default:
		rv = NNG_ENOTSUP;
	}
	return (rv);
}


// This is the global protocol structure -- our linkage to the core.
// This should be the only global non-static symbol in this file.
static nni_proto_pipe_ops nni_pub_pipe_ops = {
	.pipe_init	= nni_pub_pipe_init,
	.pipe_fini	= nni_pub_pipe_fini,
	.pipe_start	= nni_pub_pipe_start,
	.pipe_stop	= nni_pub_pipe_stop,
};

nni_proto_sock_ops nni_pub_sock_ops = {
	.sock_init	= nni_pub_sock_init,
	.sock_fini	= nni_pub_sock_fini,
	.sock_open	= nni_pub_sock_open,
	.sock_setopt	= nni_pub_sock_setopt,
	.sock_getopt	= nni_pub_sock_getopt,
};

nni_proto nni_pub_proto = {
	.proto_self	= NNG_PROTO_PUB,
	.proto_peer	= NNG_PROTO_SUB,
	.proto_name	= "pub",
	.proto_flags	= NNI_PROTO_FLAG_SND,
	.proto_sock_ops = &nni_pub_sock_ops,
	.proto_pipe_ops = &nni_pub_pipe_ops,
};