summaryrefslogtreecommitdiff
path: root/libev-example.c
diff options
context:
space:
mode:
authorPieter Noordhuis <pcnoordhuis@gmail.com>2010-11-01 10:17:28 +0100
committerPieter Noordhuis <pcnoordhuis@gmail.com>2010-11-01 10:26:23 +0100
commitd5fc7d8c53db80fc0f5cbe59c3fe371c56c2b501 (patch)
tree462d35219c0f8458f46235a9c166cb6611475c59 /libev-example.c
parentac13c9f09572227882e02a025373bf3147f49863 (diff)
Update libev and libevent examples to work with async.h
Diffstat (limited to 'libev-example.c')
-rw-r--r--libev-example.c29
1 files changed, 18 insertions, 11 deletions
diff --git a/libev-example.c b/libev-example.c
index 49945bf..2d0bc8b 100644
--- a/libev-example.c
+++ b/libev-example.c
@@ -2,30 +2,37 @@
#include <stdlib.h>
#include <string.h>
#include <hiredis/libev.h>
+#include <async.h>
#include <signal.h>
-void getCallback(redisContext *c, redisReply *reply, void *privdata) {
- if (reply == NULL) return; /* Error */
- printf("argv[%s]: %s\n", (char*)privdata, reply->reply);
+void getCallback(redisAsyncContext *c, redisReply *reply, void *privdata) {
+ printf("argv[%s]: %s\n", (char*)privdata, reply->str);
/* Disconnect after receiving the reply to GET */
- redisDisconnect(c);
+ redisAsyncDisconnect(c);
}
-void errorCallback(const redisContext *c) {
- printf("Error: %s\n", c->error);
+void disconnectCallback(const redisAsyncContext *c, int status) {
+ if (status != REDIS_OK) {
+ printf("Error: %s\n", c->error);
+ }
}
int main (int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
struct ev_loop *loop = ev_default_loop(0);
- redisContext *c = libevRedisConnect(loop, errorCallback, "127.0.0.1", 6379);
- if (c == NULL) return 1;
+ redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
+ if (c->error != NULL) {
+ /* Let *c leak for now... */
+ printf("Error: %s\n", c->error);
+ return 1;
+ }
- redisCommand(c, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
- redisCommandWithCallback(c, getCallback, (char*)"end-1", "GET key");
+ redisLibevAttach(c,loop);
+ redisAsyncSetDisconnectCallback(c,disconnectCallback);
+ redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
+ redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
ev_loop(loop, 0);
- redisFree(c);
return 0;
}