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
[])
102 // Initialize the ORB.
103 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
105 int result
= parse_args (argc
, argv
);
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);
126 // ID Assignment Policy
128 root_poa
->create_id_assignment_policy (PortableServer::USER_ID
);
132 root_poa
->create_lifespan_policy (PortableServer::PERSISTENT
);
134 // Request Processing Policy
136 root_poa
->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER
);
138 PortableServer::POA_var first_poa
;
140 // Servant Retention Policy
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 (),
153 PortableServer::POA_var second_poa
;
155 // Servant Retention Policy
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
164 second_poa
= root_poa
->create_POA (name
.c_str (),
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 ();
175 CORBA::Policy_ptr policy
= policies
[i
];
179 // Allocate the servant activator.
180 ServantActivator
activator (orb
.in ());
182 // Set ServantActivator object as the servant_manager of
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 (),
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
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 (),
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 (),
230 // Invoke object_to_string on the references created in firstPOA and
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)
251 // Set the poa_manager state to active, ready to process requests.
252 poa_manager
->activate ();
259 catch (const CORBA::Exception
& ex
)
261 ex
._tao_print_exception ("Exception caught in main");