Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3543_Regression / master.cpp
blob97d4d2f25b70e55d50d3a7974704fa2a579c3461
1 #include "ace/Synch.h"
2 #include "ace/Task.h"
3 #include "masterS.h"
4 #include "tao/ORB.h"
5 #include "ace/Get_Opt.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/Barrier.h"
8 #include <list>
10 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
11 size_t number_of_servers = 300;
12 size_t steps = 10;
13 int n_threads = 10;
14 ACE_Barrier *barrier = 0;
15 CORBA::ORB_var orb;
17 int
18 parse_args (int argc, ACE_TCHAR *argv[])
20 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:c:s:"));
21 int c;
23 while ((c = get_opts ()) != -1)
24 switch (c)
26 case 'o':
27 ior_output_file = get_opts.opt_arg ();
28 break;
29 case 'c':
30 number_of_servers = ACE_OS::atoi(get_opts.opt_arg ());
31 break;
32 case 's':
33 steps = ACE_OS::atoi(get_opts.opt_arg ());
34 break;
36 case '?':
37 default:
38 ACE_ERROR_RETURN ((LM_ERROR,
39 "usage: %s "
40 "-o <ior> "
41 "-c <number of servers> "
42 "-s <steps> "
43 "\n",
44 argv [0]),
45 -1);
47 // Indicates successful parsing of the command line
48 return 0;
51 static TAO_SYNCH_MUTEX mutex;
52 std::list<MasterClient::Server_var> servers;
53 bool started = false;
55 class MasterImpl : public POA_MasterClient::Master
57 public:
58 MasterImpl()
62 virtual void registerServer(MasterClient::Server_ptr c)
64 ACE_GUARD(TAO_SYNCH_MUTEX, mon, mutex);
65 servers.push_back(MasterClient::Server::_duplicate(c));
66 if (servers.size () == number_of_servers)
68 started = true;
70 else
72 ACE_DEBUG ((LM_DEBUG, "%d server registered with master\n", servers.size()));
77 void Dispatcher_shutdown(void)
79 std::list<MasterClient::Server_var> copiedlist;
81 ACE_GUARD(TAO_SYNCH_MUTEX, mon, mutex);
82 copiedlist = servers;
84 std::list<MasterClient::Server_var>::iterator it;
85 for(it = copiedlist.begin(); it != copiedlist.end(); ++it)
87 try
89 (*it)->shutdown();
91 catch(...)
97 void Dispatcher_step(int id, int i)
99 std::list<MasterClient::Server_var> copiedlist;
101 ACE_GUARD(TAO_SYNCH_MUTEX, mon, mutex);
102 copiedlist = servers;
105 ACE_DEBUG ((LM_DEBUG, "Id %d Sending %d pings in iteration %d\n", id, copiedlist.size(), i));
106 int ok = 0;
107 int timeout = 0;
108 int transient = 0;
109 int corba = 0;
110 int unknown = 0;
112 std::list<MasterClient::Server_var>::iterator it;
113 for(it = copiedlist.begin(); it != copiedlist.end(); ++it)
117 (*it)->ping();
118 ++ok;
120 catch(CORBA::TIMEOUT &)
122 ++timeout;
124 catch(CORBA::TRANSIENT &)
126 ++transient;
128 catch(CORBA::Exception &e)
130 ACE_ERROR ((LM_ERROR, "ERROR: Caught CORBA exception %C", e._info ().c_str()));
131 ++corba;
133 catch(...)
135 ++unknown;
138 if(ok)
140 ACE_ERROR ((LM_ERROR, "Ok: %d\n", ok));
142 if(timeout)
144 ACE_ERROR ((LM_ERROR, "TIMEOUT: %d\n", timeout));
146 if(transient)
148 ACE_ERROR ((LM_ERROR, "TRANSIENT: %d\n", transient));
150 if(corba)
152 ACE_ERROR ((LM_ERROR, "CORBA: %d\n", corba));
154 if(unknown)
156 ACE_ERROR ((LM_ERROR, "UNKNOWN: %d\n", unknown));
160 void Dispatcher_run(int id)
162 size_t i = 0;
163 while(1)
165 if(started)
167 Dispatcher_step (id, i);
168 ++i;
170 if (i >= steps)
172 ACE_DEBUG ((LM_DEBUG, "Id %d ready\n", id));
173 break;
175 ACE_OS::sleep(1);
177 ACE_DEBUG ((LM_DEBUG, "Waiting for barrier\n"));
178 barrier->wait ();
179 if (id == 0)
181 ACE_DEBUG ((LM_DEBUG, "Shutting down ORB\n"));
182 orb->shutdown (1);
187 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
191 orb = CORBA::ORB_init (argc, argv);
193 if (parse_args (argc, argv) != 0)
194 return 1;
196 CORBA::Object_var poa_object =
197 orb->resolve_initial_references("RootPOA");
199 PortableServer::POA_var root_poa =
200 PortableServer::POA::_narrow (poa_object.in ());
202 if (CORBA::is_nil (root_poa.in ()))
203 ACE_ERROR_RETURN ((LM_ERROR,
204 " (%P|%t) Panic: nil RootPOA\n"),
207 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
209 MasterImpl *hello_impl = 0;
210 ACE_NEW_RETURN (hello_impl,
211 MasterImpl,
213 PortableServer::ServantBase_var owner_transfer(hello_impl);
215 PortableServer::ObjectId_var id =
216 root_poa->activate_object (hello_impl);
218 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
220 MasterClient::Master_var hello = MasterClient::Master::_narrow (object.in ());
222 poa_manager->activate ();
224 CORBA::String_var ior = orb->object_to_string (hello.in ());
226 // Output the IOR to the <ior_output_file>
227 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
228 if (output_file == 0)
229 ACE_ERROR_RETURN ((LM_ERROR,
230 "Cannot open output file for writing IOR: %s\n",
231 ior_output_file),
233 ACE_OS::fprintf (output_file, "%s", ior.in ());
234 ACE_OS::fclose (output_file);
236 barrier = new ACE_Barrier (n_threads);
238 ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance ();
240 for(int i = 0; i < n_threads; ++i)
242 thr_mgr->spawn_n (1, ACE_THR_FUNC (Dispatcher_run), reinterpret_cast<void *> (i), THR_NEW_LWP | THR_DETACHED);
245 orb->run();
247 ACE_DEBUG ((LM_DEBUG, "Orb shutdown\n"));
249 thr_mgr->wait ();
251 ACE_DEBUG ((LM_DEBUG, "Worker threads ready\n"));
253 Dispatcher_shutdown ();
255 servers.clear ();
257 root_poa->destroy (1, 1);
259 orb->destroy ();
261 orb = CORBA::ORB::_nil ();
263 delete barrier;
265 catch (const CORBA::Exception& ex)
267 ex._tao_print_exception ("Exception caught:");
268 return 1;
271 return 0;