summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Interoperable_Naming/ncontextext_client_i.cpp
blob: 2f6f2d7ad72dca8e113065c1bee43167151f7aa7 (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
// $Id$
//

// ===========================================================
//
// = LIBRARY
//    TAO/ORBSVCS/tests/SimpleNaming
//
// = FILENAME
//    ncontextext_client_i.cpp
//
// = DESCRIPTION
//    This class implements a simple CORBA client which
//    converts a Name to a string and viceversa, forms a IIOPNAME
//    url address and can resolve a stringified name.
//
// = AUTHORS
//    Priyanka Gontla <pgontla@ece.uci.edu>
//
//============================================================

#include "ncontextext_client_i.h"
#include "tao/debug.h"
#include "ace/Get_Opt.h"
#include "ace/Read_Buffer.h"

// FUZZ: disable check_for_streams_include
#include "ace/streams.h"

// Constructor
NContextExt_Client_i::NContextExt_Client_i (void)
{
}

NContextExt_Client_i::~NContextExt_Client_i (void)
{
}


// Parses the command line arguments and returns an
// error status
int
NContextExt_Client_i::parse_args (void)
{

  ACE_Get_Opt get_opts (argc_, argv_, "dvs");
  int c;

  this->view_ = 1;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':  // debug flag
        TAO_debug_level++;
        break;
      case 'v':  // output needed
        this->view_ = 0;
        break;
      case 's':
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           " [-d]"
                           " [-v]"
                           "\n",
                           this->argv_ [0]),
                          -1);
      }

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

char *
NContextExt_Client_i::get_name ()
{

  // USe time (NULL) to produce the seed:
  srand (time (0));

  const int len = 10;
  char *name_component = CORBA::string_alloc (len);
  char *name_componentPtr = name_component;


  for (int i = 0; i < len; ++i)
    {

      int rand_value = rand () % 10;

      switch (rand_value)
        {
        case 0:
          *name_componentPtr = '/';
          ++name_componentPtr;
          break;

        case 1:
          *name_componentPtr = '.';
          ++name_componentPtr;
          break;

        case 2:
          *name_componentPtr = '\\';
          ++name_componentPtr;
          break;

        case 3:
          *name_componentPtr = '<';
          ++name_componentPtr;
          break;

        case 4:
          *name_componentPtr = '>';
          ++name_componentPtr;
          break;

        case 5:
          *name_componentPtr = ' ';
          ++name_componentPtr;
          break;

        case 6:
          *name_componentPtr = '%';
          ++name_componentPtr;
          break;

        case 7:
        case 8:
        case 9:
          *name_componentPtr = 'A' + ( rand () % 26 );
          ++name_componentPtr;
          break;

        default:
          ACE_ERROR ((LM_ERROR, "shouldnt come here"));
          break;
        }
    }

  *name_componentPtr = '\0';

  return name_component;

}

