Skip to content

Commit bff9aab

Browse files
committedAug 28, 2015
Merge pull request signalwire#450 in FS/freeswitch from mod_hiredis to master
* commit 'd582e08da8e98912966f5cb6d9d7c1df8d828996': Startiing the deprecation mod_redis in favor of mod_hiredis. FS-8075
2 parents db63d5d + d582e08 commit bff9aab

File tree

10 files changed

+737
-1
lines changed

10 files changed

+737
-1
lines changed
 

‎build/modules.conf.in

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ applications/mod_fifo
2323
#applications/mod_fsk
2424
applications/mod_fsv
2525
applications/mod_hash
26+
#applications/mod_hiredis
2627
applications/mod_httapi
2728
#applications/mod_http_cache
2829
#applications/mod_ladspa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<configuration name="hiredis.conf" description="mod_hiredis">
2+
<profiles>
3+
<profile name="default">
4+
<connections>
5+
<connection name="primary">
6+
<param name="hostname" value="172.18.101.101"/>
7+
<param name="password" value="redis"/>
8+
<param name="port" value="6379"/>
9+
<param name="timeout_ms" value="500"/>
10+
</connection>
11+
<connection name="secondary">
12+
<param name="hostname" value="localhost"/>
13+
<param name="password" value="redis"/>
14+
<param name="port" value="6380"/>
15+
<param name="timeout_ms" value="500"/>
16+
</connection>
17+
</connections>
18+
<params>
19+
<param name="debug" value="1"/>
20+
</params>
21+
</profile>
22+
</profiles>
23+
</configuration>

‎configure.ac

+5
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,10 @@ PKG_CHECK_MODULES([SMPP34], [libsmpp34 >= 1.10],[
13901390
AM_CONDITIONAL([HAVE_SMPP34],[true])],[
13911391
AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_SMPP34],[false])])
13921392

1393+
PKG_CHECK_MODULES([HIREDIS], [hiredis >= 0.11.0],[
1394+
AM_CONDITIONAL([HAVE_HIREDIS],[true])],[
1395+
AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_HIREDIS],[false])])
1396+
13931397
AC_ARG_ENABLE(core-libedit-support,
13941398
[AS_HELP_STRING([--disable-core-libedit-support], [Compile without libedit Support])])
13951399

@@ -1688,6 +1692,7 @@ AC_CONFIG_FILES([Makefile
16881692
src/mod/applications/mod_fsk/Makefile
16891693
src/mod/applications/mod_fsv/Makefile
16901694
src/mod/applications/mod_hash/Makefile
1695+
src/mod/applications/mod_hiredis/Makefile
16911696
src/mod/applications/mod_httapi/Makefile
16921697
src/mod/applications/mod_http_cache/Makefile
16931698
src/mod/applications/mod_ladspa/Makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include $(top_srcdir)/build/modmake.rulesam
2+
MODNAME=mod_hiredis
3+
4+
if HAVE_HIREDIS
5+
6+
mod_LTLIBRARIES = mod_hiredis.la
7+
mod_hiredis_la_SOURCES = mod_hiredis.c hiredis_utils.c hiredis_profile.c
8+
mod_hiredis_la_CFLAGS = $(AM_CFLAGS) $(HIREDIS_CFLAGS)
9+
mod_hiredis_la_LIBADD = $(switch_builddir)/libfreeswitch.la
10+
mod_hiredis_la_LDFLAGS = -avoid-version -module -no-undefined -shared $(HIREDIS_LIBS) $(SWITCH_AM_LDFLAGS)
11+
12+
else
13+
install: error
14+
all: error
15+
error:
16+
$(error You must install libhiredis-dev to build this module)
17+
endif

‎src/mod/applications/mod_hiredis/TODO

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1. jsapi interface(mod_verto)
2+
2. add lock for hiredis_profile for destroy vs running commands
3+
3. Look into refactor/cleanup of xml processing
4+
4. Add tab complete for profile names for APIs, and possibly for supported actions(and in theory look into key listing from redis)
5+
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3+
* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
4+
*
5+
* Version: MPL 1.1
6+
*
7+
* The contents of this file are subject to the Mozilla Public License Version
8+
* 1.1 (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
* http://www.mozilla.org/MPL/
11+
*
12+
* Software distributed under the License is distributed on an "AS IS" basis,
13+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14+
* for the specific language governing rights and limitations under the
15+
* License.
16+
*
17+
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18+
*
19+
* The Initial Developer of the Original Code is
20+
* Anthony Minessale II <anthm@freeswitch.org>
21+
* Portions created by the Initial Developer are Copyright (C)
22+
* the Initial Developer. All Rights Reserved.
23+
*
24+
* Contributor(s):
25+
*
26+
* William King <william.king@quentustech.com>
27+
*
28+
* mod_hiredis.c -- redis client built using the C client library hiredis
29+
*
30+
*/
31+
32+
#include <mod_hiredis.h>
33+
34+
switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t port)
35+
{
36+
hiredis_profile_t *profile = NULL;
37+
switch_memory_pool_t *pool = NULL;
38+
39+
switch_core_new_memory_pool(&pool);
40+
41+
profile = switch_core_alloc(pool, sizeof(hiredis_profile_t));
42+
43+
profile->pool = pool;
44+
profile->name = name ? switch_core_strdup(profile->pool, name) : "default";
45+
profile->conn = NULL;
46+
profile->conn_head = NULL;
47+
48+
switch_core_hash_insert(mod_hiredis_globals.profiles, name, (void *) profile);
49+
50+
*new_profile = profile;
51+
52+
return SWITCH_STATUS_SUCCESS;
53+
}
54+
55+
switch_status_t hiredis_profile_destroy(hiredis_profile_t **old_profile)
56+
{
57+
hiredis_profile_t *profile = NULL;
58+
59+
if ( !old_profile || !*old_profile ) {
60+
return SWITCH_STATUS_SUCCESS;
61+
} else {
62+
profile = *old_profile;
63+
*old_profile = NULL;
64+
}
65+
66+
switch_core_hash_delete(mod_hiredis_globals.profiles, profile->name);
67+
switch_core_destroy_memory_pool(&(profile->pool));
68+
69+
return SWITCH_STATUS_SUCCESS;
70+
}
71+
72+
switch_status_t hiredis_profile_connection_add(hiredis_profile_t *profile, char *host, char *password, uint32_t port, uint32_t timeout_ms)
73+
{
74+
hiredis_connection_t *connection = NULL, *new_conn = NULL;
75+
76+
new_conn = switch_core_alloc(profile->pool, sizeof(hiredis_connection_t));
77+
new_conn->host = host ? switch_core_strdup(profile->pool, host) : "localhost";
78+
new_conn->password = password ? switch_core_strdup(profile->pool, password) : NULL;
79+
new_conn->port = port ? port : 6379;
80+
81+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "hiredis: adding conn[%d]\n", new_conn->port);
82+
83+
if ( timeout_ms ) {
84+
new_conn->timeout.tv_sec = 0;
85+
new_conn->timeout.tv_usec = timeout_ms * 1000;
86+
} else {
87+
new_conn->timeout.tv_sec = 0;
88+
new_conn->timeout.tv_usec = 500 * 1000;
89+
}
90+
91+
if ( profile->conn_head != NULL ){
92+
/* Adding 'another' connection */
93+
connection = profile->conn_head;
94+
while ( connection->next != NULL ){
95+
connection = connection->next;
96+
}
97+
connection->next = new_conn;
98+
} else {
99+
profile->conn_head = new_conn;
100+
}
101+
102+
return SWITCH_STATUS_SUCCESS;
103+
}
104+
105+
switch_status_t hiredis_profile_reconnect(hiredis_profile_t *profile)
106+
{
107+
hiredis_connection_t *conn = profile->conn_head;
108+
profile->conn = NULL;
109+
110+
/* TODO: Needs thorough expansion to handle all disconnection scenarios */
111+
112+
while ( conn ) {
113+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hiredis: attempting[%s, %d]\n", conn->host, conn->port);
114+
conn->context = redisConnectWithTimeout(conn->host, conn->port, conn->timeout);
115+
116+
if ( conn->context && conn->context->err) {
117+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: connection error[%s]\n", conn->context->errstr);
118+
conn = conn->next;
119+
continue;
120+
}
121+
122+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hiredis: connection success[%s]\n", conn->host);
123+
124+
/* successful redis connection */
125+
profile->conn = conn;
126+
return SWITCH_STATUS_SUCCESS;
127+
}
128+
129+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: unable to reconnect\n");
130+
return SWITCH_STATUS_GENERR;
131+
}
132+
133+
switch_status_t hiredis_profile_execute_sync(hiredis_profile_t *profile, const char *data, char **resp)
134+
{
135+
char *str = NULL;
136+
redisReply *response = NULL;
137+
138+
/* Check connection */
139+
if ( !profile->conn && hiredis_profile_reconnect(profile) != SWITCH_STATUS_SUCCESS ) {
140+
*resp = strdup("hiredis profile unable to establish connection");
141+
return SWITCH_STATUS_GENERR;
142+
}
143+
144+
response = redisCommand(profile->conn->context, data);
145+
146+
switch(response->type) {
147+
case REDIS_REPLY_STATUS: /* fallthrough */
148+
case REDIS_REPLY_STRING:
149+
str = strdup(response->str);
150+
break;
151+
case REDIS_REPLY_INTEGER:
152+
str = switch_mprintf("%lld", response->integer);
153+
break;
154+
default:
155+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: response error[%s][%d]\n", response->str, response->type);
156+
freeReplyObject(response);
157+
return SWITCH_STATUS_GENERR;
158+
}
159+
160+
freeReplyObject(response);
161+
162+
*resp = str;
163+
return SWITCH_STATUS_SUCCESS;
164+
}
165+
166+
167+
/* For Emacs:
168+
* Local Variables:
169+
* mode:c
170+
* indent-tabs-mode:t
171+
* tab-width:4
172+
* c-basic-offset:4
173+
* End:
174+
* For VIM:
175+
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
176+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3+
* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
4+
*
5+
* Version: MPL 1.1
6+
*
7+
* The contents of this file are subject to the Mozilla Public License Version
8+
* 1.1 (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
* http://www.mozilla.org/MPL/
11+
*
12+
* Software distributed under the License is distributed on an "AS IS" basis,
13+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14+
* for the specific language governing rights and limitations under the
15+
* License.
16+
*
17+
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18+
*
19+
* The Initial Developer of the Original Code is
20+
* Anthony Minessale II <anthm@freeswitch.org>
21+
* Portions created by the Initial Developer are Copyright (C)
22+
* the Initial Developer. All Rights Reserved.
23+
*
24+
* Contributor(s):
25+
*
26+
* William King <william.king@quentustech.com>
27+
*
28+
* mod_hiredis.c -- redis client built using the C client library hiredis
29+
*
30+
*/
31+
32+
#include <mod_hiredis.h>
33+
34+
switch_status_t mod_hiredis_do_config()
35+
{
36+
char *conf = "hiredis.conf";
37+
switch_xml_t xml, cfg, profiles, profile, connections, connection, params, param;
38+
39+
if (!(xml = switch_xml_open_cfg(conf, &cfg, NULL))) {
40+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", conf);
41+
goto err;
42+
}
43+
44+
if ( (profiles = switch_xml_child(cfg, "profiles")) != NULL) {
45+
for (profile = switch_xml_child(profiles, "profile"); profile; profile = profile->next) {
46+
hiredis_profile_t *new_profile = NULL;
47+
int debug = 0;
48+
char *name = (char *) switch_xml_attr_soft(profile, "name");
49+
50+
// Load params
51+
if ( (params = switch_xml_child(profile, "params")) != NULL) {
52+
for (param = switch_xml_child(params, "param"); param; param = param->next) {
53+
char *var = (char *) switch_xml_attr_soft(param, "name");
54+
if ( ! strncmp(var, "debug", 5) ) {
55+
debug = atoi(switch_xml_attr_soft(param, "value"));
56+
}
57+
}
58+
}
59+
60+
if ( hiredis_profile_create(&new_profile, name, debug) == SWITCH_STATUS_SUCCESS) {
61+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created profile[%s]\n", name);
62+
} else {
63+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create profile[%s]\n", name);
64+
}
65+
66+
/* Add connection to profile */
67+
if ( (connections = switch_xml_child(profile, "connections")) != NULL) {
68+
for (connection = switch_xml_child(connections, "connection"); connection; connection = connection->next) {
69+
char *host = NULL, *password = NULL;
70+
uint32_t port = 0, timeout_ms = 0;
71+
72+
for (param = switch_xml_child(connection, "param"); param; param = param->next) {
73+
char *var = (char *) switch_xml_attr_soft(param, "name");
74+
if ( !strncmp(var, "hostname", 8) ) {
75+
host = (char *) switch_xml_attr_soft(param, "value");
76+
} else if ( !strncmp(var, "port", 4) ) {
77+
port = atoi(switch_xml_attr_soft(param, "value"));
78+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "hiredis: adding conn[%u == %s]\n", port, switch_xml_attr_soft(param, "value"));
79+
} else if ( !strncmp(var, "timeout_ms", 10) ) {
80+
timeout_ms = atoi(switch_xml_attr_soft(param, "value"));
81+
} else if ( !strncmp(var, "password", 8) ) {
82+
password = (char *) switch_xml_attr_soft(param, "value");
83+
}
84+
}
85+
86+
if ( hiredis_profile_connection_add(new_profile, host, password, port, timeout_ms) == SWITCH_STATUS_SUCCESS) {
87+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created profile[%s]\n", name);
88+
} else {
89+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create profile[%s]\n", name);
90+
}
91+
}
92+
} else {
93+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile->connections config is missing\n");
94+
goto err;
95+
}
96+
}
97+
} else {
98+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profiles config is missing\n");
99+
goto err;
100+
}
101+
102+
return SWITCH_STATUS_SUCCESS;
103+
104+
err:
105+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Configuration failed\n");
106+
return SWITCH_STATUS_GENERR;
107+
}
108+
109+
/* For Emacs:
110+
* Local Variables:
111+
* mode:c
112+
* indent-tabs-mode:t
113+
* tab-width:4
114+
* c-basic-offset:4
115+
* End:
116+
* For VIM:
117+
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
118+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.