<!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][14401] </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=14401">14401</a></dd>
<dt>Author</dt> <dd>mrene</dd>
<dt>Date</dt> <dd>2009-07-28 15:19:01 -0500 (Tue, 28 Jul 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>mod_limit: Refactor and add limit_hash_execute application (see wiki)</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_limitmod_limitc">freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodapplicationsmod_limitmod_limitc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c (14400 => 14401)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2009-07-28 18:50:33 UTC (rev 14400)
+++ freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2009-07-28 20:19:01 UTC (rev 14401)
</span><span class="lines">@@ -338,8 +338,6 @@
</span><span class="cx">                         switch_hash_this(hi, &key, &keylen, &val);
</span><span class="cx">                         
</span><span class="cx">                         item = (limit_hash_item_t*)val;
</span><del>-                        
-                        /* We keep the structure even though the count is 0 so we do not allocate too often */
</del><span class="cx">                         item->total_usage--;        
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Usage for %s is now %d\n", (const char*)key, item->total_usage);
</span><span class="cx">                         
</span><span class="lines">@@ -861,63 +859,26 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-#define LIMITHASH_USAGE "<realm> <id> [<max>[/interval]] [number [dialplan [context]]]"
-#define LIMITHASH_DESC "limit access to a resource and transfer to an extension if the limit is exceeded"
-SWITCH_STANDARD_APP(limit_hash_function)
</del><ins>+/* \brief Enforces limit_hash restrictions
+ * \param session current session
+ * \param realm limit realm
+ * \param id limit id
+ * \param max maximum count
+ * \param interval interval for rate limiting
+ * \return SWITCH_TRUE if the access is allowed, SWITCH_FALSE if it isnt
+ */
+static switch_bool_t do_limit_hash(switch_core_session_t *session, const char *realm, const char *id, int max, int interval)
</ins><span class="cx"> {
</span><del>-        int argc = 0;
-        char *argv[6] = { 0 };
-        char *mydata = NULL;
-        char *realm = NULL;
-        char *id = NULL;
</del><ins>+        switch_channel_t *channel = switch_core_session_get_channel(session);
</ins><span class="cx">         char *hashkey = NULL;
</span><del>-        char *xfer_exten = NULL;
-        int max = -1;
-        int interval = 0;
-        char *szinterval = NULL;
</del><ins>+        switch_bool_t status = SWITCH_TRUE;
</ins><span class="cx">         limit_hash_item_t *item = NULL;
</span><del>-        switch_channel_t *channel = switch_core_session_get_channel(session);
</del><span class="cx">         time_t now = switch_epoch_time_now(NULL);
</span><span class="cx">         limit_hash_private_t *pvt = NULL;
</span><span class="cx">         uint8_t increment = 1;
</span><span class="cx">         
</span><del>-        /* Parse application data */
-        if (!switch_strlen_zero(data)) {
-                mydata = switch_core_session_strdup(session, data);
-                argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
-        }
-        
-        if (argc < 2) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "USAGE: limit_hash %s\n", LIMITHASH_USAGE);
-                return;
-        }
-        
-        realm = argv[0];
-        id = argv[1];
-        
-        /* If max is omitted, only act as a counter and skip maximum checks */
-        if (argc > 2) {
-                if ((szinterval = strchr(argv[2], '/')))
-                {
-                        *szinterval++ = '\0';
-                        interval = atoi(szinterval);
-                }
-                
-                max = atoi(argv[2]);
-                
-                if (max < 0) {
-                        max = 0;
-                }
-        }
</del><ins>+        hashkey = switch_core_session_sprintf(session, "%s_%s", realm, id);
</ins><span class="cx">
</span><del>-        if (argc > 3) {
-                xfer_exten = argv[3];
-        } else {
-                xfer_exten = limit_def_xfer_exten;
-        }
-        
-        hashkey = switch_core_session_sprintf(session, "%s_%s", realm, id);
-        
</del><span class="cx">         switch_mutex_lock(globals.limit_hash_mutex);
</span><span class="cx">         /* Check if that realm+id 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="lines">@@ -927,7 +888,7 @@
</span><span class="cx">                 memset(item, 0, sizeof(limit_hash_item_t));
</span><span class="cx">                 switch_core_hash_insert(globals.limit_hash, hashkey, item);
</span><span class="cx">         }
</span><del>-        
</del><ins>+
</ins><span class="cx">         /* Did we already run on this channel before? */
</span><span class="cx">         if ((pvt = switch_channel_get_private(channel, "limit_hash")))
</span><span class="cx">         {
</span><span class="lines">@@ -951,32 +912,24 @@
</span><span class="cx">                 } else {
</span><span class="cx">                         /* Always increment rate when its checked as it doesnt depend on the channel */
</span><span class="cx">                         item->rate_usage++;
</span><del>-                        
</del><ins>+
</ins><span class="cx">                         if ((max >= 0) && (item->rate_usage > (uint32_t)max)) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Usage for %s exceeds maximum rate of %d/%ds, now at %d\n", hashkey, max, interval, item->rate_usage);
</span><del>-                                if (*xfer_exten == '!') {
-                                        switch_channel_hangup(channel, switch_channel_str2cause(xfer_exten+1));
-                                } else {
-                                        switch_ivr_session_transfer(session, xfer_exten, argv[4], argv[5]);
-                                }
</del><ins>+                                status = SWITCH_FALSE;
</ins><span class="cx">                                 goto end;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         } else if ((max >= 0) && (item->total_usage + increment > (uint32_t)max)) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Usage for %s is already at max value (%d)\n", hashkey, item->total_usage);
</span><del>-                if (*xfer_exten == '!') {
-                        switch_channel_hangup(channel, switch_channel_str2cause(xfer_exten+1));
-                } else {
-                        switch_ivr_session_transfer(session, xfer_exten, argv[4], argv[5]);
-                }
</del><ins>+                status = SWITCH_FALSE;
</ins><span class="cx">                 goto end;
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         if (increment) {
</span><span class="cx">                 item->total_usage++;
</span><del>-                
</del><ins>+
</ins><span class="cx">                 switch_core_hash_insert(pvt->hash, hashkey, item);
</span><del>-                
</del><ins>+
</ins><span class="cx">                 if (max == -1) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Usage for %s is now %d\n", hashkey, item->total_usage);
</span><span class="cx">                 } else if (interval == 0) {
</span><span class="lines">@@ -984,29 +937,175 @@
</span><span class="cx">                 } else {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Usage for %s is now %d/%d for the last %d seconds\n", hashkey, item->rate_usage, max, interval);
</span><span class="cx">                 }
</span><del>-                
</del><ins>+
</ins><span class="cx">                 limit_fire_event(realm, id, item->total_usage, item->rate_usage, max, max >=0 ? (uint32_t)max : 0);
</span><span class="cx">         }
</span><del>-        
</del><ins>+
</ins><span class="cx">         /* Save current usage & rate into channel variables so it can be used later in the dialplan, or added to CDR records */
</span><span class="cx">         {
</span><span class="cx">                 const char *susage = switch_core_session_sprintf(session, "%d", item->total_usage);
</span><span class="cx">                 const char *srate = switch_core_session_sprintf(session, "%d", item->rate_usage);
</span><del>-                
</del><ins>+
</ins><span class="cx">                 switch_channel_set_variable(channel, "limit_usage", susage);
</span><span class="cx">                 switch_channel_set_variable(channel, switch_core_session_sprintf(session, "limit_usage_%s", hashkey), susage);
</span><del>-                
</del><ins>+
</ins><span class="cx">                 switch_channel_set_variable(channel, "limit_rate", srate);
</span><span class="cx">                 switch_channel_set_variable(channel, switch_core_session_sprintf(session, "limit_rate_%s", hashkey), srate);
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_core_event_hook_add_state_change(session, hash_state_handler);
</span><span class="cx">
</span><del>-        
</del><span class="cx"> end:        
</span><span class="cx">         switch_mutex_unlock(globals.limit_hash_mutex);
</span><ins>+        return status;
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+/* !\brief Releases usage of a limit_hash-controlled ressource */
+static void limit_hash_release(switch_core_session_t *session, const char *realm, const char *id)
+{
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        limit_hash_private_t *pvt = switch_channel_get_private(channel, "limit_hash");
+        limit_hash_item_t *item = NULL;
+        char *hashkey = switch_core_session_sprintf(session, "%s_%s", realm, id);
+        
+        switch_mutex_lock(globals.limit_hash_mutex);
+        
+        item = (limit_hash_item_t*)switch_core_hash_find(pvt->hash, hashkey);
+        item->total_usage--;        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Usage for %s is now %d\n", (const char*)hashkey, item->total_usage);
+        
+        switch_core_hash_delete(pvt->hash, hashkey);
+        
+        if (item->total_usage == 0) {
+                /* Noone is using this item anymore */
+                switch_core_hash_delete(globals.limit_hash, (const char*)hashkey);
+                free(item);
+        }
+        
+        switch_mutex_unlock(globals.limit_hash_mutex);
+}
+
+#define LIMITHASH_USAGE "<realm> <id> [<max>[/interval]] [number [dialplan [context]]]"
+#define LIMITHASH_DESC "limit access to a resource and transfer to an extension if the limit is exceeded"
+SWITCH_STANDARD_APP(limit_hash_function)
+{
+        int argc = 0;
+        char *argv[6] = { 0 };
+        char *mydata = NULL;
+        char *realm = NULL;
+        char *id = NULL;
+        char *xfer_exten = NULL;
+        int max = -1;
+        int interval = 0;
+        char *szinterval = NULL;
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+
+        /* Parse application data */
+        if (!switch_strlen_zero(data)) {
+                mydata = switch_core_session_strdup(session, data);
+                argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
+        }
+        
+        if (argc < 2) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "USAGE: limit_hash %s\n", LIMITHASH_USAGE);
+                return;
+        }
+        
+        realm = argv[0];
+        id = argv[1];
+        
+        /* If max is omitted, only act as a counter and skip maximum checks */
+        if (argc > 2) {
+                if ((szinterval = strchr(argv[2], '/')))
+                {
+                        *szinterval++ = '\0';
+                        interval = atoi(szinterval);
+                }
+                
+                max = atoi(argv[2]);
+                
+                if (max < 0) {
+                        max = 0;
+                }
+        }
+
+        if (argc > 3) {
+                xfer_exten = argv[3];
+        } else {
+                xfer_exten = limit_def_xfer_exten;
+        }
+        
+        if (!do_limit_hash(session, realm, id, max, interval)) {
+                /* Limit exceeded */
+                if (*xfer_exten == '!') {
+                        switch_channel_hangup(channel, switch_channel_str2cause(xfer_exten+1));
+                } else {
+                        switch_ivr_session_transfer(session, xfer_exten, argv[4], argv[5]);
+                }
+        }
+}
+
+
+#define LIMITHASHEXECUTE_USAGE "<realm> <id> [<max>[/interval]] [number [dialplan [context]]]"
+#define LIMITHASHEXECUTE_DESC "limit access to a resource. the specified application will only be executed if the resource is available"
+SWITCH_STANDARD_APP(limit_hash_execute_function)
+{
+        int argc = 0;
+        char *argv[6] = { 0 };
+        char *mydata = NULL;
+        char *realm = NULL;
+        char *id = NULL;
+        char *app = NULL;
+        char *app_arg = NULL;
+        int max = -1;
+        int interval = 0;
+        char *szinterval = NULL;
+
+        /* Parse application data */
+        if (!switch_strlen_zero(data)) {
+                mydata = switch_core_session_strdup(session, data);
+                argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
+        }
+        
+        if (argc < 2) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "USAGE: limit_hash_execute %s\n", LIMITHASHEXECUTE_USAGE);
+                return;
+        }
+        
+        realm = argv[0];
+        id = argv[1];
+        
+        /* If max is omitted, only act as a counter and skip maximum checks */
+        if (argc > 2) {
+                if ((szinterval = strchr(argv[2], '/')))
+                {
+                        *szinterval++ = '\0';
+                        interval = atoi(szinterval);
+                }
+                
+                max = atoi(argv[2]);
+                
+                if (max < 0) {
+                        max = 0;
+                }
+        }
+
+
+        app = argv[3];
+        app_arg = argv[4];
+
+        if (switch_strlen_zero(app)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing application\n");
+                return;
+        }
+
+        if (do_limit_hash(session, realm, id, max, interval)) {
+                switch_core_session_execute_application(session, app, app_arg);
+                limit_hash_release(session, realm, id);
+        }
+}
+
+
</ins><span class="cx"> #define LIMIT_HASH_USAGE_USAGE "<realm> <id>"
</span><span class="cx"> SWITCH_STANDARD_API(limit_hash_usage_function)
</span><span class="cx"> {
</span><span class="lines">@@ -1080,6 +1179,7 @@
</span><span class="cx">
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "limit", "Limit", LIMIT_DESC, limit_function, LIMIT_USAGE, SAF_SUPPORT_NOMEDIA);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "limit_hash", "Limit (hash)", LIMITHASH_DESC, limit_hash_function, LIMITHASH_USAGE, SAF_SUPPORT_NOMEDIA);
</span><ins>+        SWITCH_ADD_APP(app_interface, "limit_hash_execute", "Limit (hash)", LIMITHASHEXECUTE_USAGE, limit_hash_execute_function, LIMITHASHEXECUTE_USAGE, SAF_SUPPORT_NOMEDIA);
</ins><span class="cx">         SWITCH_ADD_APP(app_interface, "db", "Insert to the db", DB_DESC, db_function, DB_USAGE, SAF_SUPPORT_NOMEDIA);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "hash", "Insert into the hashtable", HASH_DESC, hash_function, HASH_USAGE, SAF_SUPPORT_NOMEDIA)
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "group", "Manage a group", GROUP_DESC, group_function, GROUP_USAGE, SAF_SUPPORT_NOMEDIA);
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>