2 //=============================================================================
6 * Explicit creation of servants.
8 * @author Irfan Pyarali
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/SString.h"
16 #include "ace/OS_NS_stdio.h"
18 const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("ior");
21 parse_args (int argc
, ACE_TCHAR
**argv
)
23 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:"));
26 while ((c
= get_opts ()) != -1)
30 ior_output_file
= get_opts
.opt_arg ();
35 ACE_ERROR_RETURN ((LM_ERROR
,
43 // Indicates successful parsing of command line.
48 write_iors_to_file (const char *first_ior
,
49 const char *second_ior
,
50 const char *third_ior
)
52 ACE_TCHAR ior_output_file_1
[BUFSIZ
];
53 ACE_TCHAR ior_output_file_2
[BUFSIZ
];
54 ACE_TCHAR ior_output_file_3
[BUFSIZ
];
56 ACE_OS::sprintf (ior_output_file_1
, ACE_TEXT("%s_1"), ior_output_file
);
57 ACE_OS::sprintf (ior_output_file_2
, ACE_TEXT("%s_2"), ior_output_file
);
58 ACE_OS::sprintf (ior_output_file_3
, ACE_TEXT("%s_3"), ior_output_file
);
60 FILE *output_file_1
= ACE_OS::fopen (ior_output_file_1
, "w");
61 FILE *output_file_2
= ACE_OS::fopen (ior_output_file_2
, "w");
62 FILE *output_file_3
= ACE_OS::fopen (ior_output_file_3
, "w");
64 if (output_file_1
== 0 ||
67 ACE_ERROR_RETURN ((LM_ERROR
, "Cannot open output files for writing IORs: %s, %s %s\n",
75 result
= ACE_OS::fprintf (output_file_1
,
79 ACE_OS::fclose (output_file_1
);
81 if (result
!= ACE_OS::strlen (first_ior
))
82 ACE_ERROR_RETURN ((LM_ERROR
,
83 "ACE_OS::fprintf failed while writing %C to %s\n",
88 result
= ACE_OS::fprintf (output_file_2
,
92 ACE_OS::fclose (output_file_2
);
94 if (result
!= ACE_OS::strlen (second_ior
))
95 ACE_ERROR_RETURN ((LM_ERROR
,
96 "ACE_OS::fprintf failed while writing %C to %s\n",
101 result
= ACE_OS::fprintf (output_file_3
,
105 ACE_OS::fclose (output_file_3
);
107 if (result
!= ACE_OS::strlen (third_ior
))
108 ACE_ERROR_RETURN ((LM_ERROR
,
109 "ACE_OS::fprintf failed while writing %C to %s\n",
119 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
123 // Initialize the ORB first.
125 CORBA::ORB_init (argc
, argv
);
127 int result
= parse_args (argc
, argv
);
131 // Obtain the RootPOA.
132 CORBA::Object_var obj
=
133 orb
->resolve_initial_references ("RootPOA");
135 // Get the POA_var object from Object_var.
136 PortableServer::POA_var root_poa
=
137 PortableServer::POA::_narrow (obj
.in ());
139 // Get the POAManager of the RootPOA.
140 PortableServer::POAManager_var poa_manager
=
141 root_poa
->the_POAManager ();
143 // Policies for the firstPOA to be created.
144 CORBA::PolicyList
policies (3);
147 // Id Assignment Policy
149 root_poa
->create_id_assignment_policy (PortableServer::USER_ID
);
153 root_poa
->create_lifespan_policy (PortableServer::PERSISTENT
);
157 root_poa
->create_thread_policy (PortableServer::ORB_CTRL_MODEL
);
159 // Create the firstPOA under the RootPOA.
160 ACE_CString name
= "firstPOA";
161 PortableServer::POA_var first_poa
=
162 root_poa
->create_POA (name
.c_str (),
166 policies
[2]->destroy ();
170 root_poa
->create_thread_policy (PortableServer::SINGLE_THREAD_MODEL
);
172 // Create the secondPOA under the firstPOA.
174 PortableServer::POA_var second_poa
=
175 first_poa
->create_POA (name
.c_str (),
179 // Creation of POAs is over. Destroy the Policy objects.
180 for (CORBA::ULong i
= 0;
181 i
< policies
.length ();
184 policies
[i
]->destroy ();
187 // Create two Objects of Class test_i (defined in
188 // ./../GenericServant/test_i.h) Create one object at RootPOA
189 // and the other at firstPOA.
190 test_i
first_servant (orb
.in (),
192 test_i
second_servant (orb
.in (),
195 // Do "activate_object" to activate the first_servant object. It
196 // returns ObjectId for that object. Operation Used :
197 // ObjectId activate_object(in Servant p_servant)
198 // raises (ServantAlreadyActive, WrongPolicy);
199 PortableServer::ObjectId_var first_oid
=
200 root_poa
->activate_object (&first_servant
);
202 // Get Object Reference for the first_servant object.
203 test_var first_test
=
204 first_servant
._this ();
206 // Get ObjectId for object secondtest and use that ObjectId to
207 // activate the second_servant object.
209 // void activate_object_with_id(in ObjectId oid, in Servant p_servant)
210 // raises (ObjectAlreadyActive, ServantAlreadyActive, WrongPolicy);
211 PortableServer::ObjectId_var second_oid
=
212 PortableServer::string_to_ObjectId ("second test");
214 first_poa
->activate_object_with_id (second_oid
.in (),
217 // Get Object reference for second_servant object.
218 test_var second_test
=
219 second_servant
._this ();
221 // Get ObjectId for the string thirdPOA Create the object reference
222 // for thirdPOA using that ObjectId. Operation Used :
223 // Object create_reference_with_id (in ObjectId oid, in CORBA::RepositoryId intf );
224 // This operation creates an object reference that encapsulates the
225 // specified Object Id and interface repository Id values.
227 PortableServer::ObjectId_var third_oid =
228 PortableServer::string_to_ObjectId ("thirdtest");
231 // This will test how the POA handles a user given ID
232 PortableServer::ObjectId_var third_oid
=
233 PortableServer::string_to_ObjectId ("third test");
235 third_oid
[5] = (CORBA::Octet
) '\0';
237 CORBA::Object_var third_test
=
238 second_poa
->create_reference_with_id (third_oid
.in (),
241 // Stringyfy all the object references and print them out.
242 CORBA::String_var first_ior
=
243 orb
->object_to_string (first_test
.in ());
245 CORBA::String_var second_ior
=
246 orb
->object_to_string (second_test
.in ());
248 CORBA::String_var third_ior
=
249 orb
->object_to_string (third_test
.in ());
251 ACE_DEBUG ((LM_DEBUG
,
258 write_iors_to_file (first_ior
.in (),
261 if (write_result
!= 0)
264 // Activate third servant using its ObjectID.
265 test_i
third_servant (orb
.in (),
267 second_poa
->activate_object_with_id (third_oid
.in (),
270 poa_manager
->activate ();
276 catch (const CORBA::Exception
& ex
)
278 ex
._tao_print_exception ("Exception caught");