1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 
6 #include <hiredis.h>
7 #include <async.h>
8 #include <adapters/ivykis.h>
9 
getCallback(redisAsyncContext * c,void * r,void * privdata)10 void getCallback(redisAsyncContext *c, void *r, void *privdata) {
11     redisReply *reply = r;
12     if (reply == NULL) return;
13     printf("argv[%s]: %s\n", (char*)privdata, reply->str);
14 
15     /* Disconnect after receiving the reply to GET */
16     redisAsyncDisconnect(c);
17 }
18 
connectCallback(const redisAsyncContext * c,int status)19 void connectCallback(const redisAsyncContext *c, int status) {
20     if (status != REDIS_OK) {
21         printf("Error: %s\n", c->errstr);
22         return;
23     }
24     printf("Connected...\n");
25 }
26 
disconnectCallback(const redisAsyncContext * c,int status)27 void disconnectCallback(const redisAsyncContext *c, int status) {
28     if (status != REDIS_OK) {
29         printf("Error: %s\n", c->errstr);
30         return;
31     }
32     printf("Disconnected...\n");
33 }
34 
main(int argc,char ** argv)35 int main (int argc, char **argv) {
36     signal(SIGPIPE, SIG_IGN);
37 
38     iv_init();
39 
40     redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
41     if (c->err) {
42         /* Let *c leak for now... */
43         printf("Error: %s\n", c->errstr);
44         return 1;
45     }
46 
47     redisIvykisAttach(c);
48     redisAsyncSetConnectCallback(c,connectCallback);
49     redisAsyncSetDisconnectCallback(c,disconnectCallback);
50     redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
51     redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
52 
53     iv_main();
54 
55     iv_deinit();
56 
57     return 0;
58 }
59