summaryrefslogtreecommitdiff
path: root/TAO/tests/Quoter/client.cpp
blob: e783108696a3e795df16039cd1a7bfcf447f1b78 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// $Id$

#include "ace/Read_Buffer.h"
#include "orbsvcs/CosNamingC.h"
#include "client.h"

Quoter_Task::Quoter_Task (int argc, char **argv)
  : argc_ (argc), argv_ (argv)
{
  // Nothing
}

int 
Quoter_Task::svc (void)
{
  if (this->quoter_client.init (this->argc_, this->argv_) == -1)
    return 1;
  else
    return this->quoter_client.run ();

}

// Constructor.
Quoter_Client::Quoter_Client (void)
  : quoter_factory_key_ (0),
    quoter_key_ (ACE_OS::strdup ("key0")),
    shutdown_ (0),
    quoter_ (Stock::Quoter::_nil ()),
    quoter_factory_ior_file_ (0),
    f_handle_ (ACE_INVALID_HANDLE),
    use_naming_service_ (1)
{
}

// Reads the Quoter factory ior from a file

int
Quoter_Client::read_ior (char *filename)
{
  // Open the file for reading.
  this->f_handle_ = ACE_OS::open (filename,0);
  
  if (this->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 (this->f_handle_);
  this->quoter_factory_key_ = ior_buffer.read ();
  
  if (this->quoter_factory_key_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Unable to allocate memory to read ior: %p\n"),
                      -1);
   return 0;
}

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

int
Quoter_Client::parse_args (void)
{
  ACE_Get_Opt get_opts (argc_, argv_, "dn:f:k:xs");
  int c;
  int result;
  
  while ((c = get_opts ()) != -1)
    switch (c)
    {   
      case 'd':  // debug flag
        TAO_debug_level++;
        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 'k': // read the quoter IOR from the command-line.
        this->quoter_factory_key_ =
          ACE_OS::strdup (get_opts.optarg);
        break;
      case 'x': 
        this->shutdown_ = 1;
        break;
      case 's': // Don't use the TAO Naming Service.
        this->use_naming_service_ = 0;
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                          "usage:  %s"
                          " [-d]"
                          " [-f quoter_factory-obj-ref-key-file]"
                          " [-k quoter-obj-ref-key]"
                          " [-x]"
                          " [-s]"
                          "\n",
                          this->argv_ [0]),
                          -1);
    }
  
  // Indicates successful parsing of command line.
  return 0;
}

int
Quoter_Client::run (void)
{
  CORBA::Long q = this->quoter_->get_quote ("ACE Hardware", this->env_);
  if (this->env_.exception () != 0)
    {
      this->env_.print_exception ("with get_quote");
      return -1;
    }
  else
    { 
      ACE_DEBUG ((LM_DEBUG, "ACE Hardware = %i\n", q));
      return q;
    }
}

Quoter_Client::~Quoter_Client (void)
{
  // Free resources
  // Close the ior files
  if (this->quoter_factory_ior_file_)
    ACE_OS::fclose (this->quoter_factory_ior_file_);
  if (this->f_handle_ != ACE_INVALID_HANDLE)
    ACE_OS::close (this->f_handle_);

  CORBA::release (this->quoter_);

  if (this->quoter_factory_key_ != 0)
    ACE_OS::free (this->quoter_factory_key_);
  if (this->quoter_key_ != 0)
    ACE_OS::free (this->quoter_key_);
}

