Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_3276_Regression / Manager.cpp
blobaca19bf2b199bec7635c7f17137c21304373fdbd
1 #include "ace/SString.h"
2 #include "Manager.h"
3 #include "test_i.h"
5 #include "ace/Get_Opt.h"
6 #include "ace/OS_NS_stdio.h"
8 const ACE_TCHAR *control_ior = 0;
9 const ACE_TCHAR *proxy_ior = 0;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("c:p:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'c':
21 control_ior = get_opts.opt_arg ();
22 break;
23 case 'p':
24 proxy_ior = get_opts.opt_arg ();
25 break;
27 // Indicates successful parsing of the command line
28 return 0;
31 int
32 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
34 try
36 Manager manager;
38 // Initilaize the ORB, POA etc.
39 manager.init (argc, argv);
41 if (parse_args (argc, argv) == -1)
42 return -1;
44 manager.activate_servant ();
46 CORBA::ORB_var orb = manager.orb ();
47 CORBA::Object_var server_ref = manager.server ();
49 CORBA::String_var ior =
50 orb->object_to_string (server_ref.in ());
52 FILE *output_file = 0;
54 if (proxy_ior != 0)
56 ACE_DEBUG ((LM_DEBUG,
57 "Writing the servant locator object ref out to file %s\n",
58 proxy_ior));
59 output_file = ACE_OS::fopen (proxy_ior, "w");
60 if (output_file == 0)
61 ACE_ERROR_RETURN ((LM_ERROR,
62 "Cannot open output file for writing IOR: %s",
63 proxy_ior),
64 1);
65 ACE_OS::fprintf (output_file, "%s", ior.in ());
66 ACE_OS::fclose (output_file);
69 // this is only to shutdown the manager afterwards
70 Simple_Server_i server_impl (orb.in ());
72 Simple_Server_var server = server_impl._this ();
74 ior =
75 orb->object_to_string (server.in ());
77 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
79 // If the proxy_ior exists, output the ior to it
80 if (control_ior != 0)
82 ACE_DEBUG ((LM_DEBUG,
83 "Writing the root poa servant server IOR out to file %s\n",
84 control_ior));
85 output_file = ACE_OS::fopen (control_ior, "w");
86 if (output_file == 0)
87 ACE_ERROR_RETURN ((LM_ERROR,
88 "Cannot open output file for writing IOR: %s",
89 control_ior),
90 1);
91 ACE_OS::fprintf (output_file, "%s", ior.in ());
92 ACE_OS::fclose (output_file);
95 manager.run ();
97 catch (const CORBA::Exception & ex)
99 ex._tao_print_exception ("Exception caught in manager:");
100 return -1;
103 return 0;
106 Manager::Manager ()
107 : orb_ (0),
108 new_poa_var_ (0)
110 //no-op
113 Manager::~Manager ()
115 this->orb_->destroy ();
118 CORBA::ORB_ptr
119 Manager::orb ()
121 return CORBA::ORB::_duplicate (this->orb_.in ());
124 CORBA::Object_ptr
125 Manager::server ()
127 return CORBA::Object::_duplicate (this->server_.in ());
131 Manager::init (int argc, ACE_TCHAR *argv[])
133 this->orb_ = CORBA::ORB_init (argc, argv);
135 // Obtain the RootPOA.
136 CORBA::Object_var obj_var =
137 this->orb_->resolve_initial_references ("RootPOA");
139 // Get the POA_var object from Object_var.
140 PortableServer::POA_var root_poa_var =
141 PortableServer::POA::_narrow (obj_var.in ());
143 // Get the POAManager of the RootPOA.
144 PortableServer::POAManager_var poa_manager_var =
145 root_poa_var->the_POAManager ();
147 poa_manager_var->activate ();
149 // Policies for the childPOA to be created.
150 CORBA::PolicyList policies (4);
151 policies.length (4);
153 // The next two policies are common to both
154 // Id Assignment Policy
155 policies[0] =
156 root_poa_var->create_id_assignment_policy (PortableServer::USER_ID);
158 // Lifespan policy
159 policies[1] =
160 root_poa_var->create_lifespan_policy (PortableServer::PERSISTENT);
162 // Tell the POA to use a servant manager
163 policies[2] =
164 root_poa_var->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER);
166 // Servant Retention Policy -> Use a locator
167 policies[3] =
168 root_poa_var->create_servant_retention_policy (PortableServer::NON_RETAIN);
170 ACE_CString name = "newPOA";
172 this->new_poa_var_ =
173 root_poa_var->create_POA (name.c_str (),
174 poa_manager_var.in (),
175 policies);
177 // Creation of childPOAs is over. Destroy the Policy objects.
178 for (CORBA::ULong i = 0;
179 i < policies.length ();
180 ++i)
182 CORBA::Policy_ptr policy = policies[i];
183 policy->destroy ();
186 return 0;
190 Manager::activate_servant ()
192 Servant_Locator *tmp = 0;
194 ACE_NEW_THROW_EX (tmp,
195 Servant_Locator,
196 CORBA::NO_MEMORY ());
198 this->servant_locator_ = tmp;
200 // Set ServantLocator object as the servant Manager of
201 // secondPOA.
202 this->new_poa_var_->set_servant_manager (this->servant_locator_.in ());
204 // Try to create a reference with user created ID in new_poa
205 // which uses ServantLocator.
206 PortableServer::ObjectId_var oid_var =
207 PortableServer::string_to_ObjectId ("Simple_Server");
209 this->server_ =
210 new_poa_var_->create_reference_with_id (oid_var.in (),
211 "IDL:Simple_Server:1.0");
213 return 0;
217 Manager::run ()
219 this->orb_->run ();
221 return 0;