3 #include "ace/Get_Opt.h"
4 #include "ace/High_Res_Timer.h"
6 typedef ACE_Unmanaged_Singleton
<Sender
, ACE_Null_Mutex
> SENDER
;
7 // Create a singleton instance of the Sender.
9 static FILE *output_file
= 0;
10 // File handle of the file into which received data is written.
12 static const ACE_TCHAR
*output_file_name
= ACE_TEXT ("output");
13 // File name of the file into which received data is written.
17 Sender_StreamEndPoint::get_callback (const char *,
18 TAO_AV_Callback
*&callback
)
20 // Create and return the sender application callback to AVStreams
21 // for further upcalls.
22 callback
= &this->callback_
;
27 Sender_StreamEndPoint::set_protocol_object (const char *,
28 TAO_AV_Protocol_Object
*object
)
30 // Set the sender protocol object corresponding to the transport
32 SENDER::instance ()->protocol_object (object
);
36 Sender_Callback::Sender_Callback ()
42 Sender_Callback::receive_frame (ACE_Message_Block
*frame
,
47 // Upcall from the AVStreams when there is data to be received from
51 "Sender_Callback::receive_frame for frame %d\n",
52 this->frame_count_
++));
56 // Write the received data to the file.
58 ACE_OS::fwrite (frame
->rd_ptr (),
63 if (result
== frame
->length ())
64 ACE_ERROR_RETURN ((LM_ERROR
,
65 "Sender_Callback::fwrite failed\n"),
68 frame
= frame
->cont ();
71 if (SENDER::instance ()->eof () == 1)
72 SENDER::instance ()->shutdown ();
77 : sender_mmdevice_ (0),
90 Sender::protocol_object (TAO_AV_Protocol_Object
*object
)
92 // Set the sender protocol object corresponding to the transport
94 this->protocol_object_
= object
;
108 // File reading is complete, destroy the stream.
109 AVStreams::flowSpec stop_spec
;
110 this->streamctrl_
->destroy (stop_spec
);
112 // Shut the orb down.
113 TAO_AV_CORE::instance ()->orb ()->shutdown (0);
115 catch (const CORBA::Exception
& ex
)
117 ex
._tao_print_exception ("shutdown\n");
122 Sender::parse_args (int argc
,
125 // Parse command line arguments
126 ACE_Get_Opt
opts (argc
, argv
, ACE_TEXT("f:p:r:d"));
129 while ((c
= opts ()) != -1)
134 this->filename_
= ACE_TEXT_ALWAYS_CHAR (opts
.opt_arg ());
137 this->protocol_
= ACE_TEXT_ALWAYS_CHAR (opts
.opt_arg ());
140 this->frame_rate_
= ACE_OS::atoi (opts
.opt_arg ());
146 ACE_DEBUG ((LM_DEBUG
, "Unknown Option\n"));
153 // Method to get the object reference of the receiver
155 Sender::bind_to_receiver ()
157 CosNaming::Name
name (1);
160 CORBA::string_dup ("Receiver");
162 // Resolve the receiver object reference from the Naming Service
163 CORBA::Object_var receiver_mmdevice_obj
=
164 this->naming_client_
->resolve (name
);
166 this->receiver_mmdevice_
=
167 AVStreams::MMDevice::_narrow (receiver_mmdevice_obj
.in ());
169 if (CORBA::is_nil (this->receiver_mmdevice_
.in ()))
170 ACE_ERROR_RETURN ((LM_ERROR
,
171 "Could not resolve Receiver_MMdevice in Naming service\n"),
178 Sender::init (int argc
,
181 // Initialize the endpoint strategy with the orb and poa.
183 this->endpoint_strategy_
.init (TAO_AV_CORE::instance ()->orb (),
184 TAO_AV_CORE::instance ()->poa ());
188 // Initialize the naming services
190 this->naming_client_
.init (TAO_AV_CORE::instance ()->orb ());
194 // Parse the command line arguments
196 this->parse_args (argc
,
201 // Open file to read.
203 ACE_OS::fopen (this->filename_
.c_str (),
206 if (this->input_file_
== 0)
207 ACE_ERROR_RETURN ((LM_DEBUG
,
208 "Cannot open input file %C\n",
209 this->filename_
.c_str ()),
212 ACE_DEBUG ((LM_DEBUG
,
213 "File opened successfully\n"));
215 // Resolve the object reference of the receiver from the Naming Service.
216 result
= this->bind_to_receiver ();
219 ACE_ERROR_RETURN ((LM_ERROR
,
220 "(%P|%t) Error binding to the naming service\n"),
224 // Initialize the QoS
225 AVStreams::streamQoS_var
the_qos (new AVStreams::streamQoS
);
227 // Create the forward flow specification to describe the flow.
228 TAO_Forward_FlowSpec_Entry
entry ("Data_Receiver",
232 this->protocol_
.c_str (),
235 AVStreams::flowSpec
flow_spec (1);
236 flow_spec
.length (2);
237 flow_spec
[0] = CORBA::string_dup (entry
.entry_to_string ());
239 // Create the forward flow specification to describe the flow.
240 TAO_Forward_FlowSpec_Entry
entry1 ("Data_Receiver1",
244 this->protocol_
.c_str (),
247 flow_spec
[1] = CORBA::string_dup (entry1
.entry_to_string ());
249 // Register the sender mmdevice object with the ORB
250 ACE_NEW_RETURN (this->sender_mmdevice_
,
251 TAO_MMDevice (&this->endpoint_strategy_
),
254 // Servant Reference Counting to manage lifetime
255 PortableServer::ServantBase_var safe_mmdevice
=
256 this->sender_mmdevice_
;
258 AVStreams::MMDevice_var mmdevice
=
259 this->sender_mmdevice_
->_this ();
261 ACE_NEW_RETURN (this->streamctrl_
,
265 PortableServer::ServantBase_var safe_streamctrl
=
268 // Bind/Connect the sender and receiver MMDevices.
269 CORBA::Boolean bind_result
=
270 this->streamctrl_
->bind_devs (mmdevice
.in (),
271 this->receiver_mmdevice_
.in (),
275 if (bind_result
== 0)
276 ACE_ERROR_RETURN ((LM_ERROR
,
277 "streamctrl::bind_devs failed\n"),
283 // Method to send data at the specified rate
287 // The time that should lapse between two consecutive frames sent.
288 ACE_Time_Value inter_frame_time
;
290 // The time between two consecutive frames.
291 inter_frame_time
.set (1 / (double) this->frame_rate_
);
293 if (TAO_debug_level
> 0)
294 ACE_DEBUG ((LM_DEBUG
,
295 "Frame Rate = %d / second\n"
296 "Inter Frame Time = %d (msec)\n",
298 inter_frame_time
.msec ()));
302 // The time taken for sending a frame and preparing for the next frame
303 ACE_High_Res_Timer elapsed_timer
;
305 // Continue to send data till the file is read to the end.
308 // Read from the file into a message block.
309 int n
= ACE_OS::fread (this->mb_
.wr_ptr (),
315 ACE_ERROR_RETURN ((LM_ERROR
,
316 "Sender::pace_data fread failed\n"),
321 // At end of file break the loop and end the sender.
322 ACE_DEBUG ((LM_DEBUG
,"Handle_Start:End of file\n"));
327 this->mb_
.wr_ptr (n
);
329 if (this->frame_count_
> 1)
332 // Second frame and beyond
335 // Stop the timer that was started just before the previous frame was sent.
336 elapsed_timer
.stop ();
338 // Get the time elapsed after sending the previous frame.
339 ACE_Time_Value elapsed_time
;
340 elapsed_timer
.elapsed_time (elapsed_time
);
342 if (TAO_debug_level
> 0)
343 ACE_DEBUG ((LM_DEBUG
,
344 "Elapsed Time = %d\n",
345 elapsed_time
.msec ()));
347 // Check to see if the inter frame time has elapsed.
348 if (elapsed_time
< inter_frame_time
)
350 // Inter frame time has not elapsed.
352 // Calculate the time to wait before the next frame needs to be sent.
353 ACE_Time_Value
wait_time (inter_frame_time
- elapsed_time
);
355 if (TAO_debug_level
> 0)
356 ACE_DEBUG ((LM_DEBUG
,
360 // Run the orb for the wait time so the sender can
361 // continue other orb requests.
362 TAO_AV_CORE::instance ()->orb ()->run (wait_time
);
366 // Start timer before sending the frame.
367 elapsed_timer
.start ();
371 this->protocol_object_
->send_frame (&this->mb_
);
374 ACE_ERROR_RETURN ((LM_ERROR
,
376 "Sender::pace_data send\n"),
379 ACE_DEBUG ((LM_DEBUG
,
380 "Sender::pace_data frame %d was sent successfully\n",
381 ++this->frame_count_
));
383 // Reset the message block.
387 // File reading is complete, destroy the stream.
388 AVStreams::flowSpec stop_spec
;
389 this->streamctrl_
->destroy (stop_spec
);
391 // Shut the orb down.
392 //TAO_AV_CORE::instance ()->orb ()->shutdown (1,
395 catch (const CORBA::Exception
&)
397 //ACE_PRINT_EXCEPTION (ex,
398 // "Sender::pace_data Failed\n");
411 CORBA::ORB_init (argc
, argv
);
413 CORBA::Object_var obj
414 = orb
->resolve_initial_references ("RootPOA");
416 // Get the POA_var object from Object_var
417 PortableServer::POA_var root_poa
418 = PortableServer::POA::_narrow (obj
.in ());
420 PortableServer::POAManager_var mgr
421 = root_poa
->the_POAManager ();
425 // Initialize the AV Stream components.
426 /* TAO_AV_CORE::instance ()->init (orb.in (),
429 // Initialize the AVStreams components.
430 TAO_AV_CORE::instance ()->init (orb
.in (), root_poa
.in ());
432 // Initialize the Sender.
434 result
= SENDER::instance ()->init (argc
,
438 ACE_ERROR_RETURN ((LM_ERROR
,
439 "Sender::init failed\n"),
442 // Make sure we have a valid <output_file>
443 output_file
= ACE_OS::fopen (output_file_name
,
445 if (output_file
== 0)
446 ACE_ERROR_RETURN ((LM_DEBUG
,
447 "Cannot open output file %s\n",
452 ACE_DEBUG ((LM_DEBUG
,
453 "File Opened Successfully\n"));
455 // Start sending data.
456 result
= SENDER::instance ()->pace_data ();
457 ACE_Time_Value
tv(3,0);
460 catch (const CORBA::Exception
& ex
)
462 ex
._tao_print_exception ("Sender Failed\n");
466 SENDER::close (); // Explicitly finalize the Unmanaged_Singleton.
471 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
472 template ACE_Unmanaged_Singleton
<Sender
, ACE_Null_Mutex
> *ACE_Unmanaged_Singleton
<Sender
, ACE_Null_Mutex
>::singleton_
;
473 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */