Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / examples / CSD_Strategy / ThreadPool2 / ServerApp.cpp
blob15826a53af556d267f8a69b94821c38c28703b88
1 #include "ServerApp.h"
2 #include "FooServantList.h"
3 #include "FooC.h"
4 #include "OrbShutdownTask.h"
5 #include "ace/Get_Opt.h"
6 #include "tao/CSD_ThreadPool/CSD_TP_Strategy.h"
7 #include "tao/Intrusive_Ref_Count_Handle_T.h"
8 // To force static load the service.
9 #include "tao/PI/PI.h"
10 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
13 ServerApp::ServerApp()
14 : ior_filename_(ACE_TEXT("foo")),
15 num_servants_(1),
16 num_clients_(1)
21 ServerApp::~ServerApp()
26 int
27 ServerApp::run (int argc, ACE_TCHAR* argv[])
29 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
31 // Parse the command-line args for this application.
32 // * Raises -1 if problems are encountered.
33 // * Returns 1 if the usage statement was explicitly requested.
34 // * Returns 0 otherwise.
35 int result = this->parse_args (argc, argv);
36 if (result != 0)
38 return result;
41 TheOrbShutdownTask::instance()->orb (orb.in ());
43 CORBA::Object_var obj
44 = orb->resolve_initial_references("RootPOA");
46 if (CORBA::is_nil(obj.in()))
48 ACE_ERROR((LM_ERROR,
49 "(%P|%t) Failed to resolve initial ref for 'RootPOA'.\n"));
50 throw TestException();
53 PortableServer::POA_var root_poa
54 = PortableServer::POA::_narrow(obj.in());
56 if (CORBA::is_nil(root_poa.in()))
58 ACE_ERROR((LM_ERROR,
59 "(%P|%t) Failed to narrow obj ref to POA interface.\n"));
60 throw TestException();
63 PortableServer::POAManager_var poa_manager
64 = root_poa->the_POAManager();
66 // Create the child POA.
67 CORBA::PolicyList policies(1);
68 policies.length(1);
70 policies[0] = root_poa->create_id_assignment_policy(PortableServer::USER_ID);
72 PortableServer::POA_var child_poa
73 = root_poa->create_POA("ChildPoa",
74 poa_manager.in(),
75 policies);
77 if (CORBA::is_nil(child_poa.in()))
79 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
80 "Failed to create the child POA.\n"));
81 throw TestException();
84 policies[0]->destroy ();
86 // Create the thread pool servant dispatching strategy object, and
87 // hold it in a (local) smart pointer variable.
88 TAO_Intrusive_Ref_Count_Handle<TAO::CSD::TP_Strategy> csd_tp_strategy =
89 new TAO::CSD::TP_Strategy();
91 csd_tp_strategy->set_num_threads(this->num_servants_);
93 // Tell the strategy to apply itself to the child poa.
94 if (csd_tp_strategy->apply_to(child_poa.in()) == false)
96 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
97 "Failed to apply custom dispatching strategy to child poa.\n"));
98 throw TestException();
101 FooServantList servants(this->ior_filename_.c_str(),
102 this->num_servants_,
103 this->num_clients_,
104 orb.in());
106 servants.create_and_activate(child_poa.in());
108 // Activate the POA Manager
109 poa_manager->activate();
111 ACE_DEBUG((LM_DEBUG,
112 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
114 // Run the ORB event loop.
115 orb->run();
117 ACE_DEBUG((LM_DEBUG,
118 "(%P|%t) ServerApp ORB has stopped running. "
119 "Stop the CSD strategy.\n"));
121 // Sleep for 2 second to let the done() two-way call complete
122 // before cleanup.
123 ACE_OS::sleep (2);
125 ACE_DEBUG((LM_DEBUG,
126 "(%P|%t) ServerApp is waiting for OrbShutdownTask.\n"));
127 TheOrbShutdownTask::instance()->wait ();
129 ACE_DEBUG((LM_DEBUG,
130 "(%P|%t) ServerApp is destroying the Root POA.\n"));
132 // Tear-down the root poa and orb.
133 root_poa->destroy(1, 1);
135 ACE_DEBUG((LM_DEBUG,
136 "(%P|%t) ServerApp is destroying the ORB.\n"));
138 orb->destroy();
140 ACE_DEBUG((LM_DEBUG,
141 "(%P|%t) ServerApp has completed running successfully.\n"));
143 return 0;
148 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
150 this->exe_name_ = argv[0];
152 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("p:s:c:"));
154 int c;
155 int tmp;
157 while ((c = get_opts()) != -1)
159 switch (c)
161 case 'p':
162 this->ior_filename_ = get_opts.opt_arg();
163 break;
165 case 's':
166 tmp = ACE_OS::atoi(get_opts.opt_arg());
167 if (tmp < 1)
169 this->usage_statement();
170 return -1;
173 this->num_servants_ = tmp;
174 break;
176 case 'c':
177 tmp = ACE_OS::atoi(get_opts.opt_arg());
178 if (tmp < 1)
180 this->usage_statement();
181 return -1;
184 this->num_clients_ = tmp;
185 break;
187 case '?':
188 this->usage_statement();
189 return 1;
191 default:
192 this->usage_statement();
193 return -1;
197 return 0;
201 void
202 ServerApp::usage_statement()
204 ACE_ERROR((LM_ERROR,
205 "Usage: %s [options]\n\n"
206 "OPTIONS:\n\n"
207 "\t[-p <ior_filename_prefix>]\n"
208 "\t[-s <num_servants>]\n"
209 "\t[-c <num_clients>]\n"
210 "\t[-?]\n",
211 "Default ior_filename_prefix is 'foo'.\n"
212 "Default num_servants is 1.\n"
213 "Default num_clients is 1.\n\n",
214 this->exe_name_.c_str()));