=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / Big_Request_Muxing / server.cpp
blob1e5c69be2c435dc3c580b454437723d69bae01d3
1 #include "Payload_Receiver.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/OS_NS_sys_time.h"
6 namespace
8 // defaults only
9 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
10 int expected = 200;
11 int maybe_lost = 400;
14 int
15 parse_args (int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:e:l:"));
18 int c;
20 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 'o':
24 ior_output_file = get_opts.opt_arg ();
25 break;
26 case 'e':
27 expected = ACE_OS::atoi(get_opts.opt_arg());
28 break;
29 case 'l':
30 maybe_lost = ACE_OS::atoi(get_opts.opt_arg());
31 break;
32 default:
33 ACE_ERROR_RETURN ((LM_ERROR,
34 "usage: %s "
35 "-o <iorfile> [-e <expected>] [-l <maybe_lost>]"
36 "\n",
37 argv [0]),
38 -1);
40 // Indicates successful parsing of the command line
41 return 0;
44 int
45 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
47 ACE_DEBUG ((LM_DEBUG, "(%P) Starting server\n"));
49 try
51 CORBA::ORB_var orb =
52 CORBA::ORB_init (argc, argv);
54 CORBA::Object_var poa_object =
55 orb->resolve_initial_references ("RootPOA");
57 if (CORBA::is_nil (poa_object.in ()))
59 ACE_ERROR_RETURN ((LM_ERROR,
60 "(%P) Server: Unable to initialize the POA.\n"),
61 1);
64 PortableServer::POA_var root_poa =
65 PortableServer::POA::_narrow (poa_object.in ());
67 PortableServer::POAManager_var poa_manager =
68 root_poa->the_POAManager ();
70 if (parse_args (argc, argv) != 0)
72 return 1;
75 Payload_Receiver *payload_receiver_impl;
76 ACE_NEW_RETURN (payload_receiver_impl,
77 Payload_Receiver (),
78 1);
79 PortableServer::ServantBase_var
80 receiver_owner_transfer (payload_receiver_impl);
82 PortableServer::ObjectId_var id =
83 root_poa->activate_object (payload_receiver_impl);
85 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
87 Test::Payload_Receiver_var payload_receiver =
88 Test::Payload_Receiver::_narrow (object.in ());
90 CORBA::String_var ior =
91 orb->object_to_string (payload_receiver.in ());
93 // If the ior_output_file exists, output the ior to it
94 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
95 if (output_file == 0)
97 ACE_ERROR_RETURN ((LM_ERROR,
98 "(%P) Server: Cannot open output file for writing IOR: %s",
99 ior_output_file),
102 ACE_OS::fprintf (output_file, "%s", ior.in ());
103 ACE_OS::fclose (output_file);
105 poa_manager->activate ();
107 ACE_DEBUG ((LM_DEBUG,
108 "(%P) Server waiting for %d (+%d possible) messages...\n",
109 expected, maybe_lost));
111 const ACE_Time_Value start_time = ACE_OS::gettimeofday ();
112 ACE_Time_Value end_time = start_time + ACE_Time_Value (27);
113 ACE_Time_Value runtime;
114 int count = 0;
115 int maybe_lost_count = 0;
116 bool stalled = false;
117 while (payload_receiver_impl->count () < expected ||
118 payload_receiver_impl->count (true) < maybe_lost)
120 int prev_count = count;
121 int prev_maybe_lost_count = maybe_lost_count;
122 ACE_Time_Value tv (0, 100 * 1000);
123 orb->run (tv);
124 count = payload_receiver_impl->count ();
125 maybe_lost_count = payload_receiver_impl->count (true);
126 if ((count == prev_count) ||
127 (maybe_lost_count == prev_maybe_lost_count))
129 if (!stalled)
131 stalled = true;
132 end_time += ACE_Time_Value (3);
133 runtime = ACE_OS::gettimeofday () - start_time;
135 else if (ACE_OS::gettimeofday () > end_time)
137 if (count < expected)
139 ACE_DEBUG ((LM_DEBUG,
140 "(%P) Server: The clients stalled out after %d messages\n",
141 count));
143 break; // Abort waiting for more messages
146 else
148 stalled = false;
152 if (!stalled)
154 runtime = ACE_OS::gettimeofday () - start_time;
157 int result = 0;
159 if (count != expected)
161 ACE_ERROR ((LM_ERROR,
162 "(%P) Server did not receive all of the SYNC_WITH_TARGET messages\n"));
163 result = 1;
166 ACE_DEBUG ((LM_DEBUG,
167 "(%P) Server got %d of %d SYNC_WITH_TARGET messages\n"
168 " and %d of %d SYNC_WITH_TRANSPORT or SYNC_NONE messages\n"
169 " in %d.%06d sec\n",
170 count, expected,
171 maybe_lost_count, maybe_lost,
172 runtime.sec(), runtime.usec()));
174 root_poa->destroy (true, true);
176 orb->destroy ();
178 ACE_DEBUG ((LM_DEBUG, "(%P) Ending server (result %d)\n", result));
179 return result;
181 catch (const CORBA::Exception& ex)
183 ACE_DEBUG ((LM_DEBUG, "(%P) Server terminated by: "));
184 ex._tao_print_exception ("CORBA Exception:");
186 catch (...)
188 ACE_DEBUG ((LM_DEBUG, "(%P) Server terminated by unknown exception\n"));
191 return 1;