summaryrefslogtreecommitdiff
path: root/TAO/examples/Simple/chat/Client_i.cpp
blob: b8d510177dfebaedf0a572788118807135af883f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// $Id$

// ===========================================================
//
//
// = LIBRARY
//    TAO/tests/Simple/chat
//
// = FILENAME
//    Client_i.cpp
//
// = DESCRIPTION
//    Implementation of the Client_i class.
//
// = AUTHOR
//    Pradeep Gore <pradeep@cs.wustl.edu>
//
// ===========================================================

#include "Client_i.h"
#include "ace/Read_Buffer.h"
#include "tao/ORB.h"
#include "ace/Get_Opt.h"

Client_i::Client_i ()
  : ior_file_name_ ("chat.ior"),
  nickname_ ("noname")
{
}

Client_i::~Client_i (void)
{
  // Make sure to cleanup the STDIN handler.
  if (ACE_Event_Handler::remove_stdin_handler
      (TAO_ORB_Core_instance ()->reactor (),
       TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
    ACE_ERROR ((LM_ERROR,
		       "%p\n",
		       "remove_stdin_handler"));
}

int
Client_i::parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "n:f:");
  int specified_options = 0; // Flag to check if the user specified any options.
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'n':  // get the users nickname
	this->nickname_ = get_opts.optarg;
	specified_options = 1;
	break;

      case 'f':  // get the file name to write to
        this->ior_file_name_ = get_opts.optarg;
	specified_options = 1;
	break;

      default: // display help for use of the serve
      case '?':  // display help for use of the server.
	ACE_ERROR_RETURN ((LM_ERROR,
			   "usage:  %s"
			   " [-n] <your_nick_name>"
			   " [-f] <ior_input_file>"
			   "\n",
			   argv [0]),
			  -1);
      }
  if (specified_options == 0)
    ACE_DEBUG ((LM_DEBUG,
		"\nusing defaults. nickname = %s, filename = %s\n",
		this->nickname_,
		this->ior_file_name_));
  return 0;
}

int
Client_i::init (int argc, char *argv[])
{
  // Check if the command line arguments are ok.
  if (this->parse_args (argc, argv) == -1)
    return -1;

  CORBA::Environment TAO_TRY_ENV;

  TAO_TRY
    {
      // Retrieve the ORB.
      this->orb_ = CORBA::ORB_init (argc,
				    argv,
				    0,
				    TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // set the orb in the receiver_i_ object.
      this->receiver_i_.orb (this->orb_.in ());

      // read the ior from file
      if (this->read_ior (this->ior_file_name_) != 0)
	ACE_ERROR_RETURN ((LM_ERROR,
			   "could not read the ior from the file: <%s>\n",
			   this->ior_file_name_),
			  -1);

      CORBA::Object_var server_object =
	this->orb_->string_to_object (this->ior_,
				      TAO_TRY_ENV);
      TAO_CHECK_ENV;

      if (CORBA::is_nil (server_object.in ()))
	ACE_ERROR_RETURN ((LM_ERROR,
			   "invalid ior <%s>\n",
			   this->ior_),
			  -1);

      this->server_ = Broadcaster::_narrow (server_object.in (),
					    TAO_TRY_ENV);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("client_i::init\n");
      return -1;
    }
  TAO_ENDTRY;

  // Register our <Input_Handler> to handle STDIN events, which will
  // trigger the <handle_input> method to process these events.

  if (ACE_Event_Handler::register_stdin_handler
      (this,
       TAO_ORB_Core_instance ()->reactor (),
       TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "register_stdin_handler"),
		      -1);
  return 0;
}

int
Client_i::run (void)
{
  ACE_DEBUG ((LM_DEBUG,
	      "\n============= Simple Chat =================\n"
	      "========== type 'quit' to exit  ===========\n"));

  CORBA::Environment TAO_TRY_ENV;

  TAO_TRY
    {
      this->receiver_var_ =
	this->receiver_i_._this (TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // Register ourselves with the server.
      server_->add (this->receiver_var_.in (),
		    this->nickname_,
		    TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // Run the ORB.
      this->orb_->run ();
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("Client_i::run ()");
      return -1;
    }
  TAO_ENDTRY;

  return 0;
}

int
Client_i::handle_input (ACE_HANDLE)
{
  char buf[BUFSIZ];

  if (ACE_OS::fgets (buf, BUFSIZ, stdin) == 0)
    return 0;

  CORBA::Environment TAO_TRY_ENV;

  TAO_TRY
    {
      // Check if the user wants to quit.
      if (ACE_OS::strncmp (buf,
			   QUIT_STRING,
			   ACE_OS::strlen (QUIT_STRING)) == 0)
	{
	  // Remove ourselves from the server.
	  this->server_->remove (this->receiver_var_.in ());
	  this->receiver_i_.shutdown (TAO_TRY_ENV);

	  TAO_CHECK_ENV;
	  return 0;
	}

      // Call the server function <say> to pass the string typed by
      // the server.
      this->server_->say (this->receiver_var_.in (),
			  buf,
			  TAO_TRY_ENV);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("Input_Handler::init");
      return -1;
    }
  TAO_ENDTRY;

  return 0;
}

int
Client_i::read_ior (const char *filename)
{
  // Open the file for reading.
  ACE_HANDLE f_handle = ACE_OS::open (filename, 0);

  if (f_handle == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "Unable to open %s for writing: %p\n",
		       filename,
		       "invalid handle"),
		      -1);

  ACE_Read_Buffer ior_buffer (f_handle);
  char *data = ior_buffer.read ();

  if (data == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "Unable to read ior: %p\n"),
		      -1);

  this->ior_ = ACE_OS::strdup (data);
  ior_buffer.alloc ()->free (data);

  ACE_OS::close (f_handle);

  if (this->ior_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "failed to read ior from file\n",
		       ""),
		      -1);
  return 0;
}