Skip to content

Commit

Permalink
Remove last vestiges of integer option numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Sep 27, 2017
1 parent 64db0f0 commit 0736a95
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 226 deletions.
2 changes: 0 additions & 2 deletions src/core/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ nni_init_helper(void)
((rv = nni_timer_sys_init()) != 0) ||
((rv = nni_aio_sys_init()) != 0) ||
((rv = nni_random_sys_init()) != 0) ||
((rv = nni_option_sys_init()) != 0) ||
((rv = nni_sock_sys_init()) != 0) ||
((rv = nni_ep_sys_init()) != 0) ||
((rv = nni_pipe_sys_init()) != 0) ||
Expand All @@ -46,7 +45,6 @@ nni_fini(void)
nni_pipe_sys_fini();
nni_ep_sys_fini();
nni_sock_sys_fini();
nni_option_sys_fini();
nni_random_sys_fini();
nni_aio_sys_fini();
nni_timer_sys_fini();
Expand Down
127 changes: 0 additions & 127 deletions src/core/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@
#include <stdio.h>
#include <string.h>

// Dynamic options.

typedef struct nni_option nni_option;
struct nni_option {
nni_list_node o_link;
char * o_name;
int o_id;
};

static nni_mtx nni_option_lk;
static nni_list nni_options;
static int nni_option_nextid;

int
nni_chkopt_usec(const void *v, size_t sz)
{
Expand Down Expand Up @@ -293,117 +280,3 @@ nni_getopt_fd(nni_sock *s, nni_notifyfd *fd, int mask, void *val, size_t *szp)
memcpy(val, &fd->sn_rfd, sizeof(int));
return (0);
}

// nni_option_set_id sets the id for an option, if not already done so.
// (Some options have hard coded values that end-user may depend upon.)
// If the ID passed in is negative, then a new ID is allocated dynamically.
static int
nni_option_set_id(const char *name, int id)
{
nni_option *opt;
size_t len;
nni_mtx_lock(&nni_option_lk);
NNI_LIST_FOREACH (&nni_options, opt) {
if (strcmp(name, opt->o_name) == 0) {
nni_mtx_unlock(&nni_option_lk);
return (0);
}
}
if ((opt = NNI_ALLOC_STRUCT(opt)) == NULL) {
nni_mtx_unlock(&nni_option_lk);
return (NNG_ENOMEM);
}
if ((opt->o_name = nni_strdup(name)) == NULL) {
nni_mtx_unlock(&nni_option_lk);
NNI_FREE_STRUCT(opt);
return (NNG_ENOMEM);
}
if (id < 0) {
id = nni_option_nextid++;
}
opt->o_id = id;
nni_list_append(&nni_options, opt);
nni_mtx_unlock(&nni_option_lk);
return (0);
}

int
nni_option_lookup(const char *name)
{
nni_option *opt;
int id = -1;

nni_mtx_lock(&nni_option_lk);
NNI_LIST_FOREACH (&nni_options, opt) {
if (strcmp(name, opt->o_name) == 0) {
id = opt->o_id;
break;
}
}
nni_mtx_unlock(&nni_option_lk);
return (id);
}

int
nni_option_register(const char *name, int *idp)
{
int rv;

// Note that if the id was already in use, we will
// wind up leaving a gap in the ID space. That should
// be inconsequential.
if ((rv = nni_option_set_id(name, -1)) != 0) {
return (rv);
}
*idp = nni_option_lookup(name);
return (0);
}

void
nni_option_sys_fini(void)
{
if (nni_option_nextid != 0) {
nni_option *opt;
while ((opt = nni_list_first(&nni_options)) != NULL) {
nni_list_remove(&nni_options, opt);
nni_strfree(opt->o_name);
NNI_FREE_STRUCT(opt);
}
}
nni_option_nextid = 0;
}

int nni_optid_raw;
int nni_optid_recvmaxsz;
int nni_optid_maxttl;
int nni_optid_protocol;
int nni_optid_transport;
int nni_optid_locaddr;
int nni_optid_remaddr;
int nni_optid_surveyor_surveytime;

