Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / AVStreams / Multiple_Flows / receiver.cpp
blobe7989d81d56820d1db25019fe1530087f9a9e029
1 #include "receiver.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/High_Res_Timer.h"
5 int endstream = 0;
7 // Create a singleton instance of the Sender.
9 // An Unmanaged_Singleton is used to avoid static object destruction
10 // order related problems since the underlying singleton object
11 // contains references to static TypeCodes.
12 typedef ACE_Unmanaged_Singleton<Receiver,ACE_Null_Mutex> RECEIVER;
15 int
16 Receiver_StreamEndPoint::get_callback (const char *flow_name,
17 TAO_AV_Callback *&callback)
19 Receiver_Callback *callback_;
20 ACE_NEW_RETURN (callback_,
21 Receiver_Callback,
22 -1);
24 // Return the receiver application callback to the AVStreams for
25 // future upcalls.
26 callback = callback_;
27 callback_->flowname (flow_name);
28 return 0;
32 int
33 Receiver_StreamEndPoint::set_protocol_object (const char * flowname,
34 TAO_AV_Protocol_Object *object)
36 // Set the sender protocol object corresponding to the transport
37 // protocol selected.
38 if (ACE_OS::strcmp (flowname, "Data_Receiver1") == 0)
39 RECEIVER::instance ()->protocol_object (object);
40 return 0;
43 Receiver_Callback::Receiver_Callback ()
44 : frame_count_ (1),
45 mb_ (BUFSIZ)
47 ACE_DEBUG ((LM_DEBUG,
48 "Receiver_Callback::Receiver_Callback\n"));
51 void
52 Receiver_Callback::flowname (const char* flow_name)
54 this->flowname_ = flow_name;
56 // Make sure we have a valid <output_file>
57 this->output_file_ = ACE_OS::fopen (this->flowname_.c_str (),
58 "w");
59 if (this->output_file_ == 0)
60 ACE_ERROR ((LM_DEBUG,
61 "Cannot open output file %C\n",
62 this->flowname_.c_str ()));
64 else
65 ACE_DEBUG ((LM_DEBUG,
66 "%C File Opened Successfully\n",
67 this->flowname_.c_str ()));
71 int
72 Receiver_Callback::receive_frame (ACE_Message_Block *frame,
73 TAO_AV_frame_info *,
74 const ACE_Addr &)
77 // Upcall from the AVStreams when there is data to be received from
78 // the sender.
80 ACE_DEBUG ((LM_DEBUG,
81 "Receiver_Callback::receive_frame for frame %d for flow %C\n",
82 this->frame_count_++,
83 this->flowname_.c_str ()));
85 while (frame != 0)
87 // Write the received data to the file.
88 size_t result =
89 ACE_OS::fwrite (frame->rd_ptr (),
90 frame->length (),
92 this->output_file_);
94 if (result == frame->length ())
95 ACE_ERROR_RETURN ((LM_ERROR,
96 "Receiver_Callback::fwrite failed\n"),
97 -1);
99 frame = frame->cont ();
102 return 0;
106 Receiver_Callback::handle_destroy ()
108 // Called when the distributer requests the stream to be shutdown.
109 ACE_DEBUG ((LM_DEBUG,
110 "Receiver_Callback::handle_destroy\n"));
112 endstream++;
114 return 0;
117 Receiver::Receiver ()
118 : mmdevice_ (0),
119 frame_rate_ (30),
120 frame_count_ (0),
121 filename_ ("input"),
122 mb_ (BUFSIZ)
126 Receiver::~Receiver ()
130 void
131 Receiver::protocol_object (TAO_AV_Protocol_Object *object)
133 // Set the sender protocol object corresponding to the transport
134 // protocol selected.
135 this->protocol_object_ = object;
139 Receiver::parse_args (int argc,
140 ACE_TCHAR *argv[])
142 // Parse command line arguments
143 ACE_Get_Opt opts (argc, argv, ACE_TEXT("f:r:d"));
145 int c;
146 while ((c= opts ()) != -1)
148 switch (c)
150 case 'f':
151 this->filename_ = ACE_TEXT_ALWAYS_CHAR (opts.opt_arg ());
152 break;
153 case 'r':
154 this->frame_rate_ = ACE_OS::atoi (opts.opt_arg ());
155 break;
156 case 'd':
157 TAO_debug_level++;
158 break;
159 default:
160 ACE_DEBUG ((LM_DEBUG, "Unknown Option\n"));
161 return -1;
164 return 0;
168 Receiver::init (int argc,
169 ACE_TCHAR *argv[])
171 // Initialize the endpoint strategy with the orb and poa.
172 int result =
173 this->reactive_strategy_.init (TAO_AV_CORE::instance ()->orb (),
174 TAO_AV_CORE::instance ()->poa ());
175 if (result != 0)
176 return result;
178 // Parse the command line arguments
179 result =
180 this->parse_args (argc,
181 argv);
182 if (result != 0)
183 return result;
186 // Register the receiver mmdevice object with the ORB
187 ACE_NEW_RETURN (this->mmdevice_,
188 TAO_MMDevice (&this->reactive_strategy_),
189 -1);
191 // Servant Reference Counting to manage lifetime
192 PortableServer::ServantBase_var safe_mmdevice =
193 this->mmdevice_;
195 CORBA::Object_var mmdevice =
196 this->mmdevice_->_this ();
198 // Register the mmdevice with the naming service.
199 CosNaming::Name name (1);
200 name.length (1);
201 name [0].id =
202 CORBA::string_dup ("Receiver");
204 // Initialize the naming services
205 if (this->naming_client_.init (TAO_AV_CORE::instance ()->orb ()) != 0)
206 ACE_ERROR_RETURN ((LM_ERROR,
207 "Unable to initialize "
208 "the TAO_Naming_Client\n"),
209 -1);
211 // Register the receiver object with the naming server.
212 this->naming_client_->rebind (name,
213 mmdevice.in ());
215 return 0;
218 TAO_AV_Protocol_Object*
219 Receiver::protocol_object ()
221 return this->protocol_object_;
225 ACE_TMAIN (int argc,
226 ACE_TCHAR *argv[])
230 // Initialize the ORB first.
231 CORBA::ORB_var orb =
232 CORBA::ORB_init (argc, argv);
234 CORBA::Object_var obj
235 = orb->resolve_initial_references ("RootPOA");
237 // Get the POA_var object from Object_var.
238 PortableServer::POA_var root_poa =
239 PortableServer::POA::_narrow (obj.in ());
241 PortableServer::POAManager_var mgr
242 = root_poa->the_POAManager ();
244 mgr->activate ();
246 // Initialize the AVStreams components.
247 TAO_AV_CORE::instance ()->init (orb.in (),
248 root_poa.in ());
250 int result =
251 RECEIVER::instance ()->init (argc,
252 argv);
254 if (result != 0)
255 return result;
257 while (endstream != 2)
259 orb->perform_work ();
262 // Hack for now....
263 ACE_OS::sleep (1);
265 orb->destroy ();
267 catch (const CORBA::Exception& ex)
269 ex._tao_print_exception ("receiver::init");
270 return -1;
273 RECEIVER::close (); // Explicitly finalize the Unmanaged_Singleton.
275 return 0;
278 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
279 template ACE_Unmanaged_Singleton<Receiver, ACE_Null_Mutex> *ACE_Unmanaged_Singleton<Receiver, ACE_Null_Mutex>::singleton_;
280 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */