~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to TAO/examples/Simple/chat/Client_i.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Client_i.cpp,v 1.12 2002/01/29 20:20:41 okellogg Exp
2
 
 
3
 
// ===========================================================
4
 
//
5
 
//
6
 
// = LIBRARY
7
 
//    TAO/tests/Simple/chat
8
 
//
9
 
// = FILENAME
10
 
//    Client_i.cpp
11
 
//
12
 
// = DESCRIPTION
13
 
//    Implementation of the Client_i class.
14
 
//
15
 
// = AUTHOR
16
 
//    Pradeep Gore <pradeep@cs.wustl.edu>
17
 
//
18
 
// ===========================================================
19
 
 
20
 
#include "Client_i.h"
21
 
#include "tao/ORB.h"
22
 
#include "tao/ORB_Core.h"
23
 
#include "ace/Read_Buffer.h"
24
 
#include "ace/Get_Opt.h"
25
 
 
26
 
Client_i::Client_i ()
27
 
  : ior_file_name_ ("chat.ior"),
28
 
    nickname_ ("noname")
29
 
{
30
 
}
31
 
 
32
 
Client_i::~Client_i (void)
33
 
{
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)
38
 
    ACE_ERROR ((LM_ERROR,
39
 
                       "%p\n",
40
 
                       "remove_stdin_handler"));
41
 
}
42
 
 
43
 
int
44
 
Client_i::parse_args (int argc, char *argv[])
45
 
{
46
 
  ACE_Get_Opt get_opts (argc, argv, "n:f:");
47
 
  int c;
48
 
 
49
 
  while ((c = get_opts ()) != -1)
50
 
    switch (c)
51
 
      {
52
 
      case 'n':  // get the users nickname
53
 
        this->nickname_ = get_opts.opt_arg ();
54
 
        break;
55
 
 
56
 
      case 'f':  // get the file name to write to
57
 
        this->ior_file_name_ = get_opts.opt_arg ();
58
 
        break;
59
 
 
60
 
      default: // display help for use of the serve
61
 
      case '?':  // display help for use of the server.
62
 
        ACE_ERROR_RETURN ((LM_ERROR,
63
 
                           "usage:  %s"
64
 
                           " [-n <your_nick_name>]"
65
 
                           " [-f <ior_input_file>]"
66
 
                           "\n",
67
 
                           argv [0]),
68
 
                          -1);
69
 
      }
70
 
 
71
 
  ACE_DEBUG ((LM_DEBUG,
72
 
              "\nusing nickname = %s, filename = %s\n",
73
 
              this->nickname_,
74
 
              this->ior_file_name_));
75
 
  return 0;
76
 
}
77
 
 
78
 
int
79
 
Client_i::init (int argc, char *argv[])
80
 
{
81
 
  // Check if the command line arguments are ok.
82
 
  if (this->parse_args (argc, argv) == -1)
83
 
    return -1;
84
 
 
85
 
  ACE_TRY_NEW_ENV
86
 
    {
87
 
      // Retrieve the ORB.
88
 
      this->orb_manager_.init (argc,
89
 
                               argv,
90
 
                               0
91
 
                               ACE_ENV_ARG_PARAMETER);
92
 
      ACE_TRY_CHECK;
93
 
 
94
 
      CORBA::ORB_var orb = this->orb_manager_.orb ();
95
 
 
96
 
      // set the orb in the receiver_i_ object.
97
 
      this->receiver_i_.orb (orb.in ());
98
 
 
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_),
104
 
                          -1);
105
 
 
106
 
      CORBA::Object_var server_object =
107
 
        orb->string_to_object (this->ior_
108
 
                               ACE_ENV_ARG_PARAMETER);
109
 
      ACE_TRY_CHECK;
110
 
 
111
 
      if (CORBA::is_nil (server_object.in ()))
112
 
        ACE_ERROR_RETURN ((LM_ERROR,
113
 
                           "invalid ior <%s>\n",
114
 
                           this->ior_),
115
 
                          -1);
116
 
 
117
 
      this->server_ = Broadcaster::_narrow (server_object.in ()
118
 
                                            ACE_ENV_ARG_PARAMETER);
119
 
      ACE_TRY_CHECK;
120
 
    }
121
 
  ACE_CATCHANY
122
 
    {
123
 
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
124
 
                           "client_i::init\n");
125
 
      return -1;
126
 
    }
127
 
  ACE_ENDTRY;
128
 
 
129
 
  // Register our <Input_Handler> to handle STDIN events, which will
130
 
  // trigger the <handle_input> method to process these events.
131
 
 
132
 
  if (ACE_Event_Handler::register_stdin_handler
133
 
      (this,
134
 
       TAO_ORB_Core_instance ()->reactor (),
135
 
       TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
136
 
    ACE_ERROR_RETURN ((LM_ERROR,
137
 
                       "%p\n",
138
 
                       "register_stdin_handler"),
139
 
                      -1);
140
 
  return 0;
141
 
}
142
 
 
143
 
int
144
 
Client_i::run (void)
145
 
{
146
 
  ACE_DEBUG ((LM_DEBUG,
147
 
              "\n============= Simple Chat =================\n"
148
 
              "========== type 'quit' to exit  ===========\n"));
149
 
 
150
 
  ACE_TRY_NEW_ENV
151
 
    {
152
 
      PortableServer::POAManager_var poa_manager =
153
 
        this->orb_manager_.poa_manager ();
154
 
      poa_manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
155
 
      ACE_TRY_CHECK;
156
 
 
157
 
      this->receiver_var_ =
158
 
        this->receiver_i_._this (ACE_ENV_SINGLE_ARG_PARAMETER);
159
 
      ACE_TRY_CHECK;
160
 
 
161
 
      // Register ourselves with the server.
162
 
      server_->add (this->receiver_var_.in (),
163
 
                    this->nickname_
164
 
                    ACE_ENV_ARG_PARAMETER);
165
 
      ACE_TRY_CHECK;
166
 
 
167
 
      // Run the ORB.
168
 
      this->orb_manager_.run (ACE_ENV_SINGLE_ARG_PARAMETER);
169
 
      ACE_TRY_CHECK;
170
 
    }
171
 
  ACE_CATCHANY
172
 
    {
173
 
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
174
 
                           "Client_i::run ()");
175
 
      return -1;
176
 
    }
177
 
  ACE_ENDTRY;
178
 
 
179
 
  return 0;
180
 
}
181
 
 
182
 
int
183
 
Client_i::handle_input (ACE_HANDLE)
184
 
{
185
 
  char buf[BUFSIZ];
186
 
 
187
 
  if (ACE_OS::fgets (buf, BUFSIZ, stdin) == 0)
188
 
    return 0;
189
 
 
190
 
  ACE_TRY_NEW_ENV
191
 
    {
192
 
      // Check if the user wants to quit.
193
 
      if (ACE_OS::strncmp (buf,
194
 
                           QUIT_STRING,
195
 
                           ACE_OS::strlen (QUIT_STRING)) == 0)
196
 
        {
197
 
          // Remove ourselves from the server.
198
 
          this->server_->remove (this->receiver_var_.in ());
199
 
          this->receiver_i_.shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
200
 
 
201
 
          ACE_TRY_CHECK;
202
 
          return 0;
203
 
        }
204
 
 
205
 
      // Call the server function <say> to pass the string typed by
206
 
      // the server.
207
 
      this->server_->say (this->receiver_var_.in (),
208
 
                          buf
209
 
                          ACE_ENV_ARG_PARAMETER);
210
 
      ACE_TRY_CHECK;
211
 
    }
212
 
  ACE_CATCHANY
213
 
    {
214
 
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
215
 
                           "Input_Handler::init");
216
 
      return -1;
217
 
    }
218
 
  ACE_ENDTRY;
219
 
 
220
 
  return 0;
221
 
}
222
 
 
223
 
int
224
 
Client_i::read_ior (const char *filename)
225
 
{
226
 
  // Open the file for reading.
227
 
  ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
228
 
 
229
 
  if (f_handle == ACE_INVALID_HANDLE)
230
 
    ACE_ERROR_RETURN ((LM_ERROR,
231
 
                       "Unable to open %s for writing: %p\n",
232
 
                       filename,
233
 
                       "invalid handle"),
234
 
                      -1);
235
 
 
236
 
  ACE_Read_Buffer ior_buffer (f_handle);
237
 
  char *data = ior_buffer.read ();
238
 
 
239
 
  if (data == 0)
240
 
    ACE_ERROR_RETURN ((LM_ERROR,
241
 
                       "Unable to read ior: %p\n"),
242
 
                      -1);
243
 
 
244
 
  this->ior_ = ACE_OS::strdup (data);
245
 
  ior_buffer.alloc ()->free (data);
246
 
 
247
 
  ACE_OS::close (f_handle);
248
 
 
249
 
  if (this->ior_ == 0)
250
 
    ACE_ERROR_RETURN ((LM_ERROR,
251
 
                       "failed to read ior from file\n",
252
 
                       ""),
253
 
                      -1);
254
 
  return 0;
255
 
}