Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / Big_Twoways / server.cpp
blob845bf6af72c9c9912d13ba9983ebf580f06342ad
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;
85 ACE_NEW_RETURN (coordinator_impl,
86 Coordinator (peer_count),
87 1);
89 PortableServer::ObjectId_var id =
90 root_poa->activate_object (coordinator_impl);
92 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
94 Test::Coordinator_var coordinator =
95 Test::Coordinator::_narrow (object.in ());
97 CORBA::String_var ior =
98 orb->object_to_string (coordinator.in ());
100 // If the ior_output_file exists, output the ior to it
101 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
102 if (output_file == 0)
103 ACE_ERROR_RETURN ((LM_ERROR,
104 "Cannot open output file for writing IOR: %s",
105 ior_output_file),
107 ACE_OS::fprintf (output_file, "%s", ior.in ());
108 ACE_OS::fclose (output_file);
110 poa_manager->activate ();
112 ACE_DEBUG ((LM_DEBUG, "Waiting for peers . . . "));
113 for (int i = 0;
114 i != 30 && !coordinator_impl->has_all_peers ();
115 ++i)
117 ACE_Time_Value tv (1, 0);
118 orb->run (tv);
120 ACE_DEBUG ((LM_DEBUG, "done.\n"));
122 if (!coordinator_impl->has_all_peers ())
124 ACE_ERROR ((LM_DEBUG,
125 "ERROR: timeout, some peers failed to register\n"));
126 return 1;
129 ACE_DEBUG ((LM_DEBUG, "Building session list . . . "));
131 Session_Control *session_control_impl = 0;
132 ACE_NEW_RETURN (session_control_impl,
133 Session_Control (peer_count),
136 id = root_poa->activate_object (session_control_impl);
138 object = root_poa->id_to_reference (id.in ());
140 Test::Session_Control_var session_control =
141 Test::Session_Control::_narrow (object.in ());
143 Test::Session_List session_list;
144 coordinator_impl->create_session_list (session_control.in (),
145 payload_size,
146 thread_count,
147 message_count,
148 session_list);
150 ACE_ASSERT (session_list.length () == peer_count);
151 ACE_DEBUG ((LM_DEBUG, "done.\n"));
153 ACE_DEBUG ((LM_DEBUG, "Giving start signal . . . "));
154 CORBA::ULong j;
155 for (j = 0; j != peer_count; ++j)
157 // Make a copy of the sessions, excluding the j-th element
158 Test::Session_List other_sessions (peer_count - 1);
159 other_sessions.length (peer_count - 1);
160 CORBA::ULong count = 0;
161 for (CORBA::ULong k = 0; k != peer_count; ++k)
163 if (k == j)
164 continue;
165 other_sessions[count++] =
166 Test::Session::_duplicate (session_list[k]);
169 session_list[j]->start (other_sessions);
172 ACE_DEBUG ((LM_DEBUG, "done.\n"));
174 ACE_DEBUG ((LM_DEBUG, "Waiting for sessions . . .\n"));
175 for (int k = 0;
176 k != 300 && !session_control_impl->all_sessions_finished ();
177 ++k)
179 ACE_Time_Value tv (1, 0);
180 orb->run (tv);
183 if (!session_control_impl->all_sessions_finished ())
185 ACE_ERROR ((LM_ERROR,
186 "ERROR: timeout waiting for sessions\n"));
187 return 1;
190 ACE_DEBUG ((LM_DEBUG, "All sessions finished . . .\n"));
192 for (j = 0; j != peer_count; ++j)
194 session_list[j]->destroy ();
197 ACE_DEBUG ((LM_DEBUG, "Shutdown all peers . . .\n"));
199 coordinator_impl->shutdown_all_peers ();
200 ACE_OS::sleep (5); // Allow the shutdown message to be processed.
201 coordinator_impl->_remove_ref();
202 session_control_impl->_remove_ref();
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;