<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][17079] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=17079">17079</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2010-03-23 11:17:15 -0500 (Tue, 23 Mar 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>in progress limit changes</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchbranchesrupa_limitsrcmodapplicationsmod_hashmod_hashc">freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchbranchesrupa_limitsrcincludeswitch_limith">freeswitch/branches/rupa_limit/src/include/switch_limit.h</a></li>
<li><a href="#freeswitchbranchesrupa_limitsrcswitch_limitc">freeswitch/branches/rupa_limit/src/switch_limit.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchbranchesrupa_limitsrcincludeswitch_limith"></a>
<div class="addfile"><h4>Added: freeswitch/branches/rupa_limit/src/include/switch_limit.h (0 => 17079)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/include/switch_limit.h         (rev 0)
+++ freeswitch/branches/rupa_limit/src/include/switch_limit.h        2010-03-23 16:17:15 UTC (rev 17079)
</span><span class="lines">@@ -0,0 +1,126 @@
</span><ins>+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2010, 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):
+ *
+ * Rupa Schomaker <rupa@rupa.com>
+ *
+ * switch_limit.h - Limit generic implementations
+ *
+ */
+
+
+ /*!
+ \defgroup limit1 LIMIT code
+ \ingroup core1
+ \{
+*/
+#ifndef _SWITCH_LIMIT_H
+#define _SWITCH_LIMIT_H
+
+SWITCH_BEGIN_EXTERN_C
+
+/*!
+ \brief Initilize the LIMIT Core System
+ \param pool the memory pool to use for long term allocations
+ \note Generally called by the core_init
+*/
+SWITCH_DECLARE(void) switch_limit_init(switch_memory_pool_t *pool);
+
+/*!
+ \brief Increment resource.
+ \param backend to use
+ \param realm
+ \param resource
+ \param max - 0 means no limit, just count
+ \param interval - 0 means no interval
+ \return true/false - true ok, false over limit
+*/
+SWITCH_DECLARE(switch_status_t) switch_limit_incr(const char *backend, switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval);
+
+/*!
+ \brief Release resource.
+ \param backend to use
+ \param realm
+ \param resource
+ \return true/false - true ok, false over limit
+*/
+SWITCH_DECLARE(switch_status_t) switch_limit_release(const char *backend, switch_core_session_t *session, const char *realm, const char *resource);
+
+/*!
+ \brief get usage count for resource
+ \param backend to use
+ \param realm
+ \param resource
+*/
+SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource);
+
+/*!
+ \brief reset all usage counters
+ \param backend to use
+*/
+SWITCH_DECLARE(switch_status_t) switch_limit_reset(const char *backend);
+
+/*!
+ \brief fire event for limit usage
+ \param backend to use
+ \param realm
+ \param resource
+ \param usage
+ \param rate
+ \param max
+ \param ratemax
+*/
+SWITCH_DECLARE(void) switch_limit_fire_event(const char *backend, const char *realm, const char *resource, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax);
+
+/*!
+ \brief retrieve arbitrary status information
+ \param backend to use
+ \note caller must free returned value
+*/
+SWITCH_DECLARE(char *) switch_limit_status(const char *backend);
+
+/*! callback to init a backend */
+#define 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)
+#define SWITCH_LIMIT_RELEASE(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)
+#define SWITCH_LIMIT_USAGE(name) static int name (const char *realm, const char *resource)
+#define SWITCH_LIMIT_RESET(name) static switch_status_t name (void)
+#define SWITCH_LIMIT_STATUS(name) static char * name (void)
+
+#define LIMIT_IGNORE_TRANSFER_VARIABLE "limit_ignore_transfer"
+#define LIMIT_BACKEND_VARIABLE "limit_backend"
+#define LIMIT_EVENT_USAGE "limit::usage"
+#define LIMIT_DEF_XFER_EXTEN "limit_exceeded"
+
+SWITCH_END_EXTERN_C
+#endif
+/* 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:
+ */
</ins></span></pre></div>
<a id="freeswitchbranchesrupa_limitsrcmodapplicationsmod_hashmod_hashc"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c (17078 => 17079)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c        2010-03-23 16:16:48 UTC (rev 17078)
+++ freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c        2010-03-23 16:17:15 UTC (rev 17079)
</span><span class="lines">@@ -42,9 +42,9 @@
</span><span class="cx"> /* CORE STUFF */
</span><span class="cx"> static struct {
</span><span class="cx">         switch_memory_pool_t *pool;
</span><del>-        switch_mutex_t *limit_hash_mutex;
</del><ins>+        switch_thread_rwlock_t *limit_hash_rwlock;
</ins><span class="cx">         switch_hash_t *limit_hash;
</span><del>-        switch_mutex_t *db_hash_mutex;
</del><ins>+        switch_thread_rwlock_t *db_hash_rwlock;
</ins><span class="cx">         switch_hash_t *db_hash;
</span><span class="cx"> } globals;
</span><span class="cx">
</span><span class="lines">@@ -87,7 +87,7 @@
</span><span class="cx">
</span><span class="cx">         hashkey = switch_core_session_sprintf(session, "%s_%s", realm, resource);
</span><span class="cx">
</span><del>-        switch_mutex_lock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_wrlock(globals.limit_hash_rwlock);
</ins><span class="cx">         /* Check if that realm+resource has ever been checked */
</span><span class="cx">         if (!(item = (limit_hash_item_t *) switch_core_hash_find(globals.limit_hash, hashkey))) {
</span><span class="cx">                 /* No, create an empty structure and add it, then continue like as if it existed */
</span><span class="lines">@@ -163,7 +163,7 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> end:
</span><del>-        switch_mutex_unlock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
</ins><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -180,7 +180,7 @@
</span><span class="cx">                 return SWITCH_STATUS_SUCCESS;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        switch_mutex_lock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_wrlock(globals.limit_hash_rwlock);
</ins><span class="cx">
</span><span class="cx">         /* clear for uuid */
</span><span class="cx">         if (realm == NULL && resource == NULL) {
</span><span class="lines">@@ -222,7 +222,7 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        switch_mutex_unlock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
</ins><span class="cx">         
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="lines">@@ -233,7 +233,7 @@
</span><span class="cx">         limit_hash_item_t *item = NULL;
</span><span class="cx">         int count = 0;
</span><span class="cx">
</span><del>-        switch_mutex_lock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_rdlock(globals.limit_hash_rwlock);
</ins><span class="cx">
</span><span class="cx">         hash_key = switch_mprintf("%s_%s", realm, resource);
</span><span class="cx">
</span><span class="lines">@@ -242,7 +242,7 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_safe_free(hash_key);
</span><del>-        switch_mutex_unlock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
</ins><span class="cx">
</span><span class="cx">         return count;
</span><span class="cx"> }
</span><span class="lines">@@ -254,19 +254,22 @@
</span><span class="cx">
</span><span class="cx"> SWITCH_LIMIT_STATUS(limit_status_hash)
</span><span class="cx"> {
</span><ins>+/*
</ins><span class="cx">         switch_hash_index_t *hi = NULL;
</span><span class="cx">         int count = 0;
</span><span class="cx">         char *ret = NULL;
</span><span class="cx">         
</span><del>-        switch_mutex_lock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_rdlock(globals.limit_hash_rwlock);
</ins><span class="cx">         
</span><span class="cx">         while ((hi = switch_hash_first(NULL, globals.limit_hash))) {
</span><span class="cx">                 count++;
</span><span class="cx">         }
</span><del>-        switch_mutex_unlock(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
</ins><span class="cx">         
</span><span class="cx">         ret = switch_mprintf("There are %d elements being tracked.", count);
</span><span class="cx">         return ret;
</span><ins>+*/
+return "-ERR Hash status does not work due to locking issues.";
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /* APP/API STUFF */
</span><span class="lines">@@ -284,7 +287,7 @@
</span><span class="cx">         char *hash_key = NULL;
</span><span class="cx">         char *value = NULL;
</span><span class="cx">
</span><del>-        switch_mutex_lock(globals.db_hash_mutex);
</del><ins>+        switch_thread_rwlock_wrlock(globals.db_hash_rwlock);
</ins><span class="cx">
</span><span class="cx">         if (!zstr(data)) {
</span><span class="cx">                 mydata = strdup(data);
</span><span class="lines">@@ -324,7 +327,7 @@
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: hash %s\n", HASH_USAGE);
</span><span class="cx">
</span><span class="cx"> done:
</span><del>-        switch_mutex_unlock(globals.db_hash_mutex);
</del><ins>+        switch_thread_rwlock_unlock(globals.db_hash_rwlock);
</ins><span class="cx">         switch_safe_free(mydata);
</span><span class="cx">         switch_safe_free(hash_key);
</span><span class="cx"> }
</span><span class="lines">@@ -338,8 +341,6 @@
</span><span class="cx">         char *value = NULL;
</span><span class="cx">         char *hash_key = NULL;
</span><span class="cx">
</span><del>-        switch_mutex_lock(globals.db_hash_mutex);
-
</del><span class="cx">         if (!zstr(cmd)) {
</span><span class="cx">                 mydata = strdup(cmd);
</span><span class="cx">                 switch_assert(mydata);
</span><span class="lines">@@ -356,6 +357,7 @@
</span><span class="cx">                 if (argc < 4) {
</span><span class="cx">                         goto usage;
</span><span class="cx">                 }
</span><ins>+                switch_thread_rwlock_wrlock(globals.db_hash_rwlock);
</ins><span class="cx">                 if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
</span><span class="cx">                         switch_safe_free(value);
</span><span class="cx">                         switch_core_hash_delete(globals.db_hash, hash_key);
</span><span class="lines">@@ -365,6 +367,7 @@
</span><span class="cx">                 switch_core_hash_insert(globals.db_hash, hash_key, value);
</span><span class="cx">                 stream->write_function(stream, "+OK\n");
</span><span class="cx">         } else if (!strcasecmp(argv[0], "delete")) {
</span><ins>+                switch_thread_rwlock_wrlock(globals.db_hash_rwlock);
</ins><span class="cx">                 if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
</span><span class="cx">                         switch_safe_free(value);
</span><span class="cx">                         switch_core_hash_delete(globals.db_hash, hash_key);
</span><span class="lines">@@ -373,6 +376,7 @@
</span><span class="cx">                         stream->write_function(stream, "-ERR Not found\n");
</span><span class="cx">                 }
</span><span class="cx">         } else if (!strcasecmp(argv[0], "select")) {
</span><ins>+                switch_thread_rwlock_rdlock(globals.db_hash_rwlock);
</ins><span class="cx">                 if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
</span><span class="cx">                         stream->write_function(stream, "%s", value);
</span><span class="cx">                 }
</span><span class="lines">@@ -386,7 +390,7 @@
</span><span class="cx">         stream->write_function(stream, "-ERR Usage: hash %s\n", HASH_API_USAGE);
</span><span class="cx">
</span><span class="cx"> done:
</span><del>-        switch_mutex_unlock(globals.db_hash_mutex);
</del><ins>+        switch_thread_rwlock_unlock(globals.db_hash_rwlock);
</ins><span class="cx">         switch_safe_free(mydata);
</span><span class="cx">         switch_safe_free(hash_key);
</span><span class="cx">
</span><span class="lines">@@ -409,8 +413,8 @@
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        switch_mutex_init(&globals.limit_hash_mutex, SWITCH_MUTEX_NESTED, globals.pool);
-        switch_mutex_init(&globals.db_hash_mutex, SWITCH_MUTEX_NESTED, globals.pool);
</del><ins>+        switch_thread_rwlock_create(&globals.limit_hash_rwlock, globals.pool);
+        switch_thread_rwlock_create(&globals.db_hash_rwlock, globals.pool);
</ins><span class="cx">         switch_core_hash_init(&globals.limit_hash, pool);
</span><span class="cx">         switch_core_hash_init(&globals.db_hash, pool);
</span><span class="cx">
</span><span class="lines">@@ -435,8 +439,8 @@
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_hash_shutdown)
</span><span class="cx"> {
</span><span class="cx">
</span><del>-        switch_mutex_destroy(globals.db_hash_mutex);
-        switch_mutex_destroy(globals.limit_hash_mutex);
</del><ins>+        switch_thread_rwlock_destroy(globals.db_hash_rwlock);
+        switch_thread_rwlock_destroy(globals.limit_hash_rwlock);
</ins><span class="cx">
</span><span class="cx">         switch_core_hash_destroy(&globals.limit_hash);
</span><span class="cx">         switch_core_hash_destroy(&globals.db_hash);
</span></span></pre></div>
<a id="freeswitchbranchesrupa_limitsrcswitch_limitc"></a>
<div class="addfile"><h4>Added: freeswitch/branches/rupa_limit/src/switch_limit.c (0 => 17079)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/switch_limit.c         (rev 0)
+++ freeswitch/branches/rupa_limit/src/switch_limit.c        2010-03-23 16:17:15 UTC (rev 17079)
</span><span class="lines">@@ -0,0 +1,219 @@
</span><ins>+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2009, 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):
+ *
+ * Rupa Schomaker <rupa@rupa.com>
+ *
+ * switch_limit.c Limit support
+ *
+ */
+
+#include <switch.h>
+
+static switch_limit_interface_t *get_backend(const char *backend) {
+        switch_limit_interface_t *limit = NULL;
+        
+        if (!backend) {
+                return NULL;
+        }
+        
+        if (!(limit = switch_loadable_module_get_limit_interface(backend))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Unable to locate limit backend: %s\n", backend);
+        }
+        
+        return limit;
+}
+
+static void release_backend(switch_limit_interface_t *limit) {
+        if (limit) {
+                UNPROTECT_INTERFACE(limit);
+        }
+}
+
+SWITCH_DECLARE(void) switch_limit_init(switch_memory_pool_t *pool) {
+        if (switch_event_reserve_subclass(LIMIT_EVENT_USAGE) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldnt register event subclass \"%s\"", LIMIT_EVENT_USAGE);
+        }
+}
+
+
+SWITCH_DECLARE(void) switch_limit_fire_event(const char *backend, const char *realm, const char *key, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax)
+{
+        switch_event_t *event;
+
+        if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, LIMIT_EVENT_USAGE) == SWITCH_STATUS_SUCCESS) {
+                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "backend", backend);
+                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "realm", realm);
+                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "key", key);
+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "usage", "%d", usage);
+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "rate", "%d", rate);
+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "max", "%d", max);
+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ratemax", "%d", ratemax);
+                switch_event_fire(&event);
+        }
+}
+
+static switch_status_t limit_state_handler(switch_core_session_t *session)
+{
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        switch_channel_state_t state = switch_channel_get_state(channel);
+        const char *vval = switch_channel_get_variable(channel, LIMIT_IGNORE_TRANSFER_VARIABLE);
+        const char *backendlist = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
+        int argc = 0;
+        char *argv[6] = { 0 };
+        char *mydata = NULL;
+        
+        if (zstr(backendlist)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unset limit backendlist!\n");
+                return SWITCH_STATUS_SUCCESS;
+        }
+
+        mydata = strdup(backendlist);
+        
+        if (state >= CS_HANGUP || (state == CS_ROUTING && !switch_true(vval))) {
+                int x;
+                argc = switch_separate_string(mydata, ',', argv, (sizeof(argv) / sizeof(argv[0])));
+                for (x = 0; x < argc; x++) {
+                        switch_limit_release(argv[x], session, NULL, NULL);
+                }
+                switch_core_event_hook_remove_state_change(session, limit_state_handler);
+                /* Remove limit_realm variable so we register another hook if limit is called again */
+                switch_channel_set_variable(channel, "limit_realm", NULL);
+        }
+        return SWITCH_STATUS_SUCCESS;
+}
+
+
+SWITCH_DECLARE(switch_status_t) switch_limit_incr(const char *backend, switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval) {
+        switch_limit_interface_t *limit = NULL;
+        switch_channel_t *channel = NULL;
+        int status = SWITCH_STATUS_SUCCESS;
+        
+        assert(session);
+
+        channel = switch_core_session_get_channel(session);
+
+        /* locate impl, call appropriate func */
+        if (!(limit = get_backend(backend))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+                switch_goto_status(SWITCH_STATUS_GENERR, end);
+        }
+        
+        if ((status = limit->incr(session, realm, resource, max, interval)) == SWITCH_STATUS_SUCCESS) {
+                /* race condition? what if another leg is doing the same thing? */
+                const char *existing = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
+                if (existing) {
+                        if (!strstr(existing, backend)) {
+                                switch_channel_set_variable_printf(channel, LIMIT_BACKEND_VARIABLE, "%s,%s", existing, backend);
+                        }
+                } else {
+                        switch_channel_set_variable(channel, LIMIT_BACKEND_VARIABLE, backend);
+                        switch_core_event_hook_add_state_change(session, limit_state_handler);
+                }
+        }
+        
+        release_backend(limit);
+        
+end:
+        return status;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_limit_release(const char *backend, switch_core_session_t *session, const char *realm, const char *resource) {
+        switch_limit_interface_t *limit = NULL;
+        int status = SWITCH_STATUS_SUCCESS;
+        
+        /* locate impl, call appropriate func */
+        if (!(limit = get_backend(backend))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+                switch_goto_status(SWITCH_STATUS_GENERR, end);
+        }
+        
+        status = limit->release(session, realm, resource);
+        
+end:
+        release_backend(limit);
+        return status;
+}
+
+SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource) {
+        switch_limit_interface_t *limit = NULL;
+        int usage = 0;
+        
+        /* locate impl, call appropriate func */
+        if (!(limit = get_backend(backend))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+                goto end;
+        }
+        
+        usage = limit->usage(realm, resource);
+        
+end:
+        release_backend(limit);
+        return usage;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_limit_reset(const char *backend) {
+        switch_limit_interface_t *limit = NULL;
+        int status = SWITCH_STATUS_SUCCESS;
+        
+        /* locate impl, call appropriate func */
+        if (!(limit = get_backend(backend))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+                switch_goto_status(SWITCH_STATUS_GENERR, end);
+        }
+        
+        status = limit->reset();
+        
+end:
+        release_backend(limit);
+        return status;
+}
+
+SWITCH_DECLARE(char *) switch_limit_status(const char *backend) {
+        switch_limit_interface_t *limit = NULL;
+        char *status = NULL;
+        
+        /* locate impl, call appropriate func */
+        if (!(limit = get_backend(backend))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+                switch_goto_status("-ERR", end);
+        }
+        
+        status = limit->status();
+        
+end:
+        release_backend(limit);
+        return status;
+}
+
+/* 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:
+ */
</ins><span class="cx">Property changes on: freeswitch/branches/rupa_limit/src/switch_limit.c
</span><span class="cx">___________________________________________________________________
</span><span class="cx">Name: svn:executable
</span><span class="cx"> + *
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>