<!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][14870] </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=14870">14870</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2009-09-15 13:13:25 -0500 (Tue, 15 Sep 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>add limit_execute (like limit_hash_execute)
allow -#s to act as counter only
add INFO level logs to limit similar to limit_hash</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 (14869 => 14870)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2009-09-15 05:05:07 UTC (rev 14869)
+++ freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2009-09-15 18:13:25 UTC (rev 14870)
</span><span class="lines">@@ -790,6 +790,67 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/* \brief Enforces limit restrictions
+ * \param session current session
+ * \param realm limit realm
+ * \param id limit id
+ * \param max maximum count
+ * \return SWITCH_TRUE if the access is allowed, SWITCH_FALSE if it isnt
+ */
+static switch_bool_t do_limit(switch_core_session_t *session, const char *realm, const char *id, int max)
+{
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        int got = 0;
+        char *sql = NULL;
+        char buf[80] = "";
+        callback_t cbt = { 0 };
+        switch_status_t status = SWITCH_TRUE;
+
+        switch_mutex_lock(globals.mutex);
+
+        switch_channel_set_variable(channel, "limit_realm", realm);
+        switch_channel_set_variable(channel, "limit_id", id);
+        switch_channel_set_variable(channel, "limit_max", switch_core_session_sprintf(session, "%d", max));
+
+        cbt.buf = buf;
+        cbt.len = sizeof(buf);
+        sql = switch_mprintf("select count(hostname) from limit_data where realm='%q' and id like '%q'", realm, id);
+        limit_execute_sql_callback(NULL, sql, sql2str_callback, &cbt);
+        switch_safe_free(sql);
+        got = atoi(buf);
+
+        if (max < 0) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Usage for %s_%s is now %d\n", realm, id, got+1);
+        } else {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Usage for %s_%s is now %d/%d\n", realm, id, got+1, max);
+        }
+
+        if (max >= 0 && got + 1 > max) {
+                status = SWITCH_FALSE;
+                goto done;
+        }
+
+        switch_core_event_hook_add_state_change(session, db_state_handler);
+
+        sql =
+                switch_mprintf("insert into limit_data (hostname, realm, id, uuid) values('%q','%q','%q','%q');", globals.hostname, realm, id,
+                                         switch_core_session_get_uuid(session));
+        limit_execute_sql(sql, NULL);
+        switch_safe_free(sql);
+        
+        {
+                const char *susage = switch_core_session_sprintf(session, "%d", ++got);
+
+                switch_channel_set_variable(channel, "limit_usage", susage);
+                switch_channel_set_variable(channel, switch_core_session_sprintf(session, "limit_usage_%s_%s", realm, id), susage);
+        }
+        limit_fire_event(realm, id, got, 0, max, 0);
+
+ done:
+        switch_mutex_unlock(globals.mutex);
+        return status;
+}
+
</ins><span class="cx"> #define LIMIT_USAGE "<realm> <id> [<max> [number [dialplan [context]]]]"
</span><span class="cx"> #define LIMIT_DESC "limit access to a resource and transfer to an extension if the limit is exceeded"
</span><span class="cx"> static char *limit_def_xfer_exten = "limit_exceeded";
</span><span class="lines">@@ -799,13 +860,10 @@
</span><span class="cx">         int argc = 0;
</span><span class="cx">         char *argv[6] = { 0 };
</span><span class="cx">         char *mydata = NULL;
</span><del>-        char *sql = NULL;
</del><span class="cx">         char *realm = NULL;
</span><span class="cx">         char *id = NULL;
</span><span class="cx">         char *xfer_exten = NULL;
</span><del>-        int max = 0, got = 0;
-        char buf[80] = "";
-        callback_t cbt = { 0 };
</del><ins>+        int max = 0;
</ins><span class="cx">         switch_channel_t *channel = switch_core_session_get_channel(session);
</span><span class="cx">
</span><span class="cx">         if (!switch_strlen_zero(data)) {
</span><span class="lines">@@ -818,20 +876,19 @@
</span><span class="cx">                 return;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        switch_mutex_lock(globals.mutex);
-
</del><span class="cx">         realm = argv[0];
</span><span class="cx">         id = argv[1];
</span><span class="cx">         
</span><del>-        if (argc > 2) {
</del><ins>+        /* Accept '-' as unlimited (act as counter)*/
+        if (argv[2][0] == '-') {
+                max = -1;
+        } else {
</ins><span class="cx">                 max = atoi(argv[2]);
</span><ins>+
</ins><span class="cx">                 if (max < 0) {
</span><del>-                        max = 0;
-                }
</del><ins>+                        max = 0;
+                }        
</ins><span class="cx">         }
</span><del>-        else {
-                max = -1;
-        }
</del><span class="cx">
</span><span class="cx">         if (argc >= 4) {
</span><span class="cx">                 xfer_exten = argv[3];
</span><span class="lines">@@ -839,45 +896,88 @@
</span><span class="cx">                 xfer_exten = limit_def_xfer_exten;
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        if (!do_limit(session, realm, id, max)) {
+                /* 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]);
+                }
+        }
</ins><span class="cx">         
</span><del>-        switch_channel_set_variable(channel, "limit_realm", realm);
-        switch_channel_set_variable(channel, "limit_id", id);
-        switch_channel_set_variable(channel, "limit_max", argv[2]);
</del><ins>+}
</ins><span class="cx">
</span><ins>+/* !\brief Releases usage of a limit_-controlled ressource */
+static void limit_release(switch_core_session_t *session, const char *realm, const char *id)
+{
+        char *sql = NULL;
+        char buf[80] = "";
+        callback_t cbt = { 0 };
+
+        switch_mutex_lock(globals.mutex);
+
</ins><span class="cx">         cbt.buf = buf;
</span><span class="cx">         cbt.len = sizeof(buf);
</span><del>-        sql = switch_mprintf("select count(hostname) from limit_data where realm='%q' and id like '%q'", realm, id);
</del><ins>+        sql = switch_mprintf("delete from limit_data where uuid='%q' and realm='%q' and id like '%q'",
+                                                switch_core_session_get_uuid(session), realm, id);
</ins><span class="cx">         limit_execute_sql_callback(NULL, sql, sql2str_callback, &cbt);
</span><span class="cx">         switch_safe_free(sql);
</span><del>-        got = atoi(buf);
</del><ins>+        
+        switch_mutex_unlock(globals.mutex);
+}
</ins><span class="cx">
</span><del>-        if (max >= 0 && got + 1 > max) {
-                switch_ivr_session_transfer(session, xfer_exten, argv[4], argv[5]);
-                goto done;
</del><ins>+#define LIMITEXECUTE_USAGE "<realm> <id> [<max>] [application] [application arguments]"
+#define LIMITEXECUTE_DESC "limit access to a resource. the specified application will only be executed if the resource is available"
+SWITCH_STANDARD_APP(limit_execute_function)
+{
+        int argc = 0;
+        char *argv[5] = { 0 };
+        char *mydata = NULL;
+        char *realm = NULL;
+        char *id = NULL;
+        char *app = NULL;
+        char *app_arg = NULL;
+        int max = -1;
+
+        /* 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])));
</ins><span class="cx">         }
</span><ins>+        
+        if (argc < 2) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: limit_execute %s\n", LIMITEXECUTE_USAGE);
+                return;
+        }
+        
+        realm = argv[0];
+        id = argv[1];
+        
+        /* Accept '-' as unlimited (act as counter)*/
+        if (argv[2][0] == '-') {
+                max = -1;
+        } else {
+                max = atoi(argv[2]);
</ins><span class="cx">
</span><del>-        switch_core_event_hook_add_state_change(session, db_state_handler);
-        switch_core_event_hook_add_state_change(session, db_state_handler);
</del><ins>+                if (max < 0) {
+                        max = 0;
+                }        
+        }
</ins><span class="cx">
</span><del>-        sql =
-                switch_mprintf("insert into limit_data (hostname, realm, id, uuid) values('%q','%q','%q','%q');", globals.hostname, realm, id,
-                                         switch_core_session_get_uuid(session));
-        limit_execute_sql(sql, NULL);
-        switch_safe_free(sql);
-        
-        {
-                const char *susage = switch_core_session_sprintf(session, "%d", ++got);
</del><ins>+        app = argv[3];
+        app_arg = argv[4];
</ins><span class="cx">
</span><del>-                switch_channel_set_variable(channel, "limit_usage", susage);
-                switch_channel_set_variable(channel, switch_core_session_sprintf(session, "limit_usage_%s_%s", realm, id), susage);
</del><ins>+        if (switch_strlen_zero(app)) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Missing application\n");
+                return;
</ins><span class="cx">         }
</span><del>-        limit_fire_event(realm, id, got, 0, max, 0);
</del><span class="cx">
</span><del>- done:
-        switch_mutex_unlock(globals.mutex);
</del><ins>+        if (do_limit(session, realm, id, max)) {
+                switch_core_session_execute_application(session, app, app_arg);
+                limit_release(session, realm, id);
+        }
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-
</del><span class="cx"> #define LIMIT_USAGE_USAGE "<realm> <id>"
</span><span class="cx"> SWITCH_STANDARD_API(limit_usage_function)
</span><span class="cx"> {
</span><span class="lines">@@ -1591,6 +1691,7 @@
</span><span class="cx">
</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><ins>+        SWITCH_ADD_APP(app_interface, "limit_execute", "Limit", LIMITEXECUTE_USAGE, limit_execute_function, LIMITEXECUTE_USAGE, SAF_SUPPORT_NOMEDIA);
</ins><span class="cx">         SWITCH_ADD_APP(app_interface, "limit_hash", "Limit (hash)", LIMITHASH_DESC, limit_hash_function, LIMITHASH_USAGE, SAF_SUPPORT_NOMEDIA);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "limit_hash_execute", "Limit (hash)", LIMITHASHEXECUTE_USAGE, limit_hash_execute_function, LIMITHASHEXECUTE_USAGE, SAF_SUPPORT_NOMEDIA);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "limit_memcache", "Limit (memcache)", LIMITMEMCACHE_DESC, limit_memcache_function, LIMITMEMCACHE_USAGE, SAF_SUPPORT_NOMEDIA);
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>