Merge pull request #2306 from mitza-oci/warnings
[ACE_TAO.git] / TAO / examples / Simple / chat / Client_i.cpp
blob714bebc94c0f3b767e5263ca6d7e6fed97787b55
2 //=============================================================================
3 /**
4 * @file Client_i.cpp
6 * Implementation of the Client_i class.
8 * @author Pradeep Gore <pradeep@cs.wustl.edu>
9 */
10 //=============================================================================
13 #include "Client_i.h"
14 #include "tao/ORB.h"
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"
21 Client_i::Client_i ()
22 : ior_ ("")
23 , ior_file_name_ (ACE_TEXT ("chat.ior"))
24 , nickname_ ("noname")
26 Receiver_i *tmp = 0;
27 ACE_NEW_THROW_EX (tmp,
28 Receiver_i (),
29 CORBA::NO_MEMORY ());
30 this->receiver_i_ = tmp;
33 int
34 Client_i::parse_args (int argc, ACE_TCHAR *argv[])
36 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("n:f:"));
37 int c;
39 while ((c = get_opts ()) != -1)
40 switch (c)
42 case 'n': // get the users nickname
43 this->nickname_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
44 break;
46 case 'f': // get the file name to write to
47 this->ior_file_name_ = get_opts.opt_arg ();
48 break;
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>]")
56 ACE_TEXT ("\n"),
57 argv [0]),
58 -1);
61 ACE_DEBUG ((LM_DEBUG,
62 ACE_TEXT ("\nusing nickname = %C, filename = %s\n"),
63 this->nickname_.c_str (),
64 this->ior_file_name_));
65 return 0;
68 int
69 Client_i::init (int argc, ACE_TCHAR *argv[])
71 try
73 // Retrieve the ORB.
74 this->orb_manager_.init (argc,
75 argv,
76 0);
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)
82 return -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_),
92 -1);
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 ()),
101 -1);
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")),
108 -1);
111 catch (const CORBA::Exception& ex)
113 ex._tao_print_exception ("client_i::init\n");
114 return -1;
117 return 0;
121 Client_i::run ()
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
143 (this,
144 TAO_ORB_Core_instance ()->reactor (),
145 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
146 ACE_ERROR_RETURN ((LM_ERROR,
147 ACE_TEXT ("%p\n"),
148 ACE_TEXT ("register_stdin_handler")),
149 -1);
151 // Run the ORB.
152 this->orb_manager_.run ();
154 catch (const CORBA::Exception& ex)
156 ex._tao_print_exception ("Client_i::run ()");
157 return -1;
160 return 0;
164 Client_i::handle_input (ACE_HANDLE)
166 char buf[BUFSIZ];
168 if (ACE_OS::fgets (buf, BUFSIZ, stdin) == 0)
169 return 0;
173 // Check if the user wants to quit.
174 if (ACE_OS::strncmp (buf,
175 QUIT_STRING,
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 ();
189 return -1;
192 // Call the server function <say> to pass the string typed by
193 // the server.
194 this->server_->say (this->receiver_var_.in (),
195 buf);
197 catch (const CORBA::Exception& ex)
199 ex._tao_print_exception ("Input_Handler::init");
200 return -1;
203 return 0;
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"),
215 filename,
216 ACE_TEXT ("open")),
217 -1);
219 ACE_Read_Buffer ior_buffer (f_handle);
220 char *data = ior_buffer.read ();
222 if (data == 0)
223 ACE_ERROR_RETURN ((LM_ERROR,
224 ACE_TEXT ("Unable to read ior (%p)\n"),
225 ACE_TEXT ("read")),
226 -1);
228 this->ior_ = data;
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")),
236 -1);
237 return 0;