Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / Portable_Interceptors / ForwardRequest / server.cpp
blob10edc0ff78b39c3c0a2f099613ae11565885d6c6
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
4 #include "test_i.h"
5 #include "Server_ORBInitializer.h"
6 #include "Server_Request_Interceptor.h"
8 #include "tao/ORBInitializer_Registry.h"
9 #include "tao/AnyTypeCode/AnyTypeCode_Adapter_Impl.h"
10 #include "ace/OS_NS_stdio.h"
12 const ACE_TCHAR *ior1_file = 0;
13 const ACE_TCHAR *ior2_file = 0;
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 if (argc != 5) // foo -o IOR_1 -o IOR_2
19 ACE_ERROR_RETURN ((LM_ERROR,
20 "Wrong number of arguments.\n"),
21 -1);
23 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
24 int c;
26 int ior_count = 1;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'o':
33 if (ior_count == 1)
35 ior1_file = get_opts.opt_arg ();
36 ++ior_count;
38 else if (ior_count == 2)
40 ior2_file = get_opts.opt_arg ();
41 ++ior_count;
44 break;
45 default:
46 ACE_ERROR_RETURN ((LM_ERROR,
47 "Usage: %s "
48 "-o IOR_1 -o IOR_2\n",
49 argv[0]),
50 -1);
53 return 0;
56 int
57 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
59 try
61 #if TAO_HAS_INTERCEPTORS == 1
62 Server_ORBInitializer *temp_initializer = 0;
63 ACE_NEW_RETURN (temp_initializer,
64 Server_ORBInitializer,
65 -1); // No exceptions yet!
66 PortableInterceptor::ORBInitializer_var orb_initializer =
67 temp_initializer;
69 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
70 #endif /* TAO_HAS_INTERCEPTORS == 1 */
72 CORBA::ORB_var orb =
73 CORBA::ORB_init (argc, argv, "Server ORB");
75 CORBA::Object_var poa_object =
76 orb->resolve_initial_references ("RootPOA");
78 if (CORBA::is_nil (poa_object.in ()))
79 ACE_ERROR_RETURN ((LM_ERROR,
80 " (%P|%t) Unable to initialize the POA.\n"),
81 1);
83 PortableServer::POA_var root_poa =
84 PortableServer::POA::_narrow (poa_object.in ());
86 PortableServer::POAManager_var poa_manager =
87 root_poa->the_POAManager ();
89 if (::parse_args (argc, argv) != 0)
90 return -1;
92 CORBA::PolicyList policies; // Empty policy list.
94 // Servant 1
95 test_i servant1 (1, orb.in ());
97 PortableServer::POA_var first_poa =
98 root_poa->create_POA ("first POA",
99 poa_manager.in (),
100 policies);
102 PortableServer::ObjectId_var oid1 =
103 first_poa->activate_object (&servant1);
105 CORBA::Object_var obj1 =
106 first_poa->servant_to_reference (&servant1);
108 CORBA::String_var ior1 =
109 orb->object_to_string (obj1.in ());
111 ACE_DEBUG ((LM_DEBUG,
112 "ForwardRequestTest::test servant 1: <%C>\n",
113 ior1.in ()));
115 // Servant 2
116 test_i servant2 (2, orb.in ());
118 PortableServer::POA_var second_poa =
119 root_poa->create_POA ("second POA",
120 poa_manager.in (),
121 policies);
123 PortableServer::ObjectId_var oid2 =
124 second_poa->activate_object (&servant2);
126 CORBA::Object_var obj2 =
127 second_poa->servant_to_reference (&servant2);
129 CORBA::String_var ior2 =
130 orb->object_to_string (obj2.in ());
132 ACE_DEBUG ((LM_DEBUG,
133 "ForwardRequestTest::test servant 2: <%C>\n",
134 ior2.in ()));
136 poa_manager->activate ();
138 #if TAO_HAS_INTERCEPTORS == 1
139 // Set the forward references in the server request interceptor.
140 PortableInterceptor::ServerRequestInterceptor_var
141 server_interceptor = temp_initializer->server_interceptor ();
143 ForwardRequestTest::ServerRequestInterceptor_var interceptor =
144 ForwardRequestTest::ServerRequestInterceptor::_narrow (
145 server_interceptor.in ());
147 if (CORBA::is_nil (interceptor.in ()))
148 ACE_ERROR_RETURN ((LM_ERROR,
149 "(%P|%t) Could not obtain reference to "
150 "server request interceptor.\n"),
151 -1);
153 interceptor->forward_references (obj1.in (),
154 obj2.in ());
155 #endif /* TAO_HAS_INTERCEPTORS == 1 */
157 // Write each IOR to a file.
159 // IOR 1
160 FILE *output_file= ACE_OS::fopen (ior1_file, "w");
161 if (output_file == 0)
162 ACE_ERROR_RETURN ((LM_ERROR,
163 "Cannot open output file <%s> for writing "
164 "IOR: %s",
165 ior1.in ()),
167 ACE_OS::fprintf (output_file, "%s", ior1.in ());
168 ACE_OS::fclose (output_file);
170 // IOR 2
171 output_file= ACE_OS::fopen (ior2_file, "w");
172 if (output_file == 0)
173 ACE_ERROR_RETURN ((LM_ERROR,
174 "Cannot open output file for writing IOR: %s",
175 ior2.in ()),
177 ACE_OS::fprintf (output_file, "%s", ior2.in ());
178 ACE_OS::fclose (output_file);
180 // Run the ORB event loop.
181 orb->run ();
183 root_poa->destroy (true, true);
185 orb->destroy ();
187 ACE_DEBUG ((LM_DEBUG, "Event loop finished.\n"));
189 catch (const CORBA::Exception& ex)
191 ex._tao_print_exception ("Caught exception:");
192 return -1;
195 return 0;