File: bonobo-async.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (421 lines) | stat: -rw-r--r-- 11,314 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <bonobo/bonobo-async.h>

struct _BonoboAsyncReply {
	CORBA_Object        object;
	gpointer           *args;

	const BonoboAsyncMethod *method;

	CORBA_Environment   ev;
	CORBA_Environment  *cur_ev;

	BonoboAsyncCallback cb;
	gpointer            user_data;

	GIOPConnection     *request_cnx;

	guint               timeout_msec;
	GIOP_unsigned_long  request_id;

	GIOPRecvBuffer     *recv_buffer;
};

static BonoboAsyncReply *
handle_new (const BonoboAsyncMethod *method,
	    BonoboAsyncCallback      cb,
	    gpointer                 user_data,
	    guint                    timeout_msec,
	    CORBA_Object             object,
	    gpointer                *args,
	    CORBA_Environment       *ev)
{
	BonoboAsyncReply *handle = g_new0 (BonoboAsyncReply, 1);
	int             i, num_args = 0;
	gpointer       *src;
	BonoboAsyncArg *arg;

	for (arg = (BonoboAsyncArg *) method->arguments; arg && arg->flag; ++arg) 
		++num_args;

	handle->cb = cb;
	handle->object = CORBA_Object_duplicate (object, ev);;
	handle->method = method;
	handle->user_data = user_data;
	handle->request_cnx = ORBit_object_get_connection (object);
	giop_connection_ref (handle->request_cnx);
	handle->request_id = GPOINTER_TO_UINT (handle);
	handle->args = g_new0 (gpointer, num_args);
	handle->timeout_msec = timeout_msec;

	/*
	 * We have to copy in case of an evil forwarding
	 * later and the raving inefficiency this gives us,
	 * FIXME: should prolly just keep the marshalled buffer
	 * instead.
	 */
	src = args;
	for (i = 0; i < num_args; i++) {
		/* No point in passing inouts by reference. */
		if (method->arguments [i].flag & BONOBO_ASYNC_IN) {
			handle->args [i] = ORBit_copy_value (
				*src, method->arguments [i].type);
			src++;
		}
	}
	
	CORBA_exception_init (&handle->ev);
	handle->cur_ev = ev;

	return handle;
}

static void
got_reply (BonoboAsyncReply *handle)
{
	g_source_remove_by_user_data (handle);
}

static void
handle_free (BonoboAsyncReply *handle)
{
	int i = 0;
	CORBA_Environment ev;

	got_reply (handle);

	CORBA_exception_init (&ev);
	CORBA_Object_release (handle->object, &ev);
	CORBA_exception_free (&ev);

	while (&handle->method->arguments [i] && handle->method->arguments [i].type) {
		if (handle->args [i])
			CORBA_free (handle->args [i]);
		i++;
	}

	giop_connection_unref (handle->request_cnx);
	g_free (handle->args);
	g_free (handle);
}


/*
 * ORBit_handle_exception: hacked from env.c; we don't want our own
 * de-marshallers, use a fully generic mechanism instead.
 */
static void
demarshal_exception (BonoboAsyncReply *handle,
		     GIOPRecvBuffer   *rb)
{
	CORBA_ORB              orb = handle->object->orb;
	CORBA_SystemException *new;
	CORBA_unsigned_long     len, completion_status;
	CORBA_char             *my_repoid;
	CORBA_Environment      *ev = handle->cur_ev;
	const CORBA_TypeCode   *types = handle->method->exceptions;

	g_return_if_fail (GIOP_MESSAGE_BUFFER (rb)->message_header.message_type == GIOP_REPLY);

	rb->cur = ALIGN_ADDRESS (rb->cur, sizeof (len));
	rb->decoder (&len, rb->cur, sizeof (len));
	rb->cur = ((guchar *)rb->cur) + sizeof (len);

	if (len) {
		my_repoid = rb->cur;
		rb->cur = ((guchar *)rb->cur) + len;
	} else
		my_repoid = NULL;

	if (rb->message.u.reply.reply_status == CORBA_SYSTEM_EXCEPTION) {
		CORBA_unsigned_long minor;

		ev->_major = CORBA_SYSTEM_EXCEPTION;

		rb->cur = ALIGN_ADDRESS (rb->cur, sizeof (minor));
		rb->decoder (&minor, rb->cur, sizeof (minor));
		rb->cur = ((guchar *) rb->cur) + sizeof (minor);

		rb->cur = ALIGN_ADDRESS (rb->cur, sizeof (completion_status));
		rb->decoder (&completion_status, rb->cur, sizeof (completion_status));
		rb->cur = ((guchar *)rb->cur) + sizeof (completion_status);

		new = ORBit_alloc (sizeof (CORBA_SystemException), NULL, NULL);

		if (new) {
			new->minor = minor;
			new->completed = completion_status;
			
			/* XXX what should the repo ID be? */
			CORBA_exception_set (ev, CORBA_SYSTEM_EXCEPTION,
					     my_repoid, new);
		}
	} else if (rb->message.u.reply.reply_status == CORBA_USER_EXCEPTION) {
		int i;

		if (!types) {
			/* weirdness; they raised an exception that we don't
			   know about */
			CORBA_exception_set_system (ev, ex_CORBA_MARSHAL,
						    CORBA_COMPLETED_MAYBE);
		} else {
			for (i = 0; types [i]; i++) {
				if (!strcmp (types [i]->repo_id, my_repoid))
					break;
			}

			if (!types [i]) {
				/* weirdness; they raised an exception
				   that we don't know about */
				CORBA_exception_set_system (ev, ex_CORBA_MARSHAL,
							    CORBA_COMPLETED_MAYBE);
			} else {
				gpointer data =
					ORBit_demarshal_arg (rb, types [i], TRUE, orb);
				CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
						     types [i]->repo_id, data);
			}
		}
	};
}

