LIMIT code
[Core Library]

Collaboration diagram for LIMIT code:

Defines

#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, uint32_t *rcount)
#define SWITCH_LIMIT_RESET(name)   static switch_status_t name (void)
#define SWITCH_LIMIT_INTERVAL_RESET(name)   static switch_status_t name (const char *realm, const char *resource)
#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"

Functions

SWITCH_BEGIN_EXTERN_C void switch_limit_init (switch_memory_pool_t *pool)
 Initilize the LIMIT Core System.
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)
 Increment resource.
switch_status_t switch_limit_release (const char *backend, switch_core_session_t *session, const char *realm, const char *resource)
 Release resource.
int switch_limit_usage (const char *backend, const char *realm, const char *resource, uint32_t *rcount)
 get usage count for resource
switch_status_t switch_limit_interval_reset (const char *backend, const char *realm, const char *resource)
 reset interval usage counter for a given resource
switch_status_t switch_limit_reset (const char *backend)
 reset all usage counters
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)
 fire event for limit usage
char * switch_limit_status (const char *backend)
 retrieve arbitrary status information


Define Documentation

#define LIMIT_BACKEND_VARIABLE   "limit_backend"

Definition at line 121 of file switch_limit.h.

Referenced by limit_state_handler(), and switch_limit_incr().

#define LIMIT_DEF_XFER_EXTEN   "limit_exceeded"

Definition at line 123 of file switch_limit.h.

#define LIMIT_EVENT_USAGE   "limit::usage"

Definition at line 122 of file switch_limit.h.

Referenced by switch_limit_fire_event(), and switch_limit_init().

#define LIMIT_IGNORE_TRANSFER_VARIABLE   "limit_ignore_transfer"

Definition at line 120 of file switch_limit.h.

Referenced by limit_state_handler().

#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)

callback to init a backend

Definition at line 113 of file switch_limit.h.

#define SWITCH_LIMIT_INTERVAL_RESET ( name   )     static switch_status_t name (const char *realm, const char *resource)

Definition at line 117 of file switch_limit.h.

#define SWITCH_LIMIT_RELEASE ( name   )     static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)

Definition at line 114 of file switch_limit.h.

#define SWITCH_LIMIT_RESET ( name   )     static switch_status_t name (void)

Definition at line 116 of file switch_limit.h.

#define SWITCH_LIMIT_STATUS ( name   )     static char * name (void)

Definition at line 118 of file switch_limit.h.

#define SWITCH_LIMIT_USAGE ( name   )     static int name (const char *realm, const char *resource, uint32_t *rcount)

Definition at line 115 of file switch_limit.h.


Function Documentation

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 
)

fire event for limit usage

Parameters:
backend to use
realm 
resource 
usage 
rate 
max 
ratemax 

Definition at line 62 of file switch_limit.c.

References LIMIT_EVENT_USAGE, switch_event_add_header(), switch_event_add_header_string(), switch_event_create_subclass, SWITCH_EVENT_CUSTOM, switch_event_fire, SWITCH_STACK_BOTTOM, and SWITCH_STATUS_SUCCESS.

00063 {
00064         switch_event_t *event;
00065 
00066         if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, LIMIT_EVENT_USAGE) == SWITCH_STATUS_SUCCESS) {
00067                 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "backend", backend);
00068                 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "realm", realm);
00069                 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "key", key);
00070                 switch_event_add_header(event, SWITCH_STACK_BOTTOM, "usage", "%d", usage);
00071                 switch_event_add_header(event, SWITCH_STACK_BOTTOM, "rate", "%d", rate);
00072                 switch_event_add_header(event, SWITCH_STACK_BOTTOM, "max", "%d", max);
00073                 switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ratemax", "%d", ratemax);
00074                 switch_event_fire(&event);
00075         }
00076 }

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 
)

Increment resource.

Parameters:
backend to use
realm 
resource 
max - 0 means no limit, just count
interval - 0 means no interval
Returns:
true/false - true ok, false over limit

Definition at line 111 of file switch_limit.c.

References get_backend(), switch_limit_interface::incr, LIMIT_BACKEND_VARIABLE, limit_state_handler(), release_backend(), switch_channel_get_variable, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_variable, switch_channel_set_variable_printf(), switch_core_session_get_channel(), switch_goto_status, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

00111                                                                                                                                                                                    {
00112         switch_limit_interface_t *limit = NULL;
00113         switch_channel_t *channel = NULL;
00114         int status = SWITCH_STATUS_SUCCESS;
00115         
00116         assert(session);
00117 
00118         channel = switch_core_session_get_channel(session);
00119 
00120         /* locate impl, call appropriate func */
00121         if (!(limit = get_backend(backend))) {
00122                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
00123                 switch_goto_status(SWITCH_STATUS_GENERR, end);
00124         }
00125 
00126         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "incr called: %s_%s max:%d, interval:%d\n",
00127                                           realm, resource, max, interval);
00128         
00129         if ((status = limit->incr(session, realm, resource, max, interval)) == SWITCH_STATUS_SUCCESS) {
00130                 /* race condition? what if another leg is doing the same thing? */
00131                 const char *existing = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
00132                 if (existing) {
00133                         if (!strstr(existing, backend)) {
00134                                 switch_channel_set_variable_printf(channel, LIMIT_BACKEND_VARIABLE, "%s,%s", existing, backend);
00135                         }
00136                 } else {
00137                         switch_channel_set_variable(channel, LIMIT_BACKEND_VARIABLE, backend);
00138                         switch_core_event_hook_add_state_change(session, limit_state_handler);
00139                 }
00140         }
00141         
00142         release_backend(limit);
00143         
00144 end:
00145         return status;
00146 }

SWITCH_BEGIN_EXTERN_C void switch_limit_init ( switch_memory_pool_t pool  ) 

Initilize the LIMIT Core System.

Parameters:
pool the memory pool to use for long term allocations
Note:
Generally called by the core_init

Definition at line 55 of file switch_limit.c.

References LIMIT_EVENT_USAGE, SWITCH_CHANNEL_LOG, switch_event_reserve_subclass, SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_STATUS_SUCCESS.

00055                                                                    {
00056         if (switch_event_reserve_subclass(LIMIT_EVENT_USAGE) != SWITCH_STATUS_SUCCESS) {
00057                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldnt register event subclass \"%s\"", LIMIT_EVENT_USAGE);
00058         }
00059 }

switch_status_t switch_limit_interval_reset ( const char *  backend,
const char *  realm,
const char *  resource 
)

reset interval usage counter for a given resource

Parameters:
backend 
realm 
resource 

Definition at line 199 of file switch_limit.c.

References get_backend(), switch_limit_interface::interval_reset, release_backend(), SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

00199                                                                                                                           {
00200         switch_limit_interface_t *limit = NULL;
00201         int status = SWITCH_STATUS_SUCCESS;
00202         
00203         /* locate impl, call appropriate func */
00204         if (!(limit = get_backend(backend))) {
00205                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
00206                 switch_goto_status(SWITCH_STATUS_GENERR, end);
00207         }
00208         
00209         if (!limit->interval_reset) {
00210                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s does not implement interval_reset!\n", backend);
00211                 switch_goto_status(SWITCH_STATUS_GENERR, end);
00212         }
00213 
00214         status = limit->interval_reset(realm, resource);
00215         
00216 end:
00217         release_backend(limit);
00218         return status;
00219 }

switch_status_t switch_limit_release ( const char *  backend,
switch_core_session_t session,
const char *  realm,
const char *  resource 
)

Release resource.

Parameters:
backend to use
realm 
resource 
Returns:
true/false - true ok, false over limit

Definition at line 148 of file switch_limit.c.

References get_backend(), switch_limit_interface::release, release_backend(), SWITCH_CHANNEL_SESSION_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

Referenced by limit_state_handler().

00148                                                                                                                                                    {
00149         switch_limit_interface_t *limit = NULL;
00150         int status = SWITCH_STATUS_SUCCESS;
00151         
00152         /* locate impl, call appropriate func */
00153         if (!(limit = get_backend(backend))) {
00154                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
00155                 switch_goto_status(SWITCH_STATUS_GENERR, end);
00156         }
00157         
00158         status = limit->release(session, realm, resource);
00159         
00160 end:
00161         release_backend(limit);
00162         return status;
00163 }

switch_status_t switch_limit_reset ( const char *  backend  ) 

reset all usage counters

Parameters:
backend to use

Definition at line 182 of file switch_limit.c.

References get_backend(), release_backend(), switch_limit_interface::reset, SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

00182                                                                         {
00183         switch_limit_interface_t *limit = NULL;
00184         int status = SWITCH_STATUS_SUCCESS;
00185         
00186         /* locate impl, call appropriate func */
00187         if (!(limit = get_backend(backend))) {
00188                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
00189                 switch_goto_status(SWITCH_STATUS_GENERR, end);
00190         }
00191         
00192         status = limit->reset();
00193         
00194 end:
00195         release_backend(limit);
00196         return status;
00197 }

char* switch_limit_status ( const char *  backend  ) 

retrieve arbitrary status information

Parameters:
backend to use
Note:
caller must free returned value

Definition at line 221 of file switch_limit.c.

References get_backend(), release_backend(), switch_limit_interface::status, SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, and switch_log_printf().

00221                                                                 {
00222         switch_limit_interface_t *limit = NULL;
00223         char *status = NULL;
00224         
00225         /* locate impl, call appropriate func */
00226         if (!(limit = get_backend(backend))) {
00227                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
00228                 switch_goto_status(strdup("-ERR"), end);
00229         }
00230         
00231         status = limit->status();
00232         
00233 end:
00234         release_backend(limit);
00235         return status;
00236 }

int switch_limit_usage ( const char *  backend,
const char *  realm,
const char *  resource,
uint32_t *  rcount 
)

get usage count for resource

Parameters:
backend to use
realm 
resource 
rcount - output paramter, rate counter

Definition at line 165 of file switch_limit.c.

References get_backend(), release_backend(), SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), and switch_limit_interface::usage.

00165                                                                                                                        {
00166         switch_limit_interface_t *limit = NULL;
00167         int usage = 0;
00168         
00169         /* locate impl, call appropriate func */
00170         if (!(limit = get_backend(backend))) {
00171                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
00172                 goto end;
00173         }
00174         
00175         usage = limit->usage(realm, resource, rcount);
00176         
00177 end:
00178         release_backend(limit);
00179         return usage;
00180 }


Generated on Wed May 16 04:00:19 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7