Selaa lähdekoodia

ndb_redis: export functions via inter-module api

- allow other modules to work with same pool of connections managed by
  this module
Daniel-Constantin Mierla 8 vuotta sitten
vanhempi
commit
304d1cf1ba

+ 67 - 0
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

+ 27 - 0
src/modules/ndb_redis/ndb_redis_mod.c

@@ -34,6 +34,7 @@
 #include "../../core/trim.h"
 #include "../../core/trim.h"
 
 
 #include "redis_client.h"
 #include "redis_client.h"
+#include "api.h"
 
 
 MODULE_VERSION
 MODULE_VERSION
 
 
@@ -59,6 +60,8 @@ static int w_redis_free_reply(struct sip_msg* msg, char* res);
 static void mod_destroy(void);
 static void mod_destroy(void);
 static int  child_init(int rank);
 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,
 static int pv_get_redisc(struct sip_msg *msg,  pv_param_t *param,
 		pv_value_t *res);
 		pv_value_t *res);
 static int pv_parse_redisc_name(pv_spec_p sp, str *in);
 static int pv_parse_redisc_name(pv_spec_p sp, str *in);
@@ -81,6 +84,10 @@ static cmd_export_t cmds[]={
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
 	{"redis_free", (cmd_function)w_redis_free_reply, 1, fixup_spve_null,
 	{"redis_free", (cmd_function)w_redis_free_reply, 1, fixup_spve_null,
 		0, ANY_ROUTE},
 		0, ANY_ROUTE},
+
+	{"bind_ndb_redis",  (cmd_function)bind_ndb_redis,  0,
+		0, 0, 0},
+
 	{0, 0, 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);
 			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;
+}
+

+ 1 - 1
src/modules/ndb_redis/redis_client.c

@@ -446,7 +446,7 @@ error_exec:
  * @param argvlen vector of command string lenghts or NULL.
  * @param argvlen vector of command string lenghts or NULL.
  * @return redisReply structure or NULL if there was an error.
  * @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)
 		const size_t *argvlen)
 {
 {
 	redisReply *res=NULL;
 	redisReply *res=NULL;

+ 2 - 1
src/modules/ndb_redis/redis_client.h

@@ -62,7 +62,8 @@ int redisc_reconnect_server(redisc_server_t *rsrv);
 
 
 /* Command related functions */
 /* Command related functions */
 int redisc_exec(str *srv, str *res, str *cmd, ...);
 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);
 redisc_reply_t *redisc_get_reply(str *name);
 int redisc_free_reply(str *name);
 int redisc_free_reply(str *name);
 int redisc_check_auth(redisc_server_t *rsrv, char *pass);
 int redisc_check_auth(redisc_server_t *rsrv, char *pass);