2 //=============================================================================
6 * Server to test the Servant Activator and Servant Locator for a POA.
8 * @author Irfan Pyarali <irfan@cs.wustl.edu>
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");
23 parse_args (int argc
, ACE_TCHAR
**argv
)
25 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:"));
28 while ((c
= get_opts ()) != -1)
32 ior_output_file
= get_opts
.opt_arg ();
37 ACE_ERROR_RETURN ((LM_ERROR
,
39 "[-f ior_output_file] "
45 // Indicates successful parsing of command line.
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 ||
64 ACE_ERROR_RETURN ((LM_ERROR
, "Cannot open output files for writing IORs: %C, %C\n",
69 int result
= ACE_OS::fprintf (output_file_1
,
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",
80 result
= ACE_OS::fprintf (output_file_2
,
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",
91 ACE_OS::fclose (output_file_1
);
92 ACE_OS::fclose (output_file_2
);
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
);
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);
127 // ID Assignment Policy
129 root_poa
->create_id_assignment_policy (PortableServer::USER_ID
);
133 root_poa
->create_lifespan_policy (PortableServer::PERSISTENT
);
135 // Request Processing Policy
137 root_poa
->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER
);
139 PortableServer::POA_var first_poa
;
141 // Servant Retention Policy
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 (),
155 PortableServer::POA_var second_poa
;
157 // Servant Retention Policy
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
166 second_poa
= root_poa
->create_POA (name
.c_str (),
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 ();
178 CORBA::Policy_ptr policy
= policies
[i
];
182 // Allocate the servant activator.
183 ServantActivator
activator (orb
.in ());
185 // Set ServantActivator object as the servant_manager of
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 (),
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
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 (),
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 (),
233 // Invoke object_to_string on the references created in firstPOA and
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)
254 // Set the poa_manager state to active, ready to process requests.
255 poa_manager
->activate ();
262 catch (const CORBA::Exception
& ex
)
264 ex
._tao_print_exception ("Exception caught in main");