1 //
2 // Created by Дмитрий Бахвалов on 13.07.15.
3 // Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved.
4 //
5
6 #include <stdio.h>
7
8 #include <hiredis.h>
9 #include <async.h>
10 #include <adapters/macosx.h>
11
getCallback(redisAsyncContext * c,void * r,void * privdata)12 void getCallback(redisAsyncContext *c, void *r, void *privdata) {
13 redisReply *reply = r;
14 if (reply == NULL) return;
15 printf("argv[%s]: %s\n", (char*)privdata, reply->str);
16
17 /* Disconnect after receiving the reply to GET */
18 redisAsyncDisconnect(c);
19 }
20
connectCallback(const redisAsyncContext * c,int status)21 void connectCallback(const redisAsyncContext *c, int status) {
22 if (status != REDIS_OK) {
23 printf("Error: %s\n", c->errstr);
24 return;
25 }
26 printf("Connected...\n");
27 }
28
disconnectCallback(const redisAsyncContext * c,int status)29 void disconnectCallback(const redisAsyncContext *c, int status) {
30 if (status != REDIS_OK) {
31 printf("Error: %s\n", c->errstr);
32 return;
33 }
34 CFRunLoopStop(CFRunLoopGetCurrent());
35 printf("Disconnected...\n");
36 }
37
main(int argc,char ** argv)38 int main (int argc, char **argv) {
39 signal(SIGPIPE, SIG_IGN);
40
41 CFRunLoopRef loop = CFRunLoopGetCurrent();
42 if( !loop ) {
43 printf("Error: Cannot get current run loop\n");
44 return 1;
45 }
46
47 redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
48 if (c->err) {
49 /* Let *c leak for now... */
50 printf("Error: %s\n", c->errstr);
51 return 1;
52 }
53
54 redisMacOSAttach(c, loop);
55
56 redisAsyncSetConnectCallback(c,connectCallback);
57 redisAsyncSetDisconnectCallback(c,disconnectCallback);
58
59 redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
60 redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
61
62 CFRunLoopRun();
63
64 return 0;
65 }
66
67