Merge pull request #2318 from jwillemsen/jwi-add_forward_profiles
[ACE_TAO.git] / TAO / orbsvcs / tests / Concurrency / CC_client.cpp
blob89ff1865f37b80e27ca50fc62fb090ea161869d1
2 //=============================================================================
3 /**
4 * @file CC_client.cpp
6 * This is the test class for the concurrency service. The class
7 * implements a client to the concurrency service.
8 * This file contains the main function for the test.
10 * @author Torben Worm <tworm@cs.wustl.edu>
12 //=============================================================================
15 #include "CC_client.h"
17 #include "tao/debug.h"
19 #include "ace/Read_Buffer.h"
20 #include "ace/OS_NS_stdio.h"
21 #include "ace/OS_NS_unistd.h"
22 #include "ace/OS_NS_ctype.h"
23 #include "ace/OS_NS_string.h"
24 #include "ace/OS_NS_fcntl.h"
27 // Constructor.
28 CC_Client::CC_Client ()
29 : naming_service_ (0),
30 cc_factory_ior_file_ (0),
31 cc_factory_key_ (0),
32 f_handle_ (ACE_INVALID_HANDLE),
33 shutdown_ (0),
34 use_naming_service_ (1),
35 run_basic_tests_ (0),
36 run_extended_tests_ (0),
37 use_script_file_ (0),
38 script_file_ (0)
42 CC_Client::~CC_Client ()
44 // Free resources and close the ior files.
45 if (this->cc_factory_ior_file_)
46 ACE_OS::fclose (this->cc_factory_ior_file_);
48 if (this->f_handle_ != ACE_INVALID_HANDLE)
49 ACE_OS::close (this->f_handle_);
51 if (this->cc_factory_key_ != 0)
52 ACE_OS::free (this->cc_factory_key_);
54 if (naming_service_!=0)
55 delete naming_service_;
58 // Reads the lock set factory ior from a file
60 int
61 CC_Client::read_ior (ACE_TCHAR *filename)
63 // Open the file for reading.
64 this->f_handle_ = ACE_OS::open (filename,0);
66 if (this->f_handle_ == ACE_INVALID_HANDLE)
67 ACE_ERROR_RETURN ((LM_ERROR,
68 "Unable to open %s for writing: %p\n",
69 filename),
70 -1);
71 ACE_Read_Buffer ior_buffer (this->f_handle_);
72 this->cc_factory_key_ = ior_buffer.read ();
74 if (this->cc_factory_key_ == 0)
75 ACE_ERROR_RETURN ((LM_ERROR,
76 "Unable to allocate memory to read ior: %p\n"),
77 -1);
78 return 0;
81 // Parses the command line arguments and returns an error status.
83 int
84 CC_Client::parse_args ()
86 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dc:sf:k:xbhe:"));
87 int c;
88 int result;
90 if(argc_==1) // No arguments given on command line
92 print_usage();
93 return -1;
96 while ((c = get_opts ()) != -1)
97 switch (c)
99 case 'b': // debug flag
100 this->run_basic_tests_ = 1;
101 break;
102 case 'c':
103 this->use_script_file_ = 1;
104 this->script_file_ = ACE_OS::strdup (get_opts.opt_arg ());
105 break;
106 case 'd': // debug flag
107 TAO_debug_level++;
108 break;
109 case 'e': // debug flag
110 run_extended_tests_ = 1;
111 this->extended_tests_params_ = ACE_OS::strdup (get_opts.opt_arg ());
112 break;
113 case 'f': // read the IOR from the file.
114 result = this->read_ior (get_opts.opt_arg ());
115 if (result < 0)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 "Unable to read ior from %s : %p\n",
118 get_opts.opt_arg ()),
119 -1);
120 break;
121 case 'k': // read the cubit IOR from the command-line.
122 this->cc_factory_key_ =
123 ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
124 break;
125 case 'x':
126 this->shutdown_ = 1;
127 break;
128 case 's': // Don't use the TAO Naming Service.
129 this->use_naming_service_ = 0;
130 break;
131 case 'h':
132 default:
133 print_usage ();
134 ACE_ERROR_RETURN ((LM_ERROR, ""), -1);
137 // Indicates successful parsing of command line.
138 return 0;
141 // Execute client example code.
144 CC_Client::run ()
146 int tests_run = 0;
147 // Tells whether any tests have been run
149 int success = CC_SUCCESS;
150 // Did test succeed?
152 if (this->run_basic_tests_ && success == CC_SUCCESS)
154 success = run_basic_tests ();
155 if(success==CC_FAIL)
156 ACE_DEBUG((LM_DEBUG, "Basic tests did not succeed\n"));
157 tests_run = 1;
160 if (this->run_extended_tests_ && success == CC_SUCCESS)
162 success = run_extended_tests (this->extended_tests_params_);
163 if(success==CC_FAIL)
164 ACE_DEBUG((LM_DEBUG, "Extended tests did not succeed\n"));
165 tests_run = 1;
168 if(this->use_script_file_ && success == CC_SUCCESS)
170 cmdlist = new CC_CommandList();
171 FILE *f;
173 // Open the command file for parsing if the filename!=stdin
174 if(ACE_OS::strcmp(this->script_file_, ACE_TEXT("stdin"))!=0)
176 f = ACE_OS::fopen(this->script_file_, "r");
177 if(f==0)
178 ACE_ERROR_RETURN ((LM_ERROR,
179 "Unable to open %s\n",
180 this->script_file_),
181 -1);
182 ace_cc_yyin = f;
184 ace_cc_yyparse();
187 // Other tests go here
188 // if (other_test_flag && success == CC_SUCCESS) ...
190 if (this->shutdown_)
191 // @@TAO is this needed??
193 if (tests_run == 0)
195 print_usage ();
196 ACE_ERROR_RETURN ((LM_ERROR,
197 "No tests given\n"),
198 -1);
201 return success;
204 // This function runs basic tests concerned with only one lock set
207 CC_Client::run_basic_tests ()
209 Test_Single_Lock_With_Mode t1 (naming_service_,
210 CosConcurrencyControl::read);
211 Test_Single_Lock_With_Mode t2 (naming_service_,
212 CosConcurrencyControl::write);
213 Test_Single_Lock_With_Mode t3 (naming_service_,
214 CosConcurrencyControl::upgrade);
215 Test_Single_Lock_With_Mode t4 (naming_service_,
216 CosConcurrencyControl::intention_read);
217 Test_Single_Lock_With_Mode t5 (naming_service_,
218 CosConcurrencyControl::intention_write);
219 // This test should be run for several different lock mode, but
220 // since we do not support
221 Test_Release_Not_Held_Lock t6 (naming_service_,
222 CosConcurrencyControl::read);
223 if (t1.run () == CC_SUCCESS &&
224 t2.run () == CC_SUCCESS &&
225 t3.run () == CC_SUCCESS &&
226 t4.run () == CC_SUCCESS &&
227 t5.run () == CC_SUCCESS &&
228 t6.run () == CC_SUCCESS )
229 return CC_SUCCESS;
230 else
231 return CC_FAIL;
235 CC_Client::check_extended_test_params(ACE_TCHAR *params)
237 // Format (regexp): [0-9]+;.*;.*
238 int no_of_params = 0;
239 ACE_TCHAR *cp = params; // pointer to walk along the string
240 enum {TAO_START, TAO_NUMBER, TAO_ARG, TAO_ERROR} state = TAO_START;
242 while(*cp!='\0')
244 switch(state)
246 case TAO_START:
247 if(ACE_OS::ace_isdigit(*cp))
248 state = TAO_NUMBER;
249 else
250 state = TAO_ERROR;
251 break;
253 case TAO_NUMBER:
254 if((*cp)==';')
256 state = TAO_ARG;
257 no_of_params++;
259 else
260 if(!ACE_OS::ace_isdigit(*cp))
261 state = TAO_ERROR;
262 break;
264 case TAO_ARG:
265 if((*cp)==';')
267 no_of_params++;
269 break;
271 case TAO_ERROR:
272 return -1;
273 // break;
275 default:
276 ACE_ERROR_RETURN((LM_ERROR,
277 "CC_Client::check_extended_test_params\n"), -1);
279 cp++;
281 if (state==TAO_ERROR) // there was only one character given and it was wrong
282 return -1;
283 else
284 return no_of_params;
288 CC_Client::run_extended_tests (ACE_TCHAR *params)
290 int success = CC_FAIL;
291 int no_of_args = 0;
293 ACE_DEBUG ((LM_DEBUG,
294 "Params: %s\n",
295 params));
297 no_of_args = check_extended_test_params(params);
298 if(no_of_args==-1)
300 ACE_ERROR_RETURN((LM_ERROR,
301 "Error in parameter string (%s). Format: '<test#>;<arg1>;<arg2>'\n", params), CC_FAIL);
304 ACE_DEBUG((LM_DEBUG, "Number of arguments: %i\n", no_of_args));
306 char *cmd = ACE_OS::strtok (ACE_TEXT_ALWAYS_CHAR(params), ";");
307 char *arg1 = ACE_OS::strtok (0, ";");
308 // char *arg2 = ACE_OS::strtok (0, ";");
310 // A possible scenario using test 1,2, and 3 Create and lock the
311 // lock set with the name 'Name'
312 // ./CC_client -e '1;Name'
313 // Try to lock the same lock set. The process will hang
314 // ./CC_client -e '2:Name'
315 // Unlocks the lock set. Now test 2 will continue.
316 // ./CC_client -e '3:Name'
318 if (ACE_OS::strcmp (cmd, "1") == 0)
320 Test_Setup_LockSet t1 (naming_service_, arg1);
321 success = t1.run ();
324 if (ACE_OS::strcmp (cmd, "2") == 0)
326 Test_Use_Already_Created_LockSet t2 (naming_service_, arg1);
327 success = t2.run ();
330 if (ACE_OS::strcmp (cmd, "3") == 0)
332 Test_Unlock_Already_Created_LockSet t3 (naming_service_, arg1);
333 success = t3.run ();
336 return success;
339 void
340 CC_Client::print_usage ()
342 ACE_ERROR ((LM_ERROR,
343 "usage: %s"
344 " [-b]"
345 " [-c] cc-test-script"
346 " [-d]"
347 " [-f cc_factory-obj-ref-key-file]"
348 " [-k cc-obj-ref-key]"
349 " [-x]"
350 " [-s]"
351 "\n",
352 this->argv_ [0]));
356 CC_Client::init_naming_service ()
360 ACE_NEW_RETURN (naming_service_,
361 CC_naming_service,
362 -1);
364 this->naming_service_->Init (this->orb_);
366 catch (const CORBA::Exception&)
368 return -1;
370 return 0;
374 CC_Client::init (int argc, ACE_TCHAR **argv)
376 int naming_result;
377 this->argc_ = argc;
378 this->argv_ = argv;
382 // Retrieve the ORB.
383 this->orb_ = CORBA::ORB_init (this->argc_,
384 this->argv_,
385 "internet");
387 // Parse command line and verify parameters.
388 if (this->parse_args () == -1)
389 return -1;
391 if (this->use_naming_service_)
393 naming_result = this->init_naming_service ();
394 if (naming_result < 0)
395 return naming_result;
397 else
399 if (this->cc_factory_key_ == 0)
400 ACE_ERROR_RETURN ((LM_ERROR,
401 "%s: no lock set factory key specified\n",
402 this->argv_[0]),
403 -1);
406 CORBA::Object_var factory_object =
407 this->orb_->string_to_object (this->cc_factory_key_);
409 #if 0
410 // The test cannot currently run without the naming service.
411 this->factory_ =
412 CosConcurrencyControl::LockSetFactory::_narrow
413 (factory_object.in ());
415 if (CORBA::is_nil (this->factory_.in ()))
416 ACE_ERROR_RETURN ((LM_ERROR,
417 "invalid factory key <%s>\n",
418 this->cc_factory_key_),
419 -1);
420 #endif /* 0 */
423 ACE_DEBUG ((LM_DEBUG,
424 "Factory received OK\n"));
426 catch (const CORBA::Exception& ex)
428 ex._tao_print_exception ("CC_Client::init");
429 return -1;
432 return 0;
435 // This function runs the test.
438 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
440 CC_Client cc_client;
442 ACE_DEBUG ((LM_DEBUG,
443 "\n \t CosConcurrencyControl: client \n\n"));
445 if (cc_client.init (argc, argv) == -1)
447 ACE_DEBUG ((LM_DEBUG,
448 "Did not initialize correctly\n"));
449 return 1;
451 else
453 ACE_DEBUG ((LM_DEBUG,
454 "Running the test\n"));
455 return cc_client.run ();