static void bonobo_async_marshal (BonoboAsyncReply *handle);

static void
handle_wire_data (GIOPRecvBuffer   *recv_buffer,
		  BonoboAsyncReply *handle)
{
	got_reply (handle);

	handle->recv_buffer = recv_buffer;

	if (recv_buffer->message.u.reply.reply_status != GIOP_NO_EXCEPTION) {
		if (recv_buffer->message.u.reply.reply_status == GIOP_LOCATION_FORWARD) {

			if (handle->object->forward_locations != NULL)
				ORBit_delete_profiles (handle->object->forward_locations);

			handle->object->forward_locations = ORBit_demarshal_IOR (recv_buffer);
			giop_recv_buffer_unuse (recv_buffer);

			giop_connection_unref (handle->request_cnx);
			handle->request_cnx =
				ORBit_object_get_forwarded_connection (handle->object);

			bonobo_async_marshal (handle);
			return;
		} else {
			demarshal_exception (handle, recv_buffer);
			giop_recv_buffer_unuse (recv_buffer);
		}
	}
}

static void
timeout_fn (BonoboAsyncReply *handle)
{
	got_reply (handle);

	CORBA_exception_set_system (handle->cur_ev,
				    ex_CORBA_COMM_FAILURE,
				    CORBA_COMPLETED_NO);

	handle->cb (handle, handle->cur_ev, handle->user_data);

	handle_free (handle);
}

static gboolean
idle_fn (BonoboAsyncReply *handle)
{
	GIOPRecvBuffer *recv_buffer;

	if (!(recv_buffer =
	      giop_recv_reply_buffer_use_2 (
		      handle->request_cnx,
		      handle->request_id, FALSE)))
		return TRUE;

	handle_wire_data (recv_buffer, handle);

	handle->cb (handle, handle->cur_ev, handle->user_data);
	
	handle_free (handle);

	return FALSE;
}

typedef struct {
	CORBA_unsigned_long len;
	char                opname[1];
} OpData;

static void
bonobo_async_marshal (BonoboAsyncReply *handle)
{
	int             namelen, i = 0;
	OpData         *data;
	struct iovec    vector;
	GIOPSendBuffer *send_buffer;

	namelen = strlen (handle->method->name);
	data = g_malloc (sizeof (OpData) + namelen);
	data->len = namelen + 1;
	memcpy (&data->opname, handle->method->name, namelen + 1);
	
	vector.iov_base = data;
	vector.iov_len  = data->len + sizeof (CORBA_unsigned_long);

/*	g_warning ("Sending '%s' with id 0x%x ", ((guchar *)data) + 4,
	handle->request_id);*/
					       
	send_buffer = giop_send_request_buffer_use (
		handle->request_cnx, NULL, handle->request_id, CORBA_TRUE,
		&(handle->object->active_profile->object_key_vec),
		&vector, &ORBit_default_principal_iovec);

	if (send_buffer) {
		while (&handle->method->arguments [i] && handle->method->arguments [i].type) {
			if (handle->method->arguments [i].flag & BONOBO_ASYNC_IN)
				ORBit_marshal_arg (send_buffer, 
						   handle->args [i],
						   handle->method->arguments [i].type);
			i++;
		}

		g_assert (handle->request_cnx->fd > 0);

		/* We add the timeout function. */
		g_timeout_add (handle->timeout_msec,
			       (GSourceFunc) timeout_fn, handle);
			
		/*
		 * an old and broken scheme
		 *
		g_io_add_watch (handle->channel,
				G_IO_ERR | G_IO_HUP | G_IO_NVAL,
				(GIOFunc) demarshal_err_fn, handle);*/

		g_idle_add ((GSourceFunc) idle_fn, handle);

		giop_send_buffer_write (send_buffer);
		giop_send_buffer_unuse (send_buffer);

		handle->cur_ev = &handle->ev;
	} else {
		handle_free (handle);
		CORBA_exception_set_system (handle->cur_ev,
					    ex_CORBA_COMM_FAILURE,
					    CORBA_COMPLETED_NO);
	}
	g_free (data);
}

