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.
9 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
12 ServerApp::ServerApp()
18 ServerApp::~ServerApp()
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
);
38 TheOrbShutdownTask::instance()->orb (orb
.in ());
41 = orb
->resolve_initial_references("RootPOA");
43 if (CORBA::is_nil(obj
.in()))
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()))
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);
67 PortableServer::POA_var child_poa
68 = root_poa
->create_POA("ChildPoa",
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()))
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");
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();
135 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
137 // Run the ORB event loop.
141 "(%P|%t) ServerApp ORB has stopped running. "
142 "Stop the CSD strategy.\n"));
145 "(%P|%t) ServerApp is waiting for OrbShutdownTask.\n"));
146 TheOrbShutdownTask::instance()->wait ();
149 "(%P|%t) ServerApp is destroying the Root POA.\n"));
151 // Sleep for 2 second to let the done() two-way call complete
155 // Tear-down the root poa and orb.
156 root_poa
->destroy(1, 1);
159 "(%P|%t) ServerApp is destroying the ORB.\n"));
164 "(%P|%t) ServerApp has completed running successfully.\n"));
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:"));
179 while ((c
= get_opts()) != -1)
184 this->ior_filename_
= get_opts
.opt_arg();
189 int tmp
= ACE_OS::atoi(get_opts
.opt_arg());
192 this->usage_statement();
196 this->num_clients_
= tmp
;
201 this->usage_statement();
205 this->usage_statement();
215 ServerApp::usage_statement()
218 "Usage: %s [options]\n\n"
220 "\t[-o <ior_filename>]\n"
221 "\t[-n <num_clients>]\n"
223 this->exe_name_
.c_str()));