]> git.saurik.com Git - redis.git/blob - deps/hiredis/example-ae.c
Merge pull request #743 from Cofyc/fixtypo
[redis.git] / deps / hiredis / example-ae.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include "hiredis.h"
6 #include "async.h"
7 #include "adapters/ae.h"
8
9 /* Put event loop in the global scope, so it can be explicitly stopped */
10 static aeEventLoop *loop;
11
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
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
29 void disconnectCallback(const redisAsyncContext *c, int status) {
30 if (status != REDIS_OK) {
31 printf("Error: %s\n", c->errstr);
32 return;
33 }
34 printf("Disconnected...\n");
35 }
36
37 int main (int argc, char **argv) {
38 signal(SIGPIPE, SIG_IGN);
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 loop = aeCreateEventLoop();
48 redisAeAttach(loop, c);
49 redisAsyncSetConnectCallback(c,connectCallback);
50 redisAsyncSetDisconnectCallback(c,disconnectCallback);
51 redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
52 redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
53 aeMain(loop);
54 return 0;
55 }
56