1
// Client_i.cpp,v 1.12 2002/01/29 20:20:41 okellogg Exp
3
// ===========================================================
7
// TAO/tests/Simple/chat
13
// Implementation of the Client_i class.
16
// Pradeep Gore <pradeep@cs.wustl.edu>
18
// ===========================================================
22
#include "tao/ORB_Core.h"
23
#include "ace/Read_Buffer.h"
24
#include "ace/Get_Opt.h"
27
: ior_file_name_ ("chat.ior"),
32
Client_i::~Client_i (void)
34
// Make sure to cleanup the STDIN handler.
35
if (ACE_Event_Handler::remove_stdin_handler
36
(TAO_ORB_Core_instance ()->reactor (),
37
TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
40
"remove_stdin_handler"));
44
Client_i::parse_args (int argc, char *argv[])
46
ACE_Get_Opt get_opts (argc, argv, "n:f:");
49
while ((c = get_opts ()) != -1)
52
case 'n': // get the users nickname
53
this->nickname_ = get_opts.opt_arg ();
56
case 'f': // get the file name to write to
57
this->ior_file_name_ = get_opts.opt_arg ();
60
default: // display help for use of the serve
61
case '?': // display help for use of the server.
62
ACE_ERROR_RETURN ((LM_ERROR,
64
" [-n <your_nick_name>]"
65
" [-f <ior_input_file>]"
72
"\nusing nickname = %s, filename = %s\n",
74
this->ior_file_name_));
79
Client_i::init (int argc, char *argv[])
81
// Check if the command line arguments are ok.
82
if (this->parse_args (argc, argv) == -1)
88
this->orb_manager_.init (argc,
91
ACE_ENV_ARG_PARAMETER);
94
CORBA::ORB_var orb = this->orb_manager_.orb ();
96
// set the orb in the receiver_i_ object.
97
this->receiver_i_.orb (orb.in ());
99
// read the ior from file
100
if (this->read_ior (this->ior_file_name_) != 0)
101
ACE_ERROR_RETURN ((LM_ERROR,
102
"could not read the ior from the file: <%s>\n",
103
this->ior_file_name_),
106
CORBA::Object_var server_object =
107
orb->string_to_object (this->ior_
108
ACE_ENV_ARG_PARAMETER);
111
if (CORBA::is_nil (server_object.in ()))
112
ACE_ERROR_RETURN ((LM_ERROR,
113
"invalid ior <%s>\n",
117
this->server_ = Broadcaster::_narrow (server_object.in ()
118
ACE_ENV_ARG_PARAMETER);
123
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
129
// Register our <Input_Handler> to handle STDIN events, which will
130
// trigger the <handle_input> method to process these events.
132
if (ACE_Event_Handler::register_stdin_handler
134
TAO_ORB_Core_instance ()->reactor (),
135
TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
136
ACE_ERROR_RETURN ((LM_ERROR,
138
"register_stdin_handler"),
146
ACE_DEBUG ((LM_DEBUG,
147
"\n============= Simple Chat =================\n"
148
"========== type 'quit' to exit ===========\n"));
152
PortableServer::POAManager_var poa_manager =
153
this->orb_manager_.poa_manager ();
154
poa_manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
157
this->receiver_var_ =
158
this->receiver_i_._this (ACE_ENV_SINGLE_ARG_PARAMETER);
161
// Register ourselves with the server.
162
server_->add (this->receiver_var_.in (),
164
ACE_ENV_ARG_PARAMETER);
168
this->orb_manager_.run (ACE_ENV_SINGLE_ARG_PARAMETER);
173
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
183
Client_i::handle_input (ACE_HANDLE)
187
if (ACE_OS::fgets (buf, BUFSIZ, stdin) == 0)
192
// Check if the user wants to quit.
193
if (ACE_OS::strncmp (buf,
195
ACE_OS::strlen (QUIT_STRING)) == 0)
197
// Remove ourselves from the server.
198
this->server_->remove (this->receiver_var_.in ());
199
this->receiver_i_.shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
205
// Call the server function <say> to pass the string typed by
207
this->server_->say (this->receiver_var_.in (),
209
ACE_ENV_ARG_PARAMETER);
214
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
215
"Input_Handler::init");
224
Client_i::read_ior (const char *filename)
226
// Open the file for reading.
227
ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
229
if (f_handle == ACE_INVALID_HANDLE)
230
ACE_ERROR_RETURN ((LM_ERROR,
231
"Unable to open %s for writing: %p\n",
236
ACE_Read_Buffer ior_buffer (f_handle);
237
char *data = ior_buffer.read ();
240
ACE_ERROR_RETURN ((LM_ERROR,
241
"Unable to read ior: %p\n"),
244
this->ior_ = ACE_OS::strdup (data);
245
ior_buffer.alloc ()->free (data);
247
ACE_OS::close (f_handle);
250
ACE_ERROR_RETURN ((LM_ERROR,
251
"failed to read ior from file\n",