|
| 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 | + */ |
0 commit comments