2 //=============================================================================
6 * Implementation of the Client_i class.
8 * @author Pradeep Gore <pradeep@cs.wustl.edu>
10 //=============================================================================
15 #include "tao/ORB_Core.h"
16 #include "ace/Read_Buffer.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/OS_NS_fcntl.h"
19 #include "ace/OS_NS_unistd.h"
23 , ior_file_name_ (ACE_TEXT ("chat.ior"))
24 , nickname_ ("noname")
27 ACE_NEW_THROW_EX (tmp
,
30 this->receiver_i_
= tmp
;
34 Client_i::parse_args (int argc
, ACE_TCHAR
*argv
[])
36 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT ("n:f:"));
39 while ((c
= get_opts ()) != -1)
42 case 'n': // get the users nickname
43 this->nickname_
= ACE_TEXT_ALWAYS_CHAR(get_opts
.opt_arg ());
46 case 'f': // get the file name to write to
47 this->ior_file_name_
= get_opts
.opt_arg ();
50 default: // display help for use of the serve
51 case '?': // display help for use of the server.
52 ACE_ERROR_RETURN ((LM_ERROR
,
53 ACE_TEXT ("usage: %s")
54 ACE_TEXT (" [-n <your_nick_name>]")
55 ACE_TEXT (" [-f <ior_input_file>]")
62 ACE_TEXT ("\nusing nickname = %C, filename = %s\n"),
63 this->nickname_
.c_str (),
64 this->ior_file_name_
));
69 Client_i::init (int argc
, ACE_TCHAR
*argv
[])
74 this->orb_manager_
.init (argc
,
78 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
80 // Check if the command line arguments are ok.
81 if (this->parse_args (argc
, argv
) == -1)
84 // set the orb in the receiver_i_ object.
85 this->receiver_i_
->orb (orb
.in ());
87 // read the ior from file
88 if (this->read_ior (this->ior_file_name_
) != 0)
89 ACE_ERROR_RETURN ((LM_ERROR
,
90 ACE_TEXT ("could not read the ior from the file: <%s>\n"),
91 this->ior_file_name_
),
94 CORBA::Object_var server_object
=
95 orb
->string_to_object (this->ior_
.c_str ());
97 if (CORBA::is_nil (server_object
.in ()))
98 ACE_ERROR_RETURN ((LM_ERROR
,
99 ACE_TEXT ("invalid ior <%C>\n"),
100 this->ior_
.c_str ()),
103 this->server_
= Broadcaster::_narrow (server_object
.in ());
104 if (CORBA::is_nil (this->server_
.in ()))
106 ACE_ERROR_RETURN ((LM_ERROR
,
107 ACE_TEXT ("Nil Server\n")),
111 catch (const CORBA::Exception
& ex
)
113 ex
._tao_print_exception ("client_i::init\n");
123 ACE_DEBUG ((LM_DEBUG
,
124 ACE_TEXT ("\n============= Simple Chat =================\n")
125 ACE_TEXT ("========== type 'quit' to exit ===========\n")));
129 PortableServer::POAManager_var poa_manager
=
130 this->orb_manager_
.poa_manager ();
131 poa_manager
->activate ();
133 this->receiver_var_
=
134 this->receiver_i_
->_this ();
136 // Register ourselves with the server.
137 server_
->add (this->receiver_var_
.in (),
138 this->nickname_
.c_str ());
140 // Register our <Input_Handler> to handle STDIN events, which will
141 // trigger the <handle_input> method to process these events.
142 if (ACE_Event_Handler::register_stdin_handler
144 TAO_ORB_Core_instance ()->reactor (),
145 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
146 ACE_ERROR_RETURN ((LM_ERROR
,
148 ACE_TEXT ("register_stdin_handler")),
152 this->orb_manager_
.run ();
154 catch (const CORBA::Exception
& ex
)
156 ex
._tao_print_exception ("Client_i::run ()");
164 Client_i::handle_input (ACE_HANDLE
)
168 if (ACE_OS::fgets (buf
, BUFSIZ
, stdin
) == 0)
173 // Check if the user wants to quit.
174 if (ACE_OS::strncmp (buf
,
176 ACE_OS::strlen (QUIT_STRING
)) == 0)
178 // Remove ourselves from the server.
181 this->server_
->remove (this->receiver_var_
.in ());
183 catch (const CORBA::Exception
&)
185 // we don't care about problems
187 this->receiver_i_
->shutdown ();
192 // Call the server function <say> to pass the string typed by
194 this->server_
->say (this->receiver_var_
.in (),
197 catch (const CORBA::Exception
& ex
)
199 ex
._tao_print_exception ("Input_Handler::init");
207 Client_i::read_ior (const ACE_TCHAR
*filename
)
209 // Open the file for reading.
210 ACE_HANDLE f_handle
= ACE_OS::open (filename
, 0);
212 if (f_handle
== ACE_INVALID_HANDLE
)
213 ACE_ERROR_RETURN ((LM_ERROR
,
214 ACE_TEXT ("Unable to open %s for writing (%p)\n"),
219 ACE_Read_Buffer
ior_buffer (f_handle
);
220 char *data
= ior_buffer
.read ();
223 ACE_ERROR_RETURN ((LM_ERROR
,
224 ACE_TEXT ("Unable to read ior (%p)\n"),
229 ior_buffer
.alloc ()->free (data
);
231 ACE_OS::close (f_handle
);
233 if (this->ior_
.length () == 0)
234 ACE_ERROR_RETURN ((LM_ERROR
,
235 ACE_TEXT ("failed to read ior from file\n")),