Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / Concurrency / CC_command.cpp
blob01abae4bab0ea03f1cbf38b8810ebd3a0f3f7f77
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(void)
27 int CC_Command::execute()
29 return 0;
32 CC_Command::CC_Command(void)
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(void)
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(void)
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(void)");
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(void)
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(void)");
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(void)
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_);
249 catch (const CORBA::Exception& ex)
251 ex._tao_print_exception ("CC_UnLock_Cmd::execute(void)");
252 return 0;
254 return 1; // CC_SUCCESS
257 CC_TryLock_Cmd::CC_TryLock_Cmd (const char *lock_set_name,
258 CosConcurrencyControl::lock_mode mode)
259 : name_ (ACE_OS::strdup (lock_set_name)),
260 mode_ (mode)
262 // printf("CC_TryLock_Cmd::CC_TryLock_Cmd: lock set: %s, mode %s\n",
263 // lock_set_name, CC_TestUtils::get_lock_mode_name(mode));
266 CC_TryLock_Cmd::~CC_TryLock_Cmd()
268 ACE_OS::free (this->name_);
271 int CC_TryLock_Cmd::execute(void)
273 if(excep_)
275 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
276 delete excep_;
277 excep_ = 0;
278 return 0; // CC_FAIL
281 ACE_OS::printf("Executing try_lock command (lock set: %s, mode: %s)\n",
282 name_, CC_TestUtils::get_lock_mode_name(mode_));
284 CORBA::Boolean lock_not_held;
288 CosConcurrencyControl::LockSet_var ccls = GetLockSet(name_);
290 lock_not_held = ccls->try_lock (mode_);
292 if (lock_not_held)
294 ACE_DEBUG ((LM_DEBUG,
295 "%C lock not held\n",
296 CC_TestUtils::get_lock_mode_name (mode_)));
298 else
299 ACE_DEBUG ((LM_DEBUG,
300 "%C lock held\n",
301 CC_TestUtils::get_lock_mode_name (mode_)));
303 catch (const CORBA::Exception& ex)
305 ex._tao_print_exception ("CC_TryLock_Cmd::execute(void)");
306 return 0;
309 return 1; // CC_SUCCESS
312 CC_ChangeMode_Cmd::
313 CC_ChangeMode_Cmd (const char *lock_set_name,
314 CosConcurrencyControl::lock_mode held_mode,
315 CosConcurrencyControl::lock_mode new_mode)
316 : name_(ACE_OS::strdup (lock_set_name)),
317 held_mode_ (held_mode),
318 new_mode_ (new_mode)
320 // printf("CC_ChangeMode_Cmd::CC_ChangeMode_Cmd: lock set: %s, held mode: %s, new mode: %s\n",
321 // lock_set_name,
322 // CC_TestUtils::get_lock_mode_name(held_mode),
323 // CC_TestUtils::get_lock_mode_name(new_mode));
326 CC_ChangeMode_Cmd::~CC_ChangeMode_Cmd()
328 ACE_OS::strdup (this->name_);
331 int CC_ChangeMode_Cmd::execute(void)
333 if(excep_)
335 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
336 delete excep_;
337 excep_ = 0;
338 return 0; // CC_FAIL
341 ACE_OS::printf("Executing change_mode command (lock set: %s, held_mode: %s, new_mode: %s)\n",
342 name_, CC_TestUtils::get_lock_mode_name(held_mode_),
343 CC_TestUtils::get_lock_mode_name(new_mode_));
347 CosConcurrencyControl::LockSet_var ccls = GetLockSet(name_);
349 ccls->change_mode (held_mode_, new_mode_);
352 catch (const CORBA::Exception& ex)
354 ex._tao_print_exception ("CC_ChangeMode_Cmd::execute(void)");
356 return 1; // CC_SUCCESS
359 CC_Sleep_Cmd::CC_Sleep_Cmd(int seconds)
360 : time_ (seconds)
364 CC_Sleep_Cmd::~CC_Sleep_Cmd()
368 int CC_Sleep_Cmd::execute(void)
370 if(excep_)
372 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
373 delete excep_;
374 excep_ = 0;
375 return 0; // CC_FAIL
378 ACE_OS::printf("Executing sleep command (time: %i)\n", time_);
380 ACE_OS::sleep(time_);
381 return 1; // CC_SUCCESS
384 CC_Repeat_Cmd::CC_Repeat_Cmd(int times)
385 : times_ (times)
389 CC_Repeat_Cmd::~CC_Repeat_Cmd()
393 int CC_Repeat_Cmd::execute(void)
395 if(excep_)
397 ACE_OS::printf("Exception: %s\n", excep_->_rep_id ());
398 delete excep_;
399 excep_ = 0;
400 return 0; // CC_FAIL
403 ACE_OS::printf("Executing repeat command (times: %i)\n", times_);
405 return 1; // CC_SUCCESS
408 CC_Wait_Cmd::CC_Wait_Cmd (const char *prompt)
409 : prompt_ (ACE_OS::strdup (prompt))
413 CC_Wait_Cmd::~CC_Wait_Cmd()
415 ACE_OS::free (this->prompt_);
418 int CC_Wait_Cmd::execute(void)
420 if (excep_)
422 ACE_OS::printf ("Exception: %s\n", excep_->_rep_id ());
423 delete excep_;
424 excep_ = 0;
425 return 0; // CC_FAIL
428 ACE_OS::printf ("Executing wait command\n");
430 ACE_OS::printf ("%s", prompt_);
431 (void) ACE_OS::fgetc (stdin);
433 return 1; // CC_SUCCESS
436 CC_Excep_Cmd::CC_Excep_Cmd (const char *excep)
437 : ex_ (ACE_OS::strdup (excep))
439 // printf("CC_Excep_Cmd::CC_Excep_Cmd: excep: %s\n", excep);
442 CC_Excep_Cmd::~CC_Excep_Cmd(void)
444 ACE_OS::free (this->ex_);
448 CC_Excep_Cmd::execute(void)
450 ACE_OS::printf ("Executing excep command (expected: %s)\n", ex_);
451 // First we check to see if an exception has occurred. If not we fail
452 // because we expected to see one
453 if(excep_==0)
454 return 0; // CC_FAIL
456 // If there is an exception check that it's the expected one
457 if(ACE_OS::strcmp(excep_->_rep_id (), ex_)==0)
459 delete excep_;
460 excep_ = 0;
461 return 1; // CC_SUCCESS
463 else
465 ACE_OS::printf ("Exception: %s\n", excep_->_rep_id ());
466 delete excep_;
467 excep_ = 0;
468 return 0; // CC_FAIL
472 CC_Dummy_Cmd::CC_Dummy_Cmd(void)
476 CC_Dummy_Cmd::~CC_Dummy_Cmd(void)
481 CC_Dummy_Cmd::execute(void)
483 return 1; // CC_SUCCESS
486 CC_Print_Cmd::CC_Print_Cmd (const char * message)
487 : msg_ (ACE_OS::strdup (message))
491 CC_Print_Cmd::~CC_Print_Cmd(void)
493 ACE_OS::free(msg_);
497 CC_Print_Cmd::execute(void)
499 ACE_OS::printf ("%s\n", msg_);
500 return 1; // CC_SUCCESS
503 CC_Lookup_Cmd::CC_Lookup_Cmd (const char *lock_set_name)
504 : name_ (ACE_OS::strdup (lock_set_name))
508 CC_Lookup_Cmd::~CC_Lookup_Cmd()
510 if(name_)
512 ACE_OS::free(name_);
513 name_ = 0;
518 CC_Lookup_Cmd::execute(void)
520 if(excep_)
522 ACE_OS::printf ("Exception: %s\n", excep_->_rep_id ());
523 delete excep_;
524 excep_ = 0;
525 return 0; // CC_FAIL
528 ACE_OS::printf ("Executing lookup command (lock set: %s)\n", name_);
530 // Do the lookup if we haven't done it before
531 if(cc_lockset_.in() == 0)
535 CORBA::Object_var ccls_obj =
536 CC_naming_service::Instance()->get_obj_from_name ("",
537 name_);
539 CosConcurrencyControl::LockSet_var ccls =
540 CosConcurrencyControl::LockSet::_narrow (ccls_obj.in ());
542 cc_lockset_ = ccls;
545 catch (const CORBA::Exception& ex)
547 ex._tao_print_exception ("CC_UnLock_Cmd::execute(void)");
550 return 1; // CC_SUCCESS
553 CC_CommandElem::CC_CommandElem(CC_Command *cmd, CC_CommandElem *next)
554 : next_ (next), cmd_ (cmd)
558 CC_CommandElem::~CC_CommandElem(void)
562 CC_Command *CC_CommandElem::GetCommand(void)
564 return cmd_;
567 CC_CommandElem *
568 CC_CommandElem::GetNext(void)
570 return next_;
573 void
574 CC_CommandElem::SetNext(CC_CommandElem *next)
576 next_ = next;
579 CC_CommandList::CC_CommandList(void)
580 : head_ (0), last_ (0), times_ (1)
582 ACE_OS::printf("CC_CommandList::CC_CommandList\n");
585 CC_CommandList::~CC_CommandList(void)
590 CC_CommandList::add(CC_Command *cmd)
592 if(head_==0)
594 head_ = new CC_CommandElem(cmd, 0);
595 last_ = head_;
597 else
599 CC_CommandElem *tmp = new CC_CommandElem(cmd, 0);
600 last_->SetNext(tmp);
601 last_ = tmp;
603 return 0;
607 CC_CommandList::execute(void)
609 CC_CommandElem *current = head_;
611 for(int i=0; i<times_; i++)
613 current = head_;
614 while(current!=0)
616 if(current->GetCommand()->execute()==0) // == CC_FAIL
618 return 0; // CC_FAIL
620 current = current->GetNext();
623 return 1; // CC_SUCCESS
626 void
627 CC_CommandList::setrepeat(int times)
629 times_ = times;