Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / CSD_Strategy / ThreadPool5 / ClientApp.cpp
blobf9772d5c7a695f0d33b0d432eccddbd970bd42c0
1 #include "ClientApp.h"
2 #include "Callback_i.h"
3 #include "ClientTask.h"
4 #include "tao/CSD_ThreadPool/CSD_TP_Strategy.h"
5 #include "tao/Intrusive_Ref_Count_Handle_T.h"
6 #include "ace/Get_Opt.h"
7 #include "ace/Log_Msg.h"
8 // To force static load the service.
9 #include "tao/PI/PI.h"
10 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
13 ClientApp::ClientApp()
18 ClientApp::~ClientApp()
23 int
24 ClientApp::run (int argc, ACE_TCHAR* argv[])
26 CORBA::ORB_var orb
27 = CORBA::ORB_init (argc, argv);
29 // Parse the command-line args for this application.
30 // * Raises -1 if problems are encountered.
31 // * Returns 1 if the usage statement was explicitly requested.
32 // * Returns 0 otherwise.
33 int result = this->parse_args (argc, argv);
34 if (result != 0)
36 return result;
39 CORBA::Object_var obj
40 = orb->string_to_object(this->ior_.c_str());
42 if (CORBA::is_nil(obj.in()))
44 ACE_ERROR((LM_ERROR,
45 "(%P|%t) Failed to convert IOR string to obj ref.\n"));
46 throw TestException();
49 Foo_var foo = Foo::_narrow(obj.in());
51 if (CORBA::is_nil(foo.in()))
53 ACE_ERROR((LM_ERROR,
54 "(%P|%t) Failed to narrow obj ref to Foo interface.\n"));
55 throw TestException();
58 // Create the callback object using the child poa with the custom
59 // strategy.
60 obj = orb->resolve_initial_references("RootPOA");
62 if (CORBA::is_nil(obj.in()))
64 ACE_ERROR((LM_ERROR,
65 "(%P|%t) Failed to resolve initial ref for 'RootPOA'.\n"));
66 throw TestException();
69 PortableServer::POA_var root_poa
70 = PortableServer::POA::_narrow(obj.in());
72 if (CORBA::is_nil(root_poa.in()))
74 ACE_ERROR((LM_ERROR,
75 "(%P|%t) Failed to narrow obj ref to POA interface.\n"));
76 throw TestException();
79 PortableServer::POAManager_var poa_manager
80 = root_poa->the_POAManager();
82 // Create the child POA.
83 CORBA::PolicyList policies(0);
84 policies.length(0);
86 PortableServer::POA_var child_poa
87 = root_poa->create_POA("ChildPoa",
88 poa_manager.in(),
89 policies);
91 if (CORBA::is_nil(child_poa.in()))
93 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
94 "Failed to create the child POA.\n"));
95 throw TestException();
98 // Create the thread pool servant dispatching strategy object, and
99 // hold it in a (local) smart pointer variable.
100 TAO_Intrusive_Ref_Count_Handle<TAO::CSD::TP_Strategy> csd_tp_strategy =
101 new TAO::CSD::TP_Strategy();
103 csd_tp_strategy->set_num_threads(1);
105 // Tell the strategy to apply itself to the child poa.
106 if (csd_tp_strategy->apply_to(child_poa.in()) == false)
108 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
109 "Failed to apply custom dispatching strategy to child poa.\n"));
110 throw TestException();
113 // Create the servant object.
114 Callback_i* servant = new Callback_i ();
116 // local smart pointer variable to deal with releasing the reference
117 // to the servant object when the smart pointer object falls out of scope.
118 PortableServer::ServantBase_var owner_transfer(servant);
120 // Activate the servant using the Child POA.
121 PortableServer::ObjectId_var oid
122 = child_poa->activate_object(servant);
124 // Obtain the object reference.
125 obj = child_poa->servant_to_reference(servant);
127 if (CORBA::is_nil(obj.in()))
129 ACE_ERROR((LM_ERROR,
130 "(%P|%t) Failed to activate servant (Callback_i).\n"));
131 throw TestException();
134 Callback_var callback = Callback::_narrow (obj.in ());
136 ClientTask client_task(orb.in (), foo.in (), callback.in ());
138 if (client_task.open () != 0)
140 throw TestException();
143 // Activate the POA Manager
144 poa_manager->activate();
146 ACE_DEBUG((LM_DEBUG,
147 "(%P|%t) ClientApp is ready.\n"));
149 orb->run();
151 client_task.wait ();
153 ACE_DEBUG((LM_DEBUG,
154 "(%P|%t) ClientApp is destroying the Root POA.\n"));
156 // Tear-down the root poa and orb.
157 root_poa->destroy(1, 1);
159 ACE_DEBUG((LM_DEBUG,
160 "(%P|%t) ClientApp is destroying the ORB.\n"));
162 orb->destroy();
164 ACE_DEBUG((LM_DEBUG,
165 "(%P|%t) ClientApp has completed running successfully.\n"));
167 return 0;
172 ClientApp::parse_args(int argc, ACE_TCHAR* argv[])
174 this->exe_name_ = argv[0];
176 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("i:"));
178 int c;
180 while ((c = get_opts()) != -1)
182 switch (c)
184 case 'i':
185 this->ior_ = get_opts.opt_arg();
186 break;
188 case '?':
189 this->usage_statement();
190 return 1;
191 default:
192 this->usage_statement();
193 return -1;
197 return 0;
201 void
202 ClientApp::usage_statement()
204 ACE_ERROR((LM_ERROR,
205 "Usage: %s [options]\n\n"
206 "OPTIONS:\n\n"
207 "\t[-i <ior>]\n"
208 "\t[-?]\n\n",
209 this->exe_name_.c_str()));