]> git.ipfire.org Git - thirdparty/freeswitch.git/blob - src/switch_core_timer.c
[Core, mod_curl, mod_httapi, mod_http_cache] Compatible with libcurl>=7.87.0
[thirdparty/freeswitch.git] / src / switch_core_timer.c
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>
36 #include "private/switch_core_pvt.h"
37
38 SWITCH_DECLARE(switch_status_t) switch_core_timer_init(switch_timer_t *timer, const char *timer_name, int interval, int samples,
39 switch_memory_pool_t *pool)
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 }
61 switch_set_flag(timer, SWITCH_TIMER_FLAG_FREE_POOL);
62 }
63
64 return timer->timer_interface->timer_init(timer);
65 }
66
67 SWITCH_DECLARE(switch_status_t) switch_core_timer_next(switch_timer_t *timer)
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
82 SWITCH_DECLARE(switch_status_t) switch_core_timer_step(switch_timer_t *timer)
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
93 SWITCH_DECLARE(switch_status_t) switch_core_timer_sync(switch_timer_t *timer)
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
103 SWITCH_DECLARE(switch_status_t) switch_core_timer_check(switch_timer_t *timer, switch_bool_t step)
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
114 SWITCH_DECLARE(switch_status_t) switch_core_timer_destroy(switch_timer_t *timer)
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
124 if (switch_test_flag(timer, SWITCH_TIMER_FLAG_FREE_POOL)) {
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 */