summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Concurrency/CC_client.cpp
blob: 0e85c5d7de799efe762e8c9e813348df1d940d90 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/orbsvcs/tests
//
// = FILENAME
//    CC_client.h
//
// = DESCRIPTION
//      This is the test class for the concurrency service. The class
//      implements a client to the concurrency service.
//      This file contains the main function for the test.
//
// = AUTHORS
//      Torben Worm <tworm@cs.wustl.edu>
//
// ============================================================================

#include "CC_client.h"

#include "tao/debug.h"

#include "ace/Read_Buffer.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_unistd.h"
#include "ace/os_include/os_ctype.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_fcntl.h"

ACE_RCSID(Concurrency, CC_client, "$Id$")

// Constructor.
CC_Client::CC_Client (void)
  : naming_service_ (0),
    cc_factory_ior_file_ (0),
    cc_factory_key_ (0),
    f_handle_ (ACE_INVALID_HANDLE),
    shutdown_ (0),
    use_naming_service_ (1),
    run_basic_tests_ (0),
    run_extended_tests_ (0),
    use_script_file_ (0),
    script_file_ (0)
{
}

CC_Client::~CC_Client (void)
{
  // Free resources and close the ior files.
  if (this->cc_factory_ior_file_)
    ACE_OS::fclose (this->cc_factory_ior_file_);

  if (this->f_handle_ != ACE_INVALID_HANDLE)
    ACE_OS::close (this->f_handle_);

  if (this->cc_factory_key_ != 0)
    ACE_OS::free (this->cc_factory_key_);

  if (naming_service_!=0)
    delete naming_service_;
}

// Reads the lock set factory ior from a file

int
CC_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->cc_factory_key_ = ior_buffer.read ();

  if (this->cc_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
CC_Client::parse_args (void)
{
  ACE_Get_Arg_Opt<char> get_opts (argc_, argv_, "dc:sf:k:xbhe:");
  int c;
  int result;

  if(argc_==1) // No arguments given on command line
    {
      print_usage();
      return -1;
    }

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'b':  // debug flag
        this->run_basic_tests_ = 1;
        break;
      case 'c':
        this->use_script_file_ = 1;
        this->script_file_ = ACE_OS::strdup (get_opts.opt_arg ());
        break;
      case 'd':  // debug flag
        TAO_debug_level++;
        break;
      case 'e':  // debug flag
        run_extended_tests_ = 1;
        this->extended_tests_params_ = ACE_OS::strdup (get_opts.opt_arg ());
        break;
      case 'f': // read the IOR from the file.
        result = this->read_ior (get_opts.opt_arg ());
        if (result < 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Unable to read ior from %s : %p\n",
                             get_opts.opt_arg ()),
                            -1);
            break;
      case 'k': // read the cubit IOR from the command-line.
        this->cc_factory_key_ =
          ACE_OS::strdup (get_opts.opt_arg ());
        break;
      case 'x':
        this->shutdown_ = 1;
        break;
      case 's': // Don't use the TAO Naming Service.
        this->use_naming_service_ = 0;
        break;
      case 'h':
      default:
        print_usage ();
        ACE_ERROR_RETURN ((LM_ERROR, ""), -1);
      }

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

// Execute client example code.

int
CC_Client::run (void)
{
  int tests_run = 0;
  // Tells whether any tests have been run

  int success = CC_SUCCESS;
  // Did test succeed?

  if (this->run_basic_tests_ && success == CC_SUCCESS)
    {
      success = run_basic_tests ();
      if(success==CC_FAIL)
        ACE_DEBUG((LM_DEBUG, "Basic tests did not succeed\n"));
      tests_run = 1;
    }

  if (this->run_extended_tests_ && success == CC_SUCCESS)
    {
      success = run_extended_tests (this->extended_tests_params_);
      if(success==CC_FAIL)
        ACE_DEBUG((LM_DEBUG, "Extended tests did not succeed\n"));
      tests_run = 1;
    }

  if(this->use_script_file_ && success == CC_SUCCESS)
    {
      cmdlist = new CC_CommandList();
      FILE *f;

      // Open the command file for parsing if the filename!=stdin
      if(ACE_OS::strcmp(this->script_file_, "stdin")!=0)
        {
          f = ACE_OS::fopen(this->script_file_, ACE_TEXT("r"));
          if(f==0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "Unable to open %s\n",
                               this->script_file_),
                              -1);
          ace_cc_yyin = f;
        }
      ace_cc_yyparse();
    }

  // Other tests go here
  // if (other_test_flag && success == CC_SUCCESS) ...

  if (this->shutdown_)
    // @@TAO is this needed??

  if (tests_run == 0)
    {
      print_usage ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         "No tests given\n"),
                        -1);
    }

  return success;
}

// This function runs basic tests concerned with only one lock set

int
CC_Client::run_basic_tests (void)
{
  Test_Single_Lock_With_Mode t1 (naming_service_,
                                 CosConcurrencyControl::read);
  Test_Single_Lock_With_Mode t2 (naming_service_,
                                 CosConcurrencyControl::write);
  Test_Single_Lock_With_Mode t3 (naming_service_,
                                 CosConcurrencyControl::upgrade);
  Test_Single_Lock_With_Mode t4 (naming_service_,
                                 CosConcurrencyControl::intention_read);
  Test_Single_Lock_With_Mode t5 (naming_service_,
                                 CosConcurrencyControl::intention_write);
  // This test should be run for several different lock mode, but
  // since we do not support
  Test_Release_Not_Held_Lock t6 (naming_service_,
                                 CosConcurrencyControl::read);
  if (t1.run () == CC_SUCCESS &&
      t2.run () == CC_SUCCESS &&
      t3.run () == CC_SUCCESS &&
      t4.run () == CC_SUCCESS &&
      t5.run () == CC_SUCCESS &&
      t6.run () == CC_SUCCESS )
    return CC_SUCCESS;
  else
    return CC_FAIL;
}

