]> git.saurik.com Git - redis.git/blame - deps/hiredis/example-libev.c
Merge remote-tracking branch 'origin/unstable' into unstable
[redis.git] / deps / hiredis / example-libev.c
CommitLineData
24f753a8
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/libev.h"
8
9void getCallback(redisAsyncContext *c, void *r, void *privdata) {
10 redisReply *reply = r;
11 if (reply == NULL) return;
12 printf("argv[%s]: %s\n", (char*)privdata, reply->str);
13
14 /* Disconnect after receiving the reply to GET */
15 redisAsyncDisconnect(c);
16}
17
a1e97d69
PN
18void connectCallback(const redisAsyncContext *c) {
19 ((void)c);
20 printf("connected...\n");
21}
22
24f753a8
PN
23void disconnectCallback(const redisAsyncContext *c, int status) {
24 if (status != REDIS_OK) {
25 printf("Error: %s\n", c->errstr);
26 }
a1e97d69 27 printf("disconnected...\n");
24f753a8
PN
28}
29
30int main (int argc, char **argv) {
31 signal(SIGPIPE, SIG_IGN);
24f753a8
PN
32
33 redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
34 if (c->err) {
35 /* Let *c leak for now... */
36 printf("Error: %s\n", c->errstr);
37 return 1;
38 }
39
a1e97d69
PN
40 redisLibevAttach(EV_DEFAULT_ c);
41 redisAsyncSetConnectCallback(c,connectCallback);
24f753a8
PN
42 redisAsyncSetDisconnectCallback(c,disconnectCallback);
43 redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
44 redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
a1e97d69 45 ev_loop(EV_DEFAULT_ 0);
24f753a8
PN
46 return 0;
47}