From 304d1cf1babdb6925731f6974edf84d3796753c5 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Mon, 20 Feb 2017 15:34:44 +0100 Subject: [PATCH] ndb_redis: export functions via inter-module api - allow other modules to work with same pool of connections managed by this module --- src/modules/ndb_redis/api.h | 67 +++++++++++++++++++++++++++ src/modules/ndb_redis/ndb_redis_mod.c | 27 +++++++++++ src/modules/ndb_redis/redis_client.c | 2 +- src/modules/ndb_redis/redis_client.h | 3 +- 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/modules/ndb_redis/api.h diff --git a/src/modules/ndb_redis/api.h b/src/modules/ndb_redis/api.h new file mode 100644 index 00000000000..c94fd29408b --- /dev/null +++ b/src/modules/ndb_redis/api.h @@ -0,0 +1,67 @@ +/** + * Copyright (C) 2017 Daniel-Constantin Mierla (asipto.com) + * + * This file is part of Kamailio, a free SIP server. + * + * Kamailio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version + * + * Kamailio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef _NDB_REDIS_API_H_ +#define _NDB_REDIS_API_H_ + +#include "redis_client.h" + +typedef redisc_server_t* (*redisc_get_server_f)(str *name); +typedef int (*redisc_exec_f)(str *srv, str *res, str *cmd, ...); +typedef redisReply* (*redisc_exec_argv_f)(redisc_server_t *rsrv, int argc, + const char **argv, const size_t *argvlen); +typedef redisc_reply_t* (*redisc_get_reply_f)(str *name); +typedef int (*redisc_free_reply_f)(str *name); + + +/** + * @brief NDB_REDIS API structure + */ +typedef struct ndb_redis_api { + redisc_get_server_f get_server; + redisc_exec_f exec; + redisc_exec_argv_f exec_argv; + redisc_get_reply_f get_reply; + redisc_free_reply_f free_reply; +} ndb_redis_api_t; + +typedef int (*bind_ndb_redis_f)(ndb_redis_api_t* api); + +/** + * @brief Load the NDB_REDIS API + */ +static inline int ndb_redis_load_api(ndb_redis_api_t *api) +{ + bind_ndb_redis_f bindndbredis; + + bindndbredis = (bind_ndb_redis_f)find_export("bind_ndb_redis", 0, 0); + if(bindndbredis == 0) { + LM_ERR("cannot find bind_ndb_redis\n"); + return -1; + } + if (bindndbredis(api)==-1) { + LM_ERR("cannot bind ndb_redis api\n"); + return -1; + } + return 0; +} + +#endif diff --git a/src/modules/ndb_redis/ndb_redis_mod.c b/src/modules/ndb_redis/ndb_redis_mod.c index bd76a76129f..5bfe9a5fe28 100644 --- a/src/modules/ndb_redis/ndb_redis_mod.c +++ b/src/modules/ndb_redis/ndb_redis_mod.c @@ -34,6 +34,7 @@ #include "../../core/trim.h" #include "redis_client.h" +#include "api.h" MODULE_VERSION @@ -59,6 +60,8 @@ static int w_redis_free_reply(struct sip_msg* msg, char* res); static void mod_destroy(void); static int child_init(int rank); +int bind_ndb_redis(ndb_redis_api_t *api); + static int pv_get_redisc(struct sip_msg *msg, pv_param_t *param, pv_value_t *res); static int pv_parse_redisc_name(pv_spec_p sp, str *in); @@ -81,6 +84,10 @@ static cmd_export_t cmds[]={ 0, ANY_ROUTE}, {"redis_free", (cmd_function)w_redis_free_reply, 1, fixup_spve_null, 0, ANY_ROUTE}, + + {"bind_ndb_redis", (cmd_function)bind_ndb_redis, 0, + 0, 0, 0}, + {0, 0, 0, 0, 0, 0} }; @@ -608,3 +615,23 @@ static int pv_get_redisc(struct sip_msg *msg, pv_param_t *param, return pv_get_null(msg, param, res); } } + +/** + * @brief bind functions to NDB_REDIS API structure + */ +int bind_ndb_redis(ndb_redis_api_t *api) +{ + if (!api) { + ERR("Invalid parameter value\n"); + return -1; + } + memset(api, 0, sizeof(ndb_redis_api_t)); + api->get_server = redisc_get_server; + api->exec = redisc_exec; + api->exec_argv = redisc_exec_argv; + api->get_reply = redisc_get_reply; + api->free_reply = redisc_free_reply; + + return 0; +} + diff --git a/src/modules/ndb_redis/redis_client.c b/src/modules/ndb_redis/redis_client.c index c96d1986078..c60066d6bd8 100644 --- a/src/modules/ndb_redis/redis_client.c +++ b/src/modules/ndb_redis/redis_client.c @@ -446,7 +446,7 @@ int redisc_exec(str *srv, str *res, str *cmd, ...) * @param argvlen vector of command string lenghts or NULL. * @return redisReply structure or NULL if there was an error. */ -void * redisc_exec_argv(redisc_server_t *rsrv, int argc, const char **argv, +redisReply* redisc_exec_argv(redisc_server_t *rsrv, int argc, const char **argv, const size_t *argvlen) { redisReply *res=NULL; diff --git a/src/modules/ndb_redis/redis_client.h b/src/modules/ndb_redis/redis_client.h index 799172a117f..3f16784e5c6 100644 --- a/src/modules/ndb_redis/redis_client.h +++ b/src/modules/ndb_redis/redis_client.h @@ -62,7 +62,8 @@ int redisc_reconnect_server(redisc_server_t *rsrv); /* Command related functions */ int redisc_exec(str *srv, str *res, str *cmd, ...); -void* redisc_exec_argv(redisc_server_t *rsrv, int argc, const char **argv, const size_t *argvlen); +redisReply* redisc_exec_argv(redisc_server_t *rsrv, int argc, const char **argv, + const size_t *argvlen); redisc_reply_t *redisc_get_reply(str *name); int redisc_free_reply(str *name); int redisc_check_auth(redisc_server_t *rsrv, char *pass);