int
CC_Client::check_extended_test_params(char *params)
{
  // Format (regexp): [0-9]+;.*;.*
  int no_of_params = 0;
  char *cp = params; // pointer to walk along the string
  enum {TAO_START, TAO_NUMBER, TAO_ARG, TAO_ERROR} state = TAO_START;

  while(*cp!='\0')
    {
      switch(state)
        {
        case TAO_START:
          if(isdigit(*cp))
            state = TAO_NUMBER;
          else
            state = TAO_ERROR;
          break;

        case TAO_NUMBER:
          if((*cp)==';')
            {
              state = TAO_ARG;
              no_of_params++;
            }
          else
            if(!isdigit(*cp))
              state = TAO_ERROR;
          break;

        case TAO_ARG:
          if((*cp)==';')
            {
              no_of_params++;
            }
          break;

        case TAO_ERROR:
          return -1;
          // break;

        default:
          ACE_ERROR_RETURN((LM_ERROR,
                            "CC_Client::check_extended_test_params\n"), -1);
        }
      cp++;
    }
  if (state==TAO_ERROR) // there was only one character given and it was wrong
    return -1;
  else
    return no_of_params;
}

int
CC_Client::run_extended_tests (char *params)
{
  int success = CC_FAIL;
  int no_of_args = 0;

  ACE_DEBUG ((LM_DEBUG,
              "Params: %s\n",
              params));

  no_of_args = check_extended_test_params(params);
  if(no_of_args==-1)
    {
      ACE_ERROR_RETURN((LM_ERROR,
                        "Error in parameter string (%s). Format: '<test#>;<arg1>;<arg2>'\n", params), CC_FAIL);
    }

  ACE_DEBUG((LM_DEBUG, "Number of arguments: %i\n", no_of_args));

  char *cmd  = ACE_OS::strtok (params, ";");
  char *arg1 = ACE_OS::strtok (0, ";");
  //  char *arg2 = ACE_OS::strtok (0, ";");

  // A possible scenario using test 1,2, and 3 Create and lock the
  // lock set with the name 'Name'
  // ./CC_client -e '1;Name'
  // Try to lock the same lock set. The process will hang
  // ./CC_client -e '2:Name'
  // Unlocks the lock set. Now test 2 will continue.
  // ./CC_client -e '3:Name'

  if (ACE_OS::strcmp (cmd, "1") == 0)
    {
      Test_Setup_LockSet t1 (naming_service_, arg1);
      success = t1.run ();
    }

  if (ACE_OS::strcmp (cmd, "2") == 0)
    {
      Test_Use_Already_Created_LockSet t2 (naming_service_, arg1);
      success = t2.run ();
    }

  if (ACE_OS::strcmp (cmd, "3") == 0)
    {
      Test_Unlock_Already_Created_LockSet t3 (naming_service_, arg1);
      success = t3.run ();
    }

  return success;
}

void
CC_Client::print_usage (void)
{
  ACE_ERROR ((LM_ERROR,
              "usage:  %s"
              " [-b]"
              " [-c] cc-test-script"
              " [-d]"
              " [-f cc_factory-obj-ref-key-file]"
              " [-k cc-obj-ref-key]"
              " [-x]"
              " [-s]"
              "\n",
              this->argv_ [0]));
}

int
CC_Client::init_naming_service (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      ACE_NEW_RETURN (naming_service_,
                      CC_naming_service,
                      -1);

      this->naming_service_->Init (this->orb_ ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      return -1;
    }
  ACE_ENDTRY;
  return 0;
}

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

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

      // Parse command line and verify parameters.
      if (this->parse_args () == -1)
        return -1;

      if (this->use_naming_service_)
        {
          naming_result = this->init_naming_service ();
          if (naming_result < 0)
            return naming_result;
        }
      else
        {
          if (this->cc_factory_key_ == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "%s: no lock set factory key specified\n",
                               this->argv_[0]),
                              -1);


          CORBA::Object_var factory_object =
            this->orb_->string_to_object (this->cc_factory_key_
                                          ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

#if 0
          // The test cannot currently run without the naming service.
          this->factory_ =
            CosConcurrencyControl::LockSetFactory::_narrow
            (factory_object.in () ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          if (CORBA::is_nil (this->factory_.in ()))
            ACE_ERROR_RETURN ((LM_ERROR,
                               "invalid factory key <%s>\n",
                               this->cc_factory_key_),
                              -1);
#endif /* 0 */
        }

      ACE_DEBUG ((LM_DEBUG,
                  "Factory received OK\n"));

    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "CC_Client::init");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}

// This function runs the test.

int
ACE_TMAIN (int argc, ACE_TCHAR **argv)
{
  ACE_Argv_Type_Converter convert (argc, argv);

  CC_Client cc_client;

  ACE_DEBUG ((LM_DEBUG,
              "\n \t CosConcurrencyControl: client \n\n"));

  if (cc_client.init (convert.get_argc(), convert.get_ASCII_argv()) == -1)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Did not initialize correctly\n"));
      return 1;
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Running the test\n"));
      return cc_client.run ();
    }
}