/**
 * bonobo_async_demarshal:
 * @handle: the reply handle
 * @retval: pointer to variable to store retval in
 * @out_args: array of pointers to vars for InOut / Out params.
 * 
 * When a user's async callback happens ( when a method's return data
 * arrives back ) this function is typicaly used by the callback
 * to de-marshal the arguments associated with the method, such as
 * the @retval, and an array of pointers to memory in which to store
 * the returned InOut / Out arguments in order.
 **/
void
bonobo_async_demarshal (BonoboAsyncReply *handle,
			gpointer          retval,
			gpointer         *out_args)
{
	GIOPRecvBuffer *recv_buffer;
	gpointer        value, src;
	CORBA_TypeCode  tc;
	CORBA_ORB       orb;
	int             i = 0;
       
	g_return_if_fail (handle != NULL);
	g_return_if_fail (retval != NULL);

	recv_buffer = handle->recv_buffer;

	if (recv_buffer->message.u.reply.reply_status != GIOP_NO_EXCEPTION) {
		g_warning ("Trying to demarshal args, when exception was fired");
		return;
	}

	orb = handle->object->orb;

	/* This is sluggish because ORBit_demarshal_value is not public */
	tc = handle->method->ret_type;
	src = value = ORBit_demarshal_arg (recv_buffer, tc, TRUE, orb);
	_ORBit_copy_value (&src, &retval, tc);
	CORBA_free (value);

	while (&handle->method->arguments [i] && handle->method->arguments [i].type) {
		tc = handle->method->arguments [i].type;

		/* This is sluggish because ORBit_demarshal_value is not public */
		if (handle->method->arguments [i].flag & BONOBO_ASYNC_OUT) {
			gpointer dest;

			g_return_if_fail (out_args != NULL);

			dest = *out_args++;
			src = value = ORBit_demarshal_arg (recv_buffer, tc, TRUE, orb);
			_ORBit_copy_value (&src, &dest, tc);
			CORBA_free (value);
		}
	}
}

/**
 * bonobo_async_invoke:
 * @method: method description
 * @cb: async callback
 * @user_data: associated callback user data
 * @timeout_msec: timeout in milli-seconds
 * @object: object to invoke method on
 * @args: array of ordered In/InOut arguments in same order as in method
 * @ev: CORBA exception environment
 * 
 * Given a type based description of a CORBA @method
 * the method is invoked asynchronously. If an error occurs
 * in invocation an exception is fired in @ev and @cb will
 * never be invoked.
 * Otherwise, @cb is invoked, either on the timeout - in
 * which case an ex_CORBA_COMM_FAILURE system exception will be
 * returned, or when the method returns with whatever data /
 * exceptions are relevant.
 **/
void
bonobo_async_invoke (const BonoboAsyncMethod *method,
		     BonoboAsyncCallback      cb,
		     gpointer                 user_data,
		     guint                    timeout_msec,
		     CORBA_Object             object,
		     gpointer                *args,
		     CORBA_Environment       *ev)
{
	BonoboAsyncReply *handle = handle_new (method, cb, user_data,
					       timeout_msec,
					       object, args, ev);

	bonobo_async_marshal (handle);
}

/**
 * bonobo_async_handle_get_recv:
 * @handle: the reply handle.
 * 
 * This method can be used to extract the internal GIOP
 * buffer on the reply, for advanced custom de-marshaling.
 * 
 * Return value: the internal reply buffer.
 **/
GIOPRecvBuffer *
bonobo_async_handle_get_recv (BonoboAsyncReply *handle)
{
	g_return_val_if_fail (handle != NULL, NULL);

	return handle->recv_buffer;
}