Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / MProfile_Forwarding / Manager.cpp
blob04f43f17f9cc98882594f665ee9727826452605d
1 #include "Manager.h"
3 #include "tao/IORManipulation/IORManip_Loader.h"
5 #include "ace/SString.h"
6 #include "ace/Get_Opt.h"
7 #include "ace/OS_NS_stdio.h"
9 const ACE_TCHAR *first_ior = 0;
10 const ACE_TCHAR *second_ior = 0;
11 const ACE_TCHAR *third_ior = 0;
12 const ACE_TCHAR *ior_output_file = 0;
14 int
15 parse_args (int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("a:b:c:d:"));
18 int c;
20 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 'a':
24 first_ior = get_opts.opt_arg ();
25 break;
26 case 'b':
27 second_ior = get_opts.opt_arg ();
28 break;
29 case 'c':
30 third_ior = get_opts.opt_arg ();
31 break;
32 case 'd':
33 ior_output_file = get_opts.opt_arg ();
34 break;
35 case '?':
36 default:
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "usage: %s "
39 "-a <iorfile>"
40 "-b <iorfile>"
41 "-c <iorfile>"
42 "\n",
43 argv [0]),
44 -1);
46 // Indicates successful parsing of the command line
47 return 0;
50 int
51 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
53 Manager manager;
55 try
57 // Initilaize the ORB, POA etc.
58 manager.init (argc, argv);
60 if (parse_args (argc, argv) == -1)
61 return -1;
63 manager.activate_servant ();
65 manager.make_iors_register ();
67 manager.run ();
69 catch (const CORBA::Exception& ex)
71 ex._tao_print_exception ("Caught");
72 return -1;
75 return 0;
78 Manager::Manager ()
79 :orb_ (0),
80 new_poa_var_ (0)
82 //no-op
85 int
86 Manager::init (int argc, ACE_TCHAR *argv[])
88 this->orb_ = CORBA::ORB_init (argc, argv);
90 // Obtain the RootPOA.
91 CORBA::Object_var obj_var =
92 this->orb_->resolve_initial_references ("RootPOA");
94 // Get the POA_var object from Object_var.
95 PortableServer::POA_var root_poa_var =
96 PortableServer::POA::_narrow (obj_var.in ());
98 // Get the POAManager of the RootPOA.
99 PortableServer::POAManager_var poa_manager_var =
100 root_poa_var->the_POAManager ();
102 poa_manager_var->activate ();
104 // Policies for the childPOA to be created.
105 CORBA::PolicyList policies (4);
106 policies.length (4);
108 // The next two policies are common to both
109 // Id Assignment Policy
110 policies[0] =
111 root_poa_var->create_id_assignment_policy (PortableServer::USER_ID);
113 // Lifespan policy
114 policies[1] =
115 root_poa_var->create_lifespan_policy (PortableServer::PERSISTENT);
117 // Tell the POA to use a servant manager
118 policies[2] =
119 root_poa_var->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
121 // Servant Retention Policy -> Use a locator
122 policies[3] =
123 root_poa_var->create_servant_retention_policy (PortableServer::NON_RETAIN);
125 ACE_CString name = "newPOA";
127 new_poa_var_ =
128 root_poa_var->create_POA (name.c_str (),
129 poa_manager_var.in (),
130 policies);
132 // Creation of childPOAs is over. Destroy the Policy objects.
133 for (CORBA::ULong i = 0;
134 i < policies.length ();
135 ++i)
137 CORBA::Policy_ptr policy = policies[i];
138 policy->destroy ();
141 return 0;
145 Manager::activate_servant ()
147 ACE_NEW_THROW_EX (this->servant_locator_,
148 Servant_Locator (this->orb_.in ()),
149 CORBA::NO_MEMORY ());
151 this->servant_locator_var_ = this->servant_locator_;
153 // Set ServantLocator object as the servant Manager of
154 // secondPOA.
155 this->new_poa_var_->set_servant_manager (this->servant_locator_);
157 // Try to create a reference with user created ID in new_poa
158 // which uses ServantLocator.
160 PortableServer::ObjectId_var second_foo_oid_var =
161 PortableServer::string_to_ObjectId ("Simple_Server");
163 this->new_manager_ior_ =
164 new_poa_var_->create_reference_with_id (second_foo_oid_var.in (),
165 "IDL:Simple_Server:1.0");
167 return 0;
171 Manager::make_iors_register ()
173 // First server
174 CORBA::Object_var object_primary =
175 this->orb_->string_to_object (first_ior);
177 //Second server
178 CORBA::Object_var object_secondary =
179 this->orb_->string_to_object (second_ior);
181 if (third_ior == 0)
182 ACE_DEBUG ((LM_DEBUG,
183 "Here is the culprit\n"));
184 // Third Server
185 CORBA::Object_var object_tertiary =
186 this->orb_->string_to_object (third_ior);
188 // Get an object reference for the ORBs IORManipultion object!
189 CORBA::Object_ptr IORM =
190 this->orb_->resolve_initial_references (TAO_OBJID_IORMANIPULATION,
193 TAO_IOP::TAO_IOR_Manipulation_ptr iorm =
194 TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM);
197 // Create the list
198 TAO_IOP::TAO_IOR_Manipulation::IORList iors (3);
199 iors.length(3);
200 iors [0] = CORBA::Object::_duplicate (object_primary.in ());
201 iors [1] = CORBA::Object::_duplicate (object_secondary.in ());
202 iors [2] = CORBA::Object::_duplicate (this->new_manager_ior_.in ());
204 // Create a merged set 1;
205 CORBA::Object_var merged_set1 =
206 iorm->merge_iors (iors);
208 if (object_secondary.in () == 0)
209 ACE_DEBUG ((LM_DEBUG,
210 " There is a problem 1 "));
212 if (object_tertiary.in () == 0)
213 ACE_DEBUG ((LM_DEBUG,
214 " There is a problem\n"));
216 TAO_IOP::TAO_IOR_Manipulation::IORList iors_again (3);
217 iors_again.length(3);
218 iors_again [0] = CORBA::Object::_duplicate (object_secondary.in ());
219 iors_again [1] = CORBA::Object::_duplicate (object_tertiary.in ());
220 iors_again [2] = CORBA::Object::_duplicate (this->new_manager_ior_.in ());
222 // Create merged set 2
223 CORBA::Object_var merged_set2 =
224 iorm->merge_iors (iors_again);
226 CORBA::String_var iorref1 =
227 this->orb_->object_to_string (merged_set1.in ());
229 if (ior_output_file != 0)
231 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
232 if (output_file == 0)
233 ACE_ERROR_RETURN ((LM_ERROR,
234 "Cannot open output file for writing IOR: %s",
235 ior_output_file),
237 ACE_OS::fprintf (output_file, "%s", iorref1.in ());
238 ACE_OS::fclose (output_file);
241 this->servant_locator_->set (merged_set2);
243 return 0;
247 Manager::run ()
249 this->orb_->run ();
251 return 0;