Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / MProfile_Forwarding / Manager.cpp
blob6c708867d65bd508b94dbfdf90495c589ad87d44
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[])
54 Manager manager;
56 try
58 // Initilaize the ORB, POA etc.
59 manager.init (argc, argv);
61 if (parse_args (argc, argv) == -1)
62 return -1;
64 manager.activate_servant ();
66 manager.make_iors_register ();
68 manager.run ();
70 catch (const CORBA::Exception& ex)
72 ex._tao_print_exception ("Caught");
73 return -1;
76 return 0;
79 Manager::Manager (void)
80 :orb_ (0),
81 new_poa_var_ (0)
83 //no-op
86 int
87 Manager::init (int argc, ACE_TCHAR *argv[])
89 this->orb_ = CORBA::ORB_init (argc, argv);
91 // Obtain the RootPOA.
92 CORBA::Object_var obj_var =
93 this->orb_->resolve_initial_references ("RootPOA");
95 // Get the POA_var object from Object_var.
96 PortableServer::POA_var root_poa_var =
97 PortableServer::POA::_narrow (obj_var.in ());
99 // Get the POAManager of the RootPOA.
100 PortableServer::POAManager_var poa_manager_var =
101 root_poa_var->the_POAManager ();
103 poa_manager_var->activate ();
105 // Policies for the childPOA to be created.
106 CORBA::PolicyList policies (4);
107 policies.length (4);
109 // The next two policies are common to both
110 // Id Assignment Policy
111 policies[0] =
112 root_poa_var->create_id_assignment_policy (PortableServer::USER_ID);
114 // Lifespan policy
115 policies[1] =
116 root_poa_var->create_lifespan_policy (PortableServer::PERSISTENT);
118 // Tell the POA to use a servant manager
119 policies[2] =
120 root_poa_var->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
122 // Servant Retention Policy -> Use a locator
123 policies[3] =
124 root_poa_var->create_servant_retention_policy (PortableServer::NON_RETAIN);
126 ACE_CString name = "newPOA";
128 new_poa_var_ =
129 root_poa_var->create_POA (name.c_str (),
130 poa_manager_var.in (),
131 policies);
133 // Creation of childPOAs is over. Destroy the Policy objects.
134 for (CORBA::ULong i = 0;
135 i < policies.length ();
136 ++i)
138 CORBA::Policy_ptr policy = policies[i];
139 policy->destroy ();
142 return 0;
146 Manager::activate_servant (void)
149 ACE_NEW_THROW_EX (this->servant_locator_,
150 Servant_Locator (this->orb_.in ()),
151 CORBA::NO_MEMORY ());
153 this->servant_locator_var_ = this->servant_locator_;
155 // Set ServantLocator object as the servant Manager of
156 // secondPOA.
157 this->new_poa_var_->set_servant_manager (this->servant_locator_);
159 // Try to create a reference with user created ID in new_poa
160 // which uses ServantLocator.
162 PortableServer::ObjectId_var second_foo_oid_var =
163 PortableServer::string_to_ObjectId ("Simple_Server");
165 this->new_manager_ior_ =
166 new_poa_var_->create_reference_with_id (second_foo_oid_var.in (),
167 "IDL:Simple_Server:1.0");
169 return 0;
173 Manager::make_iors_register (void)
175 // First server
176 CORBA::Object_var object_primary =
177 this->orb_->string_to_object (first_ior);
179 //Second server
180 CORBA::Object_var object_secondary =
181 this->orb_->string_to_object (second_ior);
183 if (third_ior == 0)
184 ACE_DEBUG ((LM_DEBUG,
185 "Here is the culprit\n"));
186 // Third Server
187 CORBA::Object_var object_tertiary =
188 this->orb_->string_to_object (third_ior);
190 // Get an object reference for the ORBs IORManipultion object!
191 CORBA::Object_ptr IORM =
192 this->orb_->resolve_initial_references (TAO_OBJID_IORMANIPULATION,
195 TAO_IOP::TAO_IOR_Manipulation_ptr iorm =
196 TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM);
199 // Create the list
200 TAO_IOP::TAO_IOR_Manipulation::IORList iors (3);
201 iors.length(3);
202 iors [0] = CORBA::Object::_duplicate (object_primary.in ());
203 iors [1] = CORBA::Object::_duplicate (object_secondary.in ());
204 iors [2] = CORBA::Object::_duplicate (this->new_manager_ior_.in ());
206 // Create a merged set 1;
207 CORBA::Object_var merged_set1 =
208 iorm->merge_iors (iors);
210 if (object_secondary.in () == 0)
211 ACE_DEBUG ((LM_DEBUG,
212 " There is a problem 1 "));
214 if (object_tertiary.in () == 0)
215 ACE_DEBUG ((LM_DEBUG,
216 " There is a problem\n"));
218 TAO_IOP::TAO_IOR_Manipulation::IORList iors_again (3);
219 iors_again.length(3);
220 iors_again [0] = CORBA::Object::_duplicate (object_secondary.in ());
221 iors_again [1] = CORBA::Object::_duplicate (object_tertiary.in ());
222 iors_again [2] = CORBA::Object::_duplicate (this->new_manager_ior_.in ());
224 // Create merged set 2
225 CORBA::Object_var merged_set2 =
226 iorm->merge_iors (iors_again);
228 CORBA::String_var iorref1 =
229 this->orb_->object_to_string (merged_set1.in ());
231 if (ior_output_file != 0)
233 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
234 if (output_file == 0)
235 ACE_ERROR_RETURN ((LM_ERROR,
236 "Cannot open output file for writing IOR: %s",
237 ior_output_file),
239 ACE_OS::fprintf (output_file, "%s", iorref1.in ());
240 ACE_OS::fclose (output_file);
243 this->servant_locator_->set (merged_set2);
245 return 0;
249 Manager::run (void)
251 this->orb_->run ();
253 return 0;