1 //=============================================================================
3 * @file Broadcaster_i.cpp
5 * Implementation of the Broadcaster_i class. This class is the servant
6 * object for the chat server.
8 * @author Pradeep Gore <pradeep@cs.wustl.edu>
10 //=============================================================================
12 #include "Broadcaster_i.h"
15 Broadcaster_i::Receiver_Data::operator == (const Broadcaster_i::Receiver_Data
&receiver_data
) const
17 // The <_is_equivalent> function checks if the _var and _ptr objects
18 // are the same. NOTE: this call might not behave well on other
19 // ORBs since <_is_equivalent> isn't guaranteed to differentiate
22 this->receiver_
->_is_equivalent (receiver_data
.receiver_
.in ())
23 && this->nickname_
== receiver_data
.nickname_
;
27 Broadcaster_i::add (Receiver_ptr receiver
,
30 Broadcaster_i::Receiver_Data receiver_data
;
32 // Store the client information.
33 receiver_data
.receiver_
= Receiver::_duplicate (receiver
);
34 receiver_data
.nickname_
= nickname
;
36 // Insert the Receiver reference to the set
37 if (receiver_set_
.insert (receiver_data
) == -1)
38 throw Broadcaster::CannotAdd ("failed to add to the receiver set\n");
40 // Tell everyone which person just joined the chat.
41 ACE_CString broadcast_string
=
43 + ACE_CString (nickname
)
44 + ACE_CString (" has joined the chat ****\n");
48 this->broadcast (broadcast_string
.fast_rep ());
50 catch (const CORBA::Exception
& ex
)
52 ex
._tao_print_exception ("Broadcaster_i::broadcast failed.\t\n");
57 Broadcaster_i::remove (Receiver_ptr receiver
)
59 Broadcaster_i::Receiver_Data receiver_data_to_remove
;
61 // Go through the list of <Receiver_Data> to find which registered client
62 // wants to be removed.
63 for (RECEIVER_SET_ITERATOR iter
= this->receiver_set_
.begin ();
64 iter
!= this->receiver_set_
.end ();
67 // The <_is_equivalent> function checks if the _var and _ptr objects
68 // are the same. NOTE: this call might not behave well on other
69 // ORBs since <_is_equivalent> isn't guaranteed to differentiate
71 if ((*iter
).receiver_
.in ()->_is_equivalent (receiver
) == 1)
73 receiver_data_to_remove
= *iter
;
78 // Remove the reference from our list.
79 if (this->receiver_set_
.remove (receiver_data_to_remove
) == -1)
80 throw Broadcaster::CannotRemove ("failed to remove from receiver set\n");
82 // Tell everyone, which person left the chat.
83 ACE_CString broadcast_string
= "**** "
84 + receiver_data_to_remove
.nickname_
88 this->broadcast (broadcast_string
.fast_rep ());
92 Broadcaster_i::say (Receiver_ptr receiver
,
97 ACE_CString
sender_nickname ("Sender Unknown");
99 // Find the nickname for this receiver.
101 for (RECEIVER_SET_ITERATOR iter
= this->receiver_set_
.begin ();
102 iter
!= this->receiver_set_
.end ();
105 // The <_is_equivalent> function checks if the _var and
106 // _ptr objects are the same. NOTE: this call might not
107 // behave well on other ORBs since <_is_equivalent> isn't
108 // guaranteed to differentiate object references.
109 if ((*iter
).receiver_
.in ()->_is_equivalent (receiver
) == 1)
110 sender_nickname
= (*iter
).nickname_
;
113 // Broadcast the message to all registered clients
114 ACE_CString
broadcast_string ("[" + sender_nickname
+ "] " + text
);
116 this->broadcast (broadcast_string
.fast_rep ());
118 catch (const CORBA::Exception
& ex
)
120 ex
._tao_print_exception ("Broadcaster_i::say\t\n");
125 Broadcaster_i::broadcast (const char *text
)
127 // Broadcast the message to all registered clients.
129 for (RECEIVER_SET_ITERATOR iter
= this->receiver_set_
.begin ();
130 iter
!= this->receiver_set_
.end ();
135 (*iter
).receiver_
->message (text
);
137 catch (const CORBA::Exception
& ex
)
139 ex
._tao_print_exception ("Failed to send a message\n");