1 #include "Connection_Manager.h"
4 Connection_Manager::Connection_Manager ()
8 Connection_Manager::~Connection_Manager ()
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"),
25 Connection_Manager::bind_to_receivers (const ACE_CString
&sender_name
,
26 AVStreams::MMDevice_ptr sender
)
32 AVStreams::MMDevice::_duplicate (sender
);
34 CosNaming::Name
name (1);
39 // Try binding the sender context in the NS
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
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
66 // Get the sender context.
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.
78 CORBA::string_dup ("Receivers");
81 this->sender_context_
->resolve (name
);
83 this->receiver_context_
=
84 CosNaming::NamingContext::_narrow (object
.in ());
86 this->find_receivers ();
90 CORBA::string_dup (this->sender_name_
.c_str ());
92 // Register the sender object with the sender context.
93 this->sender_context_
->rebind (name
,
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
,
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.
119 more
= iterator
->next_n (chunk
,
122 this->add_to_receivers (binding_list
);
128 Connection_Manager::add_to_receivers (CosNaming::BindingList
&binding_list
)
130 for (CORBA::ULong i
= 0;
131 i
< binding_list
.length ();
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);
141 CORBA::string_dup (receiver_name
.c_str ());
143 // Resolve the reference of the receiver from the receiver
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
=
156 this->receivers_
.bind (flowname
,
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 ();
169 // Initialize the QoS
170 AVStreams::streamQoS_var
the_qos (new AVStreams::streamQoS
);
172 ACE_CString flowname
=
175 // Create the forward flow specification to describe the flow.
176 TAO_Forward_FlowSpec_Entry
sender_entry (flowname
.c_str (),
183 // Set the flow specification for the stream between receiver
185 AVStreams::flowSpec
flow_spec (1);
186 flow_spec
.length (1);
188 CORBA::string_dup (sender_entry
.entry_to_string ());
190 // Create the stream control for this stream.
191 TAO_StreamCtrl
*streamctrl
;
195 // Servant Reference Counting to manage lifetime
196 PortableServer::ServantBase_var safe_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
,
208 // Bind the sender and receiver MMDevices.
209 (void) streamctrl
->bind_devs (this->sender_
.in (),
210 (*iterator
).int_id_
.in (),
217 Connection_Manager::bind_to_sender (const ACE_CString
&sender_name
,
218 const ACE_CString
&receiver_name
,
219 AVStreams::MMDevice_ptr receiver
)
224 this->receiver_name_
=
228 AVStreams::MMDevice::_duplicate (receiver
);
232 CosNaming::Name
name (1);
235 int sender_context_exists
= 0;
239 // Try binding the sender context in the NS
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 ());
256 CORBA::string_dup ("Receivers");
258 // Find the receivers context under the sender's context
260 this->sender_context_
->resolve (name
);
262 this->receiver_context_
=
263 CosNaming::NamingContext::_narrow (object
.in ());
265 catch (const CosNaming::NamingContext::NotFound
&)
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
);
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.
287 CORBA::string_dup (this->receiver_name_
.c_str ());
289 // Register this receiver object under the receiver context.
290 this->receiver_context_
->rebind (name
,
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
303 CORBA::string_dup (this->sender_name_
.c_str ());
305 CORBA::Object_var object
=
306 this->sender_context_
->resolve (name
);
309 AVStreams::MMDevice::_narrow (object
.in ());
311 catch (const CosNaming::NamingContext::NotFound
&)
313 // No problem if the sender was not there.
319 Connection_Manager::connect_to_sender ()
321 if (CORBA::is_nil (this->sender_
.in ()))
324 ACE_CString flowname
=
327 this->receiver_name_
;
329 // Create the forward flow specification to describe the flow.
330 TAO_Forward_FlowSpec_Entry
sender_entry (flowname
.c_str (),
337 // Set the flow specification for the stream between sender and
339 AVStreams::flowSpec
flow_spec (1);
340 flow_spec
.length (1);
342 CORBA::string_dup (sender_entry
.entry_to_string ());
344 // Create the stream control for this stream
345 TAO_StreamCtrl
* streamctrl
;
349 // Servant Reference Counting to manage lifetime
350 PortableServer::ServantBase_var safe_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
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 (),
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
);
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
);
405 Connection_Manager::destroy (const ACE_CString
&flowname
)
407 this->protocol_objects_
.unbind (flowname
);
408 this->receivers_
.unbind (flowname
);
410 this->streamctrls_
.unbind(flowname
);
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);
424 // Try binding the sender context in the NS
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"));
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.
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);
486 // Try binding the sender context in the NS
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"));
505 CORBA::string_dup ("Receivers");
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 ());
518 CORBA::string_dup (receiver_name
.c_str ());
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_
;