More tests update
[ACE_TAO.git] / TAO / tests / POA / On_Demand_Activation / server.cpp
blob252da700f7bf6b807743c8372e3f184a500b320f
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * Server to test the Servant Activator and Servant Locator for a POA.
8 * @author Irfan Pyarali <irfan@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "ace/streams.h"
14 #include "ace/Get_Opt.h"
15 #include "ace/SString.h"
16 #include "Servant_Activator.h"
17 #include "Servant_Locator.h"
18 #include "ace/OS_NS_stdio.h"
20 static const ACE_TCHAR *ior_output_file = ACE_TEXT("ior");
22 static int
23 parse_args (int argc, ACE_TCHAR **argv)
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'f':
32 ior_output_file = get_opts.opt_arg ();
33 break;
35 case '?':
36 default:
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "usage: %s "
39 "[-f ior_output_file] "
40 "\n",
41 argv [0]),
42 -1);
45 // Indicates successful parsing of command line.
46 return 0;
49 static int
50 write_iors_to_file (const char *first_ior,
51 const char *second_ior)
53 char ior_output_file_1[BUFSIZ];
54 char ior_output_file_2[BUFSIZ];
56 ACE_OS::sprintf (ior_output_file_1, "%s_1", ACE_TEXT_ALWAYS_CHAR(ior_output_file));
57 ACE_OS::sprintf (ior_output_file_2, "%s_2", ACE_TEXT_ALWAYS_CHAR(ior_output_file));
59 FILE *output_file_1 = ACE_OS::fopen (ior_output_file_1, "w");
60 FILE *output_file_2 = ACE_OS::fopen (ior_output_file_2, "w");
62 if (output_file_1 == 0 ||
63 output_file_2 == 0)
64 ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output files for writing IORs: %C, %C\n",
65 ior_output_file_1,
66 ior_output_file_2),
67 -1);
69 int result = ACE_OS::fprintf (output_file_1,
70 "%s",
71 first_ior);
72 if (result <= 0
73 || static_cast<size_t> (result) != ACE_OS::strlen (first_ior))
74 ACE_ERROR_RETURN ((LM_ERROR,
75 "ACE_OS::fprintf failed while writing %C to %s\n",
76 first_ior,
77 ior_output_file_1),
78 -1);
80 result = ACE_OS::fprintf (output_file_2,
81 "%s",
82 second_ior);
83 if (result <= 0
84 || static_cast<size_t> (result) != ACE_OS::strlen (second_ior))
85 ACE_ERROR_RETURN ((LM_ERROR,
86 "ACE_OS::fprintf failed while writing %C to %s\n",
87 second_ior,
88 ior_output_file_2),
89 -1);
91 ACE_OS::fclose (output_file_1);
92 ACE_OS::fclose (output_file_2);
94 return 0;
97 int
98 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
103 // Initialize the ORB.
104 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
106 int result = parse_args (argc, argv);
107 if (result != 0)
108 return result;
110 // Obtain the RootPOA.
111 CORBA::Object_var obj =
112 orb->resolve_initial_references ("RootPOA");
114 // Narrow the Object reference to a POA reference
115 PortableServer::POA_var root_poa =
116 PortableServer::POA::_narrow (obj.in ());
118 // Get the POAManager of RootPOA
120 PortableServer::POAManager_var poa_manager =
121 root_poa->the_POAManager ();
124 CORBA::PolicyList policies (4);
125 policies.length (4);
127 // ID Assignment Policy
128 policies[0] =
129 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
131 // Lifespan Policy
132 policies[1] =
133 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
135 // Request Processing Policy
136 policies[2] =
137 root_poa->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
139 PortableServer::POA_var first_poa;
141 // Servant Retention Policy
142 policies[3] =
143 root_poa->create_servant_retention_policy (PortableServer::RETAIN);
145 ACE_CString name = "firstPOA";
147 // Create firstPOA as the child of RootPOA with the above policies
148 // firstPOA will use SERVANT_ACTIVATOR because of RETAIN policy.
149 first_poa = root_poa->create_POA (name.c_str (),
150 poa_manager.in (),
151 policies);
155 PortableServer::POA_var second_poa;
157 // Servant Retention Policy
158 policies[3] =
159 root_poa->create_servant_retention_policy (PortableServer::NON_RETAIN);
161 ACE_CString name = "secondPOA";
163 // Create secondPOA as child of RootPOA with the above policies
164 // secondPOA will use a SERVANT_LOCATOR because of NON_RETAIN
165 // policy.
166 second_poa = root_poa->create_POA (name.c_str (),
167 poa_manager.in (),
168 policies);
172 // Destroy the policy objects as they have been passed to
173 // create_POA and no longer needed.
174 for (CORBA::ULong i = 0;
175 i < policies.length ();
176 ++i)
178 CORBA::Policy_ptr policy = policies[i];
179 policy->destroy ();
182 // Allocate the servant activator.
183 ServantActivator activator (orb.in ());
185 // Set ServantActivator object as the servant_manager of
186 // firstPOA.
187 first_poa->set_servant_manager (&activator);
188 // For the code above, we're using the CORBA 3.0 servant manager
189 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
190 // use the following code in place of the previous line:
192 // PortableServer::ServantManager_var servant_activator =
193 // activator->_this ();
195 // first_poa->set_servant_manager (servant_activator.in (),
196 //);
198 // Create a reference with user created ID in firstPOA which uses
199 // the ServantActivator.
201 PortableServer::ObjectId_var first_test_oid =
202 PortableServer::string_to_ObjectId ("first test");
204 CORBA::Object_var first_test =
205 first_poa->create_reference_with_id (first_test_oid.in (), "IDL:test:1.0");
207 // Allocate the servant locator.
208 ServantLocator locator (orb.in ());
210 // Set ServantLocator object as the servant Manager of
211 // secondPOA.
212 second_poa->set_servant_manager (&locator);
213 // For the code above, we're using the CORBA 3.0 servant manager
214 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
215 // use the following code in place of the previous line:
217 // PortableServer::ServantManager_var servant_locator =
218 // locator->_this ();
220 // second_poa->set_servant_manager (servant_locator.in (),
221 //);
223 // Try to create a reference with user created ID in second_poa
224 // which uses ServantLocator.
226 PortableServer::ObjectId_var second_test_oid =
227 PortableServer::string_to_ObjectId ("second test");
229 CORBA::Object_var second_test =
230 second_poa->create_reference_with_id (second_test_oid.in (),
231 "IDL:test:1.0");
233 // Invoke object_to_string on the references created in firstPOA and
234 // secondPOA.
236 CORBA::String_var first_test_ior =
237 orb->object_to_string (first_test.in ());
240 CORBA::String_var second_test_ior =
241 orb->object_to_string (second_test.in ());
243 // Print the ior's of first_test and second_test.
245 ACE_DEBUG((LM_DEBUG,"Cs\n%C\n",
246 first_test_ior.in (),
247 second_test_ior.in ()));
249 int write_result = write_iors_to_file (first_test_ior.in (),
250 second_test_ior.in ());
251 if (write_result != 0)
252 return write_result;
254 // Set the poa_manager state to active, ready to process requests.
255 poa_manager->activate ();
257 // Run the ORB.
258 orb->run ();
260 orb->destroy ();
262 catch (const CORBA::Exception& ex)
264 ex._tao_print_exception ("Exception caught in main");
265 return -1;
268 return 0;