New-style raft options

This commit is contained in:
Steve Dee 2014-01-17 11:30:19 -08:00
parent ec82c58982
commit 6df5fb3155
4 changed files with 98 additions and 4 deletions

View File

@ -534,6 +534,7 @@ V_OFILES=\
v/http.o \
v/loop.o \
v/main.o \
v/raft.o \
v/reck.o \
v/save.o \
v/time.o \

View File

@ -382,6 +382,22 @@
struct _u2_utty* nex_u; // next in host list
} u2_utty;
/* u2_rnam: raft peer name.
*/
typedef struct _u2_rnam {
c3_c* str_c;
c3_c* nam_c;
c3_s por_s;
struct _u2_rnam* nex_u;
} u2_rnam;
/* u2_ropt: raft options.
*/
typedef struct {
u2_rnam* nam_u;
c3_s por_s;
} u2_ropt;
/* u2_opts:
*/
typedef struct _u2_opts {
@ -389,11 +405,10 @@
c3_c* imp_c;
c3_c* hom_c;
c3_c* nam_c;
c3_c* raf_c;
c3_w kno_w;
c3_w fuz_w;
c3_s por_s;
c3_s rap_s;
u2_ropt rop_u;
u2_bean abo;
u2_bean bat;
u2_bean gab;
@ -976,3 +991,10 @@
*/
void
u2_http_io_poll(void);
/** Raft log syncing.
**/
/* u2_raft_readopt(): parse command line options.
*/
u2_bean
u2_raft_readopt(u2_ropt* rop_u, const c3_c* arg_c);

View File

@ -96,7 +96,7 @@ _main_getopt(c3_i argc, c3_c** argv)
c3_w arg_w;
if ( u2_yes == _main_readw(optarg, 65536, &arg_w) ) {
u2_Host.ops_u.rap_s = arg_w;
u2_Host.ops_u.rop_u.por_s = arg_w;
} else return u2_no;
break;
}
@ -114,7 +114,9 @@ _main_getopt(c3_i argc, c3_c** argv)
break;
}
case 'r': {
u2_Host.ops_u.raf_c = strdup(optarg);
if ( u2_no == u2_raft_readopt(&u2_Host.ops_u.rop_u, optarg) ) {
return u2_no;
}
break;
}
case 'L': { u2_Host.ops_u.loh = u2_yes; break; }
@ -132,6 +134,11 @@ _main_getopt(c3_i argc, c3_c** argv)
}
}
if ( (u2_Host.ops_u.rop_u.por_s == 0 && u2_Host.ops_u.rop_u.nam_u != 0) ) {
fprintf(stderr, "The -r flag requires -l.\n");
return u2_no;
}
if ( u2_yes == u2_Host.ops_u.bat ) {
u2_Host.ops_u.dem = u2_yes;
u2_Host.ops_u.nuu = u2_yes;

64
v/raft.c Normal file
View File

@ -0,0 +1,64 @@
/* v/raft.c
**
** This file is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <uv.h>
#include "all.h"
#include "v/vere.h"
/* _raft_readname(): parse a raft host:port peer name.
*/
u2_bean
_raft_readname(u2_ropt* rop_u, const c3_c* str_c, c3_w siz_w)
{
u2_rnam* nam_u = malloc(sizeof(*nam_u));
c3_c* col_c;
c3_w por_w;
c3_w nam_w;
nam_u->str_c = malloc(siz_w + 1);
strncpy(nam_u->str_c, str_c, siz_w);
nam_u->str_c[siz_w] = '\0';
//fprintf(stderr, "raft: peer %s\n", nam_u->str_c);
if ( 0 == (col_c = strchr(nam_u->str_c, ':')) ) {
fprintf(stderr, "raft: invalid name %s\n", str_c);
return u2_no;
}
else {
nam_w = col_c - nam_u->str_c;
nam_u->nam_c = malloc(nam_w + 1);
strncpy(nam_u->nam_c, nam_u->str_c, nam_w);
nam_u->nam_c[nam_w] = '\0';
por_w = atol(col_c + 1);
if ( !(por_w > 0 && por_w < 65536) ) {
fprintf(stderr, "raft: invalid port '%s'\n", col_c + 1);
return u2_no;
}
else nam_u->por_s = por_w;
//fprintf(stderr, "raft: peer %s:%d\n", nam_u->nam_c, nam_u->por_s);
nam_u->nex_u = rop_u->nam_u;
rop_u->nam_u = nam_u;
return u2_yes;
}
}
u2_bean
u2_raft_readopt(u2_ropt* rop_u, const c3_c* arg_c)
{
c3_c* com_c;
while ( 0 != (com_c = strchr(arg_c, ',')) ) {
if ( u2_no == _raft_readname(rop_u, arg_c, com_c - arg_c) ) {
return u2_no;
} else arg_c = com_c + 1;
}
return _raft_readname(rop_u, arg_c, strlen(arg_c));
}