-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvcachecmd.c
161 lines (129 loc) · 3.7 KB
/
kvcachecmd.c
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
#define DS_IMPL
#include "ds.h"
#define NTWK_IMPL
#include "ntwk.h"
#define ARGP_IMPL
#include "argp.h"
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
void usage_err(ArgParser a) {
fprintf(stderr, "Usage: %s <port> ", a.argv[0]);
s8_fprint(stderr, a.extra_usage);
fprintf(stderr, "\n");
fprintf(stderr, "Try '%s --help' for more information.\n", a.argv[0]);
exit(1);
}
int connect_to_localhost(u16 port) {
int sock = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons((short) port);
char addrstr[NI_MAXHOST + NI_MAXSERV + 1];
snprintf(addrstr, sizeof(addrstr), "127.0.0.1:%d", port);
inet_pton(AF_INET, addrstr, &addr.sin_addr);
if (connect(sock, (struct sockaddr*) &addr, sizeof(addr))) {
perror("connect(3) error");
exit(1);
}
return sock;
}
int main(int argc, char *argv[]) {
Arena scratch = new_arena(20 * GiB);
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0)) {
printf("Usage: %s <port> <command> <parameters>\n\n", argv[0]);
printf("--------------------------\n");
printf("Commands:\n\n");
printf("S <key> [value] -- Saves value to kvcached. If no value specified reads from stdin.\n");
printf("G <key> -- Prints value of key to stdout.\n");
printf("D <key> -- Deletes the key and unloads from memory.\n");
printf("C -- Clears cache.\n");
printf("T <key> <seconds> -- Set timeout in seconds when key will be automatically deleted.\n");
return 0;
}
ArgParser argp = {
.argc = argc, .argv = argv,
.i = 1,
.extra_usage = s8("<command> <parameters>"),
.usage_err = usage_err,
};
u8 command;
u16 port;
{
s8 p = get_next_arg(&argp);
s8 c = get_next_arg(&argp);
if (c.len != 1) argp.usage_err(argp);
port = atoi(p.buf);
command = c.buf[0];
}
int sock;
switch (command) {
case 'S': {
argp.extra_usage = s8("S <key> [value]");
s8 key = get_next_arg(&argp);
s8 val = {0};
if (argp.i == argp.argc) {
val = s8_copy(&scratch, val);
u8 ch = 0;
while (read(STDIN_FILENO, &ch, 1)) {
s8_modcat(&scratch, &val, (s8) { .len = 1, .buf = &ch, });
}
}
else val = get_next_arg(&argp);
no_more_args(argp);
sock = connect_to_localhost(port);
send_buf(sock, &command, sizeof(command));
send_s8(sock, key);
send_s8(sock, val);
} break;
case 'G': {
argp.extra_usage = s8("G <key>");
s8 key = get_next_arg(&argp);
no_more_args(argp);
sock = connect_to_localhost(port);
send_buf(sock, &command, sizeof(command));
send_s8(sock, key);
} break;
case 'D': {
argp.extra_usage = s8("D <key>");
s8 key = get_next_arg(&argp);
no_more_args(argp);
sock = connect_to_localhost(port);
send_buf(sock, &command, sizeof(command));
send_s8(sock, key);
} break;
case 'C': {
argp.extra_usage = s8("C");
no_more_args(argp);
sock = connect_to_localhost(port);
send_buf(sock, &command, sizeof(command));
} break;
case 'T': {
argp.extra_usage = s8("T <key> <seconds>");
s8 key = get_next_arg(&argp);
s8 time = get_next_arg(&argp);
no_more_args(argp);
sock = connect_to_localhost(port);
send_buf(sock, &command, sizeof(command));
send_s8(sock, key);
send_s8(sock, time);
} break;
default: {
fprintf(stderr, "Error: Unrecognized command.\n");
usage_err(argp);
}
}
int status = 0;
s8 msg = recv_s8(NULL, sock, &status);
if (status != 0) {
fprintf(stderr, "Error: Error when receiving server response.\n");
exit(1);
}
if (msg.len < 0) {
msg.len *= -1;
s8_fprint(stderr, msg);
}
else s8_print(msg);
return 0;
}