summaryrefslogtreecommitdiff
path: root/TAO/examples/Callback_Quoter/Consumer_Handler.cpp
blob: 0ea7efa10023154e7bf00779ed7eafeb4c92ecbf (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// $Id$

// ===========================================================
//
//
// = LIBRARY
//    TAO/examples/Callback_Quoter
//
// = FILENAME
//    Consumer_Input_Handler.cpp
//
// = DESCRIPTION
//    Implementation of the Consumer_Handler class.
//
// = AUTHOR
//    Kirthika Parameswaran <kirthika@cs.wustl.edu>
//
// ===========================================================

#include "Consumer_Handler.h"
#include "ace/Read_Buffer.h"
#include "tao/ORB.h"
#include "ace/Get_Opt.h"
#include "ace/Read_Buffer.h"
#include "ace/OS.h"
#include "ace/Reactor.h"
#include "ace/Event_Handler.h"

Consumer_Handler::Consumer_Handler (void)
  : stock_name_ ("Unknown"),
    threshold_value_ (0),
    server_ (),
    registered_ (0),
    unregistered_ (0),
    ior_ (0),
    shutdown_ (0),
    interactive_ (0)
{

}

Consumer_Handler::~Consumer_Handler (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"));
}

// Reads the Server factory IOR from a file.

int
Consumer_Handler::read_ior (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),
                      -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);

  return 0;
}

// Parses the command line arguments and returns an error status.

int
Consumer_Handler::parse_args (void)
{
  ACE_Get_Opt get_opts (argc_, argv_, "a:t:d:f:xk:xs");
  int c;
  int result;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':  // debug flag
	TAO_debug_level++; //****
        break;

      case 'k':  // ior provide on command line
        this->ior_ = ACE_OS::strdup (get_opts.optarg);
        break;

      case 'f': // read the IOR from the file.
        result = this->read_ior (get_opts.optarg);
        if (result < 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Unable to read ior from %s : %p\n",
                             get_opts.optarg),
                            -1);
        break;

      case 's': // don't use the naming service
	this->use_naming_service_ = 0;
	break;

      case 'a':
        this->stock_name_ = get_opts.optarg;
        this->interactive_ = 0;
        break;

      case 't':
        this->threshold_value_ = ACE_OS::atoi (get_opts.optarg);
        break;

      case 'x':
        this->shutdown_ = 1;
        break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           " [-d]"
                           " [-f ior-file]"
                           " [-k ior]"
                           " [-x]"
			   " [-s]"
			   " [-a stock_name]"
			   " [-t threshold]"
                           "\n",
                           this->argv_ [0]),
                          -1);
      }

  // Indicates successful parsing of command line.
  return 0;
}

// this method uses the naming service to obtain the server object refernce.

int
Consumer_Handler::via_naming_service (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // Initialization of the naming service.
      if (naming_services_client_.init (orb_.in ()) != 0)
	ACE_ERROR_RETURN ((LM_ERROR,
			   " (%P|%t) Unable to initialize "
			   "the TAO_Naming_Client. \n"),
			  -1);

      CosNaming::Name notifier_ref_name (1);
      notifier_ref_name.length (1);
      notifier_ref_name[0].id = CORBA::string_dup ("Notifier");

      CORBA::Object_var notifier_obj =
	this->naming_services_client_->resolve (notifier_ref_name,
						ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // The CORBA::Object_var object is downcast to Notifier_var using
      // the <_narrow> method.
      this->server_ =
        Notifier::_narrow (notifier_obj.in (),
                           ACE_TRY_ENV);

      ACE_TRY_CHECK;

    }
  ACE_CATCHANY
    {
      ACE_TRY_ENV.print_exception ("Consumer_Handler::via_naming_service\n");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

// Init function.
int
Consumer_Handler::init (int argc, char **argv)
{
  this->argc_ = argc;
  this->argv_ = argv;

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

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // Retrieve the ORB.
      this->orb_ = CORBA::ORB_init (this->argc_,
                                    this->argv_,
                                    0,
                                    ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Parse command line and verify parameters.
      if (this->parse_args () == -1)
	ACE_ERROR_RETURN ((LM_ERROR,
			   "parse_args failed\n"),
			   -1);

      // use the naming service.
       if (this->use_naming_service_)
	 return  via_naming_service ();


       if (this->ior_ == 0)
         ACE_ERROR_RETURN ((LM_ERROR,
                            "%s: no ior specified\n",
                            this->argv_[0]),
                           -1);

      CORBA::Object_var server_object =
        this->orb_->string_to_object (this->ior_,
                                      ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (server_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "invalid ior <%s>\n",
                           this->ior_),
                          -1);
      // The downcasting from CORBA::Object_var to Notifier_var is
      // done using the <_narrow> method.
      this->server_ = Notifier::_narrow (server_object.in (),
                                         ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_NEW_RETURN (this->consumer_servant_,
		      Consumer_i (),
		      -1);
      // Get the consumer stub (i.e consumer object) pointer.
      this->consumer_var_ =
	this->consumer_servant_->_this (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (this->interactive_)
        {
          ACE_NEW_RETURN (consumer_input_handler_,
                          Consumer_Input_Handler (this),
                          -1);

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

          // Register the signal event handler for ^C
          ACE_NEW_RETURN (consumer_signal_handler_,
                          Consumer_Signal_Handler (this),
                          -1);

          if( this->reactor_used ()->register_handler
              (SIGINT,
               consumer_signal_handler_) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "%p\n",
                               "register_handler for SIGINT"),
                              -1);
        }
      else
        {
          // @@ Encapsulate this in a little method...

          // Register with the server.
          this->server_->register_callback (this->stock_name_,
                                            this->threshold_value_,
                                            this->consumer_var_.in (),
                                            ACE_TRY_ENV);
          ACE_TRY_CHECK;

          // Note the registration.
          this->registered_ = 1;
          this->unregistered_ = 0;

          ACE_DEBUG ((LM_DEBUG,
                      "registeration done!\n"));
        }

    }
  ACE_CATCHANY
    {
      ACE_TRY_ENV.print_exception ("Consumer_Handler::init");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}

int
Consumer_Handler::run (void)
{
  // Set the orb in the consumer_ object.
  this->consumer_servant_->orb (this->orb_.in ());

  ACE_DEBUG ((LM_DEBUG,
              " Services provided:\n "
              " * Registration <type 'r'>\n "
              " * Unregistration <type 'u'>\n "
              " * Quit <type 'q'>\n "));

  // Run the ORB.
  this->orb_->run ();
  return 0;
}

ACE_Reactor *
Consumer_Handler::reactor_used (void) const
{
  //*done* @@ Please check with Pradeep and see how to remove the reliance
  // on <TAO_ORB_Core_instance()>.  This is non-portable and we want
  // to try to use only CORBA-compliant code in our examples.

  // Cant do anything as the reactor is not accessible in any other way.
  return TAO_ORB_Core_instance ()->reactor ();
}