int
Quoter_Client::init_naming_service (void)
{
  TAO_TRY
  {
    // Resolve the Naming Service
    CORBA::Object_var naming_obj = 
      orb_->resolve_initial_references ("NameService");

    if (CORBA::is_nil (naming_obj.in ()))
      ACE_ERROR_RETURN ((LM_ERROR,
			   " (%P|%t) Unable to resolve the Name Service.\n"),
         -1);

    CosNaming::NamingContext_var naming_context = 
      CosNaming::NamingContext::_narrow (naming_obj.in (), TAO_TRY_ENV);
    TAO_CHECK_ENV;

    ACE_DEBUG ((LM_DEBUG, "Have a proper reference to the Naming Service.\n"));

    // ------------------------------------------- direct use of the Quoter Factory
    /*
    // Try to get the quoter_factory
    CosNaming::Name quoter_factory_name (2);
    quoter_factory_name.length (2);
    quoter_factory_name[0].id = CORBA::string_dup ("IDL_Quoter");
    quoter_factory_name[1].id = CORBA::string_dup ("quoter_factory");

    ACE_DEBUG ((LM_DEBUG, "Trying to resolve the Quoter Factory!\n"));

    CORBA::Object_var factory_obj =
      naming_context->resolve (quoter_factory_name,
                               TAO_TRY_ENV);
    TAO_CHECK_ENV;

    ACE_DEBUG ((LM_DEBUG, "Resolved the Quoter Factory!\n"));
    
    this->factory_var_ =
      Stock::Quoter_Factory::_narrow (factory_obj.in (), 
                                      TAO_TRY_ENV);
    TAO_CHECK_ENV;
   
    if (CORBA::is_nil (this->factory_var_->in ()))
      ACE_ERROR_RETURN ((LM_ERROR,
      " could not resolve quoter factory in Naming service <%s>\n"),
      -1);*/
    // -------------------------------------------- end of direct use

    CosNaming::Name quoterFactoryFinderName (2);
    quoterFactoryFinderName.length (2);
    quoterFactoryFinderName[0].id = CORBA::string_dup ("IDL_Quoter");
    quoterFactoryFinderName[1].id = CORBA::string_dup ("QuoterFactoryFinder");

    ACE_DEBUG ((LM_DEBUG, "Trying to resolve the Quoter Factory Finder!\n"));

    CORBA::Object_var factory_obj =
      naming_context->resolve (quoterFactoryFinderName,
                               TAO_TRY_ENV);
    TAO_CHECK_ENV;

    ACE_DEBUG ((LM_DEBUG, "Resolved the Quoter Factory Finder!\n"));
    
    Stock::QuoterFactoryFinder_var factoryFinder_var =
      Stock::QuoterFactoryFinder::_narrow (factory_obj.in (), 
                                      TAO_TRY_ENV);
    TAO_CHECK_ENV;
   
    if (CORBA::is_nil (factoryFinder_var.in ()))
      ACE_ERROR_RETURN ((LM_ERROR,
      " could not resolve quoter factory in Naming service <%s>\n"),
      -1);

    ACE_DEBUG ((LM_DEBUG, "Have a proper reference to the Quoter Factory Finder.\n"));

    // The name of the Quoter Factory
    CosLifeCycle::Key factoryName (1);  // max = 1 
    factoryName.length(1);
    factoryName[0].id =
    CORBA::string_dup ("quoter_factory");
    
    // Find an appropriate factory over there.
    CosLifeCycle::Factories_ptr factories_ptr = 
        factoryFinder_var->find_factories (factoryName, TAO_TRY_ENV);

    if (factories_ptr == 0)
      ACE_ERROR_RETURN ((LM_ERROR,
      "Did not get a Quoter Factory.\n"),
      -1);

    // Get the first object reference to a factory.
    CORBA::Object_var quoter_FactoryObj_var = (*factories_ptr)[0];


      // Narrow it to a Quoter Factory.
    factory_var_ = Stock::Quoter_Factory::_narrow (quoter_FactoryObj_var.in (),
                                                   TAO_TRY_ENV);

    TAO_CHECK_ENV;

    if (CORBA::is_nil (this->factory_var_.in ()))
      ACE_ERROR_RETURN ((LM_ERROR,
      "Factory received is not valid.\n"),
      -1);

    ACE_DEBUG ((LM_DEBUG, "Have a proper reference to the Quoter Factory.\n"));
  }
  TAO_CATCH (CosLifeCycle::NoFactory, excpt)
  {
    TAO_TRY_ENV.print_exception ("Quoter::init_naming_service: No Factory available!");
  }
  TAO_CATCHANY
  {
    TAO_TRY_ENV.print_exception ("Quoter::init_naming_service");
    return -1;
  }
  TAO_ENDTRY;
  
  return 0;
}

int
Quoter_Client::init (int argc, char **argv)
{
  this->argc_ = argc;
  this->argv_ = argv;
  
  TAO_TRY
  {
      // Retrieve the ORB.
      this->orb_ = CORBA::ORB_init (this->argc_,
                                    this->argv_,
                                    "internet",
                                    TAO_TRY_ENV);
      TAO_CHECK_ENV;
    
      // Parse command line and verify parameters.
      if (this->parse_args () == -1)
        return -1;
    
      if (this->use_naming_service_) 
      {
          int naming_result = this->init_naming_service ();
          if (naming_result == -1)
            return naming_result;
      }
      else
        {
          if (this->quoter_factory_key_ == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "%s: no quoter factory key specified\n",
                               this->argv_[0]),
                              -1);

          CORBA::Object_var factory_object = 
            this->orb_->string_to_object (this->quoter_factory_key_,
                                          TAO_TRY_ENV);
          TAO_CHECK_ENV;
      
          this->factory_var_ = Stock::Quoter_Factory::_narrow (factory_object.in (), 
                                                           TAO_TRY_ENV);
          TAO_CHECK_ENV;
      
          if (CORBA::is_nil (this->factory_var_.in ()))
            {
              ACE_ERROR_RETURN ((LM_ERROR,"invalid factory key <%s>\n",
                                 this->quoter_factory_key_),
                                -1);
            }
        }
    
      ACE_DEBUG ((LM_DEBUG, "Factory received OK\n"));
    
      // Now retrieve the Quoter obj ref corresponding to the key.
      this->quoter_ =
        this->factory_var_->create_quoter (this->quoter_key_,
                                     TAO_TRY_ENV);
      TAO_CHECK_ENV;

      ACE_DEBUG ((LM_DEBUG, "Quoter Created\n"));
    
      if (CORBA::is_nil (this->quoter_))
      {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "null quoter objref returned by factory\n"),
                            -1);
      }
    }
  TAO_CATCHANY
  {
    TAO_TRY_ENV.print_exception ("Quoter::init");
    return -1;
  }
  TAO_ENDTRY;
  
  return 0;
}


// This function runs the test.

int
main (int argc, char **argv)
{
//  Quoter_Client quoter_client;
//
//  ACE_DEBUG ((LM_DEBUG,"\n \t IDL_Quoter: client \n\n"));
//
//  if (quoter_client.init (argc, argv) == -1)
//    return 1;
//  else
//    return quoter_client.run ();

  ACE_Thread_Manager thr_mgr;

  Quoter_Task client1 (argc, argv);
//  Quoter_Task client2 (argc, argv);
//  Quoter_Task client3 (argc, argv);

  client1.activate (THR_BOUND | ACE_SCHED_FIFO, 1, 0, ACE_DEFAULT_THREAD_PRIORITY);
//  client2.activate (THR_BOUND | ACE_SCHED_FIFO, 1, 0, ACE_DEFAULT_THREAD_PRIORITY);
//  client3.activate (THR_BOUND | ACE_SCHED_FIFO, 1, 0, ACE_DEFAULT_THREAD_PRIORITY);

  return ACE_Thread_Manager::instance ()->wait ();
}