Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Muxed_GIOP_Versions / server.cpp
blob69794be33b0171c065fc2984286481d4e98a02c9
1 #include "test_i.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
5 const ACE_TCHAR *ior_output_file = 0;
6 const ACE_TCHAR *corbaloc_arg = ACE_TEXT("corbaloc:iiop:1.0@localhost:12000/ObjectName");
7 int niterations = 5;
9 int nthreads = 4;
10 int nclient_threads = nthreads;
12 int
13 parse_args (int argc, ACE_TCHAR *argv[])
15 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("c:l:i:o:n:"));
16 int c;
18 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 'c':
22 nclient_threads = ACE_OS::atoi (get_opts.opt_arg ());
23 break;
25 case 'l':
26 corbaloc_arg = get_opts.opt_arg ();
27 break;
29 case 'i':
30 niterations = ACE_OS::atoi (get_opts.opt_arg ());
31 break;
33 case 'o':
34 ior_output_file = get_opts.opt_arg ();
35 break;
37 case 'n':
38 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
39 break;
41 case '?':
42 default:
43 ACE_ERROR_RETURN ((LM_ERROR,
44 "usage: %s "
45 "-c <# client threads> "
46 "-i <# iterations> "
47 "-l <corba loc> "
48 "-n <# server threads> "
49 "-o <iorfile> "
50 "\n",
51 argv [0]),
52 -1);
54 // Indicates successful parsing of the command line
55 return 0;
58 /*****************************************************/
60 /**
61 * Run a server thread
63 * Use the ACE_Task_Base class to run server threads
65 class Worker : public ACE_Task_Base
67 public:
68 /// ctor
69 Worker (CORBA::ORB_ptr orb);
71 /// The thread entry point.
72 virtual int svc ();
74 private:
75 /// The orb
76 CORBA::ORB_var orb_;
79 /*****************************************************/
81 /**
82 * Run a client thread
84 * Use the ACE_Task_Base class to run client threads
86 class SelfClient : public ACE_Task_Base
88 public:
89 SelfClient (CORBA::ORB_ptr orb, Simple_Server_ptr server, int niterations);
90 // ctor
92 virtual int svc ();
93 // The thread entry point.
95 private:
96 void validate_connection ();
97 // Validate the connection
99 private:
100 /// The server.
101 Simple_Server_var server_;
103 CORBA::ORB_var orb_;
105 /// The number of iterations on each client thread.
106 int niterations_;
109 TAO_SYNCH_MUTEX mutex_;
112 /***************************************************/
114 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
118 CORBA::ORB_var orb =
119 CORBA::ORB_init (argc, argv);
121 CORBA::Object_var poa_object =
122 orb->resolve_initial_references ("RootPOA");
124 if (CORBA::is_nil (poa_object.in ()))
125 ACE_ERROR_RETURN ((LM_ERROR,
126 " (%P|%t) Unable to initialize the POA.\n"),
129 PortableServer::POA_var root_poa =
130 PortableServer::POA::_narrow (poa_object.in ());
132 PortableServer::POAManager_var poa_manager =
133 root_poa->the_POAManager ();
135 if (parse_args (argc, argv) != 0)
136 return 1;
138 Simple_Server_i *server_impl = 0;
139 ACE_NEW_RETURN (server_impl,
140 Simple_Server_i (orb.in ()),
141 -1);
143 PortableServer::ServantBase_var owner_transfer(server_impl);
145 PortableServer::ObjectId_var id =
146 root_poa->activate_object (server_impl);
148 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
150 Simple_Server_var server =
151 Simple_Server::_narrow (object.in ());
153 CORBA::String_var ior =
154 orb->object_to_string (server.in ());
156 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
158 // If the ior_output_file exists, output the ior to it
159 if (ior_output_file != 0)
161 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
162 if (output_file == 0)
163 ACE_ERROR_RETURN ((LM_ERROR,
164 "(%P|%t) Cannot open output file for writing IOR: %s",
165 ior_output_file),
167 ACE_OS::fprintf (output_file, "%s", ior.in ());
168 ACE_OS::fclose (output_file);
171 poa_manager->activate ();
173 Worker worker (orb.in ());
174 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
175 nthreads) != 0)
176 ACE_ERROR_RETURN ((LM_ERROR,
177 "(%P|%t) Cannot activate client threads\n"),
180 SelfClient selfabuse (orb.in(), server.in(), niterations);
181 if (selfabuse.activate (THR_NEW_LWP | THR_JOINABLE,
182 nclient_threads) != 0)
183 ACE_ERROR_RETURN ((LM_ERROR,
184 "(%P|%t) Cannot activate abusive threads\n"),
187 selfabuse.thr_mgr()->wait();
189 worker.thr_mgr ()->wait ();
191 ACE_DEBUG ((LM_DEBUG,
192 "(%P|%t) event loop finished\n"));
194 catch (const CORBA::Exception& ex)
196 ex._tao_print_exception ("Exception caught:");
197 return 1;
200 return 0;
203 // ****************************************************************
205 Worker::Worker (CORBA::ORB_ptr orb)
206 : orb_ (CORBA::ORB::_duplicate (orb))
211 Worker::svc ()
215 ACE_Time_Value tv (140, 0);
216 this->orb_->run (tv);
218 catch (const CORBA::Exception&)
221 return 0;
224 // ****************************************************************
226 SelfClient::SelfClient (CORBA::ORB_ptr orb, Simple_Server_ptr server,
227 int niterations)
228 : server_ (Simple_Server::_duplicate (server)),
229 orb_ (CORBA::ORB::_duplicate (orb)),
230 niterations_ (niterations)
234 void
235 SelfClient::validate_connection ()
237 // Ping the object 100 times, ignoring all exceptions.
238 // It would be better to use validate_connection() but the test must
239 // run on minimum CORBA builds too!
240 ACE_GUARD (TAO_SYNCH_MUTEX, guard, mutex_);
242 for (int j = 0; j != 100; ++j)
246 this->server_->test_method (j);
248 catch (const CORBA::Exception&){}
253 SelfClient::svc ()
257 this->validate_connection ();
259 for (int i = 0; i < this->niterations_; ++i)
263 CORBA::Object_var probably_not_exist =
264 orb_->string_to_object (corbaloc_arg);
267 if (CORBA::is_nil (probably_not_exist.in()))
269 ACE_DEBUG ((LM_DEBUG, "not found\n", corbaloc_arg));
271 else
273 Simple_Server_var newserver =
274 Simple_Server::_narrow (probably_not_exist.in ());
276 // should throw an exception
277 if (CORBA::is_nil (newserver.in()))
279 ACE_DEBUG ((LM_DEBUG,
280 "(%P|%t) Not found it\n"));
282 else
284 ACE_DEBUG ((LM_DEBUG,
285 "(%P|%t) Found it\n"));
290 catch (const CORBA::Exception& ex)
292 ex._tao_print_exception ("MT_SelfClient: exception raised");
295 // Just make a call
296 this->server_->test_method (i);
299 catch (const CORBA::Exception& ex)
301 ex._tao_print_exception ("MT_SelfClient: exception raised");
303 return 0;