Attempt to fix the merged build
[jra/samba/.git] / lib / tevent / tevent_liboop.c
1 /* 
2    Unix SMB/CIFS implementation.
3    main select loop and event handling
4    wrapper for http://liboop.org/
5
6    Copyright (C) Stefan Metzmacher 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "events.h"
23 #include "events_internal.h"
24
25 #include <oop.h>
26
27 /*
28  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
29
30  NOTE: this code compiles fine, but is completly *UNTESTED*
31        and is only commited as example
32
33  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   
34 */
35
36 static int oop_event_context_destructor(struct tevent_context *ev)
37 {
38         oop_source_sys *oop_sys = ev->additional_data;
39
40         oop_sys_delete(oop_sys);
41
42         return 0;
43 }
44
45 /*
46   create a oop_event_context structure.
47 */
48 static int oop_event_context_init(struct tevent_context *ev, void *private_data)
49 {
50         oop_source_sys *oop_sys = private_data;
51
52         if (!oop_sys) {
53                 oop_sys = oop_sys_new();
54                 if (!oop_sys) {
55                         return -1;
56                 }
57
58                 talloc_set_destructor(ev, oop_event_context_destructor);
59         }
60
61         ev->additional_data = oop_sys;
62
63         return 0;
64 }
65
66 static void *oop_event_fd_handler(oop_source *oop, int fd, oop_event oop_type, void *ptr)
67 {
68         struct tevent_fd *fde = ptr;
69
70         if (fd != fde->fd) return OOP_ERROR;
71
72         switch(oop_type) {
73                 case OOP_READ:
74                         fde->handler(fde->event_ctx, fde, EVENT_FD_READ, fde->private_data);
75                         return OOP_CONTINUE;
76                 case OOP_WRITE:
77                         fde->handler(fde->event_ctx, fde, EVENT_FD_WRITE, fde->private_data);
78                         return OOP_CONTINUE;                    
79                 case OOP_EXCEPTION:
80                         return OOP_ERROR;
81                 case OOP_NUM_EVENTS:
82                         return OOP_ERROR;
83         }
84
85         return OOP_ERROR;
86 }
87
88 /*
89   destroy an fd_event
90 */
91 static int oop_event_fd_destructor(struct tevent_fd *fde)
92 {
93         struct tevent_context *ev = fde->event_ctx;
94         oop_source_sys *oop_sys = ev->additional_data;
95         oop_source *oop = oop_sys_source(oop_sys);
96
97         if (fde->flags & EVENT_FD_READ)
98                 oop->cancel_fd(oop, fde->fd, OOP_READ);
99         if (fde->flags & EVENT_FD_WRITE)
100                 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
101
102         if (fde->flags & EVENT_FD_AUTOCLOSE) {
103                 close(fde->fd);
104                 fde->fd = -1;
105         }
106
107         return 0;
108 }
109
110 /*
111   add a fd based event
112   return NULL on failure (memory allocation error)
113 */
114 static struct tevent_fd *oop_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
115                                          int fd, uint16_t flags,
116                                          event_fd_handler_t handler,
117                                          void *private_data)
118 {
119         struct tevent_fd *fde;
120         oop_source_sys *oop_sys = ev->additional_data;
121         oop_source *oop = oop_sys_source(oop_sys);
122         
123         fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
124         if (!fde) return NULL;
125
126         fde->event_ctx          = ev;
127         fde->fd                 = fd;
128         fde->flags              = flags;
129         fde->handler            = handler;
130         fde->private_data       = private_data;
131         fde->additional_flags   = 0;
132         fde->additional_data    = NULL;
133
134         if (fde->flags & EVENT_FD_READ)
135                 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
136         if (fde->flags & EVENT_FD_WRITE)
137                 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
138
139         talloc_set_destructor(fde, oop_event_fd_destructor);
140
141         return fde;
142 }
143
144 /*
145   return the fd event flags
146 */
147 static uint16_t oop_event_get_fd_flags(struct tevent_fd *fde)
148 {
149         return fde->flags;
150 }
151
152 /*
153   set the fd event flags
154 */
155 static void oop_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
156 {
157         oop_source_sys *oop_sys;
158         oop_source *oop;
159
160         oop_sys = fde->event_ctx->additional_data;
161         oop = oop_sys_source(oop_sys);
162
163         if ((fde->flags & EVENT_FD_READ)&&(!(flags & EVENT_FD_READ)))
164                 oop->cancel_fd(oop, fde->fd, OOP_READ);
165
166         if ((!(fde->flags & EVENT_FD_READ))&&(flags & EVENT_FD_READ))
167                 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
168
169         if ((fde->flags & EVENT_FD_WRITE)&&(!(flags & EVENT_FD_WRITE)))
170                 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
171
172         if ((!(fde->flags & EVENT_FD_WRITE))&&(flags & EVENT_FD_WRITE))
173                 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
174
175         fde->flags = flags;
176 }
177
178 static int oop_event_timed_destructor(struct tevent_timer *te);
179
180 static int oop_event_timed_deny_destructor(struct tevent_timer *te)
181 {
182         return -1;
183 }
184
185 static void *oop_event_timed_handler(oop_source *oop, struct timeval t, void *ptr)
186 {
187         struct tevent_timer *te = ptr;
188
189         /* deny the handler to free the event */
190         talloc_set_destructor(te, oop_event_timed_deny_destructor);
191         te->handler(te->event_ctx, te, t, te->private_data);
192
193         talloc_set_destructor(te, oop_event_timed_destructor);
194         talloc_free(te);
195
196         return OOP_CONTINUE;
197 }
198
199 /*
200   destroy a timed event
201 */
202 static int oop_event_timed_destructor(struct tevent_timer *te)
203 {
204         struct tevent_context *ev = te->event_ctx;
205         oop_source_sys *oop_sys = ev->additional_data;
206         oop_source *oop = oop_sys_source(oop_sys);
207
208         oop->cancel_time(oop, te->next_event, oop_event_timed_handler, te);
209
210         return 0;
211 }
212
213 /*
214   add a timed event
215   return NULL on failure (memory allocation error)
216 */
217 static struct tevent_timer *oop_event_add_timed(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
218                                                struct timeval next_event, 
219                                                event_timed_handler_t handler, 
220                                                void *private_data) 
221 {
222         oop_source_sys *oop_sys = ev->additional_data;
223         oop_source *oop = oop_sys_source(oop_sys);
224         struct tevent_timer *te;
225
226         te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
227         if (te == NULL) return NULL;
228
229         te->event_ctx           = ev;
230         te->next_event          = next_event;
231         te->handler             = handler;
232         te->private_data        = private_data;
233         te->additional_data     = NULL;
234
235         oop->on_time(oop, te->next_event, oop_event_timed_handler, te);
236
237         talloc_set_destructor(te, oop_event_timed_destructor);
238
239         return te;
240 }
241
242 /*
243   do a single event loop using the events defined in ev 
244 */
245 static int oop_event_loop_once(struct tevent_context *ev)
246 {
247         void *oop_ret;
248         oop_source_sys *oop_sys = ev->additional_data;
249
250         oop_ret = oop_sys_run_once(oop_sys);
251         if (oop_ret == OOP_CONTINUE) {
252                 return 0;
253         }
254
255         return -1;
256 }
257
258 /*
259   return on failure or (with 0) if all fd events are removed
260 */
261 static int oop_event_loop_wait(struct tevent_context *ev)
262 {
263         void *oop_ret;
264         oop_source_sys *oop_sys = ev->additional_data;
265
266         oop_ret = oop_sys_run(oop_sys);
267         if (oop_ret == OOP_CONTINUE) {
268                 return 0;
269         }
270
271         return -1;
272 }
273
274 static const struct event_ops event_oop_ops = {
275         .context_init   = oop_event_context_init,
276         .add_fd         = oop_event_add_fd,
277         .get_fd_flags   = oop_event_get_fd_flags,
278         .set_fd_flags   = oop_event_set_fd_flags,
279         .add_timer      = oop_event_add_timed,
280         .add_signal     = common_event_add_signal,
281         .loop_once      = oop_event_loop_once,
282         .loop_wait      = oop_event_loop_wait,
283 };
284
285 const struct event_ops *event_liboop_get_ops(void)
286 {
287         return &event_oop_ops;
288 }