Skip to content

Commit

Permalink
ndb_redis: export functions via inter-module api
Browse files Browse the repository at this point in the history
- allow other modules to work with same pool of connections managed by
  this module
  • Loading branch information
miconda committed Feb 20, 2017
1 parent 9ac5de5 commit 304d1cf
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
67 changes: 67 additions & 0 deletions 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 changes: 27 additions & 0 deletions src/modules/ndb_redis/ndb_redis_mod.c
Expand Up @@ -34,6 +34,7 @@
#include "../../core/trim.h"

#include "redis_client.h"
#include "api.h"

MODULE_VERSION

Expand All @@ -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);
Expand All @@ -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}
};

Expand Down Expand Up @@ -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;
}

2 changes: 1 addition & 1 deletion src/modules/ndb_redis/redis_client.c
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/modules/ndb_redis/redis_client.h
Expand Up @@ -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);
Expand Down

0 comments on commit 304d1cf

Please sign in to comment.