Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / tests / Concurrency / CC_command.cpp
blob1ccbf5f46c18b4fe783a73806c71580776ee3305
1 //=============================================================================
2 /**
3 * @file CC_command.cpp
5 * This is the command classes for the test of the concurrency service.
7 * @author Torben Worm <tworm@cs.wustl.edu>
8 */
9 //=============================================================================
12 #include "CC_command.h"
13 #include "CC_test_utils.h"
14 #include "CC_naming_service.h"
15 #include "ace/ACE.h"
16 #include "ace/Process.h"
17 #include "ace/Log_Msg.h"
18 #include "orbsvcs/CosConcurrencyControlC.h"
19 #include "ace/OS_NS_stdio.h"
20 #include "ace/OS_NS_unistd.h"
21 #include "ace/OS_NS_string.h"
23 CC_Command::~CC_Command()
27 int CC_Command::execute()
29 return 0;
32 CC_Command::CC_Command()
36 CosConcurrencyControl::LockSet_var
37 CC_Command::GetLockSet (const char *lock_set_name)
39 CosConcurrencyControl::LockSet_var ccls_ret;
41 try
43 if(ACE_OS::strcmp(lock_set_name, "")!=0)
45 CORBA::Object_var ccls_obj =
46 CC_naming_service::Instance()->get_obj_from_name ("",
47 lock_set_name);
49 ccls_ret =
50 CosConcurrencyControl::LockSet::_narrow (ccls_obj.in ());
52 else
54 // No lock set name was given. Use the global lock set.
55 if(cc_lockset_.in()==0)
57 throw CORBA::UNKNOWN ();
59 else
60 ccls_ret = cc_lockset_.in ();
63 catch (const CORBA::Exception&)
65 throw;
68 return ccls_ret;
71 CORBA::Exception *CC_Command::excep_ = 0;
73 CosConcurrencyControl::LockSet_var CC_Command::cc_lockset_(0);
75 CC_Start_Cmd::CC_Start_Cmd (const char *config_file_name)
76 : cfg_name_ (ACE_OS::strdup (config_file_name))
78 // printf("CC_Start_Cmd::CC_Start_Cmd: config: %s\n", config_file_name);
81 CC_Start_Cmd::~CC_Start_Cmd()
83 // cfg_name_ is allocated in the lexer with strdup
84 if (cfg_name_)
86 ACE_OS::free(cfg_name_);
87 cfg_name_ = 0;
91 int CC_Start_Cmd::execute()
93 if (excep_)
95 ACE_OS::printf ("Exception: %s\n", excep_->_rep_id ());
96 delete excep_;
97 excep_ = 0;
98 return 0; // CC_FAIL
101 ACE_OS::printf ("Executing start command (script file: %s)\n", cfg_name_);
103 char cmd_line[1024];
104 int success = ACE_OS::sprintf(&cmd_line[0], "%s -c %s",
105 "./CC_client", cfg_name_);
106 if(success>=1024 || success==-1)
107 ACE_ERROR_RETURN((LM_ERROR, "Creation of process failed: %s\n",
108 cmd_line), 0);
110 ACE_Process new_process;
111 ACE_Process_Options options;
112 options.command_line(ACE_TEXT_CHAR_TO_TCHAR(cmd_line));
114 if(new_process.spawn(options) == -1)
116 ACE_ERROR_RETURN((LM_ERROR, "Creation of process failed: %C\n",
117 cmd_line), 0);
119 return 1; // CC_SUCCESS
122 CC_CreateLockSet_Cmd::CC_CreateLockSet_Cmd (const char *lock_set_name)
123 : name_ (ACE_OS::strdup (lock_set_name))
125 // printf("CC_CreateLockSet_Cmd::CC_CreateLockSet_Cmd: lock set: %s\n",
126 // lock_set_name);
129 CC_CreateLockSet_Cmd::~CC_CreateLockSet_Cmd()
131 // name_ is allocated in the lexer with strdup
132 if(name_)
134 ACE_OS::free(name_);
135 name_ = 0;
139 int CC_CreateLockSet_Cmd::execute()
141 if(excep_)
143 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
144 delete excep_;
145 excep_ = 0;
146 return 0; // CC_FAIL
149 ACE_OS::printf("Executing create command (lock set: %s)\n", name_);
153 CosConcurrencyControl::LockSet_ptr lock_set =
154 CC_TestUtils::create_lock_set();
155 if(ACE_OS::strcmp(name_,"")!=0)// Do not bind an empty name
157 CC_naming_service::Instance()->bind_name(name_,
158 lock_set);
160 else
162 // We did not specify a name => the global lock set variable is set
163 cc_lockset_ = lock_set;
166 catch (const CORBA::Exception& ex)
168 ex._tao_print_exception ("CC_CreateLockSet_Cmd::execute()");
171 return 1; // CC_SUCCESS
174 CC_Lock_Cmd::CC_Lock_Cmd (const char *lock_set_name,
175 CosConcurrencyControl::lock_mode mode)
176 : name_ (ACE_OS::strdup (lock_set_name)),
177 mode_(mode)
179 // printf("CC_Lock_Cmd::CC_Lock_Cmd: lock set: %s, mode: %s\n",
180 // lock_set_name, CC_TestUtils::get_lock_mode_name(mode));
183 CC_Lock_Cmd::~CC_Lock_Cmd()
185 ACE_OS::free (this->name_);
188 int CC_Lock_Cmd::execute()
190 if(excep_)
192 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
193 delete excep_;
194 excep_ = 0;
195 return 0; // CC_FAIL
198 ACE_OS::printf("Executing lock command (lock set: %s, mode: %s)\n",
199 name_, CC_TestUtils::get_lock_mode_name(mode_));
203 CosConcurrencyControl::LockSet_var ccls =
204 GetLockSet(name_);
205 ccls->lock (mode_);
207 catch (const CORBA::Exception& ex)
209 ex._tao_print_exception ("CC_Lock_Cmd::execute()");
210 return 0;
212 return 1; // CC_SUCCESS
215 CC_UnLock_Cmd::CC_UnLock_Cmd (const char *lock_set_name,
216 CosConcurrencyControl::lock_mode mode)
217 : name_ (ACE_OS::strdup (lock_set_name)),
218 mode_ (mode)
220 // printf("CC_UnLock_Cmd::CC_UnLock_Cmd: lock set: %s, mode: %s\n",
221 // lock_set_name, CC_TestUtils::get_lock_mode_name(mode));
224 CC_UnLock_Cmd::~CC_UnLock_Cmd()
226 ACE_OS::free (this->name_);
229 int CC_UnLock_Cmd::execute()
231 if(excep_)
233 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
234 delete excep_;
235 excep_ = 0;
236 return 0; // CC_FAIL
239 ACE_OS::printf("Executing unlock command (lock set: %s, mode: %s)\n",
240 name_, CC_TestUtils::get_lock_mode_name(mode_));
244 CosConcurrencyControl::LockSet_var ccls = GetLockSet(name_);
246 ccls->unlock (mode_);
248 catch (const CORBA::Exception& ex)
250 ex._tao_print_exception ("CC_UnLock_Cmd::execute()");
251 return 0;
253 return 1; // CC_SUCCESS
256 CC_TryLock_Cmd::CC_TryLock_Cmd (const char *lock_set_name,
257 CosConcurrencyControl::lock_mode mode)
258 : name_ (ACE_OS::strdup (lock_set_name)),
259 mode_ (mode)
261 // printf("CC_TryLock_Cmd::CC_TryLock_Cmd: lock set: %s, mode %s\n",
262 // lock_set_name, CC_TestUtils::get_lock_mode_name(mode));
265 CC_TryLock_Cmd::~CC_TryLock_Cmd()
267 ACE_OS::free (this->name_);
270 int CC_TryLock_Cmd::execute()
272 if(excep_)
274 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
275 delete excep_;
276 excep_ = 0;
277 return 0; // CC_FAIL
280 ACE_OS::printf("Executing try_lock command (lock set: %s, mode: %s)\n",
281 name_, CC_TestUtils::get_lock_mode_name(mode_));
283 CORBA::Boolean lock_not_held;
287 CosConcurrencyControl::LockSet_var ccls = GetLockSet(name_);
289 lock_not_held = ccls->try_lock (mode_);
291 if (lock_not_held)
293 ACE_DEBUG ((LM_DEBUG,
294 "%C lock not held\n",
295 CC_TestUtils::get_lock_mode_name (mode_)));
297 else
298 ACE_DEBUG ((LM_DEBUG,
299 "%C lock held\n",
300 CC_TestUtils::get_lock_mode_name (mode_)));
302 catch (const CORBA::Exception& ex)
304 ex._tao_print_exception ("CC_TryLock_Cmd::execute()");
305 return 0;
308 return 1; // CC_SUCCESS
311 CC_ChangeMode_Cmd::
312 CC_ChangeMode_Cmd (const char *lock_set_name,
313 CosConcurrencyControl::lock_mode held_mode,
314 CosConcurrencyControl::lock_mode new_mode)
315 : name_(ACE_OS::strdup (lock_set_name)),
316 held_mode_ (held_mode),
317 new_mode_ (new_mode)
319 // printf("CC_ChangeMode_Cmd::CC_ChangeMode_Cmd: lock set: %s, held mode: %s, new mode: %s\n",
320 // lock_set_name,
321 // CC_TestUtils::get_lock_mode_name(held_mode),
322 // CC_TestUtils::get_lock_mode_name(new_mode));
325 CC_ChangeMode_Cmd::~CC_ChangeMode_Cmd()
327 ACE_OS::strdup (this->name_);
330 int CC_ChangeMode_Cmd::execute()
332 if(excep_)
334 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
335 delete excep_;
336 excep_ = 0;
337 return 0; // CC_FAIL
340 ACE_OS::printf("Executing change_mode command (lock set: %s, held_mode: %s, new_mode: %s)\n",
341 name_, CC_TestUtils::get_lock_mode_name(held_mode_),
342 CC_TestUtils::get_lock_mode_name(new_mode_));
346 CosConcurrencyControl::LockSet_var ccls = GetLockSet(name_);
348 ccls->change_mode (held_mode_, new_mode_);
350 catch (const CORBA::Exception& ex)
352 ex._tao_print_exception ("CC_ChangeMode_Cmd::execute()");
354 return 1; // CC_SUCCESS
357 CC_Sleep_Cmd::CC_Sleep_Cmd(int seconds)
358 : time_ (seconds)
362 CC_Sleep_Cmd::~CC_Sleep_Cmd()
366 int CC_Sleep_Cmd::execute()
368 if(excep_)
370 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
371 delete excep_;
372 excep_ = 0;
373 return 0; // CC_FAIL
376 ACE_OS::printf("Executing sleep command (time: %i)\n", time_);
378 ACE_OS::sleep(time_);
379 return 1; // CC_SUCCESS
382 CC_Repeat_Cmd::CC_Repeat_Cmd(int times)
383 : times_ (times)
387 CC_Repeat_Cmd::~CC_Repeat_Cmd()
391 int CC_Repeat_Cmd::execute()
393 if(excep_)
395 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
396 delete excep_;
397 excep_ = 0;
398 return 0; // CC_FAIL
401 ACE_OS::printf("Executing repeat command (times: %i)\n", times_);
403 return 1; // CC_SUCCESS
406 CC_Wait_Cmd::CC_Wait_Cmd (const char *prompt)
407 : prompt_ (ACE_OS::strdup (prompt))
411 CC_Wait_Cmd::~CC_Wait_Cmd()
413 ACE_OS::free (this->prompt_);
416 int CC_Wait_Cmd::execute()
418 if (excep_)
420 ACE_OS::printf ("Exception: %s\n", excep_->_rep_id ());
421 delete excep_;
422 excep_ = 0;
423 return 0; // CC_FAIL
426 ACE_OS::printf ("Executing wait command\n");
428 ACE_OS::printf ("%s", prompt_);
429 (void) ACE_OS::fgetc (stdin);
431 return 1; // CC_SUCCESS
434 CC_Excep_Cmd::CC_Excep_Cmd (const char *excep)
435 : ex_ (ACE_OS::strdup (excep))
437 // printf("CC_Excep_Cmd::CC_Excep_Cmd: excep: %s\n", excep);
440 CC_Excep_Cmd::~CC_Excep_Cmd()
442 ACE_OS::free (this->ex_);
446 CC_Excep_Cmd::execute()
448 ACE_OS::printf ("Executing excep command (expected: %s)\n", ex_);
449 // First we check to see if an exception has occurred. If not we fail
450 // because we expected to see one
451 if(excep_==0)
452 return 0; // CC_FAIL
454 // If there is an exception check that it's the expected one
455 if(ACE_OS::strcmp(excep_->_rep_id (), ex_)==0)
457 delete excep_;
458 excep_ = 0;
459 return 1; // CC_SUCCESS
461 else
463 ACE_OS::printf ("Exception: %s\n", excep_->_rep_id ());
464 delete excep_;
465 excep_ = 0;
466 return 0; // CC_FAIL
470 CC_Dummy_Cmd::CC_Dummy_Cmd()
474 CC_Dummy_Cmd::~CC_Dummy_Cmd()
479 CC_Dummy_Cmd::execute()
481 return 1; // CC_SUCCESS
484 CC_Print_Cmd::CC_Print_Cmd (const char * message)
485 : msg_ (ACE_OS::strdup (message))
489 CC_Print_Cmd::~CC_Print_Cmd()
491 ACE_OS::free(msg_);
495 CC_Print_Cmd::execute()
497 ACE_OS::printf ("%s\n", msg_);
498 return 1; // CC_SUCCESS
501 CC_Lookup_Cmd::CC_Lookup_Cmd (const char *lock_set_name)
502 : name_ (ACE_OS::strdup (lock_set_name))
506 CC_Lookup_Cmd::~CC_Lookup_Cmd()
508 if(name_)
510 ACE_OS::free(name_);
511 name_ = 0;
516 CC_Lookup_Cmd::execute()
518 if(excep_)
520 ACE_OS::printf ("Exception: %s\n", excep_->_rep_id ());
521 delete excep_;
522 excep_ = 0;
523 return 0; // CC_FAIL
526 ACE_OS::printf ("Executing lookup command (lock set: %s)\n", name_);
528 // Do the lookup if we haven't done it before
529 if(cc_lockset_.in() == 0)
533 CORBA::Object_var ccls_obj =
534 CC_naming_service::Instance()->get_obj_from_name ("",
535 name_);
537 CosConcurrencyControl::LockSet_var ccls =
538 CosConcurrencyControl::LockSet::_narrow (ccls_obj.in ());
540 cc_lockset_ = ccls;
542 catch (const CORBA::Exception& ex)
544 ex._tao_print_exception ("CC_UnLock_Cmd::execute()");
547 return 1; // CC_SUCCESS
550 CC_CommandElem::CC_CommandElem(CC_Command *cmd, CC_CommandElem *next)
551 : next_ (next), cmd_ (cmd)
555 CC_CommandElem::~CC_CommandElem()
559 CC_Command *CC_CommandElem::GetCommand()
561 return cmd_;
564 CC_CommandElem *
565 CC_CommandElem::GetNext()
567 return next_;
570 void
571 CC_CommandElem::SetNext(CC_CommandElem *next)
573 next_ = next;
576 CC_CommandList::CC_CommandList()
577 : head_ (0), last_ (0), times_ (1)
579 ACE_OS::printf("CC_CommandList::CC_CommandList\n");
582 CC_CommandList::~CC_CommandList()
587 CC_CommandList::add(CC_Command *cmd)
589 if(head_==0)
591 head_ = new CC_CommandElem(cmd, 0);
592 last_ = head_;
594 else
596 CC_CommandElem *tmp = new CC_CommandElem(cmd, 0);
597 last_->SetNext(tmp);
598 last_ = tmp;
600 return 0;
604 CC_CommandList::execute()
606 CC_CommandElem *current = head_;
608 for(int i=0; i<times_; i++)
610 current = head_;
611 while(current!=0)
613 if(current->GetCommand()->execute()==0) // == CC_FAIL
615 return 0; // CC_FAIL
617 current = current->GetNext();
620 return 1; // CC_SUCCESS
623 void
624 CC_CommandList::setrepeat(int times)
626 times_ = times;