Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / AVStreams / Latency / ping.cpp
blob6b1135648a4c9f21f443bc855ca9b816d2f4e7a7
1 #include "ping.h"
2 #include "orbsvcs/AV/Protocol_Factory.h"
3 #include "tao/ORB.h"
4 #include "tao/Strategies/advanced_resource.h"
5 #include "ace/Get_Opt.h"
6 #include "ace/High_Res_Timer.h"
7 #include "ace/Stats.h"
8 #include "ace/Throughput_Stats.h"
10 const ACE_TCHAR *ior_output_file = ACE_TEXT ("ping.ior");
11 const char *protocol = "RTP/UDP";
12 int milliseconds = 100;
13 int respond = 1;
14 AVStreams::protocolSpec ping_protocols;
15 AVStreams::protocolSpec pong_protocols;
17 Pong_Send_Callback pong_callback;
19 ACE_hrtime_t recv_base = 0;
20 ACE_Throughput_Stats recv_latency;
22 int
23 parse_args (int argc, ACE_TCHAR *argv[])
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xo:s:r:t:"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'o':
32 ior_output_file = get_opts.opt_arg ();
33 break;
35 case 'r':
37 CORBA::ULong l = ping_protocols.length ();
38 ping_protocols.length (l + 1);
39 ping_protocols[l] = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()));
41 break;
43 case 's':
45 CORBA::ULong l = pong_protocols.length ();
46 pong_protocols.length (l + 1);
47 pong_protocols[l] = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()));
49 break;
51 case 't':
52 milliseconds = ACE_OS::atoi (get_opts.opt_arg ());
53 break;
55 case 'x':
56 respond = 0;
57 break;
59 case '?':
60 default:
61 ACE_ERROR_RETURN ((LM_ERROR,
62 "usage: %s "
63 "-o <iorfile> "
64 "-r <protocol=addr> "
65 "-s <protocol=addr> "
66 "-t <milliseconds> "
67 "\n",
68 argv [0]),
69 -1);
73 // If no protocols are specified use the default...
74 if (ping_protocols.length () == 0)
76 ping_protocols.length (1);
77 ping_protocols[0] = CORBA::string_dup ("UDP=localhost:12345");
80 if (pong_protocols.length () == 0)
82 pong_protocols.length (1);
83 pong_protocols[0] = CORBA::string_dup ("UDP=localhost:23456");
86 // Indicates successful parsing of the command line
87 return 0;
90 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
92 try
94 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
96 parse_args (argc, argv);
98 CORBA::Object_var obj
99 = orb->resolve_initial_references ("RootPOA");
101 PortableServer::POA_var poa
102 = PortableServer::POA::_narrow (obj.in ());
104 PortableServer::POAManager_var mgr
105 = poa->the_POAManager ();
107 mgr->activate ();
109 TAO_AV_CORE::instance ()->init (orb.in (),
110 poa.in ());
112 // Register the video mmdevice object with the ORB
113 Reactive_Strategy *reactive_strategy;
114 ACE_NEW_RETURN (reactive_strategy,
115 Reactive_Strategy,
117 reactive_strategy->init (orb.in (), poa.in ());
118 TAO_MMDevice *mmdevice_impl;
119 ACE_NEW_RETURN (mmdevice_impl,
120 TAO_MMDevice (reactive_strategy),
123 AVStreams::MMDevice_var mmdevice =
124 mmdevice_impl->_this ();
126 CORBA::String_var ior =
127 orb->object_to_string (mmdevice.in ());
129 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
131 // If the ior_output_file exists, output the ior to it
132 if (ior_output_file != 0)
134 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
135 if (output_file == 0)
136 ACE_ERROR_RETURN ((LM_ERROR,
137 "Cannot open output file %s for writing IOR: %C",
138 ior_output_file,
139 ior.in ()),
141 ACE_OS::fprintf (output_file, "%s", ior.in ());
142 ACE_OS::fclose (output_file);
145 Ping_Recv_FDev* ping_fdev_impl;
146 ACE_NEW_RETURN (ping_fdev_impl,
147 Ping_Recv_FDev ("Ping"),
149 Pong_Send_FDev* pong_fdev_impl;
150 ACE_NEW_RETURN (pong_fdev_impl,
151 Pong_Send_FDev ("Pong"),
154 AVStreams::FDev_var ping_fdev =
155 ping_fdev_impl->_this ();
156 AVStreams::FDev_var pong_fdev =
157 pong_fdev_impl->_this ();
159 mmdevice->add_fdev (ping_fdev.in ());
161 if (respond == 1)
163 mmdevice->add_fdev (pong_fdev.in ());
166 orb->run ();
169 ACE_DEBUG ((LM_DEBUG, "Calibrating scale factory . . . "));
170 ACE_High_Res_Timer::global_scale_factor_type gsf =
171 ACE_High_Res_Timer::global_scale_factor ();
172 ACE_DEBUG ((LM_DEBUG, "done %d\n", gsf));
174 recv_latency.dump_results (ACE_TEXT("Receive"), gsf);
176 catch (const CORBA::Exception& ex)
178 ex._tao_print_exception ("Caught exception:");
179 return 1;
182 return 0;
185 // ****************************************************************
187 Ping_Recv::Ping_Recv ()
188 : TAO_FlowConsumer ("Ping",
189 ping_protocols,
190 "UNS:ping")
195 Ping_Recv::get_callback (const char *,
196 TAO_AV_Callback *&callback)
198 ACE_DEBUG ((LM_DEBUG,"Ping_Recv::get_callback\n"));
199 callback = &this->callback_;
200 return 0;
203 Ping_Recv_Callback::Ping_Recv_Callback ()
204 : count_ (0)
209 Ping_Recv_Callback::handle_stop ()
211 ACE_DEBUG ((LM_DEBUG,"Ping_Recv_Callback::stop"));
212 TAO_AV_CORE::instance ()->orb ()->shutdown ();
214 return 0;
218 Ping_Recv_Callback::receive_frame (ACE_Message_Block *frame,
219 TAO_AV_frame_info *,
220 const ACE_Addr &)
222 this->count_++;
224 ACE_DEBUG ((LM_DEBUG,"Ping_Recv_Callback::receive_frame %d\n", this->count_));
226 if (this->count_ < 10)
228 for (const ACE_Message_Block *i = frame;
229 i != 0;
230 i = i->cont ())
232 ACE_hrtime_t stamp;
234 if (i->length () < sizeof(stamp))
235 return 0;
237 ACE_OS::memcpy (&stamp, i->rd_ptr (), sizeof(stamp));
239 ACE_hrtime_t now = ACE_OS::gethrtime ();
240 if (recv_base == 0)
242 recv_base = now;
244 else
246 recv_latency.sample (now - recv_base,
247 now - stamp);
250 if (respond == 1)
251 pong_callback.send_response (stamp);
254 else
255 TAO_AV_CORE::instance ()->orb ()->shutdown ();
256 return 0;
260 Ping_Recv_Callback::handle_destroy ()
262 ACE_DEBUG ((LM_DEBUG,"Ping_Recv_Callback::destroy\n"));
263 return 0;
266 // ****************************************************************
268 Pong_Send::Pong_Send ()
269 : TAO_FlowProducer ("Pong",
270 pong_protocols,
271 "UNS:pong")
276 Pong_Send::get_callback (const char *,
277 TAO_AV_Callback *&callback)
279 ACE_DEBUG ((LM_DEBUG,"Pong_Send::get_callback\n"));
280 callback = &pong_callback;
281 return 0;
284 void
285 Pong_Send_Callback::get_timeout (ACE_Time_Value *&tv,
286 void *&)
288 // @@ ACE_NEW (tv, ACE_Time_Value (0, milliseconds * 1000));
289 ACE_DEBUG ((LM_DEBUG,"Pong_Send_Callback::get_timeout\n"));
290 tv = 0;
294 Pong_Send_Callback::handle_timeout (void *)
296 // ACE_DEBUG ((LM_DEBUG, "pong timeout (ignored)\n"));
297 return 0;
301 Pong_Send_Callback::handle_end_stream ()
303 return 0;
307 Pong_Send_Callback::send_response (ACE_hrtime_t stamp)
309 ACE_DEBUG ((LM_DEBUG, "pong send response)\n"));
311 ACE_hrtime_t buf[2];
313 ACE_Message_Block mb (reinterpret_cast<char*> (buf),
314 sizeof(buf));
316 buf[0] = stamp;
317 buf[1] = ACE_OS::gethrtime ();
318 mb.wr_ptr (sizeof(buf));
320 int result = this->protocol_object_->send_frame (&mb);
321 if (result < 0)
322 ACE_ERROR_RETURN ((LM_ERROR,
323 "Pong_Send_Callback::send - %p\n",
324 ""),
325 -1);
327 return 0;