int
NContextExt_Client_i::run (ACE_ENV_SINGLE_ARG_DECL)
{

  ACE_TRY_EX (OuterBlock)
    {
      CosNaming::Name name;

      name.length (2);
      name[0].id = CORBA::string_dup (this->get_name ());
      name[0].kind = CORBA::string_dup (this->get_name ());
      name[1].id = CORBA::string_dup ("Iterator_Factory");
      name[1].kind = CORBA::string_dup ("factory");

      // Get the stringified form of the name
       CORBA::String_var str_name =
        this->naming_context_->to_string (name
                                          ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK_EX (OuterBlock);

      CORBA::Object_var factory_object;

      ACE_TRY_EX (InnerBlock)
        {
          // Resolve the name using the stringified form of the name
          factory_object =
            this->naming_context_->resolve_str (str_name.in ()
                                                ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK_EX (InnerBlock);
        }
      ACE_CATCH (CosNaming::NamingContext::NotFound, ex)
        {
        }
      ACE_ENDTRY;

      // Narrow
      Web_Server::Iterator_Factory_var factory =
        Web_Server::Iterator_Factory::_narrow (factory_object.in () ACE_ENV_ARG_PARAMETER);

      ACE_TRY_CHECK_EX (OuterBlock);

      // Create bindings
      CosNaming::BindingIterator_var iter;
      CosNaming::BindingList_var bindings_list;

      this->naming_context_->list (2,
                                   bindings_list.out (),
                                   iter.out ()
                                   ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK_EX (OuterBlock);

      // Convert the stringified name back as CosNaming::Name and print
      // them out.
      CosNaming::Name *nam =
        this->naming_context_->to_name (str_name.in ()
                                        ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK_EX (OuterBlock);

      // Declare a CosNaming::Name variable and assign length to it.
      CosNaming::Name nm;
      nm.length (nam->length ());

      nm = *nam;

      // Test the to_url function:
      // For the address, we are assigning
      // some random address now. But, in real applications, the address
      // denotes the address of the NamingContext possibly returned from
      // the LocateReply or LOCATION_FORWARD reply by an agent listening
      // at that address
      //
      CORBA::String_var address =
        CORBA::string_dup (":myhost.555xyz.com:9999");

      // Since we are just testing the functionality of the to_url
      // function, use a random object name.
      CORBA::String_var obj_name = get_name ();

      CORBA::String_var url_string =
        this->naming_context_->to_url (address.in (),
                                       obj_name.in()
                                       ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK_EX (OuterBlock);

      if (this->view_ == 0)
        {
          this->print_values (name,
                              str_name,
                              nm,
                              obj_name,
                              url_string);
        }
    }
  ACE_CATCH (CORBA::NO_MEMORY, ex)
    {
      ACE_PRINT_EXCEPTION (ex, "A system exception oc client side");
      return -1;
    }
  ACE_CATCH (CORBA::SystemException, ex)
    {
      ACE_PRINT_EXCEPTION (ex, "A system exception oc client side");
      return -1;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "client");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

int
NContextExt_Client_i::init (int argc, char **argv)
{
  this->argc_ = argc;
  this->argv_ = argv;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {

      // First initialize the ORB, that will remove some arguments...
      CORBA::ORB_var orb =
        CORBA::ORB_init (this->argc_,
                         this->argv_,
                         "" /* the ORB name, it can be anything! */
                         ACE_ENV_ARG_PARAMETER);

      // There must be at least one argument, the file that has to be
      // retrieved
      if (this->parse_args () == -1)
        return 1;

      // Get a reference to the Naming Service
      CORBA::Object_var naming_context_object =
        orb->resolve_initial_references ("NameService" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (naming_context_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot resolve Naming Service\n"),
                          1);

      // Narrow to get the correct reference
      this->naming_context_ =
        CosNaming::NamingContextExt::_narrow (naming_context_object.in ()
                                              ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (this->naming_context_.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot narrow Naming Service\n"),
                          1);
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "client");
      return 1;
    }
  ACE_ENDTRY;

  return 0;
}

void
NContextExt_Client_i::print_values (CosNaming::Name name,
                                    CORBA::String_var str_name,
                                    CosNaming::Name nm,
                                    CORBA::String_var obj_name,
                                    CORBA::String_var url_string)
{

  ACE_DEBUG((LM_DEBUG, ACE_TEXT ("The first component id is %s,"
             "The first component kind is %s,"
             "The second component id is %s,"
             "The second component kind is %s\n\n"),
             name[0].id.in (),
             name[0].kind.in (),
             name[1].id.in (),
             name[1].kind.in ()));

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("The string form of the input name is: \n%s\n\n"),
              str_name.in ()));

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("The unstringified version of the name components are:,"
              "The first component id is %s,"
              "The first component kind is %s,"
              "The second component id is %s,"
              "The second component kind is %s\n\n"),
              nm[0].id.in (),
              nm[0].kind.in (),
              nm[1].id.in (),
              nm[1].kind.in ()));

 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("When the address of the NamingContext is:"
             "myhost.555xyz.com:9999"
             "and the Object name is \n%s\n"),
             obj_name.in ()));

 ACE_DEBUG ((LM_DEBUG,ACE_TEXT ("The URL form of the string is \n %s\n"),
             url_string.in ()));

}