BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file event_template.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 | |||
30 | #include <stdlib.h> |
||
31 | |||
32 | #include <misc/offset.h> |
||
33 | #include <misc/debug.h> |
||
34 | #include <misc/balloc.h> |
||
35 | |||
36 | #include <ncd/modules/event_template.h> |
||
37 | |||
38 | #define TemplateLog(o, ...) NCDModuleInst_Backend_Log((o)->i, (o)->blog_channel, __VA_ARGS__) |
||
39 | |||
40 | static void enable_event (event_template *o) |
||
41 | { |
||
42 | ASSERT(!LinkedList1_IsEmpty(&o->events_list)) |
||
43 | ASSERT(!o->enabled) |
||
44 | |||
45 | // get event |
||
46 | struct event_template_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->events_list), struct event_template_event, events_list_node); |
||
47 | |||
48 | // remove from events list |
||
49 | LinkedList1_Remove(&o->events_list, &e->events_list_node); |
||
50 | |||
51 | // grab enabled map |
||
52 | o->enabled_map = e->map; |
||
53 | |||
54 | // append to free list |
||
55 | LinkedList1_Append(&o->free_list, &e->events_list_node); |
||
56 | |||
57 | // set enabled |
||
58 | o->enabled = 1; |
||
59 | |||
60 | // signal up |
||
61 | NCDModuleInst_Backend_Up(o->i); |
||
62 | } |
||
63 | |||
64 | void event_template_new (event_template *o, NCDModuleInst *i, int blog_channel, int maxevents, void *user, |
||
65 | event_template_func_free func_free) |
||
66 | { |
||
67 | ASSERT(maxevents > 0) |
||
68 | |||
69 | // init arguments |
||
70 | o->i = i; |
||
71 | o->blog_channel = blog_channel; |
||
72 | o->user = user; |
||
73 | o->func_free = func_free; |
||
74 | |||
75 | // allocate events array |
||
76 | if (!(o->events = BAllocArray(maxevents, sizeof(o->events[0])))) { |
||
77 | TemplateLog(o, BLOG_ERROR, "BAllocArray failed"); |
||
78 | goto fail0; |
||
79 | } |
||
80 | |||
81 | // init events lists |
||
82 | LinkedList1_Init(&o->events_list); |
||
83 | LinkedList1_Init(&o->free_list); |
||
84 | for (int j = 0; j < maxevents; j++) { |
||
85 | LinkedList1_Append(&o->free_list, &o->events[j].events_list_node); |
||
86 | } |
||
87 | |||
88 | // set not enabled |
||
89 | o->enabled = 0; |
||
90 | |||
91 | return; |
||
92 | |||
93 | fail0: |
||
94 | o->func_free(o->user, 1); |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | void event_template_die (event_template *o) |
||
99 | { |
||
100 | // free enabled map |
||
101 | if (o->enabled) { |
||
102 | BStringMap_Free(&o->enabled_map); |
||
103 | } |
||
104 | |||
105 | // free event maps |
||
106 | LinkedList1Node *list_node = LinkedList1_GetFirst(&o->events_list); |
||
107 | while (list_node) { |
||
108 | struct event_template_event *e = UPPER_OBJECT(list_node, struct event_template_event, events_list_node); |
||
109 | BStringMap_Free(&e->map); |
||
110 | list_node = LinkedList1Node_Next(list_node); |
||
111 | } |
||
112 | |||
113 | // free events array |
||
114 | BFree(o->events); |
||
115 | |||
116 | o->func_free(o->user, 0); |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | int event_template_getvar (event_template *o, const char *name, NCDValMem *mem, NCDValRef *out) |
||
121 | { |
||
122 | ASSERT(o->enabled) |
||
123 | ASSERT(name) |
||
124 | |||
125 | const char *val = BStringMap_Get(&o->enabled_map, name); |
||
126 | if (!val) { |
||
127 | return 0; |
||
128 | } |
||
129 | |||
130 | *out = NCDVal_NewString(mem, val); |
||
131 | return 1; |
||
132 | } |
||
133 | |||
134 | void event_template_queue (event_template *o, BStringMap map, int *out_was_empty) |
||
135 | { |
||
136 | ASSERT(!LinkedList1_IsEmpty(&o->free_list)) |
||
137 | |||
138 | // get event |
||
139 | struct event_template_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->free_list), struct event_template_event, events_list_node); |
||
140 | |||
141 | // remove from free list |
||
142 | LinkedList1_Remove(&o->free_list, &e->events_list_node); |
||
143 | |||
144 | // set map |
||
145 | e->map = map; |
||
146 | |||
147 | // insert to events list |
||
148 | LinkedList1_Append(&o->events_list, &e->events_list_node); |
||
149 | |||
150 | // enable if not already |
||
151 | if (!o->enabled) { |
||
152 | enable_event(o); |
||
153 | *out_was_empty = 1; |
||
154 | } else { |
||
155 | *out_was_empty = 0; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | void event_template_dequeue (event_template *o, int *out_is_empty) |
||
160 | { |
||
161 | ASSERT(o->enabled) |
||
162 | |||
163 | // free enabled map |
||
164 | BStringMap_Free(&o->enabled_map); |
||
165 | |||
166 | // set not enabled |
||
167 | o->enabled = 0; |
||
168 | |||
169 | // signal down |
||
170 | NCDModuleInst_Backend_Down(o->i); |
||
171 | |||
172 | // enable if there are more events |
||
173 | if (!LinkedList1_IsEmpty(&o->events_list)) { |
||
174 | enable_event(o); |
||
175 | *out_is_empty = 0; |
||
176 | } else { |
||
177 | *out_is_empty = 1; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | int event_template_is_enabled (event_template *o) |
||
182 | { |
||
183 | return o->enabled; |
||
184 | } |