]> git.saurik.com Git - redis.git/blame - deps/hiredis/example-ae.c
redis-check-dump now understands dumps produced by Redis 2.6
[redis.git] / deps / hiredis / example-ae.c
CommitLineData
a1e97d69
PN
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 */
10static aeEventLoop *loop;
11
12void 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
b66e5add 21void 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");
a1e97d69
PN
27}
28
29void disconnectCallback(const redisAsyncContext *c, int status) {
30 if (status != REDIS_OK) {
31 printf("Error: %s\n", c->errstr);
b66e5add 32 return;
a1e97d69 33 }
b66e5add 34 printf("Disconnected...\n");
a1e97d69
PN
35}
36
37int 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