Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / CSD_Strategy / ThreadPool / ServerApp.cpp
blob15b2f17f4c26083dc5c119c5a52bdb74a23f559f
1 #include "ServerApp.h"
2 #include "Foo_i.h"
3 #include "OrbShutdownTask.h"
4 #include "ace/Get_Opt.h"
5 #include "tao/CSD_ThreadPool/CSD_TP_Strategy.h"
6 #include "tao/Intrusive_Ref_Count_Handle_T.h"
7 // To force static load the service.
8 #include "tao/PI/PI.h"
9 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
12 ServerApp::ServerApp()
13 : num_clients_ (1)
18 ServerApp::~ServerApp()
23 int
24 ServerApp::run (int argc, ACE_TCHAR* argv[])
26 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
28 // Parse the command-line args for this application.
29 // * Raises -1 if problems are encountered.
30 // * Returns 1 if the usage statement was explicitly requested.
31 // * Returns 0 otherwise.
32 int result = this->parse_args (argc, argv);
33 if (result != 0)
35 return result;
38 TheOrbShutdownTask::instance()->orb (orb.in ());
40 CORBA::Object_var obj
41 = orb->resolve_initial_references("RootPOA");
43 if (CORBA::is_nil(obj.in()))
45 ACE_ERROR((LM_ERROR,
46 "(%P|%t) Failed to resolve initial ref for 'RootPOA'.\n"));
47 throw TestException();
50 PortableServer::POA_var root_poa
51 = PortableServer::POA::_narrow(obj.in());
53 if (CORBA::is_nil(root_poa.in()))
55 ACE_ERROR((LM_ERROR,
56 "(%P|%t) Failed to narrow obj ref to POA interface.\n"));
57 throw TestException();
60 PortableServer::POAManager_var poa_manager
61 = root_poa->the_POAManager();
63 // Create the child POA.
64 CORBA::PolicyList policies(0);
65 policies.length(0);
67 PortableServer::POA_var child_poa
68 = root_poa->create_POA("ChildPoa",
69 poa_manager.in(),
70 policies);
72 if (CORBA::is_nil(child_poa.in()))
74 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
75 "Failed to create the child POA.\n"));
76 throw TestException();
79 // Create the thread pool servant dispatching strategy object, and
80 // hold it in a (local) smart pointer variable.
81 TAO_Intrusive_Ref_Count_Handle<TAO::CSD::TP_Strategy> csd_tp_strategy =
82 new TAO::CSD::TP_Strategy();
84 // Tell the strategy to apply itself to the child poa.
85 if (csd_tp_strategy->apply_to(child_poa.in()) == false)
87 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
88 "Failed to apply custom dispatching strategy to child poa.\n"));
89 throw TestException();
92 // Create the servant object.
93 Foo_i* servant = new Foo_i(this->num_clients_);
95 // local smart pointer variable to deal with releasing the reference
96 // to the servant object when the smart pointer object falls out of scope.
97 PortableServer::ServantBase_var owner_transfer(servant);
99 // Activate the servant using the Child POA.
100 PortableServer::ObjectId_var oid
101 = child_poa->activate_object(servant);
103 // Obtain the object reference.
104 obj = child_poa->servant_to_reference(servant);
106 if (CORBA::is_nil(obj.in()))
108 ACE_ERROR((LM_ERROR,
109 "(%P|%t) Failed to activate servant (Foo_i).\n"));
110 throw TestException();
113 // Stringify the object reference
114 CORBA::String_var ior
115 = orb->object_to_string(obj.in());
117 // Write the stringified object reference to the ior file.
118 FILE* ior_file = ACE_OS::fopen(this->ior_filename_.c_str(), "w");
120 if (ior_file == 0)
122 ACE_ERROR((LM_ERROR,
123 "(%P|%t) Cannot open output file for writing IOR: %s",
124 this->ior_filename_.c_str()));
125 throw TestException();
128 ACE_OS::fprintf(ior_file, "%s", ior.in ());
129 ACE_OS::fclose(ior_file);
131 // Activate the POA Manager
132 poa_manager->activate();
134 ACE_DEBUG((LM_DEBUG,
135 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
137 // Run the ORB event loop.
138 orb->run();
140 ACE_DEBUG((LM_DEBUG,
141 "(%P|%t) ServerApp ORB has stopped running. "
142 "Stop the CSD strategy.\n"));
144 ACE_DEBUG((LM_DEBUG,
145 "(%P|%t) ServerApp is waiting for OrbShutdownTask.\n"));
146 TheOrbShutdownTask::instance()->wait ();
148 ACE_DEBUG((LM_DEBUG,
149 "(%P|%t) ServerApp is destroying the Root POA.\n"));
151 // Sleep for 2 second to let the done() two-way call complete
152 // before cleanup.
153 ACE_OS::sleep (2);
155 // Tear-down the root poa and orb.
156 root_poa->destroy(1, 1);
158 ACE_DEBUG((LM_DEBUG,
159 "(%P|%t) ServerApp is destroying the ORB.\n"));
161 orb->destroy();
163 ACE_DEBUG((LM_DEBUG,
164 "(%P|%t) ServerApp has completed running successfully.\n"));
166 return 0;
171 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
173 this->exe_name_ = argv[0];
175 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("o:n:"));
177 int c;
179 while ((c = get_opts()) != -1)
181 switch (c)
183 case 'o':
184 this->ior_filename_ = get_opts.opt_arg();
185 break;
187 case 'n':
189 int tmp = ACE_OS::atoi(get_opts.opt_arg());
190 if (tmp < 1)
192 this->usage_statement();
193 return -1;
196 this->num_clients_ = tmp;
198 break;
200 case '?':
201 this->usage_statement();
202 return 1;
204 default:
205 this->usage_statement();
206 return -1;
210 return 0;
214 void
215 ServerApp::usage_statement()
217 ACE_ERROR((LM_ERROR,
218 "Usage: %s [options]\n\n"
219 "OPTIONS:\n\n"
220 "\t[-o <ior_filename>]\n"
221 "\t[-n <num_clients>]\n"
222 "\t[-?]\n\n",
223 this->exe_name_.c_str()));