diff --git a/pubsub/Makefile b/pubsub/Makefile new file mode 100644 index 0000000..03266a1 --- /dev/null +++ b/pubsub/Makefile @@ -0,0 +1,21 @@ +OUT=test +HIREDIS_DIR=../hiredis +HIREDIS_OBJ=$(HIREDIS_DIR)/hiredis.o $(HIREDIS_DIR)/sds.o $(HIREDIS_DIR)/net.o $(HIREDIS_DIR)/async.o +OBJS=test.o $(HIREDIS_OBJ) +CFLAGS=-O0 -ggdb -Wall -Wextra -I. -I $(HIREDIS_DIR)/.. +LDFLAGS=-levent + +all: $(OUT) Makefile + +$(OUT): $(OBJS) Makefile + $(CC) $(LDFLAGS) -o $(OUT) $(OBJS) + +%.o: %.c %.h Makefile + $(CC) -c $(CFLAGS) -o $@ $< + +%.o: %.c Makefile + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + rm -f $(OBJS) $(OUT) + diff --git a/pubsub/test b/pubsub/test new file mode 100755 index 0000000..defe7fb Binary files /dev/null and b/pubsub/test differ diff --git a/pubsub/test.c b/pubsub/test.c new file mode 100644 index 0000000..f79dbc1 --- /dev/null +++ b/pubsub/test.c @@ -0,0 +1,56 @@ +#include +#include +#include + +#include +#include +#include + +extern int __redisPushCallback(redisCallbackList *list, redisCallback *source); + +static void +connectCallback(const redisAsyncContext *c) { + ((void)c); + printf("connected...\n"); +} + +static void +disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + } + printf("disconnected.\n"); +} + +static void +onCmd(redisAsyncContext *ac, void *r, void *privdata) { + + (void)ac; + (void)r; + (void)privdata; + redisCallback *cb; + + printf("got something from Redis, reinstalling callback.\n"); + cb = calloc(1, sizeof(redisCallback)); + cb->fn = onCmd; + + printf("__redisPushCallback: %d\n", __redisPushCallback(&ac->replies, cb)); +} + +int +main() { + + struct event_base *base = event_base_new(); + struct redisAsyncContext *ac = redisAsyncConnect("127.0.0.1", 6379); + + redisLibeventAttach(ac, base); + redisAsyncSetConnectCallback(ac, connectCallback); + redisAsyncSetDisconnectCallback(ac, disconnectCallback); + + redisAsyncCommand(ac, onCmd, NULL, "SUBSCRIBE %s", "x"); + + event_base_dispatch(base); + + return EXIT_SUCCESS; +} +