int
nni_option_sys_init(void)
{
nni_mtx_init(&nni_option_lk);
NNI_LIST_INIT(&nni_options, nni_option, o_link);
nni_option_nextid = 0x10000;
int rv;

#define OPT_REGISTER(o) nni_option_register(nng_opt_##o, &nni_optid_##o)
// Register our well-known options.
if (((rv = OPT_REGISTER(raw)) != 0) ||
((rv = OPT_REGISTER(recvmaxsz)) != 0) ||
((rv = OPT_REGISTER(maxttl)) != 0) ||
((rv = OPT_REGISTER(protocol)) != 0) ||
((rv = OPT_REGISTER(transport)) != 0) ||
((rv = OPT_REGISTER(locaddr)) != 0) ||
((rv = OPT_REGISTER(remaddr)) != 0) ||
((rv = OPT_REGISTER(surveyor_surveytime)) != 0)) {
nni_option_sys_fini();
return (rv);
}
#undef OPT_REGISTER

return (0);
}
17 changes: 0 additions & 17 deletions src/core/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,4 @@ extern int nni_chkopt_usec(const void *, size_t);
extern int nni_chkopt_int(const void *, size_t, int, int);
extern int nni_chkopt_size(const void *, size_t, size_t, size_t);

extern int nni_option_register(const char *, int *);
extern int nni_option_lookup(const char *);
extern const char *nni_option_name(int);

extern int nni_option_sys_init(void);
extern void nni_option_sys_fini(void);

extern int nni_optid_raw;
extern int nni_optid_recvmaxsz;
extern int nni_optid_maxttl;
extern int nni_optid_protocol;
extern int nni_optid_transport;
extern int nni_optid_locaddr;
extern int nni_optid_remaddr;
extern int nni_optid_req_resendtime;
extern int nni_optid_surveyor_surveytime;

#endif // CORE_OPTIONS_H
3 changes: 0 additions & 3 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1095,12 +1095,9 @@ nni_sock_getopt(nni_sock *s, const char *name, void *val, size_t *szp)
{
int rv = NNG_ENOTSUP;
nni_sockopt * sopt;
int opt;
const nni_socket_option * sso;
const nni_proto_sock_option *pso;

opt = nni_option_lookup(name);

nni_mtx_lock(&s->s_mx);
if (s->s_closing) {
nni_mtx_unlock(&s->s_mx);
Expand Down
13 changes: 0 additions & 13 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,16 +1013,3 @@ nng_thread_destroy(void *arg)

NNI_FREE_STRUCT(thr);
}

// Constant option definitions. These are for well-known options,
// so that the vast majority of consumers don't have to look these up.

const char *nng_opt_raw = "raw";
const char *nng_opt_recvmaxsz = "recv-size-max";
const char *nng_opt_maxttl = "ttl-max";
const char *nng_opt_protocol = "protocol";
const char *nng_opt_transport = "transport";
const char *nng_opt_locaddr = "local-address";
const char *nng_opt_remaddr = "remote-address";
// Well known protocol options.
const char *nng_opt_surveyor_surveytime = "surveyor:survey-time";
11 changes: 2 additions & 9 deletions src/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,22 +415,15 @@ NNG_DECL int nng_respondent0_open(nng_socket *);
#define NNG_OPT_RECONNMINT "reconnect-time-min"
#define NNG_OPT_RECONNMAXT "reconnect-time-max"

#define NNG_OPT_PAIR1_POLY "pair1:polyamorous"

#define NNG_OPT_SUB_SUBSCRIBE "sub:subscribe"
#define NNG_OPT_SUB_UNSUBSCRIBE "sub:unsubscribe"

#define NNG_OPT_REQ_RESENDTIME "req:resend-time"

#define NNG_OPT_SURVEYOR_SURVEYTIME "surveyor:survey-time"

NNG_DECL const char *nng_opt_raw;
NNG_DECL const char *nng_opt_recvmaxsz;
NNG_DECL const char *nng_opt_maxttl;
NNG_DECL const char *nng_opt_protocol;
NNG_DECL const char *nng_opt_transport;
NNG_DECL const char *nng_opt_locaddr;
NNG_DECL const char *nng_opt_remaddr;
NNG_DECL const char *nng_opt_surveyor_surveytime;

// XXX: TBD: priorities, socket names, ipv4only

// Statistics. These are for informational purposes only, and subject
Expand Down
2 changes: 1 addition & 1 deletion src/nng_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ init_opts(void)
case NN_SURVEYOR:
switch (options[i].nnopt) {
case NN_SURVEYOR_DEADLINE:
SETOPT(nng_opt_surveyor_surveytime, 1);
SETOPT(NNG_OPT_SURVEYOR_SURVEYTIME, 1);
break;
}
break;
Expand Down
8 changes: 1 addition & 7 deletions src/protocol/pair/pair_v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ static void pair1_pipe_getq_cb(void *);
static void pair1_pipe_putq_cb(void *);
static void pair1_pipe_fini(void *);

// This is exposed as an external name for external consumers.
#define NNG_OPT_PAIR1_POLY "pair1-polyamorous"
const char *nng_opt_pair1_poly = NNG_OPT_PAIR1_POLY;

// pair1_sock is our per-socket protocol private structure.
struct pair1_sock {
nni_sock * nsock;
Expand Down Expand Up @@ -73,7 +69,6 @@ pair1_sock_init(void **sp, nni_sock *nsock)
{
pair1_sock *s;
int rv;
int poly;

if ((s = NNI_ALLOC_STRUCT(s)) == NULL) {
return (NNG_ENOMEM);
Expand All @@ -87,8 +82,7 @@ pair1_sock_init(void **sp, nni_sock *nsock)
// Raw mode uses this.
nni_mtx_init(&s->mtx);

if (((rv = nni_aio_init(&s->aio_getq, pair1_sock_getq_cb, s)) != 0) ||
((rv = nni_option_register("polyamorous", &poly)) != 0)) {
if ((rv = nni_aio_init(&s->aio_getq, pair1_sock_getq_cb, s)) != 0) {
pair1_sock_fini(s);
return (rv);
}
Expand Down
34 changes: 2 additions & 32 deletions src/transport/zerotier/zerotier.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,8 @@ const char *nng_opt_zt_network_name = NNG_ZT_OPT_NETWORK_NAME;
const char *nng_opt_zt_ping_time = NNG_ZT_OPT_PING_TIME;
const char *nng_opt_zt_ping_count = NNG_ZT_OPT_PING_COUNT;

int zt_optid_home = -1;
int zt_optid_nwid = -1;
int zt_optid_node = -1;
int zt_optid_status = -1;
int zt_optid_network_name = -1;
int zt_optid_ping_time = -1;
int zt_optid_ping_count = -1;

// These values are supplied to help folks checking status. They are the
// return values from zt_optid_status.
// return values from zt_opt_status.
int nng_zt_status_configuring = ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION;
int nng_zt_status_ok = ZT_NETWORK_STATUS_OK;
int nng_zt_status_denied = ZT_NETWORK_STATUS_ACCESS_DENIED;
Expand Down Expand Up @@ -1605,23 +1597,6 @@ zt_node_find(zt_ep *ep)
static int
zt_tran_init(void)
{
int rv;
if (((rv = nni_option_register(nng_opt_zt_home, &zt_optid_home)) !=
0) ||
((rv = nni_option_register(nng_opt_zt_node, &zt_optid_node)) !=
0) ||
((rv = nni_option_register(nng_opt_zt_nwid, &zt_optid_nwid)) !=
0) ||
((rv = nni_option_register(nng_opt_zt_status, &zt_optid_status)) !=
0) ||
((rv = nni_option_register(
nng_opt_zt_network_name, &zt_optid_network_name)) != 0) ||
((rv = nni_option_register(
nng_opt_zt_ping_count, &zt_optid_ping_count)) != 0) ||
((rv = nni_option_register(
nng_opt_zt_ping_time, &zt_optid_ping_time)) != 0)) {
return (rv);
}
nni_mtx_init(&zt_lk);
NNI_LIST_INIT(&zt_nodes, zt_node, zn_link);
return (0);
Expand All @@ -1630,11 +1605,6 @@ zt_tran_init(void)
static void
zt_tran_fini(void)
{
zt_optid_home = -1;
zt_optid_nwid = -1;
zt_optid_node = -1;
zt_optid_ping_count = -1;
zt_optid_ping_time = -1;
zt_node *ztn;

nni_mtx_lock(&zt_lk);
Expand Down Expand Up @@ -2494,7 +2464,7 @@ zt_ep_conn_req_cb(void *arg)
static void
zt_ep_connect(void *arg, nni_aio *aio)
{
zt_ep * ep = arg;
zt_ep *ep = arg;

// We bind locally. We'll use the address later when we give
// it to the pipe, but this allows us to receive the initial
Expand Down
20 changes: 9 additions & 11 deletions tests/pair1.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include <string.h>

extern const char *nng_opt_pair1_poly;

#define APPENDSTR(m, s) nng_msg_append(m, s, strlen(s))
#define CHECKSTR(m, s) \
So(nng_msg_len(m) == strlen(s)); \
Expand Down Expand Up @@ -110,7 +108,7 @@ TestMain("PAIRv1 protocol", {
int i;
nng_msg *msg;

So(nng_setopt_int(s1, nng_opt_pair1_poly, 1) == 0);
So(nng_setopt_int(s1, NNG_OPT_PAIR1_POLY, 1) == 0);

So(nng_setopt_int(s1, NNG_OPT_RECVBUF, 1) == 0);
So(nng_setopt_int(s1, NNG_OPT_SENDBUF, 1) == 0);
Expand Down Expand Up @@ -165,7 +163,7 @@ TestMain("PAIRv1 protocol", {
So(nng_dial(c1, addr, NULL, 0) == 0);
nng_usleep(100000);

So(nng_setopt_int(s1, nng_opt_pair1_poly, 1) ==
So(nng_setopt_int(s1, NNG_OPT_PAIR1_POLY, 1) ==
NNG_ESTATE);
});

Expand Down Expand Up @@ -335,11 +333,11 @@ TestMain("PAIRv1 protocol", {
nng_pipe p1;
nng_pipe p2;

So(nng_getopt_int(s1, nng_opt_pair1_poly, &v) == 0);
So(nng_getopt_int(s1, NNG_OPT_PAIR1_POLY, &v) == 0);
So(v == 0);

So(nng_setopt_int(s1, nng_opt_pair1_poly, 1) == 0);
So(nng_getopt_int(s1, nng_opt_pair1_poly, &v) == 0);
So(nng_setopt_int(s1, NNG_OPT_PAIR1_POLY, 1) == 0);
So(nng_getopt_int(s1, NNG_OPT_PAIR1_POLY, &v) == 0);
So(v == 1);

So(nng_listen(s1, addr, NULL, 0) == 0);
Expand Down Expand Up @@ -396,7 +394,7 @@ TestMain("PAIRv1 protocol", {
Convey("Polyamorous default works", {
nng_msg *msg;

So(nng_setopt_int(s1, nng_opt_pair1_poly, 1) == 0);
So(nng_setopt_int(s1, NNG_OPT_PAIR1_POLY, 1) == 0);

So(nng_listen(s1, addr, NULL, 0) == 0);
So(nng_dial(c1, addr, NULL, 0) == 0);
Expand Down Expand Up @@ -429,11 +427,11 @@ TestMain("PAIRv1 protocol", {
nng_pipe p1;
nng_pipe p2;

So(nng_getopt_int(s1, nng_opt_pair1_poly, &v) == 0);
So(nng_getopt_int(s1, NNG_OPT_PAIR1_POLY, &v) == 0);
So(v == 0);

So(nng_setopt_int(s1, nng_opt_pair1_poly, 1) == 0);
So(nng_getopt_int(s1, nng_opt_pair1_poly, &v) == 0);
So(nng_setopt_int(s1, NNG_OPT_PAIR1_POLY, 1) == 0);
So(nng_getopt_int(s1, NNG_OPT_PAIR1_POLY, &v) == 0);
So(v == 1);

v = 0;
Expand Down
Loading

0 comments on commit 0736a95

Please sign in to comment.