From b76dffa81f15f4a637bd29c96067ec9743f154e7 Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Fri, 22 Mar 2024 21:22:30 +0100 Subject: fixup! all: rename output files Signed-off-by: Anna (navi) Figueiredo Gomes --- adapters/redictmoduleapi.h | 155 +++++++++++++++++++++++++++++++++++++ adapters/redismoduleapi.h | 155 ------------------------------------- examples/example-redictmoduleapi.c | 107 +++++++++++++++++++++++++ examples/example-redismoduleapi.c | 107 ------------------------- ssl.c | 2 +- test.c | 4 +- 6 files changed, 265 insertions(+), 265 deletions(-) create mode 100644 adapters/redictmoduleapi.h delete mode 100644 adapters/redismoduleapi.h create mode 100644 examples/example-redictmoduleapi.c delete mode 100644 examples/example-redismoduleapi.c diff --git a/adapters/redictmoduleapi.h b/adapters/redictmoduleapi.h new file mode 100644 index 0000000..1a134f5 --- /dev/null +++ b/adapters/redictmoduleapi.h @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2009-2011, Salvatore Sanfilippo + * + * SPDX-FileCopyrightText: 2024 Hiredict Contributors + * SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo + * + * SPDX-License-Identifier: BSD-3-Clause + * SPDX-License-Identifier: LGPL-3.0-or-later + * + */ + +#ifndef __HIREDIS_REDISMODULEAPI_H__ +#define __HIREDIS_REDISMODULEAPI_H__ + +#include "redictmodule.h" + +#include "../async.h" +#include "../hiredict.h" + +#include + +typedef struct redisModuleEvents { + redisAsyncContext *context; + RedisModuleCtx *module_ctx; + int fd; + int reading, writing; + int timer_active; + RedisModuleTimerID timer_id; +} redisModuleEvents; + +static inline void redisModuleReadEvent(int fd, void *privdata, int mask) { + (void) fd; + (void) mask; + + redisModuleEvents *e = (redisModuleEvents*)privdata; + redisAsyncHandleRead(e->context); +} + +static inline void redisModuleWriteEvent(int fd, void *privdata, int mask) { + (void) fd; + (void) mask; + + redisModuleEvents *e = (redisModuleEvents*)privdata; + redisAsyncHandleWrite(e->context); +} + +static inline void redisModuleAddRead(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (!e->reading) { + e->reading = 1; + RedisModule_EventLoopAdd(e->fd, REDISMODULE_EVENTLOOP_READABLE, redisModuleReadEvent, e); + } +} + +static inline void redisModuleDelRead(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (e->reading) { + e->reading = 0; + RedisModule_EventLoopDel(e->fd, REDISMODULE_EVENTLOOP_READABLE); + } +} + +static inline void redisModuleAddWrite(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (!e->writing) { + e->writing = 1; + RedisModule_EventLoopAdd(e->fd, REDISMODULE_EVENTLOOP_WRITABLE, redisModuleWriteEvent, e); + } +} + +static inline void redisModuleDelWrite(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (e->writing) { + e->writing = 0; + RedisModule_EventLoopDel(e->fd, REDISMODULE_EVENTLOOP_WRITABLE); + } +} + +static inline void redisModuleStopTimer(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (e->timer_active) { + RedisModule_StopTimer(e->module_ctx, e->timer_id, NULL); + } + e->timer_active = 0; +} + +static inline void redisModuleCleanup(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + redisModuleDelRead(privdata); + redisModuleDelWrite(privdata); + redisModuleStopTimer(privdata); + hi_free(e); +} + +static inline void redisModuleTimeout(RedisModuleCtx *ctx, void *privdata) { + (void) ctx; + + redisModuleEvents *e = (redisModuleEvents*)privdata; + e->timer_active = 0; + redisAsyncHandleTimeout(e->context); +} + +static inline void redisModuleSetTimeout(void *privdata, struct timeval tv) { + redisModuleEvents* e = (redisModuleEvents*)privdata; + + redisModuleStopTimer(privdata); + + mstime_t millis = tv.tv_sec * 1000 + tv.tv_usec / 1000.0; + e->timer_id = RedisModule_CreateTimer(e->module_ctx, millis, redisModuleTimeout, e); + e->timer_active = 1; +} + +/* Check if Redis version is compatible with the adapter. */ +static inline int redisModuleCompatibilityCheck(void) { + if (!RedisModule_EventLoopAdd || + !RedisModule_EventLoopDel || + !RedisModule_CreateTimer || + !RedisModule_StopTimer) { + return REDIS_ERR; + } + return REDIS_OK; +} + +static inline int redisModuleAttach(redisAsyncContext *ac, RedisModuleCtx *module_ctx) { + redisContext *c = &(ac->c); + redisModuleEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisModuleEvents*)hi_malloc(sizeof(*e)); + if (e == NULL) + return REDIS_ERR; + + e->context = ac; + e->module_ctx = module_ctx; + e->fd = c->fd; + e->reading = e->writing = 0; + e->timer_active = 0; + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisModuleAddRead; + ac->ev.delRead = redisModuleDelRead; + ac->ev.addWrite = redisModuleAddWrite; + ac->ev.delWrite = redisModuleDelWrite; + ac->ev.cleanup = redisModuleCleanup; + ac->ev.scheduleTimer = redisModuleSetTimeout; + ac->ev.data = e; + + return REDIS_OK; +} + +#endif diff --git a/adapters/redismoduleapi.h b/adapters/redismoduleapi.h deleted file mode 100644 index b9bf922..0000000 --- a/adapters/redismoduleapi.h +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2009-2011, Salvatore Sanfilippo - * - * SPDX-FileCopyrightText: 2024 Hiredict Contributors - * SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo - * - * SPDX-License-Identifier: BSD-3-Clause - * SPDX-License-Identifier: LGPL-3.0-or-later - * - */ - -#ifndef __HIREDIS_REDISMODULEAPI_H__ -#define __HIREDIS_REDISMODULEAPI_H__ - -#include "redismodule.h" - -#include "../async.h" -#include "../hiredict.h" - -#include - -typedef struct redisModuleEvents { - redisAsyncContext *context; - RedisModuleCtx *module_ctx; - int fd; - int reading, writing; - int timer_active; - RedisModuleTimerID timer_id; -} redisModuleEvents; - -static inline void redisModuleReadEvent(int fd, void *privdata, int mask) { - (void) fd; - (void) mask; - - redisModuleEvents *e = (redisModuleEvents*)privdata; - redisAsyncHandleRead(e->context); -} - -static inline void redisModuleWriteEvent(int fd, void *privdata, int mask) { - (void) fd; - (void) mask; - - redisModuleEvents *e = (redisModuleEvents*)privdata; - redisAsyncHandleWrite(e->context); -} - -static inline void redisModuleAddRead(void *privdata) { - redisModuleEvents *e = (redisModuleEvents*)privdata; - if (!e->reading) { - e->reading = 1; - RedisModule_EventLoopAdd(e->fd, REDISMODULE_EVENTLOOP_READABLE, redisModuleReadEvent, e); - } -} - -static inline void redisModuleDelRead(void *privdata) { - redisModuleEvents *e = (redisModuleEvents*)privdata; - if (e->reading) { - e->reading = 0; - RedisModule_EventLoopDel(e->fd, REDISMODULE_EVENTLOOP_READABLE); - } -} - -static inline void redisModuleAddWrite(void *privdata) { - redisModuleEvents *e = (redisModuleEvents*)privdata; - if (!e->writing) { - e->writing = 1; - RedisModule_EventLoopAdd(e->fd, REDISMODULE_EVENTLOOP_WRITABLE, redisModuleWriteEvent, e); - } -} - -static inline void redisModuleDelWrite(void *privdata) { - redisModuleEvents *e = (redisModuleEvents*)privdata; - if (e->writing) { - e->writing = 0; - RedisModule_EventLoopDel(e->fd, REDISMODULE_EVENTLOOP_WRITABLE); - } -} - -static inline void redisModuleStopTimer(void *privdata) { - redisModuleEvents *e = (redisModuleEvents*)privdata; - if (e->timer_active) { - RedisModule_StopTimer(e->module_ctx, e->timer_id, NULL); - } - e->timer_active = 0; -} - -static inline void redisModuleCleanup(void *privdata) { - redisModuleEvents *e = (redisModuleEvents*)privdata; - redisModuleDelRead(privdata); - redisModuleDelWrite(privdata); - redisModuleStopTimer(privdata); - hi_free(e); -} - -static inline void redisModuleTimeout(RedisModuleCtx *ctx, void *privdata) { - (void) ctx; - - redisModuleEvents *e = (redisModuleEvents*)privdata; - e->timer_active = 0; - redisAsyncHandleTimeout(e->context); -} - -static inline void redisModuleSetTimeout(void *privdata, struct timeval tv) { - redisModuleEvents* e = (redisModuleEvents*)privdata; - - redisModuleStopTimer(privdata); - - mstime_t millis = tv.tv_sec * 1000 + tv.tv_usec / 1000.0; - e->timer_id = RedisModule_CreateTimer(e->module_ctx, millis, redisModuleTimeout, e); - e->timer_active = 1; -} - -/* Check if Redis version is compatible with the adapter. */ -static inline int redisModuleCompatibilityCheck(void) { - if (!RedisModule_EventLoopAdd || - !RedisModule_EventLoopDel || - !RedisModule_CreateTimer || - !RedisModule_StopTimer) { - return REDIS_ERR; - } - return REDIS_OK; -} - -static inline int redisModuleAttach(redisAsyncContext *ac, RedisModuleCtx *module_ctx) { - redisContext *c = &(ac->c); - redisModuleEvents *e; - - /* Nothing should be attached when something is already attached */ - if (ac->ev.data != NULL) - return REDIS_ERR; - - /* Create container for context and r/w events */ - e = (redisModuleEvents*)hi_malloc(sizeof(*e)); - if (e == NULL) - return REDIS_ERR; - - e->context = ac; - e->module_ctx = module_ctx; - e->fd = c->fd; - e->reading = e->writing = 0; - e->timer_active = 0; - - /* Register functions to start/stop listening for events */ - ac->ev.addRead = redisModuleAddRead; - ac->ev.delRead = redisModuleDelRead; - ac->ev.addWrite = redisModuleAddWrite; - ac->ev.delWrite = redisModuleDelWrite; - ac->ev.cleanup = redisModuleCleanup; - ac->ev.scheduleTimer = redisModuleSetTimeout; - ac->ev.data = e; - - return REDIS_OK; -} - -#endif diff --git a/examples/example-redictmoduleapi.c b/examples/example-redictmoduleapi.c new file mode 100644 index 0000000..c95546c --- /dev/null +++ b/examples/example-redictmoduleapi.c @@ -0,0 +1,107 @@ +// SPDX-FileCopyrightText: 2024 Hiredict Contributors +// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo +// +// SPDX-License-Identifier: BSD-3-Clause +// SPDX-License-Identifier: LGPL-3.0-or-later + +#include +#include +#include +#include + +#include +#include +#include + +void debugCallback(redisAsyncContext *c, void *r, void *privdata) { + (void)privdata; //unused + redisReply *reply = r; + if (reply == NULL) { + /* The DEBUG SLEEP command will almost always fail, because we have set a 1 second timeout */ + printf("`DEBUG SLEEP` error: %s\n", c->errstr ? c->errstr : "unknown error"); + return; + } + /* Disconnect after receiving the reply of DEBUG SLEEP (which will not)*/ + redisAsyncDisconnect(c); +} + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) { + if (c->errstr) { + printf("errstr: %s\n", c->errstr); + } + return; + } + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* start another request that demonstrate timeout */ + redisAsyncCommand(c, debugCallback, NULL, "DEBUG SLEEP %f", 1.5); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +/* + * This example requires Redis 7.0 or above. + * + * 1- Compile this file as a shared library. Directory of "redismodule.h" must + * be in the include path. + * gcc -fPIC -shared -I../../redis/src/ -I.. example-redismoduleapi.c -o example-redismoduleapi.so + * + * 2- Load module: + * redis-server --loadmodule ./example-redismoduleapi.so value + */ +int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + + int ret = RedisModule_Init(ctx, "example-redictmoduleapi", 1, REDISMODULE_APIVER_1); + if (ret != REDISMODULE_OK) { + printf("error module init \n"); + return REDISMODULE_ERR; + } + + if (redisModuleCompatibilityCheck() != REDIS_OK) { + printf("Redis 7.0 or above is required! \n"); + return REDISMODULE_ERR; + } + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + size_t len; + const char *val = RedisModule_StringPtrLen(argv[argc-1], &len); + + RedisModuleCtx *module_ctx = RedisModule_GetDetachedThreadSafeContext(ctx); + redisModuleAttach(c, module_ctx); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncSetTimeout(c, (struct timeval){ .tv_sec = 1, .tv_usec = 0}); + + /* + In this demo, we first `set key`, then `get key` to demonstrate the basic usage of the adapter. + Then in `getCallback`, we start a `debug sleep` command to create 1.5 second long request. + Because we have set a 1 second timeout to the connection, the command will always fail with a + timeout error, which is shown in the `debugCallback`. + */ + + redisAsyncCommand(c, NULL, NULL, "SET key %b", val, len); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + return 0; +} diff --git a/examples/example-redismoduleapi.c b/examples/example-redismoduleapi.c deleted file mode 100644 index 37bba9c..0000000 --- a/examples/example-redismoduleapi.c +++ /dev/null @@ -1,107 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Hiredict Contributors -// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo -// -// SPDX-License-Identifier: BSD-3-Clause -// SPDX-License-Identifier: LGPL-3.0-or-later - -#include -#include -#include -#include - -#include -#include -#include - -void debugCallback(redisAsyncContext *c, void *r, void *privdata) { - (void)privdata; //unused - redisReply *reply = r; - if (reply == NULL) { - /* The DEBUG SLEEP command will almost always fail, because we have set a 1 second timeout */ - printf("`DEBUG SLEEP` error: %s\n", c->errstr ? c->errstr : "unknown error"); - return; - } - /* Disconnect after receiving the reply of DEBUG SLEEP (which will not)*/ - redisAsyncDisconnect(c); -} - -void getCallback(redisAsyncContext *c, void *r, void *privdata) { - redisReply *reply = r; - if (reply == NULL) { - if (c->errstr) { - printf("errstr: %s\n", c->errstr); - } - return; - } - printf("argv[%s]: %s\n", (char*)privdata, reply->str); - - /* start another request that demonstrate timeout */ - redisAsyncCommand(c, debugCallback, NULL, "DEBUG SLEEP %f", 1.5); -} - -void connectCallback(const redisAsyncContext *c, int status) { - if (status != REDIS_OK) { - printf("Error: %s\n", c->errstr); - return; - } - printf("Connected...\n"); -} - -void disconnectCallback(const redisAsyncContext *c, int status) { - if (status != REDIS_OK) { - printf("Error: %s\n", c->errstr); - return; - } - printf("Disconnected...\n"); -} - -/* - * This example requires Redis 7.0 or above. - * - * 1- Compile this file as a shared library. Directory of "redismodule.h" must - * be in the include path. - * gcc -fPIC -shared -I../../redis/src/ -I.. example-redismoduleapi.c -o example-redismoduleapi.so - * - * 2- Load module: - * redis-server --loadmodule ./example-redismoduleapi.so value - */ -int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { - - int ret = RedisModule_Init(ctx, "example-redismoduleapi", 1, REDISMODULE_APIVER_1); - if (ret != REDISMODULE_OK) { - printf("error module init \n"); - return REDISMODULE_ERR; - } - - if (redisModuleCompatibilityCheck() != REDIS_OK) { - printf("Redis 7.0 or above is required! \n"); - return REDISMODULE_ERR; - } - - redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); - if (c->err) { - /* Let *c leak for now... */ - printf("Error: %s\n", c->errstr); - return 1; - } - - size_t len; - const char *val = RedisModule_StringPtrLen(argv[argc-1], &len); - - RedisModuleCtx *module_ctx = RedisModule_GetDetachedThreadSafeContext(ctx); - redisModuleAttach(c, module_ctx); - redisAsyncSetConnectCallback(c,connectCallback); - redisAsyncSetDisconnectCallback(c,disconnectCallback); - redisAsyncSetTimeout(c, (struct timeval){ .tv_sec = 1, .tv_usec = 0}); - - /* - In this demo, we first `set key`, then `get key` to demonstrate the basic usage of the adapter. - Then in `getCallback`, we start a `debug sleep` command to create 1.5 second long request. - Because we have set a 1 second timeout to the connection, the command will always fail with a - timeout error, which is shown in the `debugCallback`. - */ - - redisAsyncCommand(c, NULL, NULL, "SET key %b", val, len); - redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); - return 0; -} diff --git a/ssl.c b/ssl.c index 60a9695..a2c3b07 100644 --- a/ssl.c +++ b/ssl.c @@ -40,7 +40,7 @@ #include "win32.h" #include "async_private.h" -#include "hiredis_ssl.h" +#include "hiredict_ssl.h" #define OPENSSL_1_1_0 0x10100000L diff --git a/test.c b/test.c index 72f94ef..02a2197 100644 --- a/test.c +++ b/test.c @@ -28,7 +28,7 @@ #include "async.h" #include "adapters/poll.h" #ifdef HIREDIS_TEST_SSL -#include "hiredis_ssl.h" +#include "hiredict_ssl.h" #endif #ifdef HIREDIS_TEST_ASYNC #include "adapters/libevent.h" @@ -2326,7 +2326,7 @@ int main(int argc, char **argv) { .port = 6379 }, .unix_sock = { - .path = "/tmp/redis.sock" + .path = "/tmp/redict.sock" } }; int throughput = 1; -- cgit v1.2.3