Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / examples / CSD_Strategy / ThreadPool6 / ServerApp.cpp
blobeb6810fee6bc74f47b3b1257bb20a09f34080b41
1 #include "ServerApp.h"
2 #include "Foo_i.h"
3 #include "FooS_T.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("server.ior"))
19 ServerApp::~ServerApp()
24 int
25 ServerApp::run (int argc, ACE_TCHAR* argv[])
27 CORBA::ORB_var orb = 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 TheOrbShutdownTask::instance()->orb (orb.in ());
41 CORBA::Object_var obj
42 = orb->resolve_initial_references("RootPOA");
44 if (CORBA::is_nil(obj.in()))
46 ACE_ERROR((LM_ERROR,
47 "(%P|%t) Failed to resolve initial ref for 'RootPOA'.\n"));
48 throw TestException();
51 PortableServer::POA_var root_poa
52 = PortableServer::POA::_narrow(obj.in());
54 if (CORBA::is_nil(root_poa.in()))
56 ACE_ERROR((LM_ERROR,
57 "(%P|%t) Failed to narrow obj ref to POA interface.\n"));
58 throw TestException();
61 PortableServer::POAManager_var poa_manager
62 = root_poa->the_POAManager();
64 // Create the child POA.
65 CORBA::PolicyList policies(1);
66 policies.length(1);
68 policies[0] =
69 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
71 PortableServer::POA_var child_poa
72 = root_poa->create_POA("ChildPoa",
73 poa_manager.in(),
74 policies);
76 if (CORBA::is_nil(child_poa.in()))
78 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
79 "Failed to create the child POA.\n"));
80 throw TestException();
83 policies[0]->destroy ();
85 // Create the thread pool servant dispatching strategy object, and
86 // hold it in a (local) smart pointer variable.
87 TAO_Intrusive_Ref_Count_Handle<TAO::CSD::TP_Strategy> csd_tp_strategy =
88 new TAO::CSD::TP_Strategy();
90 // Tell the strategy to apply itself to the child poa.
91 if (csd_tp_strategy->apply_to(child_poa.in()) == false)
93 ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: "
94 "Failed to apply custom dispatching strategy to child poa.\n"));
95 throw TestException();
98 // Create the Foo_i object.
99 Foo_i foo_i (this->num_clients_);
101 // Create tie object with the Foo_i object.
102 POA_Foo_tie<Foo_i> foo_tie_i (foo_i, child_poa.in ());
104 // Get Object Reference for the foo_tie_i object.
105 Foo_var foo = foo_tie_i._this ();
107 if (CORBA::is_nil(foo.in()))
109 ACE_ERROR((LM_ERROR,
110 "(%P|%t) Failed to activate servant foo_tie_i.\n"));
111 throw TestException();
114 // Stringify the object reference
115 CORBA::String_var ior
116 = orb->object_to_string(foo.in());
118 // Write the stringified object reference to the ior file.
119 FILE* ior_file = ACE_OS::fopen(this->ior_filename_.c_str(), "w");
121 if (ior_file == 0)
123 ACE_ERROR((LM_ERROR,
124 "(%P|%t) Cannot open output file for writing IOR: %s",
125 this->ior_filename_.c_str()));
126 throw TestException();
129 ACE_OS::fprintf(ior_file, "%s", ior.in ());
130 ACE_OS::fclose(ior_file);
132 // Activate the POA Manager
133 poa_manager->activate();
135 ACE_DEBUG((LM_DEBUG,
136 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
138 // Run the ORB event loop.
139 orb->run();
141 ACE_DEBUG((LM_DEBUG,
142 "(%P|%t) ServerApp ORB has stopped running. "
143 "Stop the CSD strategy.\n"));
145 ACE_DEBUG((LM_DEBUG,
146 "(%P|%t) ServerApp is waiting for OrbShutdownTask.\n"));
147 TheOrbShutdownTask::instance()->wait ();
149 ACE_DEBUG((LM_DEBUG,
150 "(%P|%t) ServerApp is destroying the Root POA.\n"));
152 // Sleep for 2 second to let the done() two-way call complete
153 // before cleanup.
154 ACE_OS::sleep (2);
156 // Tear-down the root poa and orb.
157 root_poa->destroy(1, 1);
159 ACE_DEBUG((LM_DEBUG,
160 "(%P|%t) ServerApp is destroying the ORB.\n"));
162 orb->destroy();
164 ACE_DEBUG((LM_DEBUG,
165 "(%P|%t) ServerApp has completed running successfully.\n"));
167 return 0;
172 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
174 this->exe_name_ = argv[0];
176 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("o:n:"));
178 int c;
180 while ((c = get_opts()) != -1)
182 switch (c)
184 case 'o':
185 this->ior_filename_ = get_opts.opt_arg();
186 break;
188 case 'n':
190 int tmp = ACE_OS::atoi(get_opts.opt_arg());
191 if (tmp < 1)
193 this->usage_statement ();
194 return -1;
197 this->num_clients_ = tmp;
199 break;
201 case '?':
202 this->usage_statement();
203 return 1;
205 default:
206 this->usage_statement();
207 return -1;
211 return 0;
215 void
216 ServerApp::usage_statement()
218 ACE_ERROR((LM_ERROR,
219 "Usage: %s [options]\n\n"
220 "OPTIONS:\n\n"
221 "\t[-o <ior_filename>]\n"
222 "\t[-n <num_clients>]\n"
223 "\t[-?]\n\n",
224 this->exe_name_.c_str()));