cc9047b4eb551f6b0c41b8e767809eb13ac68843
[amitay/samba.git] / source4 / lib / events / events_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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "lib/events/events_internal.h"
26
27 #include <oop.h>
28
29 /*
30  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
31
32  NOTE: this code compiles fine, but is completly *UNTESTED*
33        and is only commited as example
34
35  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   
36 */
37
38 static int oop_event_context_destructor(struct event_context *ev)
39 {
40         oop_source_sys *oop_sys = ev->additional_data;
41
42         oop_sys_delete(oop_sys);
43
44         return 0;
45 }
46
47 /*
48   create a oop_event_context structure.
49 */
50 static int oop_event_context_init(struct event_context *ev, void *private_data)
51 {
52         oop_source_sys *oop_sys = private_data;
53
54         if (!oop_sys) {
55                 oop_sys = oop_sys_new();
56                 if (!oop_sys) {
57                         return -1;
58                 }
59
60                 talloc_set_destructor(ev, oop_event_context_destructor);
61         }
62
63         ev->additional_data = oop_sys;
64
65         return 0;
66 }
67
68 static void *oop_event_fd_handler(oop_source *oop, int fd, oop_event oop_type, void *ptr)
69 {
70         struct fd_event *fde = ptr;
71
72         if (fd != fde->fd) return OOP_ERROR;
73
74         switch(oop_type) {
75                 case OOP_READ:
76                         fde->handler(fde->event_ctx, fde, EVENT_FD_READ, fde->private_data);
77                         return OOP_CONTINUE;
78                 case OOP_WRITE:
79                         fde->handler(fde->event_ctx, fde, EVENT_FD_WRITE, fde->private_data);
80                         return OOP_CONTINUE;                    
81                 case OOP_EXCEPTION:
82                         return OOP_ERROR;
83                 case OOP_NUM_EVENTS:
84                         return OOP_ERROR;
85         }
86
87         return OOP_ERROR;
88 }
89
90 /*
91   destroy an fd_event
92 */
93 static int oop_event_fd_destructor(struct fd_event *fde)
94 {
95         struct event_context *ev = fde->event_ctx;
96         oop_source_sys *oop_sys = ev->additional_data;
97         oop_source *oop = oop_sys_source(oop_sys);
98
99         if (fde->flags & EVENT_FD_READ)
100                 oop->cancel_fd(oop, fde->fd, OOP_READ);
101         if (fde->flags & EVENT_FD_WRITE)
102                 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
103
104         if (fde->flags & EVENT_FD_AUTOCLOSE) {
105                 close(fde->fd);
106                 fde->fd = -1;
107         }
108
109         return 0;
110 }
111
112 /*
113   add a fd based event
114   return NULL on failure (memory allocation error)
115 */
116 static struct fd_event *oop_event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
117                                          int fd, uint16_t flags,
118                                          event_fd_handler_t handler,
119                                          void *private_data)
120 {
121         struct fd_event *fde;
122         oop_source_sys *oop_sys = ev->additional_data;
123         oop_source *oop = oop_sys_source(oop_sys);
124         
125         fde = talloc(mem_ctx?mem_ctx:ev, struct fd_event);
126         if (!fde) return NULL;
127
128         fde->event_ctx          = ev;
129         fde->fd                 = fd;
130         fde->flags              = flags;
131         fde->handler            = handler;
132         fde->private_data       = private_data;
133         fde->additional_flags   = 0;
134         fde->additional_data    = NULL;
135
136         if (fde->flags & EVENT_FD_READ)
137                 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
138         if (fde->flags & EVENT_FD_WRITE)
139                 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
140
141         talloc_set_destructor(fde, oop_event_fd_destructor);
142
143         return fde;
144 }
145
146 /*
147   return the fd event flags
148 */
149 static uint16_t oop_event_get_fd_flags(struct fd_event *fde)
150 {
151         return fde->flags;
152 }
153
154 /*
155   set the fd event flags
156 */
157 static void oop_event_set_fd_flags(struct fd_event *fde, uint16_t flags)
158 {
159         oop_source_sys *oop_sys;
160         oop_source *oop;
161
162         oop_sys = fde->event_ctx->additional_data;
163         oop = oop_sys_source(oop_sys);
164
165         if ((fde->flags & EVENT_FD_READ)&&(!(flags & EVENT_FD_READ)))
166                 oop->cancel_fd(oop, fde->fd, OOP_READ);
167
168         if ((!(fde->flags & EVENT_FD_READ))&&(flags & EVENT_FD_READ))
169                 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
170
171         if ((fde->flags & EVENT_FD_WRITE)&&(!(flags & EVENT_FD_WRITE)))
172                 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
173
174         if ((!(fde->flags & EVENT_FD_WRITE))&&(flags & EVENT_FD_WRITE))
175                 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
176
177         fde->flags = flags;
178 }
179
180 static int oop_event_timed_destructor(struct timed_event *te);
181
182 static int oop_event_timed_deny_destructor(struct timed_event *te)
183 {
184         return -1;
185 }
186
187 static void *oop_event_timed_handler(oop_source *oop, struct timeval t, void *ptr)
188 {
189         struct timed_event *te = ptr;
190
191         /* deny the handler to free the event */
192         talloc_set_destructor(te, oop_event_timed_deny_destructor);
193         te->handler(te->event_ctx, te, t, te->private_data);
194
195         talloc_set_destructor(te, oop_event_timed_destructor);
196         talloc_free(te);
197
198         return OOP_CONTINUE;
199 }
200
201 /*
202   destroy a timed event
203 */
204 static int oop_event_timed_destructor(struct timed_event *te)
205 {
206         struct event_context *ev = te->event_ctx;
207         oop_source_sys *oop_sys = ev->additional_data;
208         oop_source *oop = oop_sys_source(oop_sys);
209
210         oop->cancel_time(oop, te->next_event, oop_event_timed_handler, te);
211
212         return 0;
213 }
214
215 /*
216   add a timed event
217   return NULL on failure (memory allocation error)
218 */
219 static struct timed_event *oop_event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
220                                                struct timeval next_event, 
221                                                event_timed_handler_t handler, 
222                                                void *private_data) 
223 {
224         oop_source_sys *oop_sys = ev->additional_data;
225         oop_source *oop = oop_sys_source(oop_sys);
226         struct timed_event *te;
227
228         te = talloc(mem_ctx?mem_ctx:ev, struct timed_event);
229         if (te == NULL) return NULL;
230
231         te->event_ctx           = ev;
232         te->next_event          = next_event;
233         te->handler             = handler;
234         te->private_data        = private_data;
235         te->additional_data     = NULL;
236
237         oop->on_time(oop, te->next_event, oop_event_timed_handler, te);
238
239         talloc_set_destructor(te, oop_event_timed_destructor);
240
241         return te;
242 }
243
244 /*
245   do a single event loop using the events defined in ev 
246 */
247 static int oop_event_loop_once(struct event_context *ev)
248 {
249         void *oop_ret;
250         oop_source_sys *oop_sys = ev->additional_data;
251
252         oop_ret = oop_sys_run_once(oop_sys);
253         if (oop_ret == OOP_CONTINUE) {
254                 return 0;
255         }
256
257         return -1;
258 }
259
260 /*
261   return on failure or (with 0) if all fd events are removed
262 */
263 static int oop_event_loop_wait(struct event_context *ev)
264 {
265         void *oop_ret;
266         oop_source_sys *oop_sys = ev->additional_data;
267
268         oop_ret = oop_sys_run(oop_sys);
269         if (oop_ret == OOP_CONTINUE) {
270                 return 0;
271         }
272
273         return -1;
274 }
275
276 static const struct event_ops event_oop_ops = {
277         .context_init   = oop_event_context_init,
278         .add_fd         = oop_event_add_fd,
279         .get_fd_flags   = oop_event_get_fd_flags,
280         .set_fd_flags   = oop_event_set_fd_flags,
281         .add_timed      = oop_event_add_timed,
282         .add_signal     = common_event_add_signal,
283         .loop_once      = oop_event_loop_once,
284         .loop_wait      = oop_event_loop_wait,
285 };
286
287 const struct event_ops *event_liboop_get_ops(void)
288 {
289         return &event_oop_ops;
290 }