source: trunk/ORBit2-2.14.0/test/everything/server.c

Last change on this file was 92, checked in by cinc, 19 years ago

Orbit2 modified for use with NOM

File size: 16.0 KB
Line 
1/*
2 * CORBA C language mapping tests
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2, or (at your option) any
7 * later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Author: Phil Dawes <philipd@users.sourceforge.net>
19 */
20
21#undef DO_HARDER_SEGV
22
23#include "everything.h"
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27
28#include "orbit-imodule.h"
29
30/* Singleton accessor for the test factory */
31test_TestFactory getFactoryInstance(CORBA_Environment *ev);
32
33typedef void (*init_fn_t) (PortableServer_Servant, CORBA_Environment *);
34
35CORBA_Object create_object (PortableServer_POA poa,
36 gpointer servant,
37 CORBA_Environment *ev);
38
39CORBA_ORB global_orb;
40PortableServer_POA global_poa;
41
42static void
43simple_finalize (PortableServer_Servant servant,
44 CORBA_Environment *ev)
45{
46/* g_warning ("Finalize servant %p", servant); */
47 g_free (servant);
48}
49
50static gpointer
51simple_servant_new (gpointer vepv, init_fn_t fn)
52{
53 CORBA_Environment ev[1];
54 PortableServer_ClassInfo *class_info;
55 PortableServer_ServantBase *servant =
56 g_new0 (PortableServer_ServantBase, 1);
57
58 servant->vepv = vepv;
59 g_assert (servant->vepv[0] != NULL);
60
61 CORBA_exception_init (ev);
62 fn (servant, ev);
63 if (ev->_major != CORBA_NO_EXCEPTION)
64 g_error ("object__init failed: %d\n", ev->_major);
65 g_assert (ORBIT_SERVANT_TO_CLASSINFO (servant) != NULL);
66 CORBA_exception_free (ev);
67
68 class_info = ORBIT_SERVANT_TO_CLASSINFO (servant);
69 g_assert (class_info);
70
71/* g_warning ("Create servant %p ('%s')",
72 servant, class_info->class_name); */
73
74 return servant;
75}
76
77#define SIMPLE_SERVANT_NEW(type) (simple_servant_new (&(type##_vepv), POA_test_##type##__init))
78
79PortableServer_ServantBase__epv Simple_base_epv = {NULL, simple_finalize, NULL};
80
81#include "basicServer.c"
82#include "structServer.c"
83#include "sequenceServer.c"
84#include "unionServer.c"
85#include "arrayServer.c"
86#include "anyServer.c"
87#include "contextServer.c"
88#include "deadReference.c"
89#include "lifeCycle.c"
90#include "pingServer.c"
91#include "derivedServer.c"
92
93typedef struct {
94 POA_test_TestFactory baseServant;
95
96 test_BasicServer basicServerRef;
97 test_StructServer structServerRef;
98 test_SequenceServer sequenceServerRef;
99 test_UnionServer unionServerRef;
100 test_ArrayServer arrayServerRef;
101 test_AnyServer anyServerRef;
102 test_ContextServer contextServerRef;
103 test_DerivedServer derivedServerRef;
104 GSList *pingPongServerRefs;
105} test_TestFactory_Servant;
106
107
108static test_BasicServer
109TestFactory_getBasicServer (PortableServer_Servant servant,
110 CORBA_Environment *ev)
111{
112 test_TestFactory_Servant *this = (test_TestFactory_Servant *) servant;
113
114 return CORBA_Object_duplicate (this->basicServerRef, ev);
115}
116
117static CORBA_char *
118TestFactory_getStructServerIOR (PortableServer_Servant servant,
119 CORBA_Environment *ev)
120{
121 test_TestFactory_Servant *this = (test_TestFactory_Servant *) servant;
122
123 return CORBA_ORB_object_to_string (global_orb, this->structServerRef, ev);
124}
125
126static test_StructServer
127TestFactory_getStructServer (PortableServer_Servant servant,
128 CORBA_Environment *ev)
129{
130 test_TestFactory_Servant *this = (test_TestFactory_Servant*) servant;
131
132 return CORBA_Object_duplicate (this->structServerRef, ev);
133}
134
135static
136test_SequenceServer
137TestFactory_getSequenceServer(PortableServer_Servant servant,
138 CORBA_Environment *ev)
139{
140 test_TestFactory_Servant *this = (test_TestFactory_Servant*) servant;
141
142 return CORBA_Object_duplicate(this->sequenceServerRef,ev);
143}
144
145static test_UnionServer
146TestFactory_getUnionServer (PortableServer_Servant servant,
147 CORBA_Environment *ev)
148{
149 test_TestFactory_Servant *this = (test_TestFactory_Servant*) servant;
150
151 return CORBA_Object_duplicate (this->unionServerRef, ev);
152}
153
154static test_ArrayServer
155TestFactory_getArrayServer (PortableServer_Servant servant,
156 CORBA_Environment *ev)
157{
158 test_TestFactory_Servant *this = (test_TestFactory_Servant*) servant;
159
160 return CORBA_Object_duplicate (this->arrayServerRef, ev);
161}
162
163static test_AnyServer
164TestFactory_getAnyServer (PortableServer_Servant servant,
165 CORBA_Environment *ev)
166{
167 test_TestFactory_Servant *this = (test_TestFactory_Servant*) servant;
168
169 return CORBA_Object_duplicate (this->anyServerRef, ev);
170}
171
172static test_ContextServer
173TestFactory_getContextServer (PortableServer_Servant servant,
174 CORBA_Environment *ev)
175{
176 test_TestFactory_Servant *this = (test_TestFactory_Servant*) servant;
177
178 return CORBA_Object_duplicate (this->contextServerRef, ev);
179}
180
181static test_DerivedServer
182TestFactory_getDerivedServer (PortableServer_Servant servant,
183 CORBA_Environment *ev)
184{
185 test_TestFactory_Servant *this = (test_TestFactory_Servant*) servant;
186
187 return CORBA_Object_duplicate (this->derivedServerRef, ev);
188}
189
190static test_PingPongServer
191TestFactory_createPingPongServer (PortableServer_Servant servant,
192 CORBA_Environment *ev)
193{
194 test_PingPongServer obj;
195
196 obj = create_object (
197 global_poa, create_ping_pong_servant (), ev);
198
199 if (servant) {
200 test_TestFactory_Servant *this;
201
202 this = (test_TestFactory_Servant*) servant;
203
204 this->pingPongServerRefs = g_slist_prepend (
205 this->pingPongServerRefs, obj);
206 }
207
208 return CORBA_Object_duplicate (obj, ev);
209}
210
211static void
212TestFactory_noOp (PortableServer_Servant servant,
213 CORBA_Environment *ev)
214{
215 /* do nothing, fast */
216}
217
218static test_DeadReferenceObj
219TestFactory_createDeadReferenceObj (PortableServer_Servant servant,
220 CORBA_Environment *ev)
221{
222 PortableServer_Current poa_current;
223 PortableServer_POA poa;
224 CORBA_Object obj;
225
226 poa_current = (PortableServer_Current)
227 CORBA_ORB_resolve_initial_references (global_orb,
228 "POACurrent",
229 ev);
230 g_assert (ev->_major == CORBA_NO_EXCEPTION);
231
232 poa = PortableServer_Current_get_POA (poa_current, ev);
233 g_assert (ev->_major == CORBA_NO_EXCEPTION);
234
235 obj = create_object (poa, SIMPLE_SERVANT_NEW (DeadReferenceObj), ev);
236
237 CORBA_Object_release ((CORBA_Object) poa, ev);
238 CORBA_Object_release ((CORBA_Object) poa_current, ev);
239
240 /* Note: Not duping - ORB will free it and reference
241 * should dangle. */
242 return obj;
243}
244
245static test_LifeCycleServer
246TestFactory_createLifeCycleServer (PortableServer_Servant servant,
247 CORBA_Environment *ev)
248{
249 CORBA_Object obj = create_object (global_poa, SIMPLE_SERVANT_NEW (LifeCycleServer), ev);
250 return obj;
251}
252
253static void
254TestFactory_segv (PortableServer_Servant servant,
255 const CORBA_char *when,
256 CORBA_Environment *ev)
257{
258#ifdef DO_HARDER_SEGV
259 /* Emulate a SegV */
260 exit (0);
261#else
262 CORBA_ORB_shutdown (global_orb, TRUE, ev);
263#endif
264}
265
266static void
267test_TestFactory__fini (PortableServer_Servant servant,
268 CORBA_Environment *ev)
269{
270 GSList *l;
271 test_TestFactory_Servant *this;
272
273 this = (test_TestFactory_Servant*) servant;
274
275 CORBA_Object_release (this->basicServerRef, ev);
276 CORBA_Object_release (this->structServerRef, ev);
277 CORBA_Object_release (this->sequenceServerRef, ev);
278 CORBA_Object_release (this->unionServerRef, ev);
279 CORBA_Object_release (this->arrayServerRef, ev);
280 CORBA_Object_release (this->anyServerRef, ev);
281 CORBA_Object_release (this->contextServerRef, ev);
282 CORBA_Object_release (this->derivedServerRef, ev);
283
284 for (l = this->pingPongServerRefs; l; l = l->next)
285 CORBA_Object_release (l->data, ev);
286 g_slist_free (this->pingPongServerRefs);
287
288 g_free (servant);
289}
290
291/* vtable */
292static PortableServer_ServantBase__epv TestFactory_base_epv = {
293 NULL,
294 test_TestFactory__fini,
295 NULL
296};
297
298static POA_test_TestFactory__epv TestFactory_epv = {
299 NULL,
300 TestFactory_getBasicServer,
301 TestFactory_getStructServer,
302 TestFactory_getStructServerIOR,
303 TestFactory_getSequenceServer,
304 TestFactory_getUnionServer,
305 TestFactory_getArrayServer,
306 TestFactory_getAnyServer,
307 TestFactory_getContextServer,
308 TestFactory_segv,
309 NULL, /* getBaseServer */
310 TestFactory_getDerivedServer,
311 NULL, /* getDerivedServerAsBaseServer */
312 NULL, /* getDerivedServerAsB2 */
313 NULL, /* createTransientObj */
314 TestFactory_createDeadReferenceObj,
315 TestFactory_createPingPongServer,
316 TestFactory_createLifeCycleServer,
317 TestFactory_noOp
318};
319
320static POA_test_TestFactory__vepv TestFactory_vepv = {
321 &TestFactory_base_epv,
322 &TestFactory_epv
323};
324
325static PortableServer_POA
326start_poa (CORBA_ORB orb, CORBA_Environment *ev)
327{
328 PortableServer_POAManager mgr;
329 PortableServer_POA the_poa;
330
331 the_poa = (PortableServer_POA) CORBA_ORB_resolve_initial_references (
332 orb, "RootPOA", ev);
333
334 mgr = PortableServer_POA__get_the_POAManager (the_poa, ev);
335 PortableServer_POAManager_activate (mgr, ev);
336 CORBA_Object_release ((CORBA_Object) mgr, ev);
337
338 return the_poa;
339}
340
341CORBA_Object
342create_object (PortableServer_POA poa,
343 gpointer servant,
344 CORBA_Environment *ev)
345{
346 CORBA_Object object;
347 PortableServer_ObjectId *objid;
348
349 objid = PortableServer_POA_activate_object (
350 poa, servant, ev);
351
352 if (ev->_major != CORBA_NO_EXCEPTION)
353 g_error ("activate_object failed: %d\n", ev->_major);
354
355 object = PortableServer_POA_servant_to_reference (
356 poa, servant, ev);
357 if (ev->_major != CORBA_NO_EXCEPTION)
358 g_error ("servant_to_reference failed: %d\n", ev->_major);
359
360 g_assert (ORBit_small_get_servant (object) == servant);
361 g_assert (ORBIT_SERVANT_TO_CLASSINFO (servant) != NULL);
362
363 CORBA_free (objid);
364
365 return object;
366}
367
368/* constructor */
369static void
370test_TestFactory__init (PortableServer_Servant servant,
371 PortableServer_POA poa,
372 CORBA_Environment *ev)
373{
374 test_TestFactory_Servant *this = (test_TestFactory_Servant*)servant;
375 this->baseServant._private = NULL;
376 this->baseServant.vepv = &TestFactory_vepv;
377
378 this->basicServerRef = create_object (
379 poa, SIMPLE_SERVANT_NEW (BasicServer), ev);
380
381 this->structServerRef = create_object (
382 poa, SIMPLE_SERVANT_NEW (StructServer), ev);
383
384 this->sequenceServerRef = create_object (
385 poa, SIMPLE_SERVANT_NEW (SequenceServer), ev);
386
387 this->unionServerRef = create_object (
388 poa, SIMPLE_SERVANT_NEW (UnionServer), ev);
389
390 this->arrayServerRef = create_object (
391 poa, SIMPLE_SERVANT_NEW (ArrayServer), ev);
392
393 this->anyServerRef = create_object (
394 poa, SIMPLE_SERVANT_NEW (AnyServer), ev);
395
396 this->contextServerRef = create_object (
397 poa, SIMPLE_SERVANT_NEW (ContextServer), ev);
398
399 this->derivedServerRef = create_object (
400 poa, SIMPLE_SERVANT_NEW (DerivedServer), ev);
401
402 this->pingPongServerRefs = NULL;
403
404 POA_test_TestFactory__init (
405 (PortableServer_ServantBase *) servant, ev);
406}
407
408static test_TestFactory factory;
409
410test_TestFactory
411getFactoryInstance (CORBA_Environment *ev)
412{
413 return CORBA_Object_duplicate (factory, ev);
414}
415
416#ifndef _IN_CLIENT_
417static int
418dump_ior (CORBA_ORB orb, const char *fname, CORBA_Environment *ev)
419{
420 FILE *outfile;
421 CORBA_char *ior;
422
423 outfile = fopen ("iorfile","wb");
424
425 g_return_val_if_fail (outfile != NULL, 1);
426 g_return_val_if_fail (factory != CORBA_OBJECT_NIL, 1);
427
428 ior = CORBA_ORB_object_to_string (orb, factory, ev);
429 g_return_val_if_fail (ior != NULL, 1);
430
431 fwrite (ior, strlen (ior), 1, outfile);
432
433 fclose (outfile);
434
435 CORBA_free (ior);
436
437 return 0;
438}
439#endif
440
441static CORBA_Object
442create_TestFactory (PortableServer_POA poa,
443 CORBA_Environment *ev)
444{
445 CORBA_Object object;
446 PortableServer_ObjectId *objid;
447 test_TestFactory_Servant *servant = g_new0 (test_TestFactory_Servant, 1);
448
449 g_assert (ev->_major == CORBA_NO_EXCEPTION);
450 test_TestFactory__init (servant, poa, ev);
451 if (ev->_major != CORBA_NO_EXCEPTION)
452 g_error ("object__init failed: %d\n", ev->_major);
453 g_assert (ORBIT_SERVANT_TO_CLASSINFO (servant) != NULL);
454
455 objid = PortableServer_POA_activate_object (
456 poa, servant, ev);
457 if (ev->_major != CORBA_NO_EXCEPTION)
458 g_error ("activate_object failed: %d\n", ev->_major);
459
460 object = PortableServer_POA_servant_to_reference (
461 poa, servant, ev);
462 if (ev->_major != CORBA_NO_EXCEPTION)
463 g_error ("servant_to_reference failed: %d\n", ev->_major);
464
465 g_assert (ORBit_small_get_servant (object) == servant);
466 g_assert (ORBIT_SERVANT_TO_CLASSINFO (servant) != NULL);
467
468 CORBA_free (objid);
469
470 return object;
471}
472
473static void
474init_iinterfaces (ORBit_IInterfaces *interfaces,
475 CORBA_Environment *ev)
476{
477 int i = 0;
478
479#define CLOBBER_SYM(a) G_STMT_START { \
480 g_assert (CORBA_TypeCode_equal ( \
481 interfaces->_buffer[i].tc, (a).tc, ev)); \
482 (a) = interfaces->_buffer [i]; \
483 i++; \
484 } G_STMT_END
485
486 /* This order matches that in the IDL file */
487 CLOBBER_SYM (test_TestFactory__iinterface);
488 CLOBBER_SYM (test_LifeCycleServer__iinterface);
489 CLOBBER_SYM (test_DeadReferenceObj__iinterface);
490 CLOBBER_SYM (test_TransientObj__iinterface);
491 CLOBBER_SYM (test_SequenceServer__iinterface);
492 CLOBBER_SYM (test_ArrayServer__iinterface);
493 CLOBBER_SYM (test_BasicServer__iinterface);
494 CLOBBER_SYM (test_StructServer__iinterface);
495 CLOBBER_SYM (test_BaseServer__iinterface);
496 CLOBBER_SYM (test_B1__iinterface);
497 CLOBBER_SYM (test_B2__iinterface);
498 CLOBBER_SYM (test_C1__iinterface);
499 CLOBBER_SYM (test_DerivedServer__iinterface);
500 CLOBBER_SYM (test_UnionServer__iinterface);
501 CLOBBER_SYM (test_AnyServer__iinterface);
502 CLOBBER_SYM (test_ContextServer__iinterface);
503 CLOBBER_SYM (test_PingPongServer__iinterface);
504#undef CLOBBER_SYM
505}
506
507#ifndef _IN_CLIENT_
508 int
509 main (int argc, char *argv [])
510#else
511 static CORBA_Object
512 get_server (CORBA_ORB orb,
513 CORBA_Environment *ev)
514#endif
515{
516 test_BasicServer objref;
517#ifndef _IN_CLIENT_
518 CORBA_Environment real_ev;
519 CORBA_Environment *ev = &real_ev;
520 ORBit_IInterfaces *interfaces = NULL;
521 gboolean gen_imodule = FALSE;
522 gboolean thread_safe = FALSE;
523 gboolean thread_tests = FALSE;
524 char *orb_name;
525 int i;
526
527/* g_mem_set_vtable (glib_mem_profiler_table); */
528
529 free (malloc (8)); /* -lefence */
530
531 CORBA_exception_init(&real_ev);
532
533 for (i = 0; i < argc; i++) {
534 if (!strcmp (argv [i], "--gen-imodule"))
535 gen_imodule = TRUE;
536 if (!strcmp (argv [i], "--thread-safe"))
537 thread_safe = TRUE;
538 if (!strcmp (argv [i], "--thread-tests")) {
539 thread_safe = TRUE;
540 thread_tests = TRUE;
541 }
542 }
543
544 if (thread_safe)
545 orb_name = "orbit-local-orb";
546 else
547 orb_name = "orbit-local-non-threaded-orb";
548
549 global_orb = CORBA_ORB_init (&argc, argv, orb_name, ev);
550 g_assert (ev->_major == CORBA_NO_EXCEPTION);
551
552 if (thread_tests)
553 link_set_io_thread (TRUE);
554
555 if (gen_imodule) {
556 interfaces = ORBit_iinterfaces_from_file (TEST_SRCDIR "/everything.idl", NULL, NULL);
557 g_assert (interfaces != NULL);
558
559 init_iinterfaces (interfaces, ev);
560 }
561#endif
562
563 global_poa = start_poa (global_orb, ev);
564 g_assert (ev->_major == CORBA_NO_EXCEPTION);
565
566 factory = create_TestFactory (global_poa, ev);
567 g_assert (factory != CORBA_OBJECT_NIL);
568
569 /* a quick local test */
570 objref = test_TestFactory_getBasicServer (factory, ev);
571 g_assert (ev->_major == CORBA_NO_EXCEPTION);
572 g_assert (objref != CORBA_OBJECT_NIL);
573 g_assert (CORBA_Object_is_a (objref, "IDL:orbit/test/BasicServer:1.0", ev));
574 g_assert (ev->_major == CORBA_NO_EXCEPTION);
575 CORBA_Object_release (objref, ev);
576 g_assert(ev->_major == CORBA_NO_EXCEPTION);
577 fprintf (stderr, "Local server test passed\n");
578
579#ifndef _IN_CLIENT_
580 if (!dump_ior (global_orb, "iorfile", ev))
581 CORBA_ORB_run (global_orb, ev);
582
583 CORBA_Object_release ((CORBA_Object) global_poa, ev);
584 g_assert (ev->_major == CORBA_NO_EXCEPTION);
585
586 CORBA_Object_release (factory, ev);
587 g_assert (ev->_major == CORBA_NO_EXCEPTION);
588
589 if (gen_imodule)
590 CORBA_free (interfaces);
591
592 CORBA_ORB_destroy (global_orb, ev);
593 g_assert (ev->_major == CORBA_NO_EXCEPTION);
594
595 CORBA_Object_release ((CORBA_Object) global_orb, ev);
596 g_assert (ev->_major == CORBA_NO_EXCEPTION);
597
598 CORBA_exception_free (ev);
599
600 return 0;
601#else
602 return factory;
603#endif
604}
Note: See TracBrowser for help on using the repository browser.