Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Bug_3953_Regression / server.cpp
blobbc99303785f17e91aa6b3bbc46a1c6c7ef096592
1 #include "server_i.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/Task.h"
5 #include "tao/Messaging/Messaging.h"
7 static int quiet = 0;
8 // The test is quiet...
10 static const ACE_TCHAR *ior_file = ACE_TEXT ("test.ior");
11 // File of file to which the ior is written
13 static int number_of_threads = 1;
14 // Number of server threads.
16 class Server_Task : public ACE_Task_Base
18 public:
19 Server_Task (CORBA::ORB_ptr orb);
20 int svc ();
22 private:
23 CORBA::ORB_var orb_;
26 Server_Task::Server_Task (CORBA::ORB_ptr orb)
27 : orb_ (CORBA::ORB::_duplicate (orb))
31 int
32 Server_Task::svc ()
34 try
36 CORBA::Object_var object =
37 this->orb_->resolve_initial_references ("PolicyCurrent");
39 CORBA::PolicyCurrent_var policy_current =
40 CORBA::PolicyCurrent::_narrow (object.in ());
42 TimeBase::TimeT timeout_period = 100 * 100000;
44 CORBA::Any timeout_as_any;
45 timeout_as_any <<= timeout_period;
47 CORBA::PolicyList policy_list (1);
48 policy_list.length (1);
49 policy_list[0] =
50 this->orb_->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
51 timeout_as_any);
53 policy_current->set_policy_overrides (policy_list,
54 CORBA::ADD_OVERRIDE);
56 policy_list[0]->destroy();
58 this->orb_->run ();
60 catch (const CORBA::Exception&)
62 return -1;
64 return 0;
67 static int
68 write_ior_to_file (const char *ior)
70 FILE *output_file = ACE_OS::fopen (ior_file, "w");
72 if (output_file == 0)
73 ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output files for writing IOR: %s\n",
74 ior_file),
75 -1);
77 int result = 0;
79 result = ACE_OS::fprintf (output_file,
80 "%s",
81 ior);
82 if (result < 0 ||
83 static_cast<size_t> (result) != ACE_OS::strlen (ior))
84 ACE_ERROR_RETURN ((LM_ERROR,
85 "ACE_OS::fprintf failed while writing %C to %s\n",
86 ior,
87 ior_file),
88 -1);
90 ACE_OS::fclose (output_file);
92 return 0;
95 static int
96 parse_args (int argc,
97 ACE_TCHAR **argv)
99 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("q:f:t:"));
100 int c;
102 while ((c = get_opts ()) != -1)
103 switch (c)
105 case 'q':
106 quiet = 1;
107 break;
109 case 't':
110 number_of_threads = ACE_OS::atoi (get_opts.opt_arg ());
111 break;
113 case 'f':
114 ior_file = get_opts.opt_arg ();
115 break;
117 case '?':
118 default:
119 ACE_ERROR_RETURN ((LM_ERROR,
120 "usage: %s"
121 " [-t number of threads]"
122 " [-f ior file]"
123 " [-q (quite)]"
124 "\n",
125 argv[0]),
126 -1);
129 return 0;
133 ACE_TMAIN (int argc, ACE_TCHAR **argv)
135 int result = 0;
139 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
141 result = parse_args (argc, argv);
142 if (result != 0)
143 return result;
145 CORBA::Object_var object = orb->resolve_initial_references ("RootPOA");
147 PortableServer::POA_var root_poa =
148 PortableServer::POA::_narrow (object.in ());
150 PortableServer::POAManager_var poa_manager =
151 root_poa->the_POAManager ();
153 poa_manager->activate ();
155 server_i server_servant (quiet,
156 orb.in ());
158 PortableServer::ObjectId_var id =
159 root_poa->activate_object (&server_servant);
161 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
163 server_var server_object = server::_narrow (object_act.in ());
165 CORBA::String_var ior = orb->object_to_string (server_object.in ());
167 result = write_ior_to_file (ior.in ());
168 if (result != 0)
169 return result;
171 Server_Task server_tasks (orb.in ());
173 result = server_tasks.activate (THR_BOUND,
174 number_of_threads);
176 if (result != 0)
177 return result;
179 result = ACE_Thread_Manager::instance ()->wait ();
181 if (result != 0)
182 return result;
184 root_poa->destroy (true, true);
186 if (!server_servant.exception ())
187 ++result;
189 orb->destroy ();
191 catch (const CORBA::Exception& ex)
193 ex._tao_print_exception ("server::main");
194 return -1;
197 return result;