summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/InterfaceRepo/Application_Test/ifr_dii_client.cpp
blob: f6dd2238e125c54599df848cc34f1c081a72d7ff (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// -*- C++ -*-
// $Id$

#include "ifr_dii_client.h"
#include "ace/Get_Opt.h"

ACE_RCSID (Application_Test,
           ifr_dii_client,
           "$Id$")

IFR_DII_Client::IFR_DII_Client (void)
  : namespace_name (CORBA::string_dup ("warehouse")),
    interface_name (CORBA::string_dup ("inventory")),
    op_name (CORBA::string_dup ("getCDinfo")),
    lookup_by_name_ (false),
    debug_ (false)
{
}

IFR_DII_Client::~IFR_DII_Client (void)
{
}

int
IFR_DII_Client::init (int argc,
                      char *argv[]
                      ACE_ENV_ARG_DECL)
{
  this->orb_ = CORBA::ORB_init (argc,
                                argv,
                                0
                                ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // In a reall application, we would get the scoped or
  // local name from the Interface Repository and use that
  // to get the object reference of the target via the Naming
  // Service. Since we're not testing the Naming Service here,
  // we just use the IOR which is stored in a file by the server.
  this->target_ =
    this->orb_->string_to_object ("file://iorfile"
                                  ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);


  if (CORBA::is_nil (this->target_.in ()))
  {
     ACE_ERROR_RETURN ((
        LM_ERROR,
        "Unable to find interface repository in: file://iorfile\n"),
        -1);
  }

  if (this->parse_args (argc, argv) == -1)
    {
      return -1;
    }

  return 0;
}

int
IFR_DII_Client::run (ACE_ENV_SINGLE_ARG_DECL)
{
  int result = 0;

  if (this->lookup_by_name_)
    {
      result = this->lookup_interface_def (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      if (result == -1)
        {
          return -1;
        }
    }
  else
    {
      result = this->find_interface_def (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      if (result == -1)
        {
          return (-1);
        }
    }

  this->get_operation_def (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  this->create_dii_request (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  this->invoke_and_display (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  return 0;
}

int
IFR_DII_Client::parse_args (int argc,
                            char *argv[])
{
  ACE_Get_Opt opts (argc, argv, "dn");
  int c;

  while ((c = opts ()) != -1)
    switch (c)
      {
        case 'd':
          this->debug_ = true;
          break;
        case 'n':   // Select lookup by name.
          this->lookup_by_name_ = true;
          break;
        case '?':
        default:
          ACE_ERROR_RETURN ((LM_ERROR,
                            "usage: %s"
                            " [-n]"
                            "\n",
                            argv [0]),
                            -1);
      }

  return 0;
}

int
IFR_DII_Client::find_interface_def (ACE_ENV_SINGLE_ARG_DECL)
{
  this->target_def_ =
    this->target_->_get_interface (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  if (CORBA::is_nil (this->target_def_.in ()))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "Unable to find interface def\n"),
                        -1);
    }

  return 0;
}

int
IFR_DII_Client::lookup_interface_def (ACE_ENV_SINGLE_ARG_DECL)
{
  CORBA::Object_var obj =
    this->orb_->resolve_initial_references ("InterfaceRepository"
                                            ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN(-1);

  this->repo_ = CORBA::Repository::_narrow (obj.in ()
                                            ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN(-1);

  // Is there a contained object of some kind at any level in the
  // repository called "warehouse"?
  CORBA::ContainedSeq_var candidates =
    this->repo_->lookup_name (this->namespace_name.in (),
                              -1,            // Unlimited level recursion.
                              CORBA::dk_all, // Any type of contained object.
                              1              // Exclude parents of interfaces.
                              ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN(-1);

  CORBA::ULong length = candidates->length ();
  CORBA::Container_var candidate;
  CORBA::ContainedSeq_var interfaces;
  CORBA::ULong n_interfaces = 0;
  CORBA::String_var name;

  // No point continuing; theres nothing to look at.
  if (length == 0)
  {
     return -1;
  }

  // The length is 1 in this case, but in general, it could
  // be any length.
  for (CORBA::ULong i = 0; i < length; ++i)
    {
      candidate =
        CORBA::Container::_narrow (candidates[i]
                                   ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN(-1);

      // Is this contained item itself a container?
      if (!CORBA::is_nil (candidate.in ()))
        {
          // Does this container contain any interfaces?
          interfaces = candidate->contents (CORBA::dk_Interface,
                                            1     // Exclude parents.
                                            ACE_ENV_ARG_PARAMETER);
          ACE_CHECK_RETURN(-1);

          n_interfaces = interfaces->length ();

          // Here we are just getting out of the loop (which
          // only has length 1 anyway) when we see the first
          // container that contains at least one interface.
          // In a real application, we'd probably have a more
          // useful criterion,
          if (n_interfaces > 0)
            {
              break;
            }
        }
    }

  // The length is 1 in this case, but in general, it could
  // be any length.
  for (CORBA::ULong j = 0; j < n_interfaces  ; ++j)
    {
      name = interfaces[j]->name (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN(-1);

      if (!ACE_OS::strcmp (name.in (), this->interface_name.in ()))
        {
          this->target_def_ =
            CORBA::InterfaceDef::_narrow (interfaces[j]
                                          ACE_ENV_ARG_PARAMETER);
          ACE_CHECK_RETURN(-1);
        }
    }
  return 0;
}

void
IFR_DII_Client::get_operation_def (ACE_ENV_SINGLE_ARG_DECL)
{
  // What operation(s) does this interface contain?
  CORBA::ContainedSeq_var operations =
    this->target_def_->contents (CORBA::dk_Operation,
                                 0  // Do not exclude inherited operations.
                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::ULong n_operations = operations->length ();
  CORBA::String_var operation_name;

  // The length is 1 in this case, but in general, it could
  // be any length.
  for (CORBA::ULong i = 0; i < n_operations; ++i)
    {
      operation_name = operations[i]->name (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      if (!ACE_OS::strcmp (operation_name.in (), this->op_name.in ()))
        {
          this->op_ =
            CORBA::OperationDef::_narrow (operations[i]
                                          ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;

          break;
        }
    }
}

void
IFR_DII_Client::create_dii_request (ACE_ENV_SINGLE_ARG_DECL)
{
  this->req_ = this->target_->_request (this->op_name.in ()
                                        ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  this->result_ = this->op_->result (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->req_->set_return_type (this->result_.in ());

  CORBA::ParDescriptionSeq_var params =
    this->op_->params (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::ULong length = params->length ();

  // This example of the discovery of parameter information is
  // purposely contrived for the sake of brevity. A real
  // application would have more versatile code here, and much
  // more of it.
  for (CORBA::ULong i = 0; i < length; ++i)
    {
      CORBA::TCKind const kind =
        params[i].type->kind (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      switch (params[i].mode)
      {
        case CORBA::PARAM_IN:
          if (kind == CORBA::tk_string
              && ACE_OS::strcmp (params[i].name.in (), "artist") == 0)
            {
              // The servant will match the substring 'Beatles'.
              this->req_->add_in_arg (params[i].name.in ()) <<= "the Beatles";
            }

          break;
        case CORBA::PARAM_INOUT:
          if (kind == CORBA::tk_string
              && ACE_OS::strcmp (params[i].name.in (), "title") == 0)
            {
              // This isn't the exact title, but the servant will find the
              // partial match, and return the full, correct title.
              this->req_->add_inout_arg (params[i].name.in ()) <<= "Sgt. Pepper's";
            }

          break;
        case CORBA::PARAM_OUT:
          {
            if (kind == CORBA::tk_float
                && ACE_OS::strcmp (params[i].name.in (), "price") == 0)
              {
                CORBA::Float tmp = -1.0f;
                CORBA::Any any;
                any <<= tmp;

                // The servant will return 0.0 if the title is not found.
                this->req_->arguments ()->add_value (params[i].name.in (),
                                                     any,
                                                     CORBA::ARG_OUT
                                                     ACE_ENV_ARG_PARAMETER);
                ACE_CHECK;
              }

            break;
          }
      }
    }
}

void
IFR_DII_Client::invoke_and_display (ACE_ENV_SINGLE_ARG_DECL)
{
  this->req_->invoke (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::TypeCode_var tc = this->req_->return_value ().type ();

  CORBA::TCKind const kind = tc->kind (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  if (kind == CORBA::tk_boolean)
    {
      CORBA::NVList_ptr args = this->req_->arguments ();

      const char *artist = 0;

# if (defined (_MSC_VER) && (_MSC_VER < 1310))
      ACE_ASSERT ((*args->item (0)->value () >>= artist) == 1);
# else
      ACE_ASSERT ((*args->item (0)->value () >>= artist) == true);
# endif  /* _MSC_VER <= 1310 */

      ACE_ASSERT (ACE_OS::strcmp (artist, "the Beatles") == 0);

      const char *title = 0;

# if (defined (_MSC_VER) && (_MSC_VER < 1310))
      ACE_ASSERT ((*args->item (1)->value () >>= title) == 1);
# else
      ACE_ASSERT ((*args->item (1)->value () >>= title) == true);
# endif  /* _MSC_VER <= 1310 */

      const char *correct = "Sgt. Pepper's Lonely Hearts Club Band";
      ACE_ASSERT (ACE_OS::strcmp (title, correct) == 0);
      ACE_UNUSED_ARG (correct);

      CORBA::Float price = 0.0f;

# if (defined (_MSC_VER) && (_MSC_VER < 1310))
      ACE_ASSERT ((*args->item (2)->value () >>= price) == 1);
# else
      ACE_ASSERT ((*args->item (2)->value () >>= price) == true);
# endif  /* _MSC_VER <= 1310 */

      ACE_ASSERT (price == 13.49f);

      if (this->debug_)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("%s:\t%s\n")
                      ACE_TEXT ("%s:\t%s\n")
                      ACE_TEXT ("%s:\t$%2.2f\n"),
                      args->item (0)->name (),
                      artist,
                      args->item (1)->name (),
                      title,
                      args->item (2)->name (),
                      price));
        }

      CORBA::Boolean in_stock = 0;

      CORBA::Boolean ret_status =
        (this->req_->return_value () >>= CORBA::Any::to_boolean (in_stock));
      ACE_UNUSED_ARG (ret_status);

      ACE_ASSERT (ret_status == 1);
      ACE_ASSERT (in_stock == 1);

      if (this->debug_)
        {
          if (in_stock)
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("status: in stock\n")));
            }
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("status: out of stock\n")));
            }
        }
    }
}