aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/survey
diff options
context:
space:
mode:
Diffstat (limited to 'src/protocol/survey')
-rw-r--r--src/protocol/survey/respond.c445
-rw-r--r--src/protocol/survey/survey.c393
2 files changed, 419 insertions, 419 deletions
diff --git a/src/protocol/survey/respond.c b/src/protocol/survey/respond.c
index e695a987..c3a9dd81 100644
--- a/src/protocol/survey/respond.c
+++ b/src/protocol/survey/respond.c
@@ -17,18 +17,18 @@
// the surveyor pattern. This is useful for building service discovery, or
// voting algorithsm, for example.
-typedef struct nni_resp_pipe nni_resp_pipe;
-typedef struct nni_resp_sock nni_resp_sock;
-
-static void nni_resp_recv_cb(void *);
-static void nni_resp_putq_cb(void *);
-static void nni_resp_getq_cb(void *);
-static void nni_resp_send_cb(void *);
-static void nni_resp_sock_getq_cb(void *);
-static void nni_resp_pipe_fini(void *);
-
-// An nni_resp_sock is our per-socket protocol private structure.
-struct nni_resp_sock {
+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;
@@ -37,259 +37,257 @@ struct nni_resp_sock {
nni_idhash *pipes;
char * btrace;
size_t btrace_len;
- nni_aio aio_getq;
+ nni_aio * aio_getq;
nni_mtx mtx;
};
-// An nni_resp_pipe is our per-pipe protocol private structure.
-struct nni_resp_pipe {
- nni_pipe * npipe;
- nni_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;
+// 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
-nni_resp_sock_fini(void *arg)
+resp_sock_fini(void *arg)
{
- nni_resp_sock *psock = arg;
+ resp_sock *s = arg;
- nni_aio_stop(&psock->aio_getq);
- nni_aio_fini(&psock->aio_getq);
- nni_idhash_fini(psock->pipes);
- if (psock->btrace != NULL) {
- nni_free(psock->btrace, psock->btrace_len);
+ 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(&psock->mtx);
- NNI_FREE_STRUCT(psock);
+ nni_mtx_fini(&s->mtx);
+ NNI_FREE_STRUCT(s);
}
static int
-nni_resp_sock_init(void **pp, nni_sock *nsock)
+resp_sock_init(void **sp, nni_sock *nsock)
{
- nni_resp_sock *psock;
- int rv;
+ resp_sock *s;
+ int rv;
- if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) {
+ if ((s = NNI_ALLOC_STRUCT(s)) == NULL) {
return (NNG_ENOMEM);
}
- if ((rv = nni_idhash_init(&psock->pipes)) != 0) {
- NNI_FREE_STRUCT(psock);
+ 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);
}
- psock->ttl = 8; // Per RFC
- psock->nsock = nsock;
- psock->raw = 0;
- psock->btrace = NULL;
- psock->btrace_len = 0;
- psock->urq = nni_sock_recvq(nsock);
- psock->uwq = nni_sock_sendq(nsock);
+ 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(&psock->mtx);
- nni_aio_init(&psock->aio_getq, nni_resp_sock_getq_cb, psock);
+ nni_mtx_init(&s->mtx);
- *pp = psock;
+ *sp = s;
nni_sock_senderr(nsock, NNG_ESTATE);
return (0);
}
static void
-nni_resp_sock_open(void *arg)
+resp_sock_open(void *arg)
{
- nni_resp_sock *psock = arg;
+ resp_sock *s = arg;
- nni_msgq_aio_get(psock->uwq, &psock->aio_getq);
+ nni_msgq_aio_get(s->uwq, s->aio_getq);
}
static void
-nni_resp_sock_close(void *arg)
+resp_sock_close(void *arg)
{
- nni_resp_sock *psock = arg;
+ resp_sock *s = arg;
- nni_aio_cancel(&psock->aio_getq, NNG_ECLOSED);
+ 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
-nni_resp_pipe_init(void **pp, nni_pipe *npipe, void *psock)
+resp_pipe_init(void **pp, nni_pipe *npipe, void *s)
{
- nni_resp_pipe *ppipe;
- int rv;
+ resp_pipe *p;
+ int rv;
- if ((ppipe = NNI_ALLOC_STRUCT(ppipe)) == NULL) {
+ if ((p = NNI_ALLOC_STRUCT(p)) == NULL) {
return (NNG_ENOMEM);
}
- if ((rv = nni_msgq_init(&ppipe->sendq, 2)) != 0) {
- NNI_FREE_STRUCT(ppipe);
+ 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);
}
- nni_aio_init(&ppipe->aio_putq, nni_resp_putq_cb, ppipe);
- nni_aio_init(&ppipe->aio_recv, nni_resp_recv_cb, ppipe);
- nni_aio_init(&ppipe->aio_getq, nni_resp_getq_cb, ppipe);
- nni_aio_init(&ppipe->aio_send, nni_resp_send_cb, ppipe);
-
- ppipe->npipe = npipe;
- ppipe->psock = psock;
- *pp = ppipe;
- return (0);
-}
-static void
-nni_resp_pipe_fini(void *arg)
-{
- nni_resp_pipe *ppipe = arg;
-
- nni_aio_fini(&ppipe->aio_putq);
- nni_aio_fini(&ppipe->aio_getq);
- nni_aio_fini(&ppipe->aio_send);
- nni_aio_fini(&ppipe->aio_recv);
- nni_msgq_fini(ppipe->sendq);
- NNI_FREE_STRUCT(ppipe);
+ p->npipe = npipe;
+ p->psock = s;
+ *pp = p;
+ return (0);
}
static int
-nni_resp_pipe_start(void *arg)
+resp_pipe_start(void *arg)
{
- nni_resp_pipe *ppipe = arg;
- nni_resp_sock *psock = ppipe->psock;
- int rv;
+ resp_pipe *p = arg;
+ resp_sock *s = p->psock;
+ int rv;
- ppipe->id = nni_pipe_id(ppipe->npipe);
+ p->id = nni_pipe_id(p->npipe);
- nni_mtx_lock(&psock->mtx);
- rv = nni_idhash_insert(psock->pipes, ppipe->id, ppipe);
- nni_mtx_unlock(&psock->mtx);
+ 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(ppipe->npipe, &ppipe->aio_recv);
- nni_msgq_aio_get(ppipe->sendq, &ppipe->aio_getq);
+ nni_pipe_recv(p->npipe, p->aio_recv);
+ nni_msgq_aio_get(p->sendq, p->aio_getq);
return (rv);
}
static void
-nni_resp_pipe_stop(void *arg)
+resp_pipe_stop(void *arg)
{
- nni_resp_pipe *ppipe = arg;
- nni_resp_sock *psock = ppipe->psock;
-
- nni_msgq_close(ppipe->sendq);
- nni_aio_stop(&ppipe->aio_putq);
- nni_aio_stop(&ppipe->aio_getq);
- nni_aio_stop(&ppipe->aio_send);
- nni_aio_stop(&ppipe->aio_recv);
-
- if (ppipe->id != 0) {
- nni_mtx_lock(&psock->mtx);
- nni_idhash_remove(psock->pipes, ppipe->id);
- nni_mtx_unlock(&psock->mtx);
- ppipe->id = 0;
+ 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;
}
}
-// nni_resp_sock_send watches for messages from the upper write queue,
+// 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
-nni_resp_sock_getq_cb(void *arg)
+resp_sock_getq_cb(void *arg)
{
- nni_resp_sock *psock = arg;
- nni_msg * msg;
- uint32_t id;
- nni_resp_pipe *ppipe;
- int rv;
+ resp_sock *s = arg;
+ nni_msg * msg;
+ uint32_t id;
+ resp_pipe *p;
+ int rv;
- if (nni_aio_result(&psock->aio_getq) != 0) {
+ if (nni_aio_result(s->aio_getq) != 0) {
return;
}
- msg = psock->aio_getq.a_msg;
- psock->aio_getq.a_msg = NULL;
+ 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(psock->uwq, &psock->aio_getq);
+ // 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(&psock->mtx);
- rv = nni_idhash_find(psock->pipes, id, (void **) &ppipe);
-
- if (rv != 0) {
+ nni_mtx_lock(&s->mtx);
+ if ((rv = nni_idhash_find(s->pipes, id, (void **) &p)) != 0) {
+ // Destination pipe not present.
nni_msg_free(msg);
- nni_msgq_aio_get(psock->uwq, &psock->aio_getq);
} else {
// Non-blocking put.
- if (nni_msgq_tryput(ppipe->sendq, msg) != 0) {
+ if (nni_msgq_tryput(p->sendq, msg) != 0) {
nni_msg_free(msg);
}
}
- nni_mtx_unlock(&psock->mtx);
+ nni_msgq_aio_get(s->uwq, s->aio_getq);
+ nni_mtx_unlock(&s->mtx);
}
void
-nni_resp_getq_cb(void *arg)
+resp_getq_cb(void *arg)
{
- nni_resp_pipe *ppipe = arg;
+ resp_pipe *p = arg;
- if (nni_aio_result(&ppipe->aio_getq) != 0) {
- nni_pipe_stop(ppipe->npipe);
+ if (nni_aio_result(p->aio_getq) != 0) {
+ nni_pipe_stop(p->npipe);
return;
}
- ppipe->aio_send.a_msg = ppipe->aio_getq.a_msg;
- ppipe->aio_getq.a_msg = NULL;
+ 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(ppipe->npipe, &ppipe->aio_send);
+ nni_pipe_send(p->npipe, p->aio_send);
}
void
-nni_resp_send_cb(void *arg)
+resp_send_cb(void *arg)
{
- nni_resp_pipe *ppipe = arg;
+ resp_pipe *p = arg;
- if (nni_aio_result(&ppipe->aio_send) != 0) {
- nni_msg_free(ppipe->aio_send.a_msg);
- ppipe->aio_send.a_msg = NULL;
- nni_pipe_stop(ppipe->npipe);
+ 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(ppipe->sendq, &ppipe->aio_getq);
+ nni_msgq_aio_get(p->sendq, p->aio_getq);
}
static void
-nni_resp_recv_cb(void *arg)
+resp_recv_cb(void *arg)
{
- nni_resp_pipe *ppipe = arg;
- nni_resp_sock *psock = ppipe->psock;
- nni_msgq * urq;
- nni_msg * msg;
- int hops;
- int rv;
-
- if (nni_aio_result(&ppipe->aio_recv) != 0) {
+ 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(psock->nsock);
+ urq = nni_sock_recvq(s->nsock);
- msg = ppipe->aio_recv.a_msg;
- ppipe->aio_recv.a_msg = NULL;
+ msg = nni_aio_get_msg(p->aio_recv);
+ nni_aio_set_msg(p->aio_recv, NULL);
// Store the pipe id in the header, first thing.
- if (nni_msg_header_append_u32(msg, ppipe->id) != 0) {
+ if (nni_msg_header_append_u32(msg, p->id) != 0) {
nni_msg_free(msg);
goto error;
}
@@ -300,7 +298,7 @@ nni_resp_recv_cb(void *arg)
int end = 0;
uint8_t *body;
- if (hops >= psock->ttl) {
+ if (hops >= s->ttl) {
nni_msg_free(msg);
goto error;
}
@@ -322,46 +320,46 @@ nni_resp_recv_cb(void *arg)
}
// Now send it up.
- ppipe->aio_putq.a_msg = msg;
- nni_msgq_aio_put(urq, &ppipe->aio_putq);
+ nni_aio_set_msg(p->aio_putq, msg);
+ nni_msgq_aio_put(urq, p->aio_putq);
return;
error:
- nni_pipe_stop(ppipe->npipe);
+ nni_pipe_stop(p->npipe);
}
static void
-nni_resp_putq_cb(void *arg)
+resp_putq_cb(void *arg)
{
- nni_resp_pipe *ppipe = arg;
+ resp_pipe *p = arg;
- if (nni_aio_result(&ppipe->aio_putq) != 0) {
- nni_msg_free(ppipe->aio_putq.a_msg);
- ppipe->aio_putq.a_msg = NULL;
- nni_pipe_stop(ppipe->npipe);
+ 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(ppipe->npipe, &ppipe->aio_recv);
+ nni_pipe_recv(p->npipe, p->aio_recv);
}
static int
-nni_resp_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
+resp_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
{
- nni_resp_sock *psock = arg;
- int rv = NNG_ENOTSUP;
- int oldraw;
+ resp_sock *s = arg;
+ int rv = NNG_ENOTSUP;
+ int oldraw;
if (opt == nng_optid_maxttl) {
- rv = nni_setopt_int(&psock->ttl, buf, sz, 1, 255);
+ rv = nni_setopt_int(&s->ttl, buf, sz, 1, 255);
} else if (opt == nng_optid_raw) {
- oldraw = psock->raw;
- rv = nni_setopt_int(&psock->raw, buf, sz, 0, 1);
- if (oldraw != psock->raw) {
- if (!psock->raw) {
- nni_sock_senderr(psock->nsock, 0);
+ oldraw = s->raw;
+ rv = nni_setopt_int(&s->raw, buf, sz, 0, 1);
+ if (oldraw != s->raw) {
+ if (!s->raw) {
+ nni_sock_senderr(s->nsock, 0);
} else {
- nni_sock_senderr(psock->nsock, NNG_ESTATE);
+ nni_sock_senderr(s->nsock, NNG_ESTATE);
}
}
}
@@ -370,34 +368,34 @@ nni_resp_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
}
static int
-nni_resp_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
+resp_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
{
- nni_resp_sock *psock = arg;
- int rv = NNG_ENOTSUP;
+ resp_sock *s = arg;
+ int rv = NNG_ENOTSUP;
if (opt == nng_optid_maxttl) {
- rv = nni_getopt_int(&psock->ttl, buf, szp);
+ rv = nni_getopt_int(&s->ttl, buf, szp);
} else if (opt == nng_optid_raw) {
- rv = nni_getopt_int(&psock->raw, buf, szp);
+ rv = nni_getopt_int(&s->raw, buf, szp);
}
return (rv);
}
static nni_msg *
-nni_resp_sock_sfilter(void *arg, nni_msg *msg)
+resp_sock_sfilter(void *arg, nni_msg *msg)
{
- nni_resp_sock *psock = arg;
+ resp_sock *s = arg;
- if (psock->raw) {
+ if (s->raw) {
return (msg);
}
// Cannot send again until a receive is done...
- nni_sock_senderr(psock->nsock, NNG_ESTATE);
+ 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 (psock->btrace == NULL) {
+ if (s->btrace == NULL) {
nni_msg_free(msg);
return (NULL);
}
@@ -405,79 +403,78 @@ nni_resp_sock_sfilter(void *arg, nni_msg *msg)
// drop anything else in the header...
nni_msg_header_clear(msg);
- if (nni_msg_header_append(msg, psock->btrace, psock->btrace_len) !=
- 0) {
- nni_free(psock->btrace, psock->btrace_len);
- psock->btrace = NULL;
- psock->btrace_len = 0;
+ 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(psock->btrace, psock->btrace_len);
- psock->btrace = NULL;
- psock->btrace_len = 0;
+ nni_free(s->btrace, s->btrace_len);
+ s->btrace = NULL;
+ s->btrace_len = 0;
return (msg);
}
static nni_msg *
-nni_resp_sock_rfilter(void *arg, nni_msg *msg)
+resp_sock_rfilter(void *arg, nni_msg *msg)
{
- nni_resp_sock *psock = arg;
- char * header;
- size_t len;
+ resp_sock *s = arg;
+ char * header;
+ size_t len;
- if (psock->raw) {
+ if (s->raw) {
return (msg);
}
- nni_sock_senderr(psock->nsock, 0);
+ nni_sock_senderr(s->nsock, 0);
len = nni_msg_header_len(msg);
header = nni_msg_header(msg);
- if (psock->btrace != NULL) {
- nni_free(psock->btrace, psock->btrace_len);
- psock->btrace = NULL;
- psock->btrace_len = 0;
+ if (s->btrace != NULL) {
+ nni_free(s->btrace, s->btrace_len);
+ s->btrace = NULL;
+ s->btrace_len = 0;
}
- if ((psock->btrace = nni_alloc(len)) == NULL) {
+ if ((s->btrace = nni_alloc(len)) == NULL) {
nni_msg_free(msg);
return (NULL);
}
- psock->btrace_len = len;
- memcpy(psock->btrace, header, len);
+ s->btrace_len = len;
+ memcpy(s->btrace, header, len);
nni_msg_header_clear(msg);
return (msg);
}
-static nni_proto_pipe_ops nni_resp_pipe_ops = {
- .pipe_init = nni_resp_pipe_init,
- .pipe_fini = nni_resp_pipe_fini,
- .pipe_start = nni_resp_pipe_start,
- .pipe_stop = nni_resp_pipe_stop,
+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_ops nni_resp_sock_ops = {
- .sock_init = nni_resp_sock_init,
- .sock_fini = nni_resp_sock_fini,
- .sock_open = nni_resp_sock_open,
- .sock_close = nni_resp_sock_close,
- .sock_setopt = nni_resp_sock_setopt,
- .sock_getopt = nni_resp_sock_getopt,
- .sock_rfilter = nni_resp_sock_rfilter,
- .sock_sfilter = nni_resp_sock_sfilter,
+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_setopt = resp_sock_setopt,
+ .sock_getopt = resp_sock_getopt,
+ .sock_rfilter = resp_sock_rfilter,
+ .sock_sfilter = resp_sock_sfilter,
};
-nni_proto nni_respondent_proto = {
+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 = &nni_resp_sock_ops,
- .proto_pipe_ops = &nni_resp_pipe_ops,
+ .proto_sock_ops = &resp_sock_ops,
+ .proto_pipe_ops = &resp_pipe_ops,
};
int
nng_respondent0_open(nng_socket *sidp)
{
- return (nni_proto_open(sidp, &nni_respondent_proto));
+ return (nni_proto_open(sidp, &resp_proto));
}
diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c
index d7341025..09fe0768 100644
--- a/src/protocol/survey/survey.c
+++ b/src/protocol/survey/survey.c
@@ -16,18 +16,18 @@
// Surveyor protocol. The SURVEYOR protocol is the "survey" side of the
// survey pattern. This is useful for building service discovery, voting, etc.
-typedef struct nni_surv_pipe nni_surv_pipe;
-typedef struct nni_surv_sock nni_surv_sock;
-
-static void nni_surv_sock_getq_cb(void *);
-static void nni_surv_getq_cb(void *);
-static void nni_surv_putq_cb(void *);
-static void nni_surv_send_cb(void *);
-static void nni_surv_recv_cb(void *);
-static void nni_surv_timeout(void *);
-
-// An nni_surv_sock is our per-socket protocol private structure.
-struct nni_surv_sock {
+typedef struct surv_pipe surv_pipe;
+typedef struct surv_sock surv_sock;
+
+static void surv_sock_getq_cb(void *);
+static void surv_getq_cb(void *);
+static void surv_putq_cb(void *);
+static void surv_send_cb(void *);
+static void surv_recv_cb(void *);
+static void surv_timeout(void *);
+
+// A surv_sock is our per-socket protocol private structure.
+struct surv_sock {
nni_sock * nsock;
nni_duration survtime;
nni_time expire;
@@ -36,211 +36,214 @@ struct nni_surv_sock {
uint32_t nextid; // next id
uint32_t survid; // outstanding request ID (big endian)
nni_list pipes;
- nni_aio aio_getq;
+ nni_aio * aio_getq;
nni_timer_node timer;
nni_msgq * uwq;
nni_msgq * urq;
nni_mtx mtx;
};
-// An nni_surv_pipe is our per-pipe protocol private structure.
-struct nni_surv_pipe {
- nni_pipe * npipe;
- nni_surv_sock *psock;
- nni_msgq * sendq;
- nni_list_node node;
- nni_aio aio_getq;
- nni_aio aio_putq;
- nni_aio aio_send;
- nni_aio aio_recv;
+// A surv_pipe is our per-pipe protocol private structure.
+struct surv_pipe {
+ nni_pipe * npipe;
+ surv_sock * psock;
+ nni_msgq * sendq;
+ nni_list_node node;
+ nni_aio * aio_getq;
+ nni_aio * aio_putq;
+ nni_aio * aio_send;
+ nni_aio * aio_recv;
};
static void
-nni_surv_sock_fini(void *arg)
+surv_sock_fini(void *arg)
{
- nni_surv_sock *psock = arg;
+ surv_sock *s = arg;
- nni_aio_stop(&psock->aio_getq);
- nni_aio_fini(&psock->aio_getq);
- nni_mtx_fini(&psock->mtx);
- NNI_FREE_STRUCT(psock);
+ nni_aio_stop(s->aio_getq);
+ nni_aio_fini(s->aio_getq);
+ nni_mtx_fini(&s->mtx);
+ NNI_FREE_STRUCT(s);
}
static int
-nni_surv_sock_init(void **sp, nni_sock *nsock)
+surv_sock_init(void **sp, nni_sock *nsock)
{
- nni_surv_sock *psock;
+ surv_sock *s;
+ int rv;
- if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) {
+ if ((s = NNI_ALLOC_STRUCT(s)) == NULL) {
return (NNG_ENOMEM);
}
- NNI_LIST_INIT(&psock->pipes, nni_surv_pipe, node);
- nni_mtx_init(&psock->mtx);
- nni_aio_init(&psock->aio_getq, nni_surv_sock_getq_cb, psock);
- nni_timer_init(&psock->timer, nni_surv_timeout, psock);
-
- psock->nextid = nni_random();
- psock->nsock = nsock;
- psock->raw = 0;
- psock->survtime = NNI_SECOND * 60;
- psock->expire = NNI_TIME_ZERO;
- psock->uwq = nni_sock_sendq(nsock);
- psock->urq = nni_sock_recvq(nsock);
-
- *sp = psock;
+ if ((rv = nni_aio_init(&s->aio_getq, surv_sock_getq_cb, s)) != 0) {
+ surv_sock_fini(s);
+ return (rv);
+ }
+ NNI_LIST_INIT(&s->pipes, surv_pipe, node);
+ nni_mtx_init(&s->mtx);
+ nni_timer_init(&s->timer, surv_timeout, s);
+
+ s->nextid = nni_random();
+ s->nsock = nsock;
+ s->raw = 0;
+ s->survtime = NNI_SECOND * 60;
+ s->expire = NNI_TIME_ZERO;
+ s->uwq = nni_sock_sendq(nsock);
+ s->urq = nni_sock_recvq(nsock);
+
+ *sp = s;
nni_sock_recverr(nsock, NNG_ESTATE);
return (0);
}
static void
-nni_surv_sock_open(void *arg)
+surv_sock_open(void *arg)
{
- nni_surv_sock *psock = arg;
+ surv_sock *s = arg;
- nni_msgq_aio_get(psock->uwq, &psock->aio_getq);
+ nni_msgq_aio_get(s->uwq, s->aio_getq);
}
static void
-nni_surv_sock_close(void *arg)
+surv_sock_close(void *arg)
{
- nni_surv_sock *psock = arg;
+ surv_sock *s = arg;
- nni_timer_cancel(&psock->timer);
- nni_aio_cancel(&psock->aio_getq, NNG_ECLOSED);
+ nni_timer_cancel(&s->timer);
+ nni_aio_cancel(s->aio_getq, NNG_ECLOSED);
}
static void
-nni_surv_pipe_fini(void *arg)
+surv_pipe_fini(void *arg)
{
- nni_surv_pipe *ppipe = arg;
-
- nni_aio_fini(&ppipe->aio_getq);
- nni_aio_fini(&ppipe->aio_send);
- nni_aio_fini(&ppipe->aio_recv);
- nni_aio_fini(&ppipe->aio_putq);
- nni_msgq_fini(ppipe->sendq);
- NNI_FREE_STRUCT(ppipe);
+ surv_pipe *p = arg;
+
+ nni_aio_fini(p->aio_getq);
+ nni_aio_fini(p->aio_send);
+ nni_aio_fini(p->aio_recv);
+ nni_aio_fini(p->aio_putq);
+ nni_msgq_fini(p->sendq);
+ NNI_FREE_STRUCT(p);
}
static int
-nni_surv_pipe_init(void **pp, nni_pipe *npipe, void *psock)
+surv_pipe_init(void **pp, nni_pipe *npipe, void *s)
{
- nni_surv_pipe *ppipe;
- int rv;
+ surv_pipe *p;
+ int rv;
- if ((ppipe = NNI_ALLOC_STRUCT(ppipe)) == NULL) {
+ if ((p = NNI_ALLOC_STRUCT(p)) == NULL) {
return (NNG_ENOMEM);
}
// This depth could be tunable.
- if ((rv = nni_msgq_init(&ppipe->sendq, 16)) != 0) {
- NNI_FREE_STRUCT(ppipe);
+ if (((rv = nni_msgq_init(&p->sendq, 16)) != 0) ||
+ ((rv = nni_aio_init(&p->aio_getq, surv_getq_cb, p)) != 0) ||
+ ((rv = nni_aio_init(&p->aio_putq, surv_putq_cb, p)) != 0) ||
+ ((rv = nni_aio_init(&p->aio_send, surv_send_cb, p)) != 0) ||
+ ((rv = nni_aio_init(&p->aio_recv, surv_recv_cb, p)) != 0)) {
+ surv_pipe_fini(p);
return (rv);
}
- nni_aio_init(&ppipe->aio_getq, nni_surv_getq_cb, ppipe);
- nni_aio_init(&ppipe->aio_putq, nni_surv_putq_cb, ppipe);
- nni_aio_init(&ppipe->aio_send, nni_surv_send_cb, ppipe);
- nni_aio_init(&ppipe->aio_recv, nni_surv_recv_cb, ppipe);
-
- ppipe->npipe = npipe;
- ppipe->psock = psock;
- *pp = ppipe;
+ p->npipe = npipe;
+ p->psock = s;
+ *pp = p;
return (0);
}
static int
-nni_surv_pipe_start(void *arg)
+surv_pipe_start(void *arg)
{
- nni_surv_pipe *ppipe = arg;
- nni_surv_sock *psock = ppipe->psock;
+ surv_pipe *p = arg;
+ surv_sock *s = p->psock;
- nni_mtx_lock(&psock->mtx);
- nni_list_append(&psock->pipes, ppipe);
- nni_mtx_unlock(&psock->mtx);
+ nni_mtx_lock(&s->mtx);
+ nni_list_append(&s->pipes, p);
+ nni_mtx_unlock(&s->mtx);
- nni_msgq_aio_get(ppipe->sendq, &ppipe->aio_getq);
- nni_pipe_recv(ppipe->npipe, &ppipe->aio_recv);
+ nni_msgq_aio_get(p->sendq, p->aio_getq);
+ nni_pipe_recv(p->npipe, p->aio_recv);
return (0);
}
static void
-nni_surv_pipe_stop(void *arg)
+surv_pipe_stop(void *arg)
{
- nni_surv_pipe *ppipe = arg;
- nni_surv_sock *psock = ppipe->psock;
+ surv_pipe *p = arg;
+ surv_sock *s = p->psock;
- nni_aio_stop(&ppipe->aio_getq);
- nni_aio_stop(&ppipe->aio_send);
- nni_aio_stop(&ppipe->aio_recv);
- nni_aio_stop(&ppipe->aio_putq);
+ nni_aio_stop(p->aio_getq);
+ nni_aio_stop(p->aio_send);
+ nni_aio_stop(p->aio_recv);
+ nni_aio_stop(p->aio_putq);
- nni_msgq_close(ppipe->sendq);
+ nni_msgq_close(p->sendq);
- nni_mtx_lock(&psock->mtx);
- if (nni_list_active(&psock->pipes, ppipe)) {
- nni_list_remove(&psock->pipes, ppipe);
+ nni_mtx_lock(&s->mtx);
+ if (nni_list_active(&s->pipes, p)) {
+ nni_list_remove(&s->pipes, p);
}
- nni_mtx_unlock(&psock->mtx);
+ nni_mtx_unlock(&s->mtx);
}
static void
-nni_surv_getq_cb(void *arg)
+surv_getq_cb(void *arg)
{
- nni_surv_pipe *ppipe = arg;
+ surv_pipe *p = arg;
- if (nni_aio_result(&ppipe->aio_getq) != 0) {
- nni_pipe_stop(ppipe->npipe);
+ if (nni_aio_result(p->aio_getq) != 0) {
+ nni_pipe_stop(p->npipe);
return;
}
- ppipe->aio_send.a_msg = ppipe->aio_getq.a_msg;
- ppipe->aio_getq.a_msg = NULL;
+ 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(ppipe->npipe, &ppipe->aio_send);
+ nni_pipe_send(p->npipe, p->aio_send);
}
static void
-nni_surv_send_cb(void *arg)
+surv_send_cb(void *arg)
{
- nni_surv_pipe *ppipe = arg;
+ surv_pipe *p = arg;
- if (nni_aio_result(&ppipe->aio_send) != 0) {
- nni_msg_free(ppipe->aio_send.a_msg);
- ppipe->aio_send.a_msg = NULL;
- nni_pipe_stop(ppipe->npipe);
+ 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(ppipe->psock->uwq, &ppipe->aio_getq);
+ nni_msgq_aio_get(p->psock->uwq, p->aio_getq);
}
static void
-nni_surv_putq_cb(void *arg)
+surv_putq_cb(void *arg)
{
- nni_surv_pipe *ppipe = arg;
+ surv_pipe *p = arg;
- if (nni_aio_result(&ppipe->aio_putq) != 0) {
- nni_msg_free(ppipe->aio_putq.a_msg);
- ppipe->aio_putq.a_msg = NULL;
- nni_pipe_stop(ppipe->npipe);
+ 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);
return;
}
- nni_pipe_recv(ppipe->npipe, &ppipe->aio_recv);
+ nni_pipe_recv(p->npipe, p->aio_recv);
}
static void
-nni_surv_recv_cb(void *arg)
+surv_recv_cb(void *arg)
{
- nni_surv_pipe *ppipe = arg;
- nni_msg * msg;
+ surv_pipe *p = arg;
+ nni_msg * msg;
- if (nni_aio_result(&ppipe->aio_recv) != 0) {
+ if (nni_aio_result(p->aio_recv) != 0) {
goto failed;
}
- msg = ppipe->aio_recv.a_msg;
- ppipe->aio_recv.a_msg = NULL;
+ msg = nni_aio_get_msg(p->aio_recv);
+ nni_aio_set_msg(p->aio_recv, NULL);
// We yank 4 bytes of body, and move them to the header.
if (nni_msg_len(msg) < 4) {
@@ -255,35 +258,35 @@ nni_surv_recv_cb(void *arg)
}
(void) nni_msg_trim(msg, 4);
- ppipe->aio_putq.a_msg = msg;
- nni_msgq_aio_put(ppipe->psock->urq, &ppipe->aio_putq);
+ nni_aio_set_msg(p->aio_putq, msg);
+ nni_msgq_aio_put(p->psock->urq, p->aio_putq);
return;
failed:
- nni_pipe_stop(ppipe->npipe);
+ nni_pipe_stop(p->npipe);
}
static int
-nni_surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
+surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
{
- nni_surv_sock *psock = arg;
- int rv = NNG_ENOTSUP;
- int oldraw;
+ surv_sock *s = arg;
+ int rv = NNG_ENOTSUP;
+ int oldraw;
if (opt == nng_optid_surveyor_surveytime) {
- rv = nni_setopt_usec(&psock->survtime, buf, sz);
+ rv = nni_setopt_usec(&s->survtime, buf, sz);
} else if (opt == nng_optid_raw) {
- oldraw = psock->raw;
- rv = nni_setopt_int(&psock->raw, buf, sz, 0, 1);
- if (oldraw != psock->raw) {
- if (psock->raw) {
- nni_sock_recverr(psock->nsock, 0);
+ oldraw = s->raw;
+ rv = nni_setopt_int(&s->raw, buf, sz, 0, 1);
+ if (oldraw != s->raw) {
+ if (s->raw) {
+ nni_sock_recverr(s->nsock, 0);
} else {
- nni_sock_recverr(psock->nsock, NNG_ESTATE);
+ nni_sock_recverr(s->nsock, NNG_ESTATE);
}
- psock->survid = 0;
- nni_timer_cancel(&psock->timer);
+ s->survid = 0;
+ nni_timer_cancel(&s->timer);
}
}
@@ -291,49 +294,49 @@ nni_surv_sock_setopt(void *arg, int opt, const void *buf, size_t sz)
}
static int
-nni_surv_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
+surv_sock_getopt(void *arg, int opt, void *buf, size_t *szp)
{
- nni_surv_sock *psock = arg;
- int rv = NNG_ENOTSUP;
+ surv_sock *s = arg;
+ int rv = NNG_ENOTSUP;
if (opt == nng_optid_surveyor_surveytime) {
- rv = nni_getopt_usec(&psock->survtime, buf, szp);
+ rv = nni_getopt_usec(&s->survtime, buf, szp);
} else if (opt == nng_optid_raw) {
- rv = nni_getopt_int(&psock->raw, buf, szp);
+ rv = nni_getopt_int(&s->raw, buf, szp);
}
return (rv);
}
static void
-nni_surv_sock_getq_cb(void *arg)
+surv_sock_getq_cb(void *arg)
{
- nni_surv_sock *psock = arg;
- nni_surv_pipe *ppipe;
- nni_surv_pipe *last;
- nni_msg * msg, *dup;
+ surv_sock *s = arg;
+ surv_pipe *p;
+ surv_pipe *last;
+ nni_msg * msg, *dup;
- if (nni_aio_result(&psock->aio_getq) != 0) {
+ if (nni_aio_result(s->aio_getq) != 0) {
// Should be NNG_ECLOSED.
return;
}
- msg = psock->aio_getq.a_msg;
- psock->aio_getq.a_msg = NULL;
+ msg = nni_aio_get_msg(s->aio_getq);
+ nni_aio_set_msg(s->aio_getq, NULL);
- nni_mtx_lock(&psock->mtx);
- last = nni_list_last(&psock->pipes);
- NNI_LIST_FOREACH (&psock->pipes, ppipe) {
- if (ppipe != last) {
+ nni_mtx_lock(&s->mtx);
+ last = nni_list_last(&s->pipes);
+ NNI_LIST_FOREACH (&s->pipes, p) {
+ if (p != last) {
if (nni_msg_dup(&dup, msg) != 0) {
continue;
}
} else {
dup = msg;
}
- if (nni_msgq_tryput(ppipe->sendq, dup) != 0) {
+ if (nni_msgq_tryput(p->sendq, dup) != 0) {
nni_msg_free(dup);
}
}
- nni_mtx_unlock(&psock->mtx);
+ nni_mtx_unlock(&s->mtx);
if (last == NULL) {
// If there were no pipes to send on, just toss the message.
@@ -342,23 +345,23 @@ nni_surv_sock_getq_cb(void *arg)
}
static void
-nni_surv_timeout(void *arg)
+surv_timeout(void *arg)
{
- nni_surv_sock *psock = arg;
+ surv_sock *s = arg;
- nni_sock_lock(psock->nsock);
- psock->survid = 0;
- nni_sock_recverr(psock->nsock, NNG_ESTATE);
- nni_msgq_set_get_error(psock->urq, NNG_ETIMEDOUT);
- nni_sock_unlock(psock->nsock);
+ nni_sock_lock(s->nsock);
+ s->survid = 0;
+ nni_sock_recverr(s->nsock, NNG_ESTATE);
+ nni_msgq_set_get_error(s->urq, NNG_ETIMEDOUT);
+ nni_sock_unlock(s->nsock);
}
static nni_msg *
-nni_surv_sock_sfilter(void *arg, nni_msg *msg)
+surv_sock_sfilter(void *arg, nni_msg *msg)
{
- nni_surv_sock *psock = arg;
+ surv_sock *s = arg;
- if (psock->raw) {
+ if (s->raw) {
// No automatic retry, and the request ID must
// be in the header coming down.
return (msg);
@@ -367,9 +370,9 @@ nni_surv_sock_sfilter(void *arg, nni_msg *msg)
// Generate a new request ID. We always set the high
// order bit so that the peer can locate the end of the
// backtrace. (Pipe IDs have the high order bit clear.)
- psock->survid = (psock->nextid++) | 0x80000000u;
+ s->survid = (s->nextid++) | 0x80000000u;
- if (nni_msg_header_append_u32(msg, psock->survid) != 0) {
+ if (nni_msg_header_append_u32(msg, s->survid) != 0) {
// Should be ENOMEM.
nni_msg_free(msg);
return (NULL);
@@ -378,28 +381,28 @@ nni_surv_sock_sfilter(void *arg, nni_msg *msg)
// If another message is there, this cancels it. We move the
// survey expiration out. The timeout thread will wake up in
// the wake below, and reschedule itself appropriately.
- psock->expire = nni_clock() + psock->survtime;
- nni_timer_schedule(&psock->timer, psock->expire);
+ s->expire = nni_clock() + s->survtime;
+ nni_timer_schedule(&s->timer, s->expire);
// Clear the error condition.
- nni_sock_recverr(psock->nsock, 0);
+ nni_sock_recverr(s->nsock, 0);
// nni_msgq_set_get_error(nni_sock_recvq(psock->nsock), 0);
return (msg);
}
static nni_msg *
-nni_surv_sock_rfilter(void *arg, nni_msg *msg)
+surv_sock_rfilter(void *arg, nni_msg *msg)
{
- nni_surv_sock *ssock = arg;
+ surv_sock *s = arg;
- if (ssock->raw) {
+ if (s->raw) {
// Pass it unmolested
return (msg);
}
if ((nni_msg_header_len(msg) < sizeof(uint32_t)) ||
- (nni_msg_header_trim_u32(msg) != ssock->survid)) {
+ (nni_msg_header_trim_u32(msg) != s->survid)) {
// Wrong request id
nni_msg_free(msg);
return (NULL);
@@ -408,35 +411,35 @@ nni_surv_sock_rfilter(void *arg, nni_msg *msg)
return (msg);
}
-static nni_proto_pipe_ops nni_surv_pipe_ops = {
- .pipe_init = nni_surv_pipe_init,
- .pipe_fini = nni_surv_pipe_fini,
- .pipe_start = nni_surv_pipe_start,
- .pipe_stop = nni_surv_pipe_stop,
+static nni_proto_pipe_ops surv_pipe_ops = {
+ .pipe_init = surv_pipe_init,
+ .pipe_fini = surv_pipe_fini,
+ .pipe_start = surv_pipe_start,
+ .pipe_stop = surv_pipe_stop,
};
-static nni_proto_sock_ops nni_surv_sock_ops = {
- .sock_init = nni_surv_sock_init,
- .sock_fini = nni_surv_sock_fini,
- .sock_open = nni_surv_sock_open,
- .sock_close = nni_surv_sock_close,
- .sock_setopt = nni_surv_sock_setopt,
- .sock_getopt = nni_surv_sock_getopt,
- .sock_rfilter = nni_surv_sock_rfilter,
- .sock_sfilter = nni_surv_sock_sfilter,
+static nni_proto_sock_ops surv_sock_ops = {
+ .sock_init = surv_sock_init,
+ .sock_fini = surv_sock_fini,
+ .sock_open = surv_sock_open,
+ .sock_close = surv_sock_close,
+ .sock_setopt = surv_sock_setopt,
+ .sock_getopt = surv_sock_getopt,
+ .sock_rfilter = surv_sock_rfilter,
+ .sock_sfilter = surv_sock_sfilter,
};
-nni_proto nni_surveyor_proto = {
+static nni_proto surv_proto = {
.proto_version = NNI_PROTOCOL_VERSION,
.proto_self = { NNG_PROTO_SURVEYOR_V0, "surveyor" },
.proto_peer = { NNG_PROTO_RESPONDENT_V0, "respondent" },
.proto_flags = NNI_PROTO_FLAG_SNDRCV,
- .proto_sock_ops = &nni_surv_sock_ops,
- .proto_pipe_ops = &nni_surv_pipe_ops,
+ .proto_sock_ops = &surv_sock_ops,
+ .proto_pipe_ops = &surv_pipe_ops,
};
int
nng_surveyor0_open(nng_socket *sidp)
{
- return (nni_proto_open(sidp, &nni_surveyor_proto));
+ return (nni_proto_open(sidp, &surv_proto));
}