Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / CSD_Strategy_Tests / TP_Test_1 / ServerApp.cpp
blob4b32a37d00013379ed2da5bf80c2c9e12c219d51
1 #include "ServerApp.h"
2 #include "Foo_A_i.h"
3 #include "AppHelper.h"
4 #include "TestAppExceptionC.h"
5 #include "AppShutdown.h"
6 #include "ace/Get_Opt.h"
7 #include "tao/CSD_ThreadPool/CSD_TP_Strategy.h"
8 #include "tao/Intrusive_Ref_Count_Handle_T.h"
9 // To force static load the service.
10 #include "tao/PI/PI.h"
11 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
13 ServerApp::ServerApp()
14 : TestAppBase("TP_Test_1_Server"),
15 num_clients_ (1)
19 int
20 ServerApp::run_i(int argc, ACE_TCHAR* argv[])
22 // Initialize the ORB before parsing our own args.
23 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
25 // Parse the command-line args for this application.
26 // * Returns -1 if problems are encountered.
27 // * Returns 1 if the usage statement was explicitly requested.
28 // * Returns 0 otherwise.
29 int result = this->parse_args(argc, argv);
30 if (result != 0)
32 return result;
35 TheAppShutdown->init(orb.in(), num_clients_);
37 // Get the Root POA
38 PortableServer::POA_var root_poa =
39 RefHelper<PortableServer::POA>::resolve_initial_ref(orb.in(),
40 "RootPOA");
42 // Get the POAManager from the Root POA.
43 PortableServer::POAManager_var poa_manager
44 = root_poa->the_POAManager();
46 // Create the child POA Policies.
47 CORBA::PolicyList policies(0);
48 policies.length(0);
50 // Create the child POA
51 PortableServer::POA_var child_poa =
52 AppHelper::create_poa("ChildPoa",
53 root_poa.in(),
54 poa_manager.in(),
55 policies);
58 // Create the thread pool servant dispatching strategy object, and
59 // hold it in a (local) smart pointer variable.
60 TAO_Intrusive_Ref_Count_Handle<TAO::CSD::TP_Strategy> csd_strategy =
61 new TAO::CSD::TP_Strategy();
63 // Tell the strategy to apply itself to the child poa.
64 if (csd_strategy->apply_to(child_poa.in()) == false)
66 ACE_ERROR((LM_ERROR,
67 "Failed to apply CSD strategy to child poa.\n"));
68 return -1;
71 // Create the servant object.
72 Foo_A_i* servant = new Foo_A_i();
74 // Local smart pointer variable to deal with releasing the reference
75 // to the servant object when the variable falls out of scope.
76 PortableServer::ServantBase_var servant_owner(servant);
78 // Obtain the object reference using the servant
79 CORBA::Object_var obj = AppHelper::activate_servant(child_poa.in(),
80 servant);
82 // Stringify and save the object reference to a file
83 AppHelper::ref_to_file(orb.in(),
84 obj.in(),
85 this->ior_filename_.c_str());
87 // Activate the POA Manager
88 poa_manager->activate();
90 ACE_DEBUG((LM_DEBUG,
91 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
93 // Run the ORB event loop.
94 orb->run ();
96 ACE_DEBUG((LM_DEBUG,
97 "(%P|%t) ServerApp ORB event loop has completed.\n"));
99 TheAppShutdown->wait ();
101 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
102 // that the main thread might exit before all CSD Threads exit.
104 // Wait for all CSD task threads exit.
105 ACE_Thread_Manager::instance ()->wait ();
107 ACE_DEBUG((LM_DEBUG,
108 "(%P|%t) ServerApp is destroying the Root POA.\n"));
110 root_poa->destroy(1, 1);
112 ACE_DEBUG((LM_DEBUG,
113 "(%P|%t) ServerApp is destroying the ORB.\n"));
115 orb->destroy();
117 ACE_DEBUG((LM_DEBUG,
118 "(%P|%t) ServerApp has completed running successfully.\n"));
120 return 0;
125 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
127 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("o:n:"));
129 int c;
131 while ((c = get_opts()) != -1)
133 switch (c)
135 case 'o':
136 this->ior_filename_ = get_opts.opt_arg();
137 break;
139 case 'n':
141 int tmp = ACE_OS::atoi(get_opts.opt_arg());
142 if (tmp < 1)
144 ACE_ERROR((LM_ERROR,
145 "(%P|%t) Error. -n must be followed by an integer "
146 "value greater than 0.\n"));
147 return -1;
150 this->num_clients_ = tmp;
152 break;
154 case '?':
155 ACE_ERROR((LM_ERROR,
156 "(%P|%t) usage: %s -o <ior_filename> -n <num_clients>\n",
157 argv[0]));
158 return 1;
160 default:
161 ACE_ERROR((LM_ERROR,
162 "(%P|%t) usage: %s -o <ior_filename> -n <num_clients>\n",
163 argv[0]));
164 return -1;
168 return 0;