Use =default for skeleton copy constructor
[ACE_TAO.git] / TAO / tests / POA / On_Demand_Activation / server.cpp
blob0d892a9bcc68ad843fd4ca6225e34420515a4450
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[])
102 // Initialize the ORB.
103 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
105 int result = parse_args (argc, argv);
106 if (result != 0)
107 return result;
109 // Obtain the RootPOA.
110 CORBA::Object_var obj =
111 orb->resolve_initial_references ("RootPOA");
113 // Narrow the Object reference to a POA reference
114 PortableServer::POA_var root_poa =
115 PortableServer::POA::_narrow (obj.in ());
117 // Get the POAManager of RootPOA
119 PortableServer::POAManager_var poa_manager =
120 root_poa->the_POAManager ();
123 CORBA::PolicyList policies (4);
124 policies.length (4);
126 // ID Assignment Policy
127 policies[0] =
128 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
130 // Lifespan Policy
131 policies[1] =
132 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
134 // Request Processing Policy
135 policies[2] =
136 root_poa->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
138 PortableServer::POA_var first_poa;
140 // Servant Retention Policy
141 policies[3] =
142 root_poa->create_servant_retention_policy (PortableServer::RETAIN);
144 ACE_CString name = "firstPOA";
146 // Create firstPOA as the child of RootPOA with the above policies
147 // firstPOA will use SERVANT_ACTIVATOR because of RETAIN policy.
148 first_poa = root_poa->create_POA (name.c_str (),
149 poa_manager.in (),
150 policies);
153 PortableServer::POA_var second_poa;
155 // Servant Retention Policy
156 policies[3] =
157 root_poa->create_servant_retention_policy (PortableServer::NON_RETAIN);
159 ACE_CString name = "secondPOA";
161 // Create secondPOA as child of RootPOA with the above policies
162 // secondPOA will use a SERVANT_LOCATOR because of NON_RETAIN
163 // policy.
164 second_poa = root_poa->create_POA (name.c_str (),
165 poa_manager.in (),
166 policies);
169 // Destroy the policy objects as they have been passed to
170 // create_POA and no longer needed.
171 for (CORBA::ULong i = 0;
172 i < policies.length ();
173 ++i)
175 CORBA::Policy_ptr policy = policies[i];
176 policy->destroy ();
179 // Allocate the servant activator.
180 ServantActivator activator (orb.in ());
182 // Set ServantActivator object as the servant_manager of
183 // firstPOA.
184 first_poa->set_servant_manager (&activator);
185 // For the code above, we're using the CORBA 3.0 servant manager
186 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
187 // use the following code in place of the previous line:
189 // PortableServer::ServantManager_var servant_activator =
190 // activator->_this ();
192 // first_poa->set_servant_manager (servant_activator.in (),
193 //);
195 // Create a reference with user created ID in firstPOA which uses
196 // the ServantActivator.
198 PortableServer::ObjectId_var first_test_oid =
199 PortableServer::string_to_ObjectId ("first test");
201 CORBA::Object_var first_test =
202 first_poa->create_reference_with_id (first_test_oid.in (), "IDL:test:1.0");
204 // Allocate the servant locator.
205 ServantLocator locator (orb.in ());
207 // Set ServantLocator object as the servant Manager of
208 // secondPOA.
209 second_poa->set_servant_manager (&locator);
210 // For the code above, we're using the CORBA 3.0 servant manager
211 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
212 // use the following code in place of the previous line:
214 // PortableServer::ServantManager_var servant_locator =
215 // locator->_this ();
217 // second_poa->set_servant_manager (servant_locator.in (),
218 //);
220 // Try to create a reference with user created ID in second_poa
221 // which uses ServantLocator.
223 PortableServer::ObjectId_var second_test_oid =
224 PortableServer::string_to_ObjectId ("second test");
226 CORBA::Object_var second_test =
227 second_poa->create_reference_with_id (second_test_oid.in (),
228 "IDL:test:1.0");
230 // Invoke object_to_string on the references created in firstPOA and
231 // secondPOA.
233 CORBA::String_var first_test_ior =
234 orb->object_to_string (first_test.in ());
237 CORBA::String_var second_test_ior =
238 orb->object_to_string (second_test.in ());
240 // Print the ior's of first_test and second_test.
242 ACE_DEBUG((LM_DEBUG,"Cs\n%C\n",
243 first_test_ior.in (),
244 second_test_ior.in ()));
246 int write_result = write_iors_to_file (first_test_ior.in (),
247 second_test_ior.in ());
248 if (write_result != 0)
249 return write_result;
251 // Set the poa_manager state to active, ready to process requests.
252 poa_manager->activate ();
254 // Run the ORB.
255 orb->run ();
257 orb->destroy ();
259 catch (const CORBA::Exception& ex)
261 ex._tao_print_exception ("Exception caught in main");
262 return -1;
265 return 0;