f04309bbfb9a88719b20bf436bdf378d662cebca
[kai/samba.git] / source4 / libcli / composite / composite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   composite API helper functions
22 */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/composite/composite.h"
28
29
30 /*
31   block until a composite function has completed, then return the status
32 */
33 NTSTATUS composite_wait(struct composite_context *c)
34 {
35         if (c == NULL) return NT_STATUS_NO_MEMORY;
36
37         while (c->state < SMBCLI_REQUEST_DONE) {
38                 if (event_loop_once(c->event_ctx) != 0) {
39                         return NT_STATUS_UNSUCCESSFUL;
40                 }
41         }
42
43         return c->status;
44 }
45
46
47 /* 
48    callback from composite_trigger_done() 
49 */
50 static void composite_trigger(struct event_context *ev, struct timed_event *te,
51                               struct timeval t, void *ptr)
52 {
53         struct composite_context *c = talloc_get_type(ptr, struct composite_context);
54         c->state = SMBCLI_REQUEST_DONE;
55         if (c->async.fn) {
56                 c->async.fn(c);
57         }
58 }
59
60
61 /*
62   trigger an immediate 'done' event on a composite context
63   this is used when the composite code works out that the call
64   can be completed without waiting for any external event
65 */
66 void composite_trigger_done(struct composite_context *c)
67 {
68         /* a zero timeout means immediate */
69         event_add_timed(c->event_ctx, c, timeval_zero(), composite_trigger, c);
70 }