BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file timer.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Synopsis:
32 * periodic_timer(string down_time, string up_time)
33 *
34 * Description:
35 * Periodically goes up and down. Starting in the down state, waits 'down_time'
36 * milliseconds. Then it goes up, and after 'up_time' milliseconds, goes back down,
37 * and the process repeats. When requested to terminate, terminates immedietely.
38 */
39  
40 #include <stddef.h>
41  
42 #include <misc/debug.h>
43  
44 #include <ncd/module_common.h>
45  
46 #include <generated/blog_channel_ncd_timer.h>
47  
48 #define STATE_DOWN 1
49 #define STATE_UP 2
50  
51 struct instance {
52 NCDModuleInst *i;
53 btime_t down_time;
54 btime_t up_time;
55 BTimer timer;
56 int state;
57 };
58  
59 static void timer_handler (void *vo)
60 {
61 struct instance *o = vo;
62  
63 switch (o->state) {
64 case STATE_DOWN: {
65 o->state = STATE_UP;
66 BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->up_time);
67 NCDModuleInst_Backend_Up(o->i);
68 } break;
69  
70 case STATE_UP: {
71 o->state = STATE_DOWN;
72 BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->down_time);
73 NCDModuleInst_Backend_Down(o->i);
74 } break;
75  
76 default: ASSERT(0);
77 }
78 }
79  
80 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
81 {
82 struct instance *o = vo;
83 o->i = i;
84  
85 // check arguments
86 NCDValRef down_time_arg;
87 NCDValRef up_time_arg;
88 if (!NCDVal_ListRead(params->args, 2, &down_time_arg, &up_time_arg)) {
89 ModuleLog(o->i, BLOG_ERROR, "wrong arity");
90 goto fail0;
91 }
92  
93 // read times
94 if (!ncd_read_time(down_time_arg, &o->down_time)) {
95 ModuleLog(o->i, BLOG_ERROR, "wrong down time");
96 goto fail0;
97 }
98 if (!ncd_read_time(up_time_arg, &o->up_time)) {
99 ModuleLog(o->i, BLOG_ERROR, "wrong up time");
100 goto fail0;
101 }
102  
103 // init timer
104 BTimer_Init(&o->timer, 0, timer_handler, o);
105  
106 // set state down
107 o->state = STATE_DOWN;
108  
109 // set timer
110 BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->down_time);
111 return;
112  
113 fail0:
114 NCDModuleInst_Backend_DeadError(i);
115 }
116  
117 static void func_die (void *vo)
118 {
119 struct instance *o = vo;
120  
121 // free timer
122 BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
123  
124 NCDModuleInst_Backend_Dead(o->i);
125 }
126  
127 static struct NCDModule modules[] = {
128 {
129 .type = "periodic_timer",
130 .func_new2 = func_new,
131 .func_die = func_die,
132 .alloc_size = sizeof(struct instance)
133 }, {
134 .type = NULL
135 }
136 };
137  
138 const struct NCDModuleGroup ncdmodule_timer = {
139 .modules = modules
140 };