Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / AVStreams / Component_Switching / Connection_Manager.cpp
blob92013d8ce7f41a203fd4164fb58840d05475c761
1 #include "Connection_Manager.h"
2 #include "tao/debug.h"
4 Connection_Manager::Connection_Manager ()
8 Connection_Manager::~Connection_Manager ()
12 int
13 Connection_Manager::init (CORBA::ORB_ptr orb)
15 // Initialize the naming service
16 if (this->naming_client_.init (orb) != 0)
17 ACE_ERROR_RETURN ((LM_ERROR,
18 " (%P|%t) Unable to initialize "
19 "the TAO_Naming_Client.\n"),
20 -1);
21 return 0;
24 void
25 Connection_Manager::bind_to_receivers (const ACE_CString &sender_name,
26 AVStreams::MMDevice_ptr sender)
28 this->sender_name_ =
29 sender_name;
31 this->sender_ =
32 AVStreams::MMDevice::_duplicate (sender);
34 CosNaming::Name name (1);
35 name.length (1);
37 try
39 // Try binding the sender context in the NS
40 name [0].id =
41 CORBA::string_dup (this->sender_name_.c_str ());
43 this->sender_context_ =
44 this->naming_client_->bind_new_context (name);
47 // We reach here if there was no exception raised in
48 // <bind_new_context>. We then create a receiver context.
51 // Create the context for storing the receivers
52 name [0].id =
53 CORBA::string_dup ("Receivers");
55 // Try binding the receivers context under the sender context.
56 this->receiver_context_ =
57 this->sender_context_->bind_new_context (name);
59 catch (const CosNaming::NamingContext::AlreadyBound&)
62 // The sender context already exists, probably created by the
63 // receiver(s).
66 // Get the sender context.
67 name [0].id =
68 CORBA::string_dup (this->sender_name_.c_str ());
70 CORBA::Object_var object =
71 this->naming_client_->resolve (name);
73 this->sender_context_ =
74 CosNaming::NamingContext::_narrow (object.in ());
76 // Find the Receiver context.
77 name [0].id =
78 CORBA::string_dup ("Receivers");
80 object =
81 this->sender_context_->resolve (name);
83 this->receiver_context_ =
84 CosNaming::NamingContext::_narrow (object.in ());
86 this->find_receivers ();
89 name [0].id =
90 CORBA::string_dup (this->sender_name_.c_str ());
92 // Register the sender object with the sender context.
93 this->sender_context_->rebind (name,
94 sender);
97 void
98 Connection_Manager::find_receivers ()
100 CosNaming::BindingIterator_var iterator;
101 CosNaming::BindingList_var binding_list;
102 const CORBA::ULong chunk = 100;
104 // Get the list of receivers registered for this sender.
105 this->receiver_context_->list (chunk,
106 binding_list,
107 iterator);
109 // Add the receivers found in the bindinglist to the <receivers>.
110 this->add_to_receivers (binding_list);
112 if (!CORBA::is_nil (iterator.in ()))
114 CORBA::Boolean more = 1;
116 // Check to see if there are more receivers listed.
117 while (more)
119 more = iterator->next_n (chunk,
120 binding_list);
122 this->add_to_receivers (binding_list);
127 void
128 Connection_Manager::add_to_receivers (CosNaming::BindingList &binding_list)
130 for (CORBA::ULong i = 0;
131 i < binding_list.length ();
132 i++)
134 // Get the receiver name from the binding list.
135 ACE_CString receiver_name =
136 binding_list [i].binding_name [0].id.in ();
138 CosNaming::Name name (1);
139 name.length (1);
140 name [0].id =
141 CORBA::string_dup (receiver_name.c_str ());
143 // Resolve the reference of the receiver from the receiver
144 // context.
145 CORBA::Object_var obj =
146 this->receiver_context_->resolve (name);
148 AVStreams::MMDevice_var receiver_device =
149 AVStreams::MMDevice::_narrow (obj.in ());
151 // Add this receiver to the receiver map.
152 ACE_CString flowname =
153 this->sender_name_ +
154 "_" +
155 receiver_name;
156 this->receivers_.bind (flowname,
157 receiver_device);
161 void
162 Connection_Manager::connect_to_receivers ()
164 // Connect to all receivers that we know about.
165 for (Receivers::iterator iterator = this->receivers_.begin ();
166 iterator != this->receivers_.end ();
167 ++iterator)
169 // Initialize the QoS
170 AVStreams::streamQoS_var the_qos (new AVStreams::streamQoS);
172 ACE_CString flowname =
173 (*iterator).ext_id_;
175 // Create the forward flow specification to describe the flow.
176 TAO_Forward_FlowSpec_Entry sender_entry (flowname.c_str (),
177 "IN",
178 "USER_DEFINED",
180 "UDP",
183 // Set the flow specification for the stream between receiver
184 // and distributer
185 AVStreams::flowSpec flow_spec (1);
186 flow_spec.length (1);
187 flow_spec [0] =
188 CORBA::string_dup (sender_entry.entry_to_string ());
190 // Create the stream control for this stream.
191 TAO_StreamCtrl *streamctrl;
192 ACE_NEW (streamctrl,
193 TAO_StreamCtrl);
195 // Servant Reference Counting to manage lifetime
196 PortableServer::ServantBase_var safe_streamctrl =
197 streamctrl;
199 // Register streamctrl.
200 AVStreams::StreamCtrl_var streamctrl_object =
201 streamctrl->_this ();
203 // Bind the flowname and the corresponding stream controller to
204 // the stream controller map
205 this->streamctrls_.bind (flowname,
206 streamctrl_object);
208 // Bind the sender and receiver MMDevices.
209 (void) streamctrl->bind_devs (this->sender_.in (),
210 (*iterator).int_id_.in (),
211 the_qos.inout (),
212 flow_spec);
216 void
217 Connection_Manager::bind_to_sender (const ACE_CString &sender_name,
218 const ACE_CString &receiver_name,
219 AVStreams::MMDevice_ptr receiver)
221 this->sender_name_ =
222 sender_name;
224 this->receiver_name_ =
225 receiver_name;
227 this->receiver_ =
228 AVStreams::MMDevice::_duplicate (receiver);
230 this->sender_ = 0 ;
232 CosNaming::Name name (1);
233 name.length (1);
235 int sender_context_exists = 0;
239 // Try binding the sender context in the NS
240 name [0].id =
241 CORBA::string_dup (this->sender_name_.c_str ());
243 CORBA::Object_var object =
244 this->naming_client_->resolve (name);
247 // We reach here if there was no exception raised in <resolve>.
248 // Therefore, there must be a valid sender context available.
250 sender_context_exists = 1;
252 this->sender_context_ =
253 CosNaming::NamingContext::_narrow (object.in ());
255 name [0].id =
256 CORBA::string_dup ("Receivers");
258 // Find the receivers context under the sender's context
259 object =
260 this->sender_context_->resolve (name);
262 this->receiver_context_ =
263 CosNaming::NamingContext::_narrow (object.in ());
265 catch (const CosNaming::NamingContext::NotFound&)
267 name [0].id =
268 CORBA::string_dup (this->sender_name_.c_str ());
270 // Create the sender context
271 this->sender_context_ =
272 this->naming_client_->bind_new_context (name);
274 name [0].id =
275 CORBA::string_dup ("Receivers");
277 // Create the receivers context under the sender's context
278 this->receiver_context_ =
279 this->sender_context_->bind_new_context (name);
283 // At this point we either have resolved the receiver context or we
284 // have created a new one.
286 name [0].id =
287 CORBA::string_dup (this->receiver_name_.c_str ());
289 // Register this receiver object under the receiver context.
290 this->receiver_context_->rebind (name,
291 receiver);
294 // Check if the sender was registered. Note that if we created the
295 // sender context, there is no point in checking for the sender.
297 if (sender_context_exists)
301 // Try binding the sender under the sender context
302 name [0].id =
303 CORBA::string_dup (this->sender_name_.c_str ());
305 CORBA::Object_var object =
306 this->sender_context_->resolve (name);
308 this->sender_ =
309 AVStreams::MMDevice::_narrow (object.in ());
311 catch (const CosNaming::NamingContext::NotFound&)
313 // No problem if the sender was not there.
318 void
319 Connection_Manager::connect_to_sender ()
321 if (CORBA::is_nil (this->sender_.in ()))
322 return;
324 ACE_CString flowname =
325 this->sender_name_ +
326 "_" +
327 this->receiver_name_;
329 // Create the forward flow specification to describe the flow.
330 TAO_Forward_FlowSpec_Entry sender_entry (flowname.c_str (),
331 "IN",
332 "USER_DEFINED",
334 "UDP",
337 // Set the flow specification for the stream between sender and
338 // receiver.
339 AVStreams::flowSpec flow_spec (1);
340 flow_spec.length (1);
341 flow_spec [0] =
342 CORBA::string_dup (sender_entry.entry_to_string ());
344 // Create the stream control for this stream
345 TAO_StreamCtrl* streamctrl;
346 ACE_NEW (streamctrl,
347 TAO_StreamCtrl);
349 // Servant Reference Counting to manage lifetime
350 PortableServer::ServantBase_var safe_streamctrl =
351 streamctrl;
353 // Register streamctrl.
354 AVStreams::StreamCtrl_var streamctrl_object =
355 streamctrl->_this ();
358 // Since senders terminate the streams, we don't need the streamctrl
359 // for these.
361 // this->streamctrls_.bind (flowname,
362 // streamctrl_object);
364 // Initialize the QoS
365 AVStreams::streamQoS_var the_qos (new AVStreams::streamQoS);
367 // Connect the sender and receiver devices.
368 CORBA::Boolean result =
369 streamctrl->bind_devs (this->sender_.in (),
370 this->receiver_.in (),
371 the_qos.inout (),
372 flow_spec);
374 if (result == 0)
375 ACE_ERROR ((LM_ERROR,
376 "Streamctrl::bind_devs failed\n"));
378 // Start the data sending.
379 AVStreams::flowSpec start_spec;
380 streamctrl->start (start_spec);
383 void
384 Connection_Manager::add_streamctrl (const ACE_CString &flowname,
385 TAO_StreamEndPoint *endpoint)
387 // Get the stream controller for this endpoint.
388 CORBA::Any_var streamctrl_any =
389 endpoint->get_property_value ("Related_StreamCtrl");
391 AVStreams::StreamCtrl_ptr streamctrl;
393 if( streamctrl_any.in() >>= streamctrl )
395 // the CORBA::Any_var owns the pointer, so we should
396 // _duplicate it before passing it around
397 AVStreams::StreamCtrl::_duplicate( streamctrl);
398 this->streamctrls_.unbind(flowname);
399 this->streamctrls_.bind (flowname, streamctrl);
404 void
405 Connection_Manager::destroy (const ACE_CString &flowname)
407 this->protocol_objects_.unbind (flowname);
408 this->receivers_.unbind (flowname);
410 this->streamctrls_.unbind(flowname);
413 void
414 Connection_Manager::unbind_sender (const ACE_CString &sender_name,
415 AVStreams::MMDevice_ptr sender_mmdevice_obj)
417 if (TAO_debug_level > 0)
418 ACE_DEBUG ((LM_DEBUG,
419 "Connection_Manager::unbind_sender\n"));
421 CosNaming::Name name (1);
422 name.length (1);
424 // Try binding the sender context in the NS
425 name [0].id =
426 CORBA::string_dup (sender_name.c_str ());
430 CORBA::Object_var object =
431 this->naming_client_->resolve (name);
433 CosNaming::NamingContext_var sender_context =
434 CosNaming::NamingContext::_narrow (object.in ());
436 if (TAO_debug_level > 0)
437 ACE_DEBUG ((LM_DEBUG,
438 "Sender Context Found\n"));
442 object =
443 sender_context->resolve (name);
445 AVStreams::MMDevice_var mmdevice =
446 AVStreams::MMDevice::_narrow (object.in ());
448 if (mmdevice->_is_equivalent (sender_mmdevice_obj))
450 sender_context->unbind (name);
452 if (TAO_debug_level > 0)
453 ACE_DEBUG ((LM_DEBUG,
454 "Sender Unbound\n"));
457 catch (const CosNaming::NamingContext::NotFound&)
459 ACE_DEBUG ((LM_DEBUG,
460 "Sender object not found\n"));
461 // Do not have to unbind.
465 catch (const CosNaming::NamingContext::NotFound&)
467 ACE_DEBUG ((LM_DEBUG,
468 "Sender Context Not Found\n"));
469 // Do not have to unbind.
474 void
475 Connection_Manager::unbind_receiver (const ACE_CString &sender_name,
476 const ACE_CString &receiver_name,
477 AVStreams::MMDevice_ptr receiver_mmdevice)
479 if (TAO_debug_level > 0)
480 ACE_DEBUG ((LM_DEBUG,
481 "Connection_Manager::unbind_receiver\n"));
483 CosNaming::Name name (1);
484 name.length (1);
486 // Try binding the sender context in the NS
487 name [0].id =
488 CORBA::string_dup (sender_name.c_str ());
492 CORBA::Object_var object =
493 this->naming_client_->resolve (name);
495 CosNaming::NamingContext_var sender_context =
496 CosNaming::NamingContext::_narrow (object.in ());
498 if (TAO_debug_level > 0)
499 ACE_DEBUG ((LM_DEBUG,
500 "Sender Context Found\n"));
504 name [0].id =
505 CORBA::string_dup ("Receivers");
507 object =
508 sender_context->resolve (name);
510 if (TAO_debug_level > 0)
511 ACE_DEBUG ((LM_DEBUG,
512 "Receivers Context Found\n"));
514 CosNaming::NamingContext_var receivers_context =
515 CosNaming::NamingContext::_narrow (object.in ());
517 name [0].id =
518 CORBA::string_dup (receiver_name.c_str ());
520 object =
521 receivers_context->resolve (name);
523 AVStreams::MMDevice_var mmdevice =
524 AVStreams::MMDevice::_narrow (object.in ());
526 if (mmdevice->_is_equivalent (receiver_mmdevice))
528 receivers_context->unbind (name);
530 if (TAO_debug_level > 0)
531 ACE_DEBUG ((LM_DEBUG,
532 "Receiver Unbound\n"));
535 catch (const CosNaming::NamingContext::NotFound&)
537 ACE_DEBUG ((LM_DEBUG,
538 "Receiver Not found\n"));
539 // Do not have to unbind.
543 catch (const CosNaming::NamingContext::NotFound&)
545 ACE_DEBUG ((LM_DEBUG,
546 "Sender Context Not Found\n"));
547 // Do not have to unbind.
552 Connection_Manager::Receivers &
553 Connection_Manager::receivers ()
555 return this->receivers_;
558 Connection_Manager::Protocol_Objects &
559 Connection_Manager::protocol_objects ()
561 return this->protocol_objects_;
564 Connection_Manager::StreamCtrls &
565 Connection_Manager::streamctrls ()
567 return this->streamctrls_;