]> git.saurik.com Git - redis.git/blob - deps/hiredis/example-ae.c
minor refactoring to networking.c adding a separated function to get a string represe...
[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) {
22 ((void)c);
23 printf("connected...\n");
24 }
25
26 void disconnectCallback(const redisAsyncContext *c, int status) {
27 if (status != REDIS_OK) {
28 printf("Error: %s\n", c->errstr);
29 }
30 printf("disconnected...\n");
31 aeStop(loop);
32 }
33
34 int main (int argc, char **argv) {
35 signal(SIGPIPE, SIG_IGN);
36
37 redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
38 if (c->err) {
39 /* Let *c leak for now... */
40 printf("Error: %s\n", c->errstr);
41 return 1;
42 }
43
44 loop = aeCreateEventLoop();
45 redisAeAttach(loop, c);
46 redisAsyncSetConnectCallback(c,connectCallback);
47 redisAsyncSetDisconnectCallback(c,disconnectCallback);
48 redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
49 redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
50 aeMain(loop);
51 return 0;
52 }
53