FreeSWITCH API Documentation  1.7.0
switch_core_timer.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, 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  * Anthony Minessale II <anthm@freeswitch.org>
27  * Michael Jerris <mike@jerris.com>
28  * Paul D. Tinsley <pdt at jackhammer.org>
29  *
30  *
31  * switch_core_timer.c -- Main Core Library (timer interface)
32  *
33  */
34 
35 #include <switch.h>
37 
38 SWITCH_DECLARE(switch_status_t) switch_core_timer_init(switch_timer_t *timer, const char *timer_name, int interval, int samples,
40 {
41  switch_timer_interface_t *timer_interface;
42  switch_status_t status;
43  memset(timer, 0, sizeof(*timer));
44  if ((timer_interface = switch_loadable_module_get_timer_interface(timer_name)) == 0 || !timer_interface->timer_init) {
45  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid timer %s!\n", timer_name);
46  return SWITCH_STATUS_GENERR;
47  }
48 
49  timer->interval = interval;
50  timer->samples = samples;
51  timer->samplecount = samples;
52  timer->timer_interface = timer_interface;
53 
54  if (pool) {
55  timer->memory_pool = pool;
56  } else {
57  if ((status = switch_core_new_memory_pool(&timer->memory_pool)) != SWITCH_STATUS_SUCCESS) {
58  UNPROTECT_INTERFACE(timer->timer_interface);
59  return status;
60  }
62  }
63 
64  return timer->timer_interface->timer_init(timer);
65 }
66 
68 {
69  if (!timer->timer_interface || !timer->timer_interface->timer_next) {
70  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
71  return SWITCH_STATUS_GENERR;
72  }
73 
74  if (timer->timer_interface->timer_next(timer) == SWITCH_STATUS_SUCCESS) {
75  return SWITCH_STATUS_SUCCESS;
76  } else {
77  return SWITCH_STATUS_GENERR;
78  }
79 
80 }
81 
83 {
84  if (!timer->timer_interface || !timer->timer_interface->timer_step) {
85  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
86  return SWITCH_STATUS_GENERR;
87  }
88 
89  return timer->timer_interface->timer_step(timer);
90 }
91 
92 
94 {
95  if (!timer->timer_interface || !timer->timer_interface->timer_sync) {
96  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
97  return SWITCH_STATUS_GENERR;
98  }
99 
100  return timer->timer_interface->timer_sync(timer);
101 }
102 
104 {
105  if (!timer->timer_interface || !timer->timer_interface->timer_check) {
106  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
107  return SWITCH_STATUS_GENERR;
108  }
109 
110  return timer->timer_interface->timer_check(timer, step);
111 }
112 
113 
115 {
116  if (!timer->timer_interface || !timer->timer_interface->timer_destroy) {
117  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
118  return SWITCH_STATUS_GENERR;
119  }
120 
121  timer->timer_interface->timer_destroy(timer);
122  UNPROTECT_INTERFACE(timer->timer_interface);
123 
125  switch_core_destroy_memory_pool(&timer->memory_pool);
126  }
127 
128  memset(timer, 0, sizeof(*timer));
129 
130  return SWITCH_STATUS_SUCCESS;
131 }
132 
133 /* For Emacs:
134  * Local Variables:
135  * mode:c
136  * indent-tabs-mode:t
137  * tab-width:4
138  * c-basic-offset:4
139  * End:
140  * For VIM:
141  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
142  */
switch_timer_interface_t * switch_loadable_module_get_timer_interface(const char *name)
Retrieve the timer interface by it's registered name.
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
switch_status_t switch_core_timer_init(switch_timer_t *timer, const char *timer_name, int interval, int samples, switch_memory_pool_t *pool)
Request a timer handle using given time module.
#define SWITCH_CHANNEL_LOG
switch_bool_t
Definition: switch_types.h:405
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_memory_pool_t * pool
switch_status_t switch_core_timer_step(switch_timer_t *timer)
Step the timer one step.
switch_status_t switch_core_timer_check(switch_timer_t *timer, switch_bool_t step)
Check if the current step has been exceeded.
A table of functions that a timer module implements.
Abstract handler to a timer module.
#define UNPROTECT_INTERFACE(_it)
switch_status_t(* timer_init)(switch_timer_t *)
switch_status_t switch_core_timer_destroy(switch_timer_t *timer)
Destroy an allocated timer.
switch_status_t switch_core_timer_next(switch_timer_t *timer)
Wait for one cycle on an existing timer.
switch_status_t
Common return values.
Main Library Header.
#define SWITCH_DECLARE(type)
struct apr_pool_t switch_memory_pool_t
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
memset(buf, 0, buflen)
switch_status_t switch_core_timer_sync(switch_timer_t *timer)