Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / Big_Oneways / server.cpp
blob6730ae08be9077ad7014f8b5556aab32a0549338
1 #include "Coordinator.h"
2 #include "Session_Control.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_unistd.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
8 CORBA::ULong peer_count = 4;
9 CORBA::ULong payload_size = 1024;
10 CORBA::ULong message_count = 1000;
11 CORBA::ULong thread_count = 4;
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:p:b:i:n:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'o':
23 ior_output_file = get_opts.opt_arg ();
24 break;
26 case 'p':
27 peer_count = ACE_OS::atoi (get_opts.opt_arg ());
28 break;
30 case 'b':
31 payload_size = ACE_OS::atoi (get_opts.opt_arg ());
32 break;
34 case 'i':
35 message_count = ACE_OS::atoi (get_opts.opt_arg ());
36 break;
38 case 'n':
39 thread_count = ACE_OS::atoi (get_opts.opt_arg ());
40 break;
42 case '?':
43 default:
44 ACE_ERROR_RETURN ((LM_ERROR,
45 "usage: %s "
46 "-o <iorfile> "
47 "-p <peer_count> "
48 "-b <payload_size> "
49 "-i <message_count> "
50 "-n <thread_count> "
51 "\n",
52 argv [0]),
53 -1);
55 // Indicates successful parsing of the command line
56 return 0;
59 int
60 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
62 try
64 CORBA::ORB_var orb =
65 CORBA::ORB_init (argc, argv);
67 CORBA::Object_var poa_object =
68 orb->resolve_initial_references("RootPOA");
70 PortableServer::POA_var root_poa =
71 PortableServer::POA::_narrow (poa_object.in ());
73 if (CORBA::is_nil (poa_object.in ()))
74 ACE_ERROR_RETURN ((LM_ERROR,
75 " (%P|%t) Panic got a nil RootPOA\n"),
76 1);
78 PortableServer::POAManager_var poa_manager =
79 root_poa->the_POAManager ();
81 if (parse_args (argc, argv) != 0)
82 return 1;
84 Coordinator *coordinator_impl = 0;
85 ACE_NEW_RETURN (coordinator_impl,
86 Coordinator (peer_count),
87 1);
88 PortableServer::ServantBase_var coordinator_owner (coordinator_impl);
90 PortableServer::ObjectId_var id =
91 root_poa->activate_object (coordinator_impl);
93 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
95 Test::Coordinator_var coordinator =
96 Test::Coordinator::_narrow (object.in ());
98 poa_manager->activate ();
100 CORBA::String_var ior =
101 orb->object_to_string (coordinator.in ());
103 // If the ior_output_file exists, output the ior to it
104 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
105 if (output_file == 0)
106 ACE_ERROR_RETURN ((LM_ERROR,
107 "Cannot open output file for writing IOR: %s",
108 ior_output_file),
110 ACE_OS::fprintf (output_file, "%s", ior.in ());
111 ACE_OS::fclose (output_file);
113 ACE_DEBUG ((LM_DEBUG, "Waiting for peers . . . "));
114 for (int i = 0;
115 i != 60 && !coordinator_impl->has_all_peers ();
116 ++i)
118 ACE_Time_Value tv (1, 0);
119 orb->run (tv);
121 ACE_DEBUG ((LM_DEBUG, "done.\n"));
123 if (!coordinator_impl->has_all_peers ())
125 ACE_ERROR ((LM_DEBUG,
126 "ERROR: timeout, some peers failed to register\n"));
127 return 1;
130 ACE_DEBUG ((LM_DEBUG, "Building session list . . . "));
132 Session_Control *session_control_impl = 0;
133 ACE_NEW_RETURN (session_control_impl,
134 Session_Control (peer_count),
136 PortableServer::ServantBase_var session_control_owner (session_control_impl);
138 PortableServer::ObjectId_var idu =
139 root_poa->activate_object (session_control_impl);
141 CORBA::Object_var objectu = root_poa->id_to_reference (idu.in ());
143 Test::Session_Control_var session_control =
144 Test::Session_Control::_narrow (objectu.in ());
146 Test::Session_List session_list;
147 coordinator_impl->create_session_list (session_control.in (),
148 payload_size,
149 thread_count,
150 message_count,
151 session_list);
153 ACE_ASSERT (session_list.length () == peer_count);
155 ACE_DEBUG ((LM_DEBUG, "done.\n"));
156 ACE_DEBUG ((LM_DEBUG, "Giving start signal . . . "));
158 CORBA::ULong j;
159 for (j = 0; j != peer_count; ++j)
161 // Make a copy of the sessions, excluding the j-th element
162 Test::Session_List other_sessions (peer_count - 1);
163 other_sessions.length (peer_count - 1);
164 CORBA::ULong count = 0;
165 for (CORBA::ULong k = 0; k != peer_count; ++k)
167 if (k == j)
168 continue;
169 other_sessions[count++] =
170 Test::Session::_duplicate (session_list[k]);
173 session_list[j]->start (other_sessions);
176 ACE_DEBUG ((LM_DEBUG ,"done\n"));
177 ACE_DEBUG ((LM_DEBUG, "Waiting for sessions to finish. . .\n"));
178 for (int k = 0;
179 k != 300 && !session_control_impl->all_sessions_finished ();
180 ++k)
182 ACE_Time_Value tv (1, 0);
183 orb->run (tv);
186 if (!session_control_impl->all_sessions_finished ())
188 ACE_ERROR ((LM_ERROR,
189 "ERROR: timeout waiting for sessions\n"));
190 return 1;
193 ACE_DEBUG ((LM_DEBUG, "All sessions finished, destroy session list . . .\n"));
195 for (j = 0; j != peer_count; ++j)
197 session_list[j]->destroy ();
200 ACE_DEBUG ((LM_DEBUG, "Shutdown all peers . . .\n"));
202 coordinator_impl->shutdown_all_peers ();
203 ACE_OS::sleep (5); // Allow the shutdown message to be processed.
204 ACE_DEBUG ((LM_DEBUG, "Shutdown poa and orb . . .\n"));
206 root_poa->destroy (true, true);
208 orb->destroy ();
210 catch (const CORBA::Exception& ex)
212 ex._tao_print_exception ("Exception caught:");
213 return 1;
216 return 0;