Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Muxed_GIOP_Versions / server.cpp
blob2e4dce3e7ddaf5489720a788c1f9a0b1c805676f
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 (void);
74 private:
76 /// The orb
77 CORBA::ORB_var orb_;
80 /*****************************************************/
82 /**
83 * Run a client thread
85 * Use the ACE_Task_Base class to run client threads
87 class SelfClient : public ACE_Task_Base
89 public:
90 SelfClient (CORBA::ORB_ptr orb, Simple_Server_ptr server, int niterations);
91 // ctor
93 virtual int svc (void);
94 // The thread entry point.
96 private:
97 void validate_connection (void);
98 // Validate the connection
100 private:
102 /// The server.
103 Simple_Server_var server_;
105 CORBA::ORB_var orb_;
107 /// The number of iterations on each client thread.
108 int niterations_;
111 TAO_SYNCH_MUTEX mutex_;
114 /***************************************************/
116 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
120 CORBA::ORB_var orb =
121 CORBA::ORB_init (argc, argv);
123 CORBA::Object_var poa_object =
124 orb->resolve_initial_references ("RootPOA");
126 if (CORBA::is_nil (poa_object.in ()))
127 ACE_ERROR_RETURN ((LM_ERROR,
128 " (%P|%t) Unable to initialize the POA.\n"),
131 PortableServer::POA_var root_poa =
132 PortableServer::POA::_narrow (poa_object.in ());
134 PortableServer::POAManager_var poa_manager =
135 root_poa->the_POAManager ();
137 if (parse_args (argc, argv) != 0)
138 return 1;
140 Simple_Server_i *server_impl = 0;
141 ACE_NEW_RETURN (server_impl,
142 Simple_Server_i (orb.in ()),
143 -1);
145 PortableServer::ServantBase_var owner_transfer(server_impl);
147 PortableServer::ObjectId_var id =
148 root_poa->activate_object (server_impl);
150 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
152 Simple_Server_var server =
153 Simple_Server::_narrow (object.in ());
155 CORBA::String_var ior =
156 orb->object_to_string (server.in ());
158 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
160 // If the ior_output_file exists, output the ior to it
161 if (ior_output_file != 0)
163 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
164 if (output_file == 0)
165 ACE_ERROR_RETURN ((LM_ERROR,
166 "(%P|%t) Cannot open output file for writing IOR: %s",
167 ior_output_file),
169 ACE_OS::fprintf (output_file, "%s", ior.in ());
170 ACE_OS::fclose (output_file);
173 poa_manager->activate ();
175 Worker worker (orb.in ());
176 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
177 nthreads) != 0)
178 ACE_ERROR_RETURN ((LM_ERROR,
179 "(%P|%t) Cannot activate client threads\n"),
182 SelfClient selfabuse (orb.in(), server.in(), niterations);
183 if (selfabuse.activate (THR_NEW_LWP | THR_JOINABLE,
184 nclient_threads) != 0)
185 ACE_ERROR_RETURN ((LM_ERROR,
186 "(%P|%t) Cannot activate abusive threads\n"),
189 selfabuse.thr_mgr()->wait();
191 worker.thr_mgr ()->wait ();
193 ACE_DEBUG ((LM_DEBUG,
194 "(%P|%t) event loop finished\n"));
196 catch (const CORBA::Exception& ex)
198 ex._tao_print_exception ("Exception caught:");
199 return 1;
202 return 0;
205 // ****************************************************************
207 Worker::Worker (CORBA::ORB_ptr orb)
208 : orb_ (CORBA::ORB::_duplicate (orb))
213 Worker::svc (void)
217 ACE_Time_Value tv (140, 0);
218 this->orb_->run (tv);
220 catch (const CORBA::Exception&)
223 return 0;
226 // ****************************************************************
228 SelfClient::SelfClient (CORBA::ORB_ptr orb, Simple_Server_ptr server,
229 int niterations)
230 : server_ (Simple_Server::_duplicate (server)),
231 orb_ (CORBA::ORB::_duplicate (orb)),
232 niterations_ (niterations)
236 void
237 SelfClient::validate_connection (void)
239 // Ping the object 100 times, ignoring all exceptions.
240 // It would be better to use validate_connection() but the test must
241 // run on minimum CORBA builds too!
242 ACE_GUARD (TAO_SYNCH_MUTEX, guard, mutex_);
244 for (int j = 0; j != 100; ++j)
248 this->server_->test_method (j);
250 catch (const CORBA::Exception&){}
255 SelfClient::svc (void)
259 this->validate_connection ();
261 for (int i = 0; i < this->niterations_; ++i)
265 CORBA::Object_var probably_not_exist =
266 orb_->string_to_object (corbaloc_arg);
269 if (CORBA::is_nil (probably_not_exist.in()))
271 ACE_DEBUG ((LM_DEBUG, "not found\n", corbaloc_arg));
273 else
275 Simple_Server_var newserver =
276 Simple_Server::_narrow (probably_not_exist.in ());
278 // should throw an exception
279 if (CORBA::is_nil (newserver.in()))
281 ACE_DEBUG ((LM_DEBUG,
282 "(%P|%t) Not found it\n"));
285 else
287 ACE_DEBUG ((LM_DEBUG,
288 "(%P|%t) Found it\n"));
293 catch (const CORBA::Exception& ex)
295 ex._tao_print_exception ("MT_SelfClient: exception raised");
298 // Just make a call
299 this->server_->test_method (i);
303 catch (const CORBA::Exception& ex)
305 ex._tao_print_exception ("MT_SelfClient: exception raised");
307 return 0;