Merge pull request #2316 from jwillemsen/jwi-taskcommenttypo
[ACE_TAO.git] / TAO / orbsvcs / tests / Security / BiDirectional / server.cpp
blob66d184ba1be5478111488d8057b9c1e9b95e8cd2
1 #include "ace/Get_Opt.h"
2 #include "ace/OS_NS_stdio.h"
3 #include "test_i.h"
4 #include "tao/BiDir_GIOP/BiDirGIOP.h"
5 #include "tao/AnyTypeCode/Any.h"
8 const ACE_TCHAR *ior_output_file = 0;
9 int no_iterations = 10;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:i:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
23 case 'i':
24 no_iterations = ACE_OS::atoi (get_opts.opt_arg ());
25 break;
26 case '?':
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "usage: %s "
30 "-o <iorfile>"
31 "-i <no_iterations>"
32 "\n",
33 argv [0]),
34 -1);
36 // Indicates successful parsing of the command line
37 return 0;
40 int
41 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
43 try
45 CORBA::ORB_var orb =
46 CORBA::ORB_init (argc, argv);
48 CORBA::Object_var poa_object =
49 orb->resolve_initial_references ("RootPOA");
51 if (CORBA::is_nil (poa_object.in ()))
52 ACE_ERROR_RETURN ((LM_ERROR,
53 " (%P|%t) Unable to initialize the POA.\n"),
54 1);
56 PortableServer::POA_var root_poa =
57 PortableServer::POA::_narrow (poa_object.in ());
59 PortableServer::POAManager_var poa_manager =
60 root_poa->the_POAManager ();
62 // Policies for the childPOA to be created.
63 CORBA::PolicyList policies (1);
64 policies.length (1);
66 CORBA::Any pol;
67 pol <<= BiDirPolicy::BOTH;
68 policies[0] =
69 orb->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
70 pol);
72 // Create POA as child of RootPOA with the above policies. This POA
73 // will receive request in the same connection in which it sent
74 // the request
75 PortableServer::POA_var child_poa =
76 root_poa->create_POA ("childPOA",
77 poa_manager.in (),
78 policies);
80 // Creation of childPOA is over. Destroy the Policy objects.
81 for (CORBA::ULong i = 0;
82 i < policies.length ();
83 ++i)
85 policies[i]->destroy ();
88 poa_manager->activate ();
90 if (parse_args (argc, argv) != 0)
91 return 1;
93 Simple_Server_i server_impl (orb.in (),
94 no_iterations);
96 PortableServer::ObjectId_var id =
97 PortableServer::string_to_ObjectId ("simple_server");
99 child_poa->activate_object_with_id (id.in (),
100 &server_impl);
102 CORBA::Object_var obj =
103 child_poa->id_to_reference (id.in ());
105 CORBA::String_var ior =
106 orb->object_to_string (obj.in ());
108 ACE_DEBUG ((LM_DEBUG,
109 ACE_TEXT ("(%P|%t) Server activated as <%C>\n"), ior.in ()));
111 // If the ior_output_file exists, output the ior to it
112 if (ior_output_file != 0)
114 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
115 if (output_file == 0)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 ACE_TEXT ("Cannot open output file for writing IOR: %s"),
118 ior_output_file),
120 ACE_OS::fprintf (output_file, "%s", ior.in ());
121 ACE_OS::fclose (output_file);
124 int retval = 0;
125 while (retval == 0)
127 // Just process one upcall. We know that we would get the
128 // clients IOR in that call.
129 CORBA::Boolean pending =
130 orb->work_pending();
132 if (pending)
134 orb->perform_work();
137 // Now that hopefully we have the clients IOR, just start
138 // making remote calls to the client.
139 retval = server_impl.call_client ();
141 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("event loop finished\n")));
143 root_poa->destroy (true, true);
145 orb->destroy ();
147 catch (const CORBA::Exception& ex)
149 ex._tao_print_exception (ACE_TEXT ("Caught exception:"));
150 return 1;
153 return 0;