Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Bug_2654_Regression / server.cpp
blobb5c839084c8549b6e96df0949b9985ad7618350a
1 #include "Hello.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/Thread_Manager.h"
5 #include "tao/BiDir_GIOP/BiDirGIOP.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
13 int c;
15 while ((c = get_opts ()) != -1)
16 switch (c)
18 case 'o':
19 ior_output_file = get_opts.opt_arg ();
20 break;
22 case '?':
23 default:
24 ACE_ERROR_RETURN ((LM_ERROR,
25 "usage: %s "
26 "-o <iorfile>"
27 "\n",
28 argv [0]),
29 -1);
31 // Indicates successful parsing of the command line
32 return 0;
35 int
36 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
38 try
40 CORBA::ORB_var orb =
41 CORBA::ORB_init (argc, argv);
43 CORBA::Object_var obj =
44 orb->resolve_initial_references("RootPOA");
46 PortableServer::POA_var root_poa =
47 PortableServer::POA::_narrow (obj.in ());
49 if (CORBA::is_nil (root_poa.in ()))
50 ACE_ERROR_RETURN ((LM_ERROR,
51 " (%P|%t) Panic: nil RootPOA\n"),
52 1);
54 PortableServer::POAManager_var poa_manager =
55 root_poa->the_POAManager ();
57 // Policies for the childPOA to be created.
58 CORBA::PolicyList policies (1);
59 policies.length (1);
61 CORBA::Any pol;
62 pol <<= BiDirPolicy::BOTH;
63 policies[0] =
64 orb->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
65 pol);
67 // Create POA as child of RootPOA with the above policies. This POA
68 // will receive request in the same connection in which it sent
69 // the request
70 PortableServer::POA_var child_poa =
71 root_poa->create_POA ("childPOA",
72 poa_manager.in (),
73 policies);
75 // Creation of childPOA is over. Destroy the Policy objects.
76 for (CORBA::ULong i = 0;
77 i < policies.length ();
78 ++i)
80 policies[i]->destroy ();
83 if (parse_args (argc, argv) != 0)
84 return 1;
86 Hello *hello_impl;
87 ACE_NEW_RETURN (hello_impl,
88 Hello (orb.in ()),
89 1);
90 PortableServer::ServantBase_var owner(hello_impl);
92 PortableServer::ObjectId_var id =
93 child_poa->activate_object(hello_impl);
95 obj = child_poa->id_to_reference (id.in());
97 Test::Hello_var hello =
98 Test::Hello::_narrow (obj.in());
100 CORBA::String_var ior =
101 orb->object_to_string (hello.in ());
103 // Output the IOR to the <ior_output_file>
104 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
105 if (output_file == 0)
106 ACE_ERROR_RETURN ((LM_ERROR,
107 "Cannot open output file for writing IOR: %s\n",
108 ior_output_file),
110 ACE_OS::fprintf (output_file, "%s", ior.in ());
111 ACE_OS::fclose (output_file);
113 poa_manager->activate ();
115 orb->run ();
117 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
119 // Wait for threads that may have been started by the Hello
120 // implementation.
121 ACE_Thread_Manager::instance()->wait();
123 root_poa->destroy (true, true);
125 orb->destroy ();
127 catch (const CORBA::Exception& ex)
129 ex._tao_print_exception ("Exception caught:");
130 return 1;
133 return 0;