r12608: Remove some unused #include lines.
[sfrench/samba-autobuild/.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 #include "lib/messaging/irpc.h"
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         c->used_wait = True;
38
39         while (c->state < COMPOSITE_STATE_DONE) {
40                 if (event_loop_once(c->event_ctx) != 0) {
41                         return NT_STATUS_UNSUCCESSFUL;
42                 }
43         }
44
45         return c->status;
46 }
47
48
49 /*
50  * Some composite helpers that are handy if you write larger composite
51  * functions.
52  */
53 BOOL composite_is_ok(struct composite_context *ctx)
54 {
55         if (NT_STATUS_IS_OK(ctx->status)) {
56                 return True;
57         }
58         ctx->state = COMPOSITE_STATE_ERROR;
59         if (ctx->async.fn != NULL) {
60                 ctx->async.fn(ctx);
61         }
62         return False;
63 }
64
65 /* 
66    callback from composite_done() and composite_error()
67
68    this is used to allow for a composite function to complete without
69    going through any state transitions. When that happens the caller
70    has had no opportunity to fill in the async callback fields
71    (ctx->async.fn and ctx->async.private) which means the usual way of
72    dealing with composite functions doesn't work. To cope with this,
73    we trigger a timer event that will happen then the event loop is
74    re-entered. This gives the caller a chance to setup the callback,
75    and allows the caller to ignore the fact that the composite
76    function completed early
77 */
78 static void composite_trigger(struct event_context *ev, struct timed_event *te,
79                               struct timeval t, void *ptr)
80 {
81         struct composite_context *c = talloc_get_type(ptr, struct composite_context);
82         if (c->async.fn) {
83                 c->async.fn(c);
84         }
85 }
86
87
88 void composite_error(struct composite_context *ctx, NTSTATUS status)
89 {
90         if (!ctx->used_wait && !ctx->async.fn) {
91                 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
92         }
93         ctx->status = status;
94         SMB_ASSERT(!composite_is_ok(ctx));
95 }
96
97 BOOL composite_nomem(const void *p, struct composite_context *ctx)
98 {
99         if (p != NULL) {
100                 return False;
101         }
102         composite_error(ctx, NT_STATUS_NO_MEMORY);
103         return True;
104 }
105
106 void composite_done(struct composite_context *ctx)
107 {
108         if (!ctx->used_wait && !ctx->async.fn) {
109                 event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
110         }
111         ctx->state = COMPOSITE_STATE_DONE;
112         if (ctx->async.fn != NULL) {
113                 ctx->async.fn(ctx);
114         }
115 }
116
117 void composite_continue(struct composite_context *ctx,
118                         struct composite_context *new_ctx,
119                         void (*continuation)(struct composite_context *),
120                         void *private_data)
121 {
122         if (composite_nomem(new_ctx, ctx)) return;
123         new_ctx->async.fn = continuation;
124         new_ctx->async.private_data = private_data;
125 }
126
127 void composite_continue_rpc(struct composite_context *ctx,
128                             struct rpc_request *new_req,
129                             void (*continuation)(struct rpc_request *),
130                             void *private_data)
131 {
132         if (composite_nomem(new_req, ctx)) return;
133         new_req->async.callback = continuation;
134         new_req->async.private = private_data;
135 }
136
137 void composite_continue_irpc(struct composite_context *ctx,
138                              struct irpc_request *new_req,
139                              void (*continuation)(struct irpc_request *),
140                              void *private_data)
141 {
142         if (composite_nomem(new_req, ctx)) return;
143         new_req->async.fn = continuation;
144         new_req->async.private = private_data;
145 }
146
147 void composite_continue_smb(struct composite_context *ctx,
148                             struct smbcli_request *new_req,
149                             void (*continuation)(struct smbcli_request *),
150                             void *private_data)
151 {
152         if (composite_nomem(new_req, ctx)) return;
153         new_req->async.fn = continuation;
154         new_req->async.private = private_data;
155 }
156
157 void composite_continue_nbt(struct composite_context *ctx,
158                             struct nbt_name_request *new_req,
159                             void (*continuation)(struct nbt_name_request *),
160                             void *private_data)
161 {
162         if (composite_nomem(new_req, ctx)) return;
163         new_req->async.fn = continuation;
164         new_req->async.private = private_data;
165 }