From 89e06225358c9b44998107bb55e80963904a30ce Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 1 Nov 2010 10:42:32 +0100 Subject: Move libev/libevent headers to adapters directory --- Makefile | 19 +++++----- adapters/libev.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ adapters/libevent.h | 76 +++++++++++++++++++++++++++++++++++++++ example-libev.c | 39 ++++++++++++++++++++ example-libevent.c | 39 ++++++++++++++++++++ extra/hiredis/libev.h | 92 ------------------------------------------------ extra/hiredis/libevent.h | 76 --------------------------------------- libev-example.c | 38 -------------------- libevent-example.c | 38 -------------------- 9 files changed, 256 insertions(+), 253 deletions(-) create mode 100644 adapters/libev.h create mode 100644 adapters/libevent.h create mode 100644 example-libev.c create mode 100644 example-libevent.c delete mode 100644 extra/hiredis/libev.h delete mode 100644 extra/hiredis/libevent.h delete mode 100644 libev-example.c delete mode 100644 libevent-example.c diff --git a/Makefile b/Makefile index a445fd6..c52d7fe 100644 --- a/Makefile +++ b/Makefile @@ -37,11 +37,12 @@ all: ${DYLIBNAME} ${BINS} # Deps (use make dep to generate this) anet.o: anet.c fmacros.h anet.h async.o: async.c async.h hiredis.h sds.h util.h +example-libev.o: example-libev.c hiredis.h async.h adapters/libev.h +example-libevent.o: example-libevent.c hiredis.h async.h adapters/libevent.h example.o: example.c hiredis.h hiredis.o: hiredis.c hiredis.h anet.h sds.h util.h sds.o: sds.c sds.h test.o: test.c hiredis.h - ${DYLIBNAME}: ${OBJ} ${DYLIB_MAKE_CMD} @@ -52,23 +53,23 @@ dynamic: ${DYLIBNAME} static: ${STLIBNAME} # Binaries: +hiredis-example-libevent: ${DYLIBNAME} + $(CC) -o $@ $(CCOPT) $(DEBUG) -L. -lhiredis -levent -Wl,-rpath,. example-libevent.c + +hiredis-example-libev: ${DYLIBNAME} + $(CC) -o $@ $(CCOPT) $(DEBUG) -L. -lhiredis -lev -Wl,-rpath,. example-libev.c + hiredis-%: %.o ${DYLIBNAME} - $(CC) -o $@ $(CCOPT) $(DEBUG) -L. -l hiredis -Wl,-rpath,. $< + $(CC) -o $@ $(CCOPT) $(DEBUG) -L. -lhiredis -Wl,-rpath,. $< test: hiredis-test ./hiredis-test -libevent-example: extra/hiredis/libevent.h libevent-example.c ${DYLIBNAME} - $(CC) -o $@ $(CCOPT) $(DEBUG) -I. -Iextra -L. -lhiredis -levent libevent-example.c - -libev-example: extra/hiredis/libev.h libev-example.c ${DYLIBNAME} - $(CC) -o $@ $(CCOPT) $(DEBUG) -I. -Iextra -L. -lhiredis -lev libev-example.c - .c.o: $(CC) -c $(CFLAGS) $(DEBUG) $(COMPILE_TIME) $< clean: - rm -rf ${DYLIBNAME} ${STLIBNAME} $(BINS) *-example *.o *.gcda *.gcno *.gcov + rm -rf ${DYLIBNAME} ${STLIBNAME} $(BINS) hiredis-example* *.o *.gcda *.gcno *.gcov dep: $(CC) -MM *.c diff --git a/adapters/libev.h b/adapters/libev.h new file mode 100644 index 0000000..79c069d --- /dev/null +++ b/adapters/libev.h @@ -0,0 +1,92 @@ +#include +#include +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisLibevEvents { + redisAsyncContext *context; + struct ev_loop *loop; + int reading, writing; + ev_io rev, wev; +} redisLibevEvents; + +void redisLibevReadEvent(struct ev_loop *loop, ev_io *watcher, int revents) { + ((void)loop); ((void)revents); + redisLibevEvents *e = watcher->data; + redisAsyncHandleRead(e->context); +} + +void redisLibevWriteEvent(struct ev_loop *loop, ev_io *watcher, int revents) { + ((void)loop); ((void)revents); + redisLibevEvents *e = watcher->data; + redisAsyncHandleWrite(e->context); +} + +void redisLibevAddRead(void *privdata) { + redisLibevEvents *e = privdata; + if (!e->reading) { + e->reading = 1; + ev_io_start(e->loop,&e->rev); + } +} + +void redisLibevDelRead(void *privdata) { + redisLibevEvents *e = privdata; + if (e->reading) { + e->reading = 0; + ev_io_stop(e->loop,&e->rev); + } +} + +void redisLibevAddWrite(void *privdata) { + redisLibevEvents *e = privdata; + if (!e->writing) { + e->writing = 1; + ev_io_start(e->loop,&e->wev); + } +} + +void redisLibevDelWrite(void *privdata) { + redisLibevEvents *e = privdata; + if (e->writing) { + e->writing = 0; + ev_io_stop(e->loop,&e->wev); + } +} + +void redisLibevCleanup(void *privdata) { + redisLibevEvents *e = privdata; + redisLibevDelRead(privdata); + redisLibevDelWrite(privdata); + free(e); +} + +int redisLibevAttach(redisAsyncContext *ac, struct ev_loop *loop) { + redisContext *c = &(ac->c); + redisLibevEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = malloc(sizeof(*e)); + e->context = ac; + e->loop = loop; + e->reading = e->writing = 0; + e->rev.data = e; + e->wev.data = e; + + /* Register functions to start/stop listening for events */ + ac->evAddRead = redisLibevAddRead; + ac->evDelRead = redisLibevDelRead; + ac->evAddWrite = redisLibevAddWrite; + ac->evDelWrite = redisLibevDelWrite; + ac->evCleanup = redisLibevCleanup; + ac->data = e; + + /* Initialize read/write events */ + ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); + ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE); + return REDIS_OK; +} diff --git a/adapters/libevent.h b/adapters/libevent.h new file mode 100644 index 0000000..1b759c1 --- /dev/null +++ b/adapters/libevent.h @@ -0,0 +1,76 @@ +#include +#include +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisLibeventEvents { + redisAsyncContext *context; + struct event rev, wev; +} redisLibeventEvents; + +void redisLibeventReadEvent(int fd, short event, void *arg) { + ((void)fd); ((void)event); + redisLibeventEvents *e = arg; + redisAsyncHandleRead(e->context); +} + +void redisLibeventWriteEvent(int fd, short event, void *arg) { + ((void)fd); ((void)event); + redisLibeventEvents *e = arg; + redisAsyncHandleWrite(e->context); +} + +void redisLibeventAddRead(void *privdata) { + redisLibeventEvents *e = privdata; + event_add(&e->rev,NULL); +} + +void redisLibeventDelRead(void *privdata) { + redisLibeventEvents *e = privdata; + event_del(&e->rev); +} + +void redisLibeventAddWrite(void *privdata) { + redisLibeventEvents *e = privdata; + event_add(&e->wev,NULL); +} + +void redisLibeventDelWrite(void *privdata) { + redisLibeventEvents *e = privdata; + event_del(&e->wev); +} + +void redisLibeventCleanup(void *privdata) { + redisLibeventEvents *e = privdata; + event_del(&e->rev); + event_del(&e->wev); + free(e); +} + +int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) { + redisContext *c = &(ac->c); + redisLibeventEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = malloc(sizeof(*e)); + e->context = ac; + + /* Register functions to start/stop listening for events */ + ac->evAddRead = redisLibeventAddRead; + ac->evDelRead = redisLibeventDelRead; + ac->evAddWrite = redisLibeventAddWrite; + ac->evDelWrite = redisLibeventDelWrite; + ac->evCleanup = redisLibeventCleanup; + ac->data = e; + + /* Initialize and install read/write events */ + event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e); + event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e); + event_base_set(base,&e->rev); + event_base_set(base,&e->wev); + return REDIS_OK; +} diff --git a/example-libev.c b/example-libev.c new file mode 100644 index 0000000..3495b2f --- /dev/null +++ b/example-libev.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include "hiredis.h" +#include "async.h" +#include "adapters/libev.h" + +void getCallback(redisAsyncContext *c, redisReply *reply, void *privdata) { + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +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); + + 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; + } + + 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); + return 0; +} diff --git a/example-libevent.c b/example-libevent.c new file mode 100644 index 0000000..101fa71 --- /dev/null +++ b/example-libevent.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include "hiredis.h" +#include "async.h" +#include "adapters/libevent.h" + +void getCallback(redisAsyncContext *c, redisReply *reply, void *privdata) { + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +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 event_base *base = event_base_new(); + + 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; + } + + redisLibeventAttach(c,base); + 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"); + event_base_dispatch(base); + return 0; +} diff --git a/extra/hiredis/libev.h b/extra/hiredis/libev.h deleted file mode 100644 index fcb7c24..0000000 --- a/extra/hiredis/libev.h +++ /dev/null @@ -1,92 +0,0 @@ -#include -#include -#include -#include - -typedef struct redisLibevEvents { - redisAsyncContext *context; - struct ev_loop *loop; - int reading, writing; - ev_io rev, wev; -} redisLibevEvents; - -void redisLibevReadEvent(struct ev_loop *loop, ev_io *watcher, int revents) { - ((void)loop); ((void)revents); - redisLibevEvents *e = watcher->data; - redisAsyncHandleRead(e->context); -} - -void redisLibevWriteEvent(struct ev_loop *loop, ev_io *watcher, int revents) { - ((void)loop); ((void)revents); - redisLibevEvents *e = watcher->data; - redisAsyncHandleWrite(e->context); -} - -void redisLibevAddRead(void *privdata) { - redisLibevEvents *e = privdata; - if (!e->reading) { - e->reading = 1; - ev_io_start(e->loop,&e->rev); - } -} - -void redisLibevDelRead(void *privdata) { - redisLibevEvents *e = privdata; - if (e->reading) { - e->reading = 0; - ev_io_stop(e->loop,&e->rev); - } -} - -void redisLibevAddWrite(void *privdata) { - redisLibevEvents *e = privdata; - if (!e->writing) { - e->writing = 1; - ev_io_start(e->loop,&e->wev); - } -} - -void redisLibevDelWrite(void *privdata) { - redisLibevEvents *e = privdata; - if (e->writing) { - e->writing = 0; - ev_io_stop(e->loop,&e->wev); - } -} - -void redisLibevCleanup(void *privdata) { - redisLibevEvents *e = privdata; - redisLibevDelRead(privdata); - redisLibevDelWrite(privdata); - free(e); -} - -int redisLibevAttach(redisAsyncContext *ac, struct ev_loop *loop) { - redisContext *c = &(ac->c); - redisLibevEvents *e; - - /* Nothing should be attached when something is already attached */ - if (ac->data != NULL) - return REDIS_ERR; - - /* Create container for context and r/w events */ - e = malloc(sizeof(*e)); - e->context = ac; - e->loop = loop; - e->reading = e->writing = 0; - e->rev.data = e; - e->wev.data = e; - - /* Register functions to start/stop listening for events */ - ac->evAddRead = redisLibevAddRead; - ac->evDelRead = redisLibevDelRead; - ac->evAddWrite = redisLibevAddWrite; - ac->evDelWrite = redisLibevDelWrite; - ac->evCleanup = redisLibevCleanup; - ac->data = e; - - /* Initialize read/write events */ - ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); - ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE); - return REDIS_OK; -} diff --git a/extra/hiredis/libevent.h b/extra/hiredis/libevent.h deleted file mode 100644 index f2851f9..0000000 --- a/extra/hiredis/libevent.h +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include -#include - -typedef struct redisLibeventEvents { - redisAsyncContext *context; - struct event rev, wev; -} redisLibeventEvents; - -void redisLibeventReadEvent(int fd, short event, void *arg) { - ((void)fd); ((void)event); - redisLibeventEvents *e = arg; - redisAsyncHandleRead(e->context); -} - -void redisLibeventWriteEvent(int fd, short event, void *arg) { - ((void)fd); ((void)event); - redisLibeventEvents *e = arg; - redisAsyncHandleWrite(e->context); -} - -void redisLibeventAddRead(void *privdata) { - redisLibeventEvents *e = privdata; - event_add(&e->rev,NULL); -} - -void redisLibeventDelRead(void *privdata) { - redisLibeventEvents *e = privdata; - event_del(&e->rev); -} - -void redisLibeventAddWrite(void *privdata) { - redisLibeventEvents *e = privdata; - event_add(&e->wev,NULL); -} - -void redisLibeventDelWrite(void *privdata) { - redisLibeventEvents *e = privdata; - event_del(&e->wev); -} - -void redisLibeventCleanup(void *privdata) { - redisLibeventEvents *e = privdata; - event_del(&e->rev); - event_del(&e->wev); - free(e); -} - -int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) { - redisContext *c = &(ac->c); - redisLibeventEvents *e; - - /* Nothing should be attached when something is already attached */ - if (ac->data != NULL) - return REDIS_ERR; - - /* Create container for context and r/w events */ - e = malloc(sizeof(*e)); - e->context = ac; - - /* Register functions to start/stop listening for events */ - ac->evAddRead = redisLibeventAddRead; - ac->evDelRead = redisLibeventDelRead; - ac->evAddWrite = redisLibeventAddWrite; - ac->evDelWrite = redisLibeventDelWrite; - ac->evCleanup = redisLibeventCleanup; - ac->data = e; - - /* Initialize and install read/write events */ - event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e); - event_set(&e->wev,c->fd,EV_WRITE,redisLibeventWriteEvent,e); - event_base_set(base,&e->rev); - event_base_set(base,&e->wev); - return REDIS_OK; -} diff --git a/libev-example.c b/libev-example.c deleted file mode 100644 index 2d0bc8b..0000000 --- a/libev-example.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include - -void getCallback(redisAsyncContext *c, redisReply *reply, void *privdata) { - printf("argv[%s]: %s\n", (char*)privdata, reply->str); - - /* Disconnect after receiving the reply to GET */ - redisAsyncDisconnect(c); -} - -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); - - 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; - } - - 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); - return 0; -} diff --git a/libevent-example.c b/libevent-example.c deleted file mode 100644 index a5a24b5..0000000 --- a/libevent-example.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include - -void getCallback(redisAsyncContext *c, redisReply *reply, void *privdata) { - printf("argv[%s]: %s\n", (char*)privdata, reply->str); - - /* Disconnect after receiving the reply to GET */ - redisAsyncDisconnect(c); -} - -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 event_base *base = event_base_new(); - - 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; - } - - redisLibeventAttach(c,base); - 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"); - event_base_dispatch(base); - return 0; -} -- cgit v1.2.3