Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Explicit_Activation / server.cpp
blobaa5ca495c5c52eabb8712b70e4a1e6db4128aeb9
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * Explicit creation of servants.
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/SString.h"
15 #include "test_i.h"
16 #include "ace/OS_NS_stdio.h"
18 const ACE_TCHAR *ior_output_file = ACE_TEXT ("ior");
20 static int
21 parse_args (int argc, ACE_TCHAR **argv)
23 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
24 int c;
26 while ((c = get_opts ()) != -1)
27 switch (c)
29 case 'f':
30 ior_output_file = get_opts.opt_arg ();
31 break;
33 case '?':
34 default:
35 ACE_ERROR_RETURN ((LM_ERROR,
36 "usage: %s "
37 "-f ior_file"
38 "\n",
39 argv [0]),
40 -1);
43 // Indicates successful parsing of command line.
44 return 0;
47 static int
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 ||
65 output_file_2 == 0 ||
66 output_file_3 == 0)
67 ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output files for writing IORs: %s, %s %s\n",
68 ior_output_file_1,
69 ior_output_file_2,
70 ior_output_file_3),
71 -1);
73 u_int result = 0;
75 result = ACE_OS::fprintf (output_file_1,
76 "%s",
77 first_ior);
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",
84 first_ior,
85 ior_output_file_1),
86 -1);
88 result = ACE_OS::fprintf (output_file_2,
89 "%s",
90 second_ior);
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",
97 second_ior,
98 ior_output_file_2),
99 -1);
101 result = ACE_OS::fprintf (output_file_3,
102 "%s",
103 third_ior);
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",
110 third_ior,
111 ior_output_file_3),
112 -1);
115 return 0;
119 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
123 // Initialize the ORB first.
124 CORBA::ORB_var orb =
125 CORBA::ORB_init (argc, argv);
127 int result = parse_args (argc, argv);
128 if (result != 0)
129 return result;
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);
145 policies.length (3);
147 // Id Assignment Policy
148 policies[0] =
149 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
151 // Lifespan policy
152 policies[1] =
153 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
155 // Threading policy
156 policies[2] =
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 (),
163 poa_manager.in (),
164 policies);
166 policies[2]->destroy ();
168 // Threading policy
169 policies[2] =
170 root_poa->create_thread_policy (PortableServer::SINGLE_THREAD_MODEL);
172 // Create the secondPOA under the firstPOA.
173 name = "secondPOA";
174 PortableServer::POA_var second_poa =
175 first_poa->create_POA (name.c_str (),
176 poa_manager.in (),
177 policies);
179 // Creation of POAs is over. Destroy the Policy objects.
180 for (CORBA::ULong i = 0;
181 i < policies.length ();
182 ++i)
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 (),
191 root_poa.in ());
192 test_i second_servant (orb.in (),
193 first_poa.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.
208 // Operation Used :
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 (),
215 &second_servant);
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 (),
239 "IDL:test:1.0");
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,
252 "%C\n%C\n%C\n",
253 first_ior.in (),
254 second_ior.in (),
255 third_ior.in ()));
257 int write_result =
258 write_iors_to_file (first_ior.in (),
259 second_ior.in (),
260 third_ior.in ());
261 if (write_result != 0)
262 return write_result;
264 // Activate third servant using its ObjectID.
265 test_i third_servant (orb.in (),
266 second_poa.in ());
267 second_poa->activate_object_with_id (third_oid.in (),
268 &third_servant);
270 poa_manager->activate ();
272 orb->run ();
274 orb->destroy ();
276 catch (const CORBA::Exception& ex)
278 ex._tao_print_exception ("Exception caught");
279 return -1;
282 return 0;