Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / Exposed_Policies / Policy_Tester.cpp
blob614efa22c281c99103e72a5a4f33f7e25ed9bbec
1 // -- App. Specific Include --
2 #include "Policy_Tester.h"
3 #include "RT_Properties.h"
4 #include "CounterC.h"
6 // -- ACE Include --
7 #include "ace/Arg_Shifter.h"
8 #include "ace/OS_NS_stdio.h"
10 // -- RTCORBA Include --
11 #include "tao/RTCORBA/RT_Policy_i.h"
13 // -- RTCORBA Include --
14 #include "tao/RTCORBA/RT_ORB.h"
16 #include "tao/ORB_Constants.h"
17 #include "tao/ORB_Core.h"
19 void
20 Policy_Tester::run ()
22 PortableServer::POAManager_var poa_manager =
23 this->child_poa_->the_POAManager ();
25 poa_manager->activate ();
27 this->orb_->run ();
30 int
31 Policy_Tester::init (int argc,
32 ACE_TCHAR *argv[])
34 // ORB Initialization.
35 this->orb_ =
36 CORBA::ORB_init (argc, argv);
38 // Get a reference to the RT-ORB.
39 CORBA::Object_var object =
40 this->orb_->resolve_initial_references ("RTORB");
42 this->rt_orb_ = RTCORBA::RTORB::_narrow (object.in ());
44 // Here we parse the command line paramether passed
45 // to the application.
47 ACE_Arg_Shifter arg_shifter (argc, argv);
49 while (arg_shifter.is_anything_left ())
51 const ACE_TCHAR *arg = 0;
52 // IOR File Name Option.
53 if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-POAConfigFile"))))
55 this->rt_poa_properties_ =
56 RT_Properties::read_from (arg);
58 else if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-ObjectConfigFile"))))
60 this->rt_object_properties_ =
61 RT_Properties::read_from (arg);
63 else if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-BaseObjectIOR"))))
65 if (this->rt_poa_properties_ == 0)
67 ACE_NEW_THROW_EX (this->rt_poa_properties_,
68 RT_Properties,
69 CORBA::NO_MEMORY (TAO::VMCID,
70 CORBA::COMPLETED_NO));
72 this->rt_poa_properties_->ior_source (arg);
74 else if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-OverriddenIOR"))))
76 if (this->rt_object_properties_ == 0)
78 ACE_NEW_THROW_EX (this->rt_object_properties_,
79 RT_Properties,
80 CORBA::NO_MEMORY (TAO::VMCID,
81 CORBA::COMPLETED_NO));
83 this->rt_object_properties_->ior_source (arg);
85 else
86 arg_shifter.consume_arg ();
89 if ((this->rt_poa_properties_ == 0) || (this->rt_object_properties_ == 0))
91 ACE_DEBUG ((LM_DEBUG,
92 ACE_TEXT ("Configuration file missing!")));
93 return -1;
96 int result =
97 this->create_objects ();
99 if (result != 0)
100 return result;
102 return 0;
105 CORBA::Boolean
106 Policy_Tester::check_reference (CORBA::Object_ptr object,
107 const char *msg)
109 if (CORBA::is_nil (object))
111 ACE_DEBUG ((LM_DEBUG, msg));
112 return 0;
114 return 1;
119 Policy_Tester::create_objects ()
121 CORBA::PolicyList poa_policy_list;
122 poa_policy_list.length (3);
124 // Create the priority policy using the RT-ORB.
125 RTCORBA::Priority priority = this->rt_poa_properties_->priority ();
126 poa_policy_list[0] =
127 this->rt_orb_->create_priority_model_policy (RTCORBA::SERVER_DECLARED,
128 priority);
130 // Create priority Banded Connection Policy.
131 RTCORBA::PriorityBands poa_priority_bands =
132 this->rt_poa_properties_->priority_bands ();
134 poa_policy_list[1] =
135 this->rt_orb_->create_priority_banded_connection_policy (poa_priority_bands);
137 // Client Protocol Policy.
138 RTCORBA::ProtocolList protocol_list;
139 protocol_list.length (1);
141 protocol_list[0].protocol_type = IOP::TAG_INTERNET_IOP;
142 protocol_list[0].orb_protocol_properties =
143 TAO_Protocol_Properties_Factory::create_orb_protocol_property (IOP::TAG_INTERNET_IOP);
145 protocol_list[0].transport_protocol_properties =
146 TAO_Protocol_Properties_Factory::create_transport_protocol_property (IOP::TAG_INTERNET_IOP,
147 this->orb_->orb_core ());
149 poa_policy_list[2] =
150 this->rt_orb_->create_client_protocol_policy (protocol_list);
152 CORBA::Object_var object =
153 this->orb_->resolve_initial_references ("RootPOA");
155 this->poa_ =
156 PortableServer::POA::_narrow (object.in ());
158 PortableServer::POAManager_var poa_mgr =
159 PortableServer::POAManager::_nil ();
161 object =
162 this->poa_->create_POA ("Child_POA",
163 poa_mgr.in (),
164 poa_policy_list);
166 this->child_poa_ =
167 RTPortableServer::POA::_narrow (object.in ());
169 // Create a Corba Object reference, using the policies
170 // set at the POA level.
171 object =
172 this->child_poa_->create_reference ("IDL:Counter:1.0");
174 ACE_DEBUG ((LM_DEBUG,
175 ACE_TEXT ("Reference Created!\n")));
177 if (!check_reference (object.in (),
178 "Unable to create Object!\n"))
179 return -1;
181 Counter_var base_object = Counter::_narrow (object.in ());
183 if (!check_reference (base_object.in(),
184 "Unable to create a Object!\n"))
185 return -1;
187 CORBA::String_var ior =
188 this->orb_->object_to_string (base_object.in ());
190 ACE_DEBUG ((LM_DEBUG,
191 ACE_TEXT ("Activated as <%C>\n"), ior.in ()));
193 FILE *output_file = ACE_OS::fopen (this->rt_poa_properties_->ior_source (), "w");
194 if (output_file == 0)
195 ACE_ERROR_RETURN ((LM_ERROR,
196 ACE_TEXT ("Cannot open output file for writing IOR: %s"),
197 this->rt_poa_properties_->ior_source ()),
198 -1);
199 ACE_OS::fprintf (output_file, "%s", ior.in ());
200 ACE_OS::fclose (output_file);
202 // Now we create an object that overrides some of the policies
203 // set at the POA level.
206 // Create a Corba Object reference, using the policies
207 // set at the POA level.
209 object =
210 this->child_poa_->create_reference_with_priority
211 ("IDL:Counter:1.0",
212 this->rt_object_properties_->priority ());
214 ACE_DEBUG ((LM_DEBUG,
215 ACE_TEXT ("Reference Created!\n")));
217 if (!check_reference (object.in (),
218 "Unable to create a Counter Object!\n"))
219 return -1;
221 Counter_var over_object = Counter::_narrow (object.in ());
223 if (!check_reference (over_object.in(),
224 "Unable to create Object!\n"))
225 return -1;
228 CORBA::String_var o_ior =
229 this->orb_->object_to_string (over_object.in ());
231 ACE_DEBUG ((LM_DEBUG,
232 ACE_TEXT ("Activated as <%C>\n"), o_ior.in ()));
234 output_file = ACE_OS::fopen (this->rt_object_properties_->ior_source (), "w");
236 if (output_file == 0)
237 ACE_ERROR_RETURN ((LM_ERROR,
238 ACE_TEXT ("Cannot open output file for writing IOR: %s"),
239 this->rt_object_properties_->ior_source ()),
240 -1);
241 ACE_OS::fprintf (output_file, "%s", o_ior.in ());
242 ACE_OS::fclose (output_file);
243 return 0;
246 void
247 Policy_Tester::shutdown ()
249 this->orb_->shutdown (false);