This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
okdb.c
1068 lines (938 loc) · 31.2 KB
/
okdb.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include "sophia.h"
#define MAX_QUERY_PARAM_CNT 4
/* Config server */
static struct config {
int hostport;
int debug;
char *subhost;
int subport;
struct sockaddr_in subaddr;
int subfd;
char *sophia_path;
char *backup_path;
} config;
/* evbase for handling shutdown */
struct event *ev;
/* Not store data on backup */
int backup_active;
/* Udp buf */
static struct evbuffer *udp_inbuf;
static struct evbuffer *udp_outbuf;
/* Sophia params */
static void *env;
static void *db;
#define INFO(...) {\
if (config.debug == 1) printf("\n%s:%d: %s():\t", __FILE__, __LINE__, __FUNCTION__);\
if (config.debug == 1) printf(__VA_ARGS__);\
}
#define ERROR(...) {\
printf("\n%s:%d: %s():\t", __FILE__, __LINE__, __FUNCTION__);\
printf(__VA_ARGS__);\
}
/* Signal handler function (defined below). */
static void sighandler(int signal);
static char
*msg_ok =
"HTTP/1.1 200 OK\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: %d\r\n"
"Keep-Alive: timeout=20, max=200\r\n"
"Server: okdb/0.1.0\r\n"
"\r\n%s";
static char
*msg_notfound =
"HTTP/1.1 404 Not Found\r\n"
"Connection: close\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: %d\r\n"
"Server: okdb/0.1.0\r\n"
"\r\n%s";
static char
http_get[] = "GET /";
static char
quit[] = "quit\r\n";
static char
cmd_get[] = "get ";
static char
cmd_set[] = "set ";
static char
st_stored[] = "STORED\r\n";
static char
st_notstored[] = "NOT_STORED\r\n";
static char
st_end[] = "END\r\n";
static char
http_end[] = "\r\n\r\n";
static char
nl[] = "\r\n";
static char
ok[] = "OK\r\n";
static char
not_found[] = "Not Found\r\n";
/* A struct to hold the query string parameter values. */
struct query_param {
char *key;
char *val;
};
/* A struct to hold commands from query. */
struct command {
char *type;
char *prefix;
char *command;
char *key;
char *value;
int limit;
};
/* Str must have at least len bytes to copy
Return NULL on error malloc
*/
static char *
make_str(const char *str, size_t len)
{
char *newstr;
newstr = malloc(len + 1);
if(newstr == NULL) {
perror("ERROR ALOCATE\n");
return NULL;
}
memcpy(newstr, str, len);
newstr[len] = 0;
return newstr;
}
/* Must be positive */
static int
get_int_len (int value)
{
int l=1;
while(value>9){ l++; value/=10; }
return l;
}
static void
close_connection(struct bufferevent *bev, void *ctx, const char *err)
{
if (err) perror(err);
if (ctx) free(ctx);
if (bev) bufferevent_free(bev);
}
static void
print_error()
{
int size;
char *error = sp_getstring(env, "sophia.error", &size);
ERROR("%s",error);
free(error);
}
/*
stealed from here: https://github.com/jacketizer/libyuarel/blob/master/yuarel.c#L160
*/
static int
parse_query(char *query, char delimiter, struct query_param *params, int max_params)
{
int i = 0;
if (query == NULL || *query == '\0')
return -1;
params[i++].key = query;
while (i < max_params && NULL != (query = strchr(query, delimiter))) {
*query = '\0';
params[i].key = ++query;
params[i].val = NULL;
/* Go back and split previous param */
if (i > 0) {
if ((params[i - 1].val = strchr(params[i - 1].key, '=')) != NULL)
*(params[i - 1].val)++ = '\0';
}
i++;
}
/* Go back and split last param */
if ((params[i - 1].val = strchr(params[i - 1].key, '=')) != NULL)
*(params[i - 1].val)++ = '\0';
return i;
}
static void
http_out(struct evbuffer *output, const char *format, const char *msg, int size, int prepend)
{
//INFO("msg:%s",msg);
if (!msg) return;
int resp_size = (strlen(format) -2*2/* % exclude */) + strlen(msg) + get_int_len(size)+1;
//INFO("http_get_response size:%d", resp_size);
char *resp = malloc(resp_size);
if (!resp) {
perror("Out of memory");
return;
}
resp[resp_size-1] = '\0';
snprintf(resp, resp_size, format, size, msg);
//INFO("http_get_response:'%s' size:%d",resp, resp_size);
if (prepend == 1) {
INFO("prepend [%s]",resp);
evbuffer_prepend(output, resp, resp_size-1);
}
else {
evbuffer_add(output, resp, resp_size-1);
}
free(resp);
}
static void
memcache_out(struct evbuffer *output, const char *key,const char *value, int size)
{
if (!value) {
return;
}
char *format = "VALUE %s 0 %d\r\n%s\r\n";
int resp_size = (strlen(format) -3*2) + strlen(key) + size + get_int_len(size) + 1;
char *resp = malloc(resp_size);
if (!resp) {
perror("Out of memory");
return;
}
resp[resp_size-1] = '\0';
snprintf(resp, resp_size,format,key,size,value);
INFO("resp:'%.*s' resp_size:%d\n", resp_size,resp, resp_size);
evbuffer_add(output, resp, resp_size-1);//remove \0
free(resp);
return;
}
static void
get_val(struct evbuffer *output, const char *key, int is_http)
{
char *query = strstr(key,"?");
if (query) {
/* Parse query like: ?type=cursor&limit=2 */
query++;
int p;
struct query_param param[MAX_QUERY_PARAM_CNT] = {{0}};
p = parse_query(query,'&', param, MAX_QUERY_PARAM_CNT);
struct command cmd = {0};
while (p-- > 0) {
INFO("\t%s: %s\n", param[p].key, param[p].val);
if (!strcmp("type",param[p].key)) cmd.type = param[p].val;
if (!strcmp("prefix",param[p].key)) cmd.prefix = param[p].val;
if (!strcmp("limit",param[p].key)) {
cmd.limit = (int) strtol(param[p].val,NULL,10);
}
if (!strcmp("command",param[p].key)) cmd.command = param[p].val;
if (!strcmp("key",param[p].key)) cmd.key = param[p].val;
if (!strcmp("value",param[p].key)) cmd.value = param[p].val;
}
if (!cmd.type) {
/* Unknown type */
if (is_http == 0) evbuffer_add(output, st_end, sizeof(st_end)-1);
return;
}
INFO("processing");
if (!strcmp("cursor",cmd.type)) {
void *o = sp_document(db);
char *prefix = NULL;
//int res = sp_setstring(o, "order", ">=", 0);
if (cmd.prefix){
/* if prefix not set - will scan all db */
sp_setstring(o, "order", ">=", 0);
prefix = make_str(cmd.prefix,strlen(cmd.prefix));
sp_setstring(o, "prefix", prefix, strlen(prefix));
//free(prefix);
}
else {
sp_setstring(o, "order", ">=", 0);
}
void *c = sp_cursor(env);
/* Default limit - 100 */
int limit = (cmd.limit==0)?100:cmd.limit;
int value_size;
int key_size;
while( (o = sp_get(c, o)) ) {
if (!limit) {
sp_destroy(o);
break;
}
char *ptr_key = (char*)sp_getstring(o, "key", &key_size);
char *new_key = make_str(ptr_key,key_size);
char *ptr_val = (char*)sp_getstring(o, "value", &value_size);
char *value = make_str(ptr_val,value_size);
if (is_http == 0) memcache_out(output,new_key,value,value_size);
INFO("key:%s\tval:%s",new_key,value);
free(new_key);
free(value);
limit--;
}
sp_destroy(c);
if (prefix) free(prefix);
if (is_http == 0) evbuffer_add(output, st_end, sizeof(st_end)-1);
INFO("end cursor\n");
}
if (!strcmp("sophia",cmd.type) && cmd.command && cmd.key) {
/* Process sophia command */
INFO("Process %s sophia command",cmd.command);
if (!strcmp("getstring",cmd.command)) {
int value_size;
char *new_key = make_str(cmd.key,strlen(cmd.key));
if (new_key) {
char *ptr_key = (char*) sp_getstring(env,new_key,&value_size);
if (ptr_key) {
if (is_http == 0) memcache_out(output,new_key,ptr_key,value_size);
free(ptr_key);
}
free(new_key);
}
}
if (!strcmp("getint",cmd.command)) {
char *new_key = make_str(cmd.key,strlen(cmd.key));
if (new_key) {
int64_t val = sp_getint(env,new_key);
char buf[128];
memset(buf, 0x00, 128);
sprintf(buf, "%lld", val);
if (is_http == 0) memcache_out(output,new_key,buf,strlen(buf));
free(new_key);
}
}
if (!strcmp("setint",cmd.command)) {
/* ?type=sophia&command=setint&key=backup.run&value=0 */
char *new_key = make_str(cmd.key,strlen(cmd.key));
if (new_key) {
if (!strcmp("backup.run",new_key)) {
/* Make db readonly on backup */
backup_active = 1;
}
int64_t val = cmd.value?strtol(cmd.value,NULL,10):0;
int result = sp_setint(env,new_key,val);
if (result == -1) {
/* Return to write state on error */
backup_active = 0;
print_error();
}
char buf[128];
memset(buf, 0x00, 128);
sprintf(buf, "%d", result);
if (is_http == 0) memcache_out(output,new_key,buf,strlen(buf));
free(new_key);
}
}
if (!strcmp("setstring",cmd.command)) {
char *new_key = make_str(cmd.key,strlen(cmd.key));
if (new_key && cmd.value) {
char *val = make_str(cmd.value,strlen(cmd.value));
int result = sp_setstring(env,new_key,val,strlen(val));
if (result == -1) print_error();
char buf[128];
memset(buf, 0x00, 128);
sprintf(buf, "%d", result);
if (is_http == 0) memcache_out(output,new_key,buf,strlen(buf));
free(new_key);
free(val);
}
}
if (is_http == 0) evbuffer_add(output, st_end, sizeof(st_end)-1);
}
return;
}
/* get value from sophia */
int value_size;
void *o = sp_document(db);
sp_setstring(o, "key", &key[0], strlen(key));
o = sp_get(db, o);
if (o) {
char *ptr = sp_getstring(o, "value", &value_size);
char *value = make_str((char*)ptr,value_size);
INFO("Val:%s",value);
sp_destroy(o);
if (is_http == 0) {
memcache_out(output,key,value,value_size);
}
else {
http_out(output,msg_ok,value,value_size,0);
}
INFO("val:'%.*s', size:%d\n", value_size,value,value_size);
free(value);
}
else {
if (is_http == 1){
http_out(output,msg_notfound,not_found,strlen(not_found),0);
}
INFO("key:'%s' not found\n",key);
}
return;
}
static void
read_buf(struct bufferevent *bev, void *ctx, struct evbuffer **input_ptr, struct evbuffer **output_ptr) {
struct evbuffer *input = *input_ptr;
struct evbuffer *output = *output_ptr;
CONTINUE_LOOP:;
/* Try copy from evbuffer to buffer */
size_t len = evbuffer_get_length(input);
char *data = malloc(len+1);
data[len] = '\0';
/* Close connection if we dont may allocate buffer */
if (!data) {
INFO("close_connection ");
close_connection(bev, ctx, "Out of memory");
return;
}
evbuffer_copyout(input, data, len);
INFO("buf data:[%s]\n",data);
/* Process quit (maybe anywhere in buffer) */
if (strstr(data,quit)) {
INFO("quit catched!\n");
evbuffer_drain(input, len);
free(data);
close_connection(bev, ctx, NULL);
return;
}
/* Check for set command */
//TODO macros?
char *cmdset_start = strstr(data,cmd_set);
if (cmdset_start) {
INFO("set catched!");
int cmd_pos = (int)(cmdset_start - data);
if (cmd_pos>0) {
/* user may send some trash before command like:'trash GET /' - remove it */
INFO("Drained:%d bytes",cmd_pos);
evbuffer_drain(input, cmd_pos);
free(data);
goto CONTINUE_LOOP;
}
/* Find first nl in command */
char *cmdset_end = strstr(data,nl);
if (!cmdset_end) {
/* no end in buffer - wait it in next packet */
free(data);
return;
}
int cmd_len = (int)(cmdset_end - data) + (sizeof(nl)-1);
/* extract key from buffer */
data+=sizeof(cmd_set)-1;
/* Find ' ' or /r/n in buffer */
size_t key_end = strcspn(data, " \r\n");
char *key = make_str(data, key_end);
/* Move pointer back */
data-=sizeof(cmd_set)-1;
if (!key) {
/* Error malloc */
free(data);
close_connection(bev, ctx, "Out of memory in key");
return;
}
INFO("set key:'%s'\n", key);
/* Check for noreply */
char *noreply = strstr(data,"noreply");
/* Lets find value size */
int minus = sizeof(nl)-1;
if (noreply) minus+=strlen("noreply")+1;
char *command = make_str(data,cmd_len-minus);
INFO("command:'%s'\n",command);
char *val_sizestr = strrchr(command, ' ');
val_sizestr+=1;//' '
int val_size = (int) strtol(val_sizestr,NULL,10);
INFO("val_size:%d\n",val_size);
free(command);
/* We know value size, check buffer */
int total_size = cmd_len+val_size+(sizeof(nl)-1);
if ((int)len<total_size) {
/* buf not arrived, wait */
free(data);
free(key);
return;
}
/* copy value from buffer */
data+=cmd_len;//shift
char *value = make_str(data,val_size);
INFO("value:'%s'\n",value);
/* Processing key/value */
if (backup_active > 0) {
/* Backup running - drain buffer and continue until it finished */
/* Check is backup running? and update status */
backup_active = (int) sp_getint(env,"backup.active");
}
if (backup_active > 0) {
/* Skip this command before backup completed */
evbuffer_add(output, st_notstored, strlen(st_notstored));
}
else {
void *o = sp_document(db);
sp_setstring(o, "key", &key[0], strlen(key));
sp_setstring(o, "value", &value[0], val_size);
int res = sp_set(db, o);
if (!noreply) {
if (res == 0) {
if (config.subfd > 0) {
/* Send message to subscriber */
char *format = "set %s 0 0 %d noreply\r\n%s\r\n";
int msg_size = (strlen(format) -3*2) + strlen(key) + val_size + get_int_len(val_size) + 1;
char *msg = malloc(msg_size);
msg[msg_size-1] = '\0';
snprintf(msg, msg_size,format,key,val_size,value);
INFO("msg:'%.*s'\n", msg_size,msg);
sendto(config.subfd, msg, msg_size-1, 0,
(struct sockaddr*)&config.subaddr, sizeof config.subaddr);
free(msg);
}
evbuffer_add(output, st_stored, strlen(st_stored));
}
else {
evbuffer_add(output, st_notstored, strlen(st_notstored));
}
}
}
free(value);
free(key);
data-=cmd_len;//unshift
/* free processed command buffer */
evbuffer_drain(input, total_size);
free(data);
/* client may send multiple commands in one buffer so continue processing */
goto CONTINUE_LOOP;
}
/* Check for get command */
char *cmdget_start = strstr(data,cmd_get);
if (cmdget_start) {
INFO("get catched!");
int cmd_pos = (int)(cmdget_start - data);
if (cmd_pos>0) {
/* user may send some trash before command like:'trash GET /' - remove it */
INFO("Drained:%d bytes",cmd_pos);
evbuffer_drain(input, cmd_pos);
free(data);
goto CONTINUE_LOOP;
}
/* Find end command */
char *cmdget_end = strstr(data,nl);
if (!cmdget_end) {
/* no end in buffer - wait it in next packet */
free(data);
return;
}
int cmd_len = (int)(cmdget_end - data) + (sizeof(nl)-1);
/* free processed command buffer */
evbuffer_drain(input, cmd_len);
/* extract key from buffer */
data+=sizeof(cmd_get)-1;
/* Find /r/n in buffer */
size_t key_end = strcspn(data, "\r\n");
char *key = make_str(data, key_end);
/* Move pointer back */
data-=sizeof(cmd_get)-1;
if (!key) {
/* Error malloc */
free(data);
close_connection(bev, ctx, "Out of memory in key");
return;
}
INFO("key:'%s'\n", key);
/* Processing keys */
char *token;
token = strtok(key, " ");
while(token) {
get_val(output, token, 0);
token = strtok(NULL, " ");
}
evbuffer_add(output, st_end, sizeof(st_end)-1);
free(key);
free(data);
/* client may send multiple commands in one buffer so continue processing */
goto CONTINUE_LOOP;
}
/* Check for http_get command */
char *httpget_start = strstr(data,http_get);
if (httpget_start) {
INFO("http GET / catched!");
int cmd_pos = (int)(httpget_start - data);
if (cmd_pos>0) {
/* user may send some trash before command like:'trash GET /' - remove it */
INFO("Drained:%d bytes",cmd_pos);
evbuffer_drain(input, cmd_pos);
free(data);
goto CONTINUE_LOOP;
}
/* Find end command */
char *httpget_end = strstr(data,http_end);
if (!httpget_end) {
/* no end in buffer - wait it in next packet */
free(data);
return;
}
int cmd_len = (int)(httpget_end - data) + (sizeof(http_end)-1);
INFO("cmd_len:%d\n",cmd_len);
/* free processed command buffer */
evbuffer_drain(input, cmd_len);
/* extract key from buffer */
data+=sizeof(http_get)-1;
/* Find end */
size_t key_end = strcspn(data, " \r\n");
char *key = make_str(data, key_end);
/* Move pointer back */
data-=sizeof(http_get)-1;
if (!key) {
/* Error malloc */
free(data);
close_connection(bev, ctx, "Out of memory in key");
return;
}
INFO("key:'%s'\n", key);
if (!strcmp("",key)) {
INFO("Send 200 resp");
http_out(output,msg_ok,ok,strlen(ok),0);
}
else {
/* We have key in request */
if (!strcmp("favicon.ico",key)) {
INFO("request:favicon.ico");
}
/* Processing keys */
struct evbuffer *tmp = evbuffer_new();
char *token;
token = strtok(key, "%20");
while(token) {
get_val(tmp, token, 0);
token = strtok(NULL, "%20");
}
evbuffer_add(tmp, st_end, sizeof(st_end)-1);
http_out(tmp,msg_ok,"",evbuffer_get_length(tmp),1);
//evbuffer_prepend_buffer(output,tmp);
evbuffer_add_buffer(output,tmp);
evbuffer_free(tmp);
}
free(key);
free(data);
/* client may send multiple commands in one buffer so continue processing */
goto CONTINUE_LOOP;
}
free(data);
}
static void
read_cb(struct bufferevent *bev, void *ctx)
{
/* This callback is invoked when there is data to read on bev. */
struct evbuffer *input = bufferevent_get_input(bev);
struct evbuffer *output = bufferevent_get_output(bev);
read_buf(bev, ctx, &input, &output);
if (evbuffer_get_length(output)>0) {
/* command catched - send response */
if (bufferevent_write_buffer(bev, output)) {
close_connection(bev, ctx, "Error sending data to client");
return;
}
}
}
static void
event_cb(struct bufferevent *bev, short events, void *ctx)
{
if (events & BEV_EVENT_ERROR)
perror("Error from bufferevent");
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
bufferevent_free(bev);
}
}
static void
accept_conn_cb(struct evconnlistener *listener,
evutil_socket_t fd, struct sockaddr *address, int socklen,
void *ctx)
{
/* We got a new connection! Set up a bufferevent for it. */
struct event_base *base = evconnlistener_get_base(listener);
struct bufferevent *bev = bufferevent_socket_new(
base, fd, BEV_OPT_CLOSE_ON_FREE);
/* Minimal command len quit/r/n */
bufferevent_setwatermark(bev, EV_READ, 6, 0);
bufferevent_setcb(bev, read_cb, NULL, event_cb, NULL);
bufferevent_enable(bev, EV_READ|EV_WRITE);
}
static void
accept_error_cb(struct evconnlistener *listener, void *ctx)
{
struct event_base *base = evconnlistener_get_base(listener);
int err = EVUTIL_SOCKET_ERROR();
fprintf(stderr, "Got an error %d (%s) on the listener. "
"Shutting down.\n", err, evutil_socket_error_to_string(err));
event_base_loopexit(base, NULL);
}
static void
udp_read_cb(int fd, short event, void *arg) {
char buf[4096];
int len;
unsigned int size = sizeof(struct sockaddr);
struct sockaddr_in client_addr;
struct evbuffer *b = (struct evbuffer*) arg;
INFO("UDP Connection\n");
memset(buf, 0, sizeof(buf));
len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_addr, &size);
if (len == -1) {
perror("recvfrom()");
} else if (len == 0) {
INFO("Connection Closed\n");
/* Drain input buf on close */
if (evbuffer_get_length(udp_inbuf)>0) {
evbuffer_drain(udp_inbuf, evbuffer_get_length(udp_inbuf));
}
} else {
int res = evbuffer_add(udp_inbuf, buf, len);
INFO("Add res:[%d]",res);
INFO("Read: len [%d] - content [%s]\n", len, buf);
read_buf(NULL, NULL, &udp_inbuf, &udp_outbuf);
size_t len_out = evbuffer_get_length(udp_outbuf);
INFO("Buf out:[%d]",(int)len_out);
if (len_out>0) {
char *data = malloc(len_out+1);
if (!data) return;//oom
data[len_out] = '\0';
/* command catched - send response */
evbuffer_remove(udp_outbuf, data, len_out);
sendto(fd, data, len_out, 0, (struct sockaddr *)&client_addr, size);
free(data);
}
}
}
static int
bind_socket() {
int sock_fd;
int flag = 1;
struct sockaddr_in sin;
/* Create endpoint */
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket()");
return -1;
}
/* Set socket option */
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) < 0) {
perror("setsockopt()");
return 1;
}
/* Set IP, port */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
/* Listen on 0.0.0.0 */
sin.sin_addr.s_addr = htonl(0);
/* Listen on the given port. */
sin.sin_port = htons(config.hostport);
//sin.sin_addr.s_addr = inet_addr("127.0.0.1");
//sin.sin_port = htons(10001);
/* Bind */
if (bind(sock_fd, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) {
perror("udp bind()");
return -1;
}
/* Init one event and add to active events */
//udp_inbuf = evbuffer_new();
event_set(&ev, sock_fd, EV_READ | EV_PERSIST, &udp_read_cb, NULL);
if (event_add(&ev, NULL) == -1) {
printf("event_add() failed\n");
}
return 0;
}
static int
run_server()
{
struct evconnlistener *listener;
struct sockaddr_in sin;
struct event_base *base;
//struct event ev;
if (config.hostport<=0 || config.hostport>65535) {
ERROR("Invalid port");
return 1;
}
else {
printf("Starting on port:%d\n",config.hostport);
}
/* Set signal handlers */
sigset_t sigset;
sigemptyset(&sigset);
struct sigaction siginfo = {
.sa_handler = sighandler,
.sa_mask = sigset,
.sa_flags = SA_RESTART,
};
sigaction(SIGINT, &siginfo, NULL);
sigaction(SIGTERM, &siginfo, NULL);
if ((base = event_init()) == NULL) {
printf("event_init() failed\n");
return -1;
}
/* Clear the sockaddr before using it, in case there are extra
* platform-specific fields that can mess us up. */
memset(&sin, 0, sizeof(sin));
/* This is an INET address */
sin.sin_family = AF_INET;
/* Listen on 0.0.0.0 */
sin.sin_addr.s_addr = htonl(0);
/* Listen on the given port. */
sin.sin_port = htons(config.hostport);
listener = evconnlistener_new_bind(base, accept_conn_cb, NULL,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1,
(struct sockaddr*)&sin, sizeof(sin));
if (!listener) {
perror("Couldn't create listener");
return 1;
}
printf("%s",ok);
evconnlistener_set_error_cb(listener, accept_error_cb);
/* Udp server listener */
/* Bind socket */
if (bind_socket() != 0) {
ERROR("bind_socket() failed\n");
return -1;
}
/* End of udp server */
/* Subscriber */
if (config.subport > 0 && config.subhost!=NULL) {
int flag = 1;
/* Create endpoint */
if ((config.subfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Subscriber");
return -1;
}
/* Set socket option */
if (setsockopt(config.subfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) < 0) {
perror("Subscriber setsockopt()");
return 1;
}
/* Set IP, port */
memset(&config.subaddr, 0, sizeof(config.subaddr));
config.subaddr.sin_family = AF_INET;
config.subaddr.sin_addr.s_addr = inet_addr(config.subhost);
config.subaddr.sin_port = htons(config.subport);
INFO("Subscriber host[%s] port[%d] fd[%d]",config.subhost,config.subport,config.subfd);
}
//event_base_dispatch(base);
event_dispatch();
return 0;
}
static int
init() {
/* open or create environment and database */
env = sp_env();
sp_setstring(env, "sophia.path", config.sophia_path, 0);
sp_setstring(env, "backup.path", config.backup_path, 0);
/* Database name - hardcoded now */
sp_setstring(env, "db", "db", 0);
//sp_setstring(env, "db.db.scheme", "key", 0);
//sp_setstring(env, "db.db.scheme.key", "string,key(0)", 0);
//sp_setstring(env, "db.db.scheme", "value", 0);
//sp_setstring(env, "db.db.scheme.value", "string", 0);
/* set mmap mode */
sp_setint(env, "db.db.mmap", 1);
sp_setint(env,"db.db.compaction.cache",419430400);//400Mb
db = sp_getobject(env, "db.db");
return sp_open(env);
}
static void
parseOptions(int argc, char **argv) {
/* Simple params - only port defined like okdb 11213 */
if (argc == 2) {
config.hostport = atoi(argv[1]);
return;
}
int i;
for (i = 1; i < argc; i++) {
int lastarg = i==argc-1;
if (!strcmp(argv[i],"-p") && !lastarg) {
config.hostport = atoi(argv[i+1]);
i++;
} else if (!strcmp(argv[i],"-sophia.path") && !lastarg) {
config.sophia_path = make_str(argv[i+1],strlen(argv[i+1]));
i++;
} else if (!strcmp(argv[i],"-backup.path") && !lastarg) {
config.backup_path = make_str(argv[i+1],strlen(argv[i+1]));
i++;
} else if (!strcmp(argv[i],"-subhost") && !lastarg) {
config.subhost = make_str(argv[i+1],strlen(argv[i+1]));
i++;
} else if (!strcmp(argv[i],"-subport") && !lastarg) {
config.subport = atoi(argv[i+1]);
i++;
} else if (!strcmp(argv[i],"-D")) {
config.debug = 1;
} else {
printf("Wrong option '%s' or option argument missing\n\n",argv[i]);
printf("Usage: okdb [-p <port>] [-sophia.path <sophia.path>] [-backup.path <backup.path>] [-D] \n\n");
//printf(" -h <hostname> Server hostname (default 127.0.0.1)\n");
printf(" -p <port> Server port (default 11213)\n");
printf(" -sophia.path <sophia.path> Sophia path (default sophia)\n");
printf(" -backup.path <backup.path> Sophia backup path (default sophia)\n");
printf(" -subhost <ip address> Replication address(default NULL)\n");
printf(" -subport <port> Replication port(default 0)\n");
printf(" -D Debug mode. more verbose.\n");
exit(1);
}
}
}
/**
* Kill the server. This function can be called from another thread to kill
* the server, causing runServer() to return.
*/
static void
killServer(void) {
fprintf(stdout, "Stopping socket listener event loop.\n");
event_loopexit(NULL);
//event_free(ev);
//if (event_base_loopexit(base, NULL)) {
// perror("Error shutting down server");
//}
/*if (config.subfd>0) {
if (event_loopexit(NULL)) {