-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
172 lines (151 loc) · 3.25 KB
/
client.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
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include "kvstore.h"
#define HOST "127.0.0.1"
#define PORT "9899"
#define DATASET 100
int validate_command(char*);
void user_test();
void test_insert();
void test_delete();
void test_search();
int main(int argc, char *argv[])
{
char mode[5];
if(argc == 2 && !strcmp(argv[1],"usertest")){
user_test();
}
else if(argc == 2 && !strcmp(argv[1],"autotest")){
printf("choose mode for test ...\n");
test: printf("test insert: -i\n");
printf("test delete: -d\n");
printf("test search: -s\n");
printf("choose: ");
fgets(mode, 100 ,stdin);
mode[strcspn(mode, "\n")] = 0;
if(!strcmp(mode, "-i")){
test_insert();
}
else if(!strcmp(mode, "-d")){
test_delete();
}
else if(!strcmp(mode, "-s")){
test_search();
}
else{
printf("invalid command\n");
goto test;
}
}
else{
printf("using `./client usertest` or `./client autotest`\n");
exit(0);
}
return 0;
}
int validate_command(char *str){
if(!strlen(str)) return 0;
char *new = (char *)malloc(BUF_LEN);
strcpy(new, str);
char delim[] = " ";
char *ptr = strtok(new, delim);
char p1[30];
int n = 0;
strcpy(p1, ptr);
if (strcmp(p1, "set") != 0 && strcmp(p1, "get") != 0 && strcmp(p1, "del") != 0)
{
printf("(error) ERR unknown command `%s`, with args beginning with:", p1);
while (ptr != NULL)
{
printf(" '%s',", ptr);
ptr = strtok(NULL, delim);
}
printf("\n");
return 0;
}
else
{
while (ptr != NULL)
{
ptr = strtok(NULL, delim);
n++;
};
if (!strcmp(p1, "set") && n != 3)
{
printf("(error) ERR wrong number of arguments for 'set' command\n");
return 0;
}
if (!strcmp(p1, "get") && n != 2)
{
printf("(error) ERR wrong number of arguments for 'get' command\n");
return 0;
}
if (!strcmp(p1, "del") && n != 2)
{
printf("(error) ERR wrong number of arguments for 'del' command\n");
return 0;
}
}
return 1;
}
void user_test(){
int sockfd;
char buf[BUF_LEN];
if((sockfd = connect_kvstore(HOST, PORT)) < 0){
printf("(error) failed to connect kvstore\n");
}
while(1){
printf("%s:%s> ", HOST, PORT);
fgets(buf, BUF_LEN ,stdin);
buf[strcspn(buf, "\n")] = 0;
if(!validate_command(buf)){
continue;
}
if(!kvstore_execute(sockfd, buf)){
exit(1);
}
printf("%s\n",buf);
}
close(sockfd);
}
void test_insert(){
char buf[100];
int sockfd;
if((sockfd = connect_kvstore(HOST, PORT)) < 0){
printf("(error) failed to connect kvstore\n");
}
for(int i = 0; i < DATASET; i ++ ){
sprintf(buf, "set %05d_key %05d_value", i, i);
printf("request: %s\n", buf);
if(kvstore_execute(sockfd, buf)){
printf("response: %s\n",buf);
}
}
}
void test_delete(){
char buf[100];
int sockfd;
if((sockfd = connect_kvstore(HOST, PORT)) < 0){
printf("(error) failed to connect kvstore\n");
}
for(int i = 0; i < DATASET; i ++ ){
sprintf(buf, "del %05d_key", i);
printf("request: %s\n", buf);
if(kvstore_execute(sockfd, buf)){
printf("response: %s\n",buf);
}
}
}
void test_search(){
char buf[100];
int sockfd;
if((sockfd = connect_kvstore(HOST, PORT)) < 0){
printf("(error) failed to connect kvstore\n");
}
for(int i = 0; i < DATASET; i ++ ){
sprintf(buf, "get %05d_key", i);
printf("request: %s\n", buf);
if(kvstore_execute(sockfd, buf)){
printf("response: %s\n",buf);
}
}
}