-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
405 lines (392 loc) · 12.7 KB
/
parser.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "solidbits.h"
static pthread_t parser;
static int idx = 0;
static struct job tjob;
static pthread_cond_t parser_job_ready;
static pthread_mutex_t parser_mutex;
bool terminate;
static int get_arg_count(char *str, size_t len)
{
off_t i, l = 0, c = 0;
len--; //without \n
DLOG("into %s(%s, %lu)", __func__, str, len);
for (i = 0; i < len; i++)
{
if (str[i] == 32) //space
{
l = i + 1; // the last space offset
if (i > 0 && str[i - 1] != 32)
{
c++;
}
}
}
if (i > l) //for last arg
c++;
DRETURN(c, 1);
}
static off_t next_space_at(char *str, size_t len)
{
off_t i;
DLOG("into %s(%s, %lu)", __func__, str, len);
for (i = 0; i < len; i++)
{
if (str[i] == 32) //space
{
DRETURN(i, 1);
}
}
DRETURN(-1, 1);
}
static void free_args(char **args, int count)
{
while (count--)
{
free(args[count]);
}
free(args);
}
static char **split_fill(char *str, size_t len, int count) //can't process multi-space, needs fix.
{
int i, o;
DLOG("into %s(%s, %lu, %d)", __func__, str, len, count);
o = next_space_at(str, len); //skip cmd name
str += o + 1;
len -= o + 1;
len--; //without \n
count--;
char **tmp = calloc(sizeof(char *), count);
for (i = 0; i < count; i++)
{
o = next_space_at(str, len);
if (o == -1)
o = len;
tmp[i] = calloc(sizeof(char), o + 1);
memcpy(tmp[i], str, o);
str += o + 1;
len -= o + 1;
DLOG("Get #%d = [%s]", i, tmp[i]);
}
DRETURN(tmp, 3);
}
static int parse_arg(int id)
{
int c, r;
long t;
char **args;
XXH64_hash_t hash;
DLOG("into %s()", __func__);
if (parsers[id].size < CMD_MININUM)
{
reply("ERR:CMD TOO SHORT\n", 18, &parsers[id].client);
DLOG("Command too short: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
c = get_arg_count(parsers[id].buf, parsers[id].size);
hash = XXH64(parsers[id].buf, 5, 0);
bzero(&tjob, sizeof(struct job)); //clear for cache hash
switch (hash)
{
case 13964470052823109277ULL:
if (c != 4)
{
reply("ERR:ARG TOO MANY OR FEW\n", 24, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
args = split_fill(parsers[id].buf, parsers[id].size, c);
tjob.cmd.setbit.offset = atoll(args[1]);
t = tjob.cmd.setbit.offset >> 3;
if (t < 0 && t > FILE_SIZE_LIMIT)
{
reply("ERR:OFFSET OUT OF RANGE\n", 24, &parsers[id].client);
free_args(args, c - 1);
DLOG("Offset out of range: [%s]", args[1]);
DRETURN(-1, 1);
}
tjob.cmd.setbit.value = atol(args[2]);
if (tjob.cmd.setbit.value & ~1)
{
reply("ERR:SETBIT(3) MUST BE 1 OR 0\n", 29, &parsers[id].client);
free_args(args, c - 1);
DLOG("setbit(3) mast be 1 or 0", args[1]);
DRETURN(-1, 1);
}
strcpy(tjob.cmd.setbit.key, args[0]);
if (prepare_file(&tjob.desc[0], tjob.cmd.setbit.key, 0))
{
reply("ERR:FILESYSTEM FAILED\n", 22, &parsers[id].client);
free_args(args, c - 1);
DLOG("Filesystem failed: [%s]", args[0]);
DRETURN(-1, 1);
}
tjob.name = SETBIT;
free_args(args, c - 1);
break;
case 4534844053247176213ULL:
if (c != 3)
{
reply("ERR:ARGS TOO MANY OR FEW\n", 25, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
args = split_fill(parsers[id].buf, parsers[id].size, c);
tjob.cmd.getbit.offset = atol(args[1]);
t = tjob.cmd.getbit.offset >> 3;
if (t < 0 && t > FILE_SIZE_LIMIT)
{
reply("ERR:OFFSET OUT OF RANGE\n", 24, &parsers[id].client);
free_args(args, c - 1);
DLOG("Offset out of range: [%s]", args[1]);
DRETURN(-1, 1);
}
strcpy(tjob.cmd.getbit.key, args[0]);
r = prepare_file(&tjob.desc[0], tjob.cmd.getbit.key, 1);
if (r == -2)
{
reply("0\n", 2, &parsers[id].client);
DLOG("File not exists: [%s]", args[0]);
free_args(args, c - 1);
DRETURN(-2, 1);
}
else if (r == -1)
{
reply("ERR:FILESYSTEM FAILED\n", 22, &parsers[id].client);
free_args(args, c - 1);
DLOG("Filesystem failed: [%s]", args[0]);
DRETURN(-1, 1);
}
tjob.name = GETBIT;
free_args(args, c - 1);
break;
case 2226304069708374537ULL:
if (c < 4 || c > 19)
{
reply("ERR:ARGS TOO MANY OR FEW\n", 25, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
args = split_fill(parsers[id].buf, parsers[id].size, c);
if ((args[0][0] == 'a' || args[0][0] == 'A') && !strcasecmp(args[0], "and"))
tjob.cmd.bitop.option = AND;
else if ((args[0][0] == 'o' || args[0][0] == 'O') && !strcasecmp(args[0], "or"))
tjob.cmd.bitop.option = OR;
else if ((args[0][0] == 'x' || args[0][0] == 'X') && !strcasecmp(args[0], "xor"))
tjob.cmd.bitop.option = XOR;
else if ((args[0][0] == 'n' || args[0][0] == 'N') && !strcasecmp(args[0], "not"))
tjob.cmd.bitop.option = NOT;
else
{
reply("ERR:UKNOW OPTION\n", 17, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
free_args(args, c - 1);
DRETURN(-1, 1);
}
if (tjob.cmd.bitop.option == NOT && c != 4)
{
reply("ERR:BITOP NOT JUST NEEDS ONE SOURCE\n", 36, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
free_args(args, c - 1);
DRETURN(-1, 1);
}
if (*args[1] == 5) //Ctrl+E mean countop
{
if (!strcasecmp(args[1] + 1, "COUNTOP"))
tjob.cmd.bitop.hook = COUNTOP;
else if (!strcasecmp(args[1] + 1, "GETOP"))
tjob.cmd.bitop.hook = GETOP;
else
{
reply("ERR:UKNOW OPTION\n", 17, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
free_args(args, c - 1);
DRETURN(-1, 1);
}
}
else
{
tjob.cmd.bitop.hook = NONE;
strcpy(tjob.cmd.bitop.key, args[1]);
if (prepare_file(&tjob.desc[0], tjob.cmd.bitop.key, 2))
{
reply("ERR:FILESYSTEM FAILED\n", 22, &parsers[id].client);
free_args(args, c - 1);
DLOG("Filesystem failed: [%s]", args[0]);
DRETURN(-1, 1);
}
}
DLOG("Set BITOP hook to %d", tjob.cmd.bitop.hook);
tjob.cmd.bitop.count = c - 3;
if (prepare_files((struct desc_table ***)&tjob.desc[1], &args[2], tjob.cmd.bitop.count))
{
reply("ERR:FILESYSTEM FAILED\n", 22, &parsers[id].client);
free_args(args, c - 1);
DLOG("Filesystem failed: [%s]", args[0]);
DRETURN(-1, 1);
}
tjob.name = BITOP;
free_args(args, c - 1);
break;
case 9951046379166068647ULL:
if (c < 2 || c > 4)
{
reply("ERR:ARGS TOO MANY OR FEW\n", 25, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
args = split_fill(parsers[id].buf, parsers[id].size, c);
strcpy(tjob.cmd.bitcount.key, args[0]);
r = prepare_file(&tjob.desc[0], tjob.cmd.getbit.key, 1);
if (r == -2)
{
reply("0\n", 2, &parsers[id].client);
DLOG("File not exists: [%s]", args[0]);
free_args(args, c - 1);
DRETURN(-2, 1);
}
else if (r == -1)
{
reply("ERR:FILESYSTEM FAILED\n", 22, &parsers[id].client);
DLOG("Filesystem failed: [%s]", args[0]);
free_args(args, c - 1);
DRETURN(-1, 1);
}
if (tjob.desc[0]->length == 0)
{
reply("0\n", 2, &parsers[id].client);
DLOG("File size is zero: [%s]", args[0]);
free_args(args, c - 1);
DRETURN(-2, 1);
}
if (c == 2)
{
tjob.cmd.bitcount.start = 0;
tjob.cmd.bitcount.end = tjob.desc[0]->length - 1;
}
else if (c == 3)
{
tjob.cmd.bitcount.start = atol(args[1]);
tjob.cmd.bitcount.end = tjob.desc[0]->length - 1;
}
else if (c == 4)
{
tjob.cmd.bitcount.start = atol(args[1]);
tjob.cmd.bitcount.end = atol(args[2]);
}
tjob.name = BITCOUNT;
free_args(args, c - 1);
break;
case 16475154490487774448ULL:
if (c < 3 && c > 5)
{
reply("ERR:ARGS TOO MANY OR FEW\n", 25, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
tjob.name = BITPOS;
args = split_fill(parsers[id].buf, parsers[id].size, c);
free_args(args, c - 1);
break;
case 16283480983475320022ULL:
if (c < 5)
{
reply("ERR:ARGS TOO FEW\n", 17, &parsers[id].client);
DLOG("Argument error: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
tjob.name = BITFIELD;
break;
default:
reply("ERR:UNKNOW CMD\n", 15, &parsers[id].client);
DLOG("Unknow command: [%s]", parsers[id].buf);
DRETURN(-1, 1);
}
tjob.created_at = parsers[id].created_at;
tjob.client = parsers[id].client;
DRETURN(0, 1);
}
void insert_parser_job(char *buf, size_t size, struct sockaddr_in *client)
{
int i;
DLOG("into %s()", __func__);
for (i = 0; i < PARSER_QUEUE_NUM; i++)
{
if (parsers[i].size == 0)
{
memcpy(parsers[i].buf, buf, size);
parsers[i].buf[size] = '\0';
parsers[i].size = size;
parsers[i].created_at = get_us();
memcpy(&parsers[i].client, client, sizeof(struct sockaddr_in));
DLOG("Accept job #%d request from %s:%d: [%d]%s", i, inet_ntoa(parsers[i].client.sin_addr),
ntohs(parsers[i].client.sin_port), parsers[i].size, parsers[i].buf);
pthread_cond_signal(&parser_job_ready); //send signal
return;
}
}
reply("ERR:QUEUE FULL\n", 15, &parsers[i].client);
DLOG("Parser queue is full, the request from %s:%d failed", inet_ntoa(parsers[i].client.sin_addr),
ntohs(parsers[i].client.sin_port));
}
static int get_parser_job(void)
{
DLOG("into %s()", __func__);
for (; idx < PARSER_QUEUE_NUM; idx++)
{
if (parsers[idx].size)
{
DLOG("Get job #%d", idx);
DRETURN(idx, 1);
}
}
idx = 0;
DRETURN(-1, 1);
}
static void del_parser_job(int id)
{
DLOG("into %s(%d)", __func__, id);
parsers[id].size = 0;
DLOG("Job #%d deleted, it took %ldus", id, get_us() - parsers[id].created_at);
}
static void *process_request(void *arg)
{
int id;
while (1)
{
id = get_parser_job();
if (id >= 0)
{
if (!parse_arg(id))
{
push_job(&tjob);
}
del_parser_job(id);
}
else
{
DLOG("No job, waitting...", id);
pthread_cond_wait(&parser_job_ready, &parser_mutex);
}
if (terminate)
{
DLOG("Parser thread #%d will be exit!", syscall(SYS_gettid));
pthread_exit(0);
}
}
pthread_exit(NULL);
}
void init_parser(void)
{
DLOG("into %s", __func__);
terminate = false;
pthread_mutex_init(&parser_mutex, NULL);
pthread_cond_init(&parser_job_ready, NULL);
parsers = calloc(sizeof(struct parser_queue), PARSER_QUEUE_NUM);
if (pthread_create(&parser, NULL, &process_request, NULL))
{
LOG("Create work failed: %s", strerror(errno));
LOG("Server terminal...");
exit(errno);
}
DLOG("Parser thread[%ld] created", parser);
}