More tests update
[ACE_TAO.git] / TAO / tests / Exposed_Policies / Policy_Tester.cpp
blob231982ae857fab4462c16d3dbbdae7820ded0eb9
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 Policy_Tester::Policy_Tester (void)
20 : rt_object_properties_ (0),
21 rt_poa_properties_ (0)
23 // No_Op
26 Policy_Tester::~Policy_Tester (void)
30 void
31 Policy_Tester::run (void)
33 PortableServer::POAManager_var poa_manager =
34 this->child_poa_->the_POAManager ();
36 poa_manager->activate ();
38 this->orb_->run ();
41 int
42 Policy_Tester::init (int argc,
43 ACE_TCHAR *argv[])
45 // ORB Initialization.
46 this->orb_ =
47 CORBA::ORB_init (argc, argv);
49 // Get a reference to the RT-ORB.
50 CORBA::Object_var object =
51 this->orb_->resolve_initial_references ("RTORB");
53 this->rt_orb_ = RTCORBA::RTORB::_narrow (object.in ());
55 // Here we parse the command line paramether passed
56 // to the application.
58 ACE_Arg_Shifter arg_shifter (argc, argv);
60 while (arg_shifter.is_anything_left ())
62 const ACE_TCHAR *arg = 0;
63 // IOR File Name Option.
64 if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-POAConfigFile"))))
66 this->rt_poa_properties_ =
67 RT_Properties::read_from (arg);
69 else if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-ObjectConfigFile"))))
71 this->rt_object_properties_ =
72 RT_Properties::read_from (arg);
74 else if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-BaseObjectIOR"))))
76 if (this->rt_poa_properties_ == 0)
78 ACE_NEW_THROW_EX (this->rt_poa_properties_,
79 RT_Properties,
80 CORBA::NO_MEMORY (TAO::VMCID,
81 CORBA::COMPLETED_NO));
83 this->rt_poa_properties_->ior_source (arg);
85 else if (0 != (arg = arg_shifter.get_the_parameter (ACE_TEXT("-OverriddenIOR"))))
87 if (this->rt_object_properties_ == 0)
89 ACE_NEW_THROW_EX (this->rt_object_properties_,
90 RT_Properties,
91 CORBA::NO_MEMORY (TAO::VMCID,
92 CORBA::COMPLETED_NO));
94 this->rt_object_properties_->ior_source (arg);
96 else
97 arg_shifter.consume_arg ();
100 if ((this->rt_poa_properties_ == 0) || (this->rt_object_properties_ == 0))
102 ACE_DEBUG ((LM_DEBUG,
103 ACE_TEXT ("Configuration file missing!")));
104 return -1;
107 int result =
108 this->create_objects ();
110 if (result != 0)
111 return result;
113 return 0;
116 CORBA::Boolean
117 Policy_Tester::check_reference (CORBA::Object_ptr object,
118 const char *msg)
120 if (CORBA::is_nil (object))
122 ACE_DEBUG ((LM_DEBUG, msg));
123 return 0;
125 return 1;
130 Policy_Tester::create_objects (void)
132 CORBA::PolicyList poa_policy_list;
133 poa_policy_list.length (3);
135 // Create the priority policy using the RT-ORB.
136 RTCORBA::Priority priority = this->rt_poa_properties_->priority ();
137 poa_policy_list[0] =
138 this->rt_orb_->create_priority_model_policy (RTCORBA::SERVER_DECLARED,
139 priority);
141 // Create priority Banded Connection Policy.
142 RTCORBA::PriorityBands poa_priority_bands =
143 this->rt_poa_properties_->priority_bands ();
145 poa_policy_list[1] =
146 this->rt_orb_->create_priority_banded_connection_policy (poa_priority_bands);
148 // Client Protocol Policy.
149 RTCORBA::ProtocolList protocol_list;
150 protocol_list.length (1);
152 protocol_list[0].protocol_type = IOP::TAG_INTERNET_IOP;
153 protocol_list[0].orb_protocol_properties =
154 TAO_Protocol_Properties_Factory::create_orb_protocol_property (IOP::TAG_INTERNET_IOP);
156 protocol_list[0].transport_protocol_properties =
157 TAO_Protocol_Properties_Factory::create_transport_protocol_property (IOP::TAG_INTERNET_IOP,
158 this->orb_->orb_core ());
160 poa_policy_list[2] =
161 this->rt_orb_->create_client_protocol_policy (protocol_list);
163 CORBA::Object_var object =
164 this->orb_->resolve_initial_references ("RootPOA");
166 this->poa_ =
167 PortableServer::POA::_narrow (object.in ());
169 PortableServer::POAManager_var poa_mgr =
170 PortableServer::POAManager::_nil ();
172 object =
173 this->poa_->create_POA ("Child_POA",
174 poa_mgr.in (),
175 poa_policy_list);
177 this->child_poa_ =
178 RTPortableServer::POA::_narrow (object.in ());
180 // Create a Corba Object reference, using the policies
181 // set at the POA level.
182 object =
183 this->child_poa_->create_reference ("IDL:Counter:1.0");
185 ACE_DEBUG ((LM_DEBUG,
186 ACE_TEXT ("Reference Created!\n")));
188 if (!check_reference (object.in (),
189 "Unable to create Object!\n"))
190 return -1;
192 Counter_var base_object = Counter::_narrow (object.in ());
194 if (!check_reference (base_object.in(),
195 "Unable to create a Object!\n"))
196 return -1;
198 CORBA::String_var ior =
199 this->orb_->object_to_string (base_object.in ());
201 ACE_DEBUG ((LM_DEBUG,
202 ACE_TEXT ("Activated as <%C>\n"), ior.in ()));
204 FILE *output_file = ACE_OS::fopen (this->rt_poa_properties_->ior_source (), "w");
205 if (output_file == 0)
206 ACE_ERROR_RETURN ((LM_ERROR,
207 ACE_TEXT ("Cannot open output file for writing IOR: %s"),
208 this->rt_poa_properties_->ior_source ()),
209 -1);
210 ACE_OS::fprintf (output_file, "%s", ior.in ());
211 ACE_OS::fclose (output_file);
213 // Now we create an object that overrides some of the policies
214 // set at the POA level.
217 // Create a Corba Object reference, using the policies
218 // set at the POA level.
220 object =
221 this->child_poa_->create_reference_with_priority
222 ("IDL:Counter:1.0",
223 this->rt_object_properties_->priority ());
225 ACE_DEBUG ((LM_DEBUG,
226 ACE_TEXT ("Reference Created!\n")));
228 if (!check_reference (object.in (),
229 "Unable to create a Counter Object!\n"))
230 return -1;
232 Counter_var over_object = Counter::_narrow (object.in ());
234 if (!check_reference (over_object.in(),
235 "Unable to create Object!\n"))
236 return -1;
239 CORBA::String_var o_ior =
240 this->orb_->object_to_string (over_object.in ());
242 ACE_DEBUG ((LM_DEBUG,
243 ACE_TEXT ("Activated as <%C>\n"), o_ior.in ()));
245 output_file = ACE_OS::fopen (this->rt_object_properties_->ior_source (), "w");
247 if (output_file == 0)
248 ACE_ERROR_RETURN ((LM_ERROR,
249 ACE_TEXT ("Cannot open output file for writing IOR: %s"),
250 this->rt_object_properties_->ior_source ()),
251 -1);
252 ACE_OS::fprintf (output_file, "%s", o_ior.in ());
253 ACE_OS::fclose (output_file);
254 return 0;
257 void
258 Policy_Tester::shutdown (void)
260 this->orb_->shutdown (0);