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.
10 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
13 ServerApp::ServerApp()
14 : ior_filename_ (ACE_TEXT("server.ior"))
19 ServerApp::~ServerApp()
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
);
39 TheOrbShutdownTask::instance()->orb (orb
.in ());
42 = orb
->resolve_initial_references("RootPOA");
44 if (CORBA::is_nil(obj
.in()))
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()))
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);
69 root_poa
->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION
);
71 PortableServer::POA_var child_poa
72 = root_poa
->create_POA("ChildPoa",
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()))
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");
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();
136 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
138 // Run the ORB event loop.
142 "(%P|%t) ServerApp ORB has stopped running. "
143 "Stop the CSD strategy.\n"));
146 "(%P|%t) ServerApp is waiting for OrbShutdownTask.\n"));
147 TheOrbShutdownTask::instance()->wait ();
150 "(%P|%t) ServerApp is destroying the Root POA.\n"));
152 // Sleep for 2 second to let the done() two-way call complete
156 // Tear-down the root poa and orb.
157 root_poa
->destroy(1, 1);
160 "(%P|%t) ServerApp is destroying the ORB.\n"));
165 "(%P|%t) ServerApp has completed running successfully.\n"));
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:"));
180 while ((c
= get_opts()) != -1)
185 this->ior_filename_
= get_opts
.opt_arg();
190 int tmp
= ACE_OS::atoi(get_opts
.opt_arg());
193 this->usage_statement ();
197 this->num_clients_
= tmp
;
202 this->usage_statement();
206 this->usage_statement();
216 ServerApp::usage_statement()
219 "Usage: %s [options]\n\n"
221 "\t[-o <ior_filename>]\n"
222 "\t[-n <num_clients>]\n"
224 this->exe_name_
.c_str()));