]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-8075
authorWilliam King <william.king@quentustech.com>
Sat, 9 May 2015 01:02:28 +0000 (18:02 -0700)
committerWilliam King <william.king@quentustech.com>
Fri, 28 Aug 2015 23:56:38 +0000 (16:56 -0700)
Initial commit of mod_hiredis. Add expanded redis support to include
raw redis command support, as well as the existing support for
FreeSWITCH limits.

build/modules.conf.in
conf/vanilla/autoload_configs/hiredis.conf.xml [new file with mode: 0644]
configure.ac
src/mod/applications/mod_hiredis/Makefile.am [new file with mode: 0644]
src/mod/applications/mod_hiredis/TODO [new file with mode: 0644]
src/mod/applications/mod_hiredis/hiredis_profile.c [new file with mode: 0644]
src/mod/applications/mod_hiredis/hiredis_utils.c [new file with mode: 0644]
src/mod/applications/mod_hiredis/mod_hiredis.c [new file with mode: 0644]
src/mod/applications/mod_hiredis/mod_hiredis.h [new file with mode: 0644]

index dff299fb688d26346ed32cd7487896e6b6aa8033..f87353c193064dadb1a3a116a97685b439ef5418 100644 (file)
@@ -23,6 +23,7 @@ applications/mod_fifo
 #applications/mod_fsk
 applications/mod_fsv
 applications/mod_hash
+#applications/mod_hiredis
 applications/mod_httapi
 #applications/mod_http_cache
 #applications/mod_ladspa
diff --git a/conf/vanilla/autoload_configs/hiredis.conf.xml b/conf/vanilla/autoload_configs/hiredis.conf.xml
new file mode 100644 (file)
index 0000000..4288cb4
--- /dev/null
@@ -0,0 +1,23 @@
+<configuration name="hiredis.conf" description="mod_hiredis">
+  <profiles>
+    <profile name="default">
+      <connections>
+       <connection name="primary">
+         <param name="hostname" value="172.18.101.101"/>
+         <param name="password" value="redis"/>        
+         <param name="port" value="6379"/>
+         <param name="timeout_ms" value="500"/>
+       </connection>
+       <connection name="secondary">
+         <param name="hostname" value="localhost"/>
+         <param name="password" value="redis"/>        
+         <param name="port" value="6380"/>
+         <param name="timeout_ms" value="500"/>
+       </connection>
+      </connections>
+      <params>
+       <param name="debug" value="1"/>
+      </params>
+    </profile>
+  </profiles>
+</configuration>
index 9233515c472c6d79bc1a7afcac35aed14a87abdb..fa11dcbd93a2b6f25ce7ec41d683e72d9ae1998e 100644 (file)
@@ -1390,6 +1390,10 @@ PKG_CHECK_MODULES([SMPP34], [libsmpp34 >= 1.10],[
   AM_CONDITIONAL([HAVE_SMPP34],[true])],[
   AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_SMPP34],[false])])
 
+PKG_CHECK_MODULES([HIREDIS], [hiredis >= 0.11.0],[
+  AM_CONDITIONAL([HAVE_HIREDIS],[true])],[
+  AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_HIREDIS],[false])])
+
 AC_ARG_ENABLE(core-libedit-support,     
   [AS_HELP_STRING([--disable-core-libedit-support], [Compile without libedit Support])])
 
@@ -1688,6 +1692,7 @@ AC_CONFIG_FILES([Makefile
                src/mod/applications/mod_fsk/Makefile
                src/mod/applications/mod_fsv/Makefile
                src/mod/applications/mod_hash/Makefile
+               src/mod/applications/mod_hiredis/Makefile
                src/mod/applications/mod_httapi/Makefile
                src/mod/applications/mod_http_cache/Makefile
                src/mod/applications/mod_ladspa/Makefile
diff --git a/src/mod/applications/mod_hiredis/Makefile.am b/src/mod/applications/mod_hiredis/Makefile.am
new file mode 100644 (file)
index 0000000..5b6e6b7
--- /dev/null
@@ -0,0 +1,17 @@
+include $(top_srcdir)/build/modmake.rulesam
+MODNAME=mod_hiredis
+
+if HAVE_HIREDIS
+
+mod_LTLIBRARIES = mod_hiredis.la
+mod_hiredis_la_SOURCES  = mod_hiredis.c hiredis_utils.c hiredis_profile.c
+mod_hiredis_la_CFLAGS   = $(AM_CFLAGS) $(HIREDIS_CFLAGS)
+mod_hiredis_la_LIBADD   = $(switch_builddir)/libfreeswitch.la
+mod_hiredis_la_LDFLAGS  = -avoid-version -module -no-undefined -shared $(HIREDIS_LIBS) $(SWITCH_AM_LDFLAGS)
+
+else
+install: error
+all: error
+error:
+       $(error You must install libhiredis-dev to build this module)
+endif
diff --git a/src/mod/applications/mod_hiredis/TODO b/src/mod/applications/mod_hiredis/TODO
new file mode 100644 (file)
index 0000000..9966f0c
--- /dev/null
@@ -0,0 +1,6 @@
+1. jsapi interface(mod_verto)
+2. add lock for hiredis_profile for destroy vs running commands
+3. Look into refactor/cleanup of xml processing
+4. Add tab complete for profile names for APIs, and possibly for supported actions(and in theory look into key listing from redis)
+
+
diff --git a/src/mod/applications/mod_hiredis/hiredis_profile.c b/src/mod/applications/mod_hiredis/hiredis_profile.c
new file mode 100644 (file)
index 0000000..f83f5cf
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
+*
+* Version: MPL 1.1
+*
+* The contents of this file are subject to the Mozilla Public License Version
+* 1.1 (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+* http://www.mozilla.org/MPL/
+*
+* Software distributed under the License is distributed on an "AS IS" basis,
+* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+* for the specific language governing rights and limitations under the
+* License.
+*
+* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+*
+* The Initial Developer of the Original Code is
+* Anthony Minessale II <anthm@freeswitch.org>
+* Portions created by the Initial Developer are Copyright (C)
+* the Initial Developer. All Rights Reserved.
+*
+* Contributor(s):
+*
+* William King <william.king@quentustech.com>
+*
+* mod_hiredis.c -- redis client built using the C client library hiredis
+*
+*/
+
+#include <mod_hiredis.h>
+       
+switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t port)
+{
+       hiredis_profile_t *profile = NULL;
+       switch_memory_pool_t *pool = NULL;
+       
+       switch_core_new_memory_pool(&pool);
+
+       profile = switch_core_alloc(pool, sizeof(hiredis_profile_t));
+
+       profile->pool = pool;
+       profile->name = name ? switch_core_strdup(profile->pool, name) : "default";
+       profile->conn = NULL;
+       profile->conn_head = NULL;
+
+       switch_core_hash_insert(mod_hiredis_globals.profiles, name, (void *) profile);
+
+       *new_profile = profile;
+       
+       return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t hiredis_profile_destroy(hiredis_profile_t **old_profile)
+{
+       hiredis_profile_t *profile = NULL;
+
+       if ( !old_profile || !*old_profile ) {
+               return SWITCH_STATUS_SUCCESS;
+       } else {
+               profile = *old_profile;
+               *old_profile = NULL;
+       }
+
+       switch_core_hash_delete(mod_hiredis_globals.profiles, profile->name);
+       switch_core_destroy_memory_pool(&(profile->pool));
+
+       return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t hiredis_profile_connection_add(hiredis_profile_t *profile, char *host, char *password, uint32_t port, uint32_t timeout_ms)
+{
+       hiredis_connection_t *connection = NULL, *new_conn = NULL;
+
+       new_conn = switch_core_alloc(profile->pool, sizeof(hiredis_connection_t));
+       new_conn->host = host ? switch_core_strdup(profile->pool, host) : "localhost";
+       new_conn->password = password ? switch_core_strdup(profile->pool, password) : NULL;
+       new_conn->port = port ? port : 6379;
+
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "hiredis: adding conn[%d]\n", new_conn->port);
+
+       if ( timeout_ms ) {
+               new_conn->timeout.tv_sec = 0;
+               new_conn->timeout.tv_usec = timeout_ms * 1000;
+       } else {
+               new_conn->timeout.tv_sec = 0;
+               new_conn->timeout.tv_usec = 500 * 1000;
+       }
+       
+       if ( profile->conn_head != NULL ){
+               /* Adding 'another' connection */
+               connection = profile->conn_head;
+               while ( connection->next != NULL ){
+                       connection = connection->next;
+               }
+               connection->next = new_conn;
+       } else {
+               profile->conn_head = new_conn;
+       }
+
+       return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t hiredis_profile_reconnect(hiredis_profile_t *profile)
+{
+       hiredis_connection_t *conn = profile->conn_head;
+       profile->conn = NULL;
+
+       /* TODO: Needs thorough expansion to handle all disconnection scenarios */
+       
+       while ( conn ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hiredis: attempting[%s, %d]\n", conn->host, conn->port);
+               conn->context = redisConnectWithTimeout(conn->host, conn->port, conn->timeout);
+       
+               if ( conn->context && conn->context->err) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: connection error[%s]\n", conn->context->errstr);
+                       conn = conn->next;
+                       continue;
+               }
+
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hiredis: connection success[%s]\n", conn->host);
+                       
+               /* successful redis connection */
+               profile->conn = conn;
+               return SWITCH_STATUS_SUCCESS;
+       }
+
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: unable to reconnect\n");
+       return SWITCH_STATUS_GENERR;
+}
+
+switch_status_t hiredis_profile_execute_sync(hiredis_profile_t *profile, const char *data, char **resp)
+{
+       char *str = NULL;
+       redisReply *response = NULL;
+
+       /* Check connection */
+       if ( !profile->conn && hiredis_profile_reconnect(profile) != SWITCH_STATUS_SUCCESS ) {
+               *resp = strdup("hiredis profile unable to establish connection");
+               return SWITCH_STATUS_GENERR;
+       }
+       
+       response = redisCommand(profile->conn->context, data);
+
+       switch(response->type) {
+       case REDIS_REPLY_STATUS: /* fallthrough */
+       case REDIS_REPLY_STRING:
+               str = strdup(response->str);
+               break;
+       case REDIS_REPLY_INTEGER:
+               str = switch_mprintf("%lld", response->integer);
+               break;
+       default:
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: response error[%s][%d]\n", response->str, response->type);
+               freeReplyObject(response);
+               return SWITCH_STATUS_GENERR;
+       }
+
+       freeReplyObject(response);
+
+       *resp = str;
+       return SWITCH_STATUS_SUCCESS;
+}
+
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
+ */
diff --git a/src/mod/applications/mod_hiredis/hiredis_utils.c b/src/mod/applications/mod_hiredis/hiredis_utils.c
new file mode 100644 (file)
index 0000000..8b552af
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
+*
+* Version: MPL 1.1
+*
+* The contents of this file are subject to the Mozilla Public License Version
+* 1.1 (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+* http://www.mozilla.org/MPL/
+*
+* Software distributed under the License is distributed on an "AS IS" basis,
+* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+* for the specific language governing rights and limitations under the
+* License.
+*
+* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+*
+* The Initial Developer of the Original Code is
+* Anthony Minessale II <anthm@freeswitch.org>
+* Portions created by the Initial Developer are Copyright (C)
+* the Initial Developer. All Rights Reserved.
+*
+* Contributor(s):
+*
+* William King <william.king@quentustech.com>
+*
+* mod_hiredis.c -- redis client built using the C client library hiredis
+*
+*/
+
+#include <mod_hiredis.h>
+
+switch_status_t mod_hiredis_do_config() 
+{
+       char *conf = "hiredis.conf";
+       switch_xml_t xml, cfg, profiles, profile, connections, connection, params, param;
+
+       if (!(xml = switch_xml_open_cfg(conf, &cfg, NULL))) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", conf);
+               goto err;
+       }
+
+       if ( (profiles = switch_xml_child(cfg, "profiles")) != NULL) {
+               for (profile = switch_xml_child(profiles, "profile"); profile; profile = profile->next) {               
+                       hiredis_profile_t *new_profile = NULL;
+                       int debug = 0;
+                       char *name = (char *) switch_xml_attr_soft(profile, "name");
+
+                       // Load params
+                       if ( (params = switch_xml_child(profile, "params")) != NULL) {
+                               for (param = switch_xml_child(params, "param"); param; param = param->next) {
+                                       char *var = (char *) switch_xml_attr_soft(param, "name");
+                                       if ( ! strncmp(var, "debug", 5) ) {
+                                               debug = atoi(switch_xml_attr_soft(param, "value"));
+                                       }
+                               }
+                       }
+
+                       if ( hiredis_profile_create(&new_profile, name, debug) == SWITCH_STATUS_SUCCESS) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created profile[%s]\n", name);
+                       } else {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create profile[%s]\n", name);
+                       }
+
+                       /* Add connection to profile */
+                       if ( (connections = switch_xml_child(profile, "connections")) != NULL) {
+                               for (connection = switch_xml_child(connections, "connection"); connection; connection = connection->next) {             
+                                       char *host = NULL, *password = NULL;
+                                       uint32_t port = 0, timeout_ms = 0;
+                                       
+                                       for (param = switch_xml_child(connection, "param"); param; param = param->next) {
+                                               char *var = (char *) switch_xml_attr_soft(param, "name");
+                                               if ( !strncmp(var, "hostname", 8) ) {
+                                                       host = (char *) switch_xml_attr_soft(param, "value");
+                                               } else if ( !strncmp(var, "port", 4) ) {
+                                                       port = atoi(switch_xml_attr_soft(param, "value"));
+                                                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "hiredis: adding conn[%u == %s]\n", port, switch_xml_attr_soft(param, "value"));
+                                               } else if ( !strncmp(var, "timeout_ms", 10) ) {
+                                                       timeout_ms = atoi(switch_xml_attr_soft(param, "value"));
+                                               } else if ( !strncmp(var, "password", 8) ) {
+                                                       password = (char *) switch_xml_attr_soft(param, "value");
+                                               }
+                                       }
+
+                                       if ( hiredis_profile_connection_add(new_profile, host, password, port, timeout_ms) == SWITCH_STATUS_SUCCESS) {
+                                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created profile[%s]\n", name);
+                                       } else {
+                                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create profile[%s]\n", name);
+                                       }
+                               }
+                       } else {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile->connections config is missing\n");
+                               goto err;
+                       }
+               }
+       } else {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profiles config is missing\n");
+               goto err;
+       }
+       
+       return SWITCH_STATUS_SUCCESS;
+       
+ err:
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Configuration failed\n");
+       return SWITCH_STATUS_GENERR;
+}
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
+ */
diff --git a/src/mod/applications/mod_hiredis/mod_hiredis.c b/src/mod/applications/mod_hiredis/mod_hiredis.c
new file mode 100644 (file)
index 0000000..f5cc90f
--- /dev/null
@@ -0,0 +1,344 @@
+/*
+* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+* Copyright (C) 2005-2012, Anthony Minessale II <anthm@freeswitch.org>
+*
+* Version: MPL 1.1
+*
+* The contents of this file are subject to the Mozilla Public License Version
+* 1.1 (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+* http://www.mozilla.org/MPL/
+*
+* Software distributed under the License is distributed on an "AS IS" basis,
+* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+* for the specific language governing rights and limitations under the
+* License.
+*
+* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+*
+* The Initial Developer of the Original Code is
+* Anthony Minessale II <anthm@freeswitch.org>
+* Portions created by the Initial Developer are Copyright (C)
+* the Initial Developer. All Rights Reserved.
+*
+* Contributor(s):
+* William King <william.king@quentustech.com>
+*
+* mod_hiredis.c -- Redis DB access module
+*
+*/
+
+#include "mod_hiredis.h"
+
+mod_hiredis_global_t mod_hiredis_globals;
+
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_hiredis_shutdown);
+SWITCH_MODULE_LOAD_FUNCTION(mod_hiredis_load);
+SWITCH_MODULE_DEFINITION(mod_hiredis, mod_hiredis_load, mod_hiredis_shutdown, NULL);
+
+SWITCH_STANDARD_APP(raw_app)
+{
+       switch_channel_t *channel = switch_core_session_get_channel(session);
+       char *response = NULL, *profile_name = NULL, *cmd = NULL;
+       hiredis_profile_t *profile = NULL;
+       
+       if ( !zstr(data) ) {
+               profile_name = strdup(data);
+       } else {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: invalid data! Use the format 'default set keyname value' \n");
+               goto done;
+       }
+
+       if ( (cmd = strchr(profile_name, ' '))) {
+               *cmd = '\0';
+               cmd++;
+       } else {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: invalid data! Use the format 'default set keyname value' \n");
+               goto done;
+       }
+
+       profile = switch_core_hash_find(mod_hiredis_globals.profiles, profile_name);
+
+       if ( !profile ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", profile_name);
+               return;
+       }
+       
+       if ( hiredis_profile_execute_sync(profile, data, &response) != SWITCH_STATUS_SUCCESS) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", profile_name, cmd, response);
+       }
+
+       switch_channel_set_variable(channel, "hiredis_raw_response", response);
+
+ done:
+       switch_safe_free(profile_name);
+       switch_safe_free(response);
+       return;
+}
+
+SWITCH_STANDARD_API(raw_api)
+{
+       hiredis_profile_t *profile = NULL;
+       char *data = NULL, *input = NULL, *response = NULL;
+       switch_status_t status = SWITCH_STATUS_SUCCESS;
+       
+       if ( !zstr(cmd) ) {
+               input = strdup(cmd);
+       } else {
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+       
+       if ( (data = strchr(input, ' '))) {
+               *data = '\0';
+               data++;
+       }
+
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: debug: profile[%s] for command [%s]\n", input, data);
+
+       profile = switch_core_hash_find(mod_hiredis_globals.profiles, input);
+
+       if ( !profile ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", input);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+       
+       if ( hiredis_profile_execute_sync(profile, data, &response) != SWITCH_STATUS_SUCCESS) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] reason:[%s]\n", input, data, response);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+       stream->write_function(stream, response);
+ done:
+       switch_safe_free(input);
+       switch_safe_free(response);
+       return status;
+}
+
+/* 
+SWITCH_LIMIT_INCR(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource, 
+                                                     const int max, const int interval)
+*/
+SWITCH_LIMIT_INCR(hiredis_limit_incr)
+{
+       switch_channel_t *channel = switch_core_session_get_channel(session);
+       hiredis_profile_t *profile = NULL;
+       char *hashkey = NULL, *response = NULL;
+       int64_t count = 0; /* Redis defines the incr action as to be performed on a 64 bit signed integer */
+       time_t now = switch_epoch_time_now(NULL);
+       switch_status_t status = SWITCH_STATUS_SUCCESS;
+
+       if ( !zstr(realm) ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+       
+       profile = switch_core_hash_find(mod_hiredis_globals.profiles, realm);
+
+       if ( !profile ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", realm);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+       if ( interval ) {
+               hashkey = switch_mprintf("incr %s_%d", resource, now % interval);
+       } else {
+               hashkey = switch_mprintf("incr %s", resource);
+       }
+
+       if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response);
+               switch_channel_set_variable(channel, "hiredis_raw_response", response);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+       switch_channel_set_variable(channel, "hiredis_raw_response", response);
+
+       count = atoll(response);
+
+       if ( !count || count > max ) {
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+ done:
+       switch_safe_free(response);
+       switch_safe_free(hashkey);
+       return status;
+}
+
+/*
+  SWITCH_LIMIT_RELEASE(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)
+*/
+SWITCH_LIMIT_RELEASE(hiredis_limit_release)
+{
+       switch_channel_t *channel = switch_core_session_get_channel(session);
+       hiredis_profile_t *profile = switch_core_hash_find(mod_hiredis_globals.profiles, realm);
+       char *hashkey = NULL, *response = NULL;
+       switch_status_t status = SWITCH_STATUS_SUCCESS;
+
+       if ( !zstr(realm) ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+       
+       if ( !profile ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", realm);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+       hashkey = switch_mprintf("decr %s", resource);
+
+       if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response);
+               switch_channel_set_variable(channel, "hiredis_raw_response", response);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+       switch_channel_set_variable(channel, "hiredis_raw_response", response);
+
+ done:
+       switch_safe_free(response);
+       switch_safe_free(hashkey);
+       return status;
+}
+
+/*
+SWITCH_LIMIT_USAGE(name) static int name (const char *realm, const char *resource, uint32_t *rcount)
+ */
+SWITCH_LIMIT_USAGE(hiredis_limit_usage)
+{
+       hiredis_profile_t *profile = switch_core_hash_find(mod_hiredis_globals.profiles, realm);
+       int64_t count = 0; /* Redis defines the incr action as to be performed on a 64 bit signed integer */    
+       char *hashkey = NULL, *response = NULL;
+       
+       if ( !zstr(realm) ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
+               goto err;
+       }
+       
+       if ( !profile ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", realm);
+               goto err;
+       }
+
+       hashkey = switch_mprintf("get %s", resource);
+
+       if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response);
+               goto err;
+       }
+
+       count = atoll(response);
+
+       switch_safe_free(response);
+       switch_safe_free(hashkey);
+       return count;
+       
+ err:
+       switch_safe_free(response);
+       switch_safe_free(hashkey);
+       return -1;
+}
+
+/*
+SWITCH_LIMIT_RESET(name) static switch_status_t name (void)
+ */
+SWITCH_LIMIT_RESET(hiredis_limit_reset)
+{
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
+                                         "hiredis: unable to globally reset hiredis limit resources. Use 'hiredis_raw set resource_name 0'\n");
+               
+       return SWITCH_STATUS_GENERR;
+}
+
+/*
+  SWITCH_LIMIT_INTERVAL_RESET(name) static switch_status_t name (const char *realm, const char *resource)
+*/
+SWITCH_LIMIT_INTERVAL_RESET(hiredis_limit_interval_reset)
+{
+       hiredis_profile_t *profile = switch_core_hash_find(mod_hiredis_globals.profiles, realm);
+       switch_status_t status = SWITCH_STATUS_SUCCESS;
+       char *hashkey = NULL, *response = NULL;
+       
+       if ( !zstr(realm) ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+       
+       if ( !profile ) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", realm);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+       hashkey = switch_mprintf("set %s 0", resource);
+
+       if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response);
+               switch_goto_status(SWITCH_STATUS_GENERR, done);
+       }
+
+ done:
+       switch_safe_free(response);
+       switch_safe_free(hashkey);
+       return status;
+}
+
+/*
+SWITCH_LIMIT_STATUS(name) static char * name (void)
+ */
+SWITCH_LIMIT_STATUS(hiredis_limit_status)
+{
+       return strdup("-ERR not supported");
+}
+
+SWITCH_MODULE_LOAD_FUNCTION(mod_hiredis_load)
+{
+       switch_application_interface_t *app_interface;
+       switch_api_interface_t *api_interface;
+       switch_limit_interface_t *limit_interface;
+
+       memset(&mod_hiredis_globals, 0, sizeof(mod_hiredis_global_t));
+       *module_interface = switch_loadable_module_create_module_interface(pool, modname);
+       mod_hiredis_globals.pool = pool;
+
+       switch_core_hash_init(&(mod_hiredis_globals.profiles));
+       
+       if ( mod_hiredis_do_config() != SWITCH_STATUS_SUCCESS ) {
+               return SWITCH_STATUS_GENERR;
+       }
+
+       SWITCH_ADD_LIMIT(limit_interface, "hiredis", hiredis_limit_incr, hiredis_limit_release, hiredis_limit_usage,
+                                        hiredis_limit_reset, hiredis_limit_status, hiredis_limit_interval_reset);
+       SWITCH_ADD_APP(app_interface, "hiredis_raw", "hiredis_raw", "hiredis_raw", raw_app, "", SAF_NONE);
+       SWITCH_ADD_API(api_interface, "hiredis_raw", "hiredis_raw", raw_api, "");
+
+       return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_hiredis_shutdown)
+{
+       switch_hash_index_t *hi;
+       hiredis_profile_t *profile = NULL;
+       /* loop through profiles, and destroy them */
+
+       while ((hi = switch_core_hash_first(mod_hiredis_globals.profiles))) {
+               switch_core_hash_this(hi, NULL, NULL, (void **)&profile);
+               hiredis_profile_destroy(&profile);
+               switch_safe_free(hi);
+       }
+
+       switch_core_hash_destroy(&(mod_hiredis_globals.profiles));
+       
+       return SWITCH_STATUS_SUCCESS;
+}
+
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4
+ */
diff --git a/src/mod/applications/mod_hiredis/mod_hiredis.h b/src/mod/applications/mod_hiredis/mod_hiredis.h
new file mode 100644 (file)
index 0000000..becb565
--- /dev/null
@@ -0,0 +1,42 @@
+
+#ifndef MOD_HIREDIS_H
+#define MOD_HIREDIS_H
+
+#include <switch.h>
+#include <hiredis/hiredis.h>
+
+typedef struct mod_hiredis_global_s {
+  switch_memory_pool_t *pool;
+  switch_hash_t *profiles;
+  uint8_t debug;
+} mod_hiredis_global_t;
+
+extern mod_hiredis_global_t mod_hiredis_globals;
+
+typedef struct hiredis_connection_s {
+  char *host;
+  char *password;
+  uint32_t port;
+  redisContext *context;
+  struct timeval timeout;
+
+  struct hiredis_connection_s *next;
+} hiredis_connection_t;
+
+typedef struct hiredis_profile_s {
+  switch_memory_pool_t *pool;
+  char *name;
+  int debug;
+
+  hiredis_connection_t *conn;
+  hiredis_connection_t *conn_head;  
+} hiredis_profile_t;
+
+switch_status_t mod_hiredis_do_config();
+switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t port);
+switch_status_t hiredis_profile_destroy(hiredis_profile_t **old_profile);
+switch_status_t hiredis_profile_connection_add(hiredis_profile_t *profile, char *host, char *password, uint32_t port, uint32_t timeout_ms);
+
+switch_status_t hiredis_profile_execute_sync(hiredis_profile_t *profile, const char *data, char **response);
+
+#endif /* MOD_HIREDIS_H */