2010-09-25 20:34:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <event.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "hiredis.h"
|
|
|
|
|
|
|
|
#define NOT_USED(x) ((void)x)
|
|
|
|
|
|
|
|
/* This struct enables us to pass both the event and the
|
|
|
|
* redisContext to the read and write handlers. */
|
|
|
|
typedef struct redisEvents {
|
|
|
|
redisContext *context;
|
2010-10-06 16:13:23 +00:00
|
|
|
struct event rev, wev;
|
2010-09-25 20:34:22 +00:00
|
|
|
} redisEvents;
|
|
|
|
|
|
|
|
void redisLibEventRead(int fd, short event, void *arg) {
|
|
|
|
NOT_USED(fd); NOT_USED(event);
|
2010-10-06 16:13:23 +00:00
|
|
|
redisEvents *e = arg;
|
2010-09-25 20:34:22 +00:00
|
|
|
|
|
|
|
/* Always re-schedule read events */
|
2010-10-06 16:13:23 +00:00
|
|
|
event_add(&e->rev,NULL);
|
2010-09-25 20:34:22 +00:00
|
|
|
|
2010-10-06 16:13:23 +00:00
|
|
|
if (redisBufferRead(e->context) == REDIS_ERR) {
|
2010-09-25 20:34:22 +00:00
|
|
|
/* Handle error. */
|
2010-10-06 16:13:23 +00:00
|
|
|
printf("Read error: %s\n", e->context->error);
|
2010-09-25 20:34:22 +00:00
|
|
|
} else {
|
|
|
|
/* Check replies. */
|
2010-10-06 16:13:23 +00:00
|
|
|
redisProcessCallbacks(e->context);
|
2010-09-25 20:34:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void redisLibEventWrite(int fd, short event, void *arg) {
|
|
|
|
NOT_USED(fd); NOT_USED(event);
|
2010-10-06 16:13:23 +00:00
|
|
|
redisEvents *e = arg;
|
2010-09-25 20:34:22 +00:00
|
|
|
int done = 0;
|
|
|
|
|
2010-10-06 16:13:23 +00:00
|
|
|
if (redisBufferWrite(e->context, &done) == REDIS_ERR) {
|
2010-09-25 20:34:22 +00:00
|
|
|
/* Handle error */
|
2010-10-06 16:13:23 +00:00
|
|
|
printf("Write error: %s\n", e->context->error);
|
2010-09-25 20:34:22 +00:00
|
|
|
} else {
|
|
|
|
/* Schedule write event again when writing is not done. */
|
|
|
|
if (!done) {
|
2010-10-06 16:13:23 +00:00
|
|
|
event_add(&e->wev,NULL);
|
2010-09-25 20:34:22 +00:00
|
|
|
} else {
|
2010-10-06 16:13:23 +00:00
|
|
|
event_add(&e->rev,NULL);
|
2010-09-25 20:34:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Schedule to be notified on a write event, so the outgoing buffer
|
|
|
|
* can be flushed to the socket. */
|
|
|
|
void redisLibEventOnWrite(redisContext *c, void *privdata) {
|
|
|
|
NOT_USED(c);
|
|
|
|
redisEvents *e = privdata;
|
2010-10-06 16:13:23 +00:00
|
|
|
event_add(&e->wev,NULL);
|
2010-09-25 20:34:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the redisEvents struct when the context is free'd. */
|
|
|
|
void redisLibEventOnFree(redisContext *c, void *privdata) {
|
|
|
|
NOT_USED(c);
|
|
|
|
redisEvents *e = privdata;
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
redisContext *redisLibEventConnect(const char *ip, int port) {
|
|
|
|
redisEvents *e = malloc(sizeof(*e));
|
|
|
|
e->context = redisConnectNonBlock(ip, port, NULL);
|
|
|
|
redisSetCommandCallback(e->context, redisLibEventOnWrite, e);
|
|
|
|
redisSetFreeCallback(e->context, redisLibEventOnFree, e);
|
2010-10-06 16:13:23 +00:00
|
|
|
event_set(&e->rev, e->context->fd, EV_READ, redisLibEventRead, e);
|
|
|
|
event_set(&e->wev, e->context->fd, EV_WRITE, redisLibEventWrite, e);
|
2010-09-25 20:34:22 +00:00
|
|
|
return e->context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void getCallback(redisContext *c, redisReply *reply, void *privdata) {
|
|
|
|
NOT_USED(c); NOT_USED(privdata);
|
|
|
|
printf("argv[end-1]: %s\n", reply->reply);
|
|
|
|
redisFree(c);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv) {
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
event_init();
|
|
|
|
|
|
|
|
redisContext *c = redisLibEventConnect("127.0.0.1", 6379);
|
|
|
|
if (c->error != NULL) {
|
|
|
|
printf("Connect error: %s\n", c->error);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
redisCommand(c, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
|
|
|
|
redisCommandWithCallback(c, getCallback, NULL, "GET key");
|
|
|
|
event_dispatch();
|
|
|
|
return 0;
|
|
|
|
}
|