1 //=============================================================================
5 * This is the command classes for the test of the concurrency service.
7 * @author Torben Worm <tworm@cs.wustl.edu>
9 //=============================================================================
12 #include "CC_command.h"
13 #include "CC_test_utils.h"
14 #include "CC_naming_service.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()
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
;
43 if(ACE_OS::strcmp(lock_set_name
, "")!=0)
45 CORBA::Object_var ccls_obj
=
46 CC_naming_service::Instance()->get_obj_from_name ("",
50 CosConcurrencyControl::LockSet::_narrow (ccls_obj
.in ());
54 // No lock set name was given. Use the global lock set.
55 if(cc_lockset_
.in()==0)
57 throw CORBA::UNKNOWN ();
60 ccls_ret
= cc_lockset_
.in ();
63 catch (const CORBA::Exception
&)
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
86 ACE_OS::free(cfg_name_
);
91 int CC_Start_Cmd::execute(void)
95 ACE_OS::printf ("Exception: %s\n", excep_
->_rep_id ());
101 ACE_OS::printf ("Executing start command (script file: %s)\n", cfg_name_
);
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",
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",
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",
129 CC_CreateLockSet_Cmd::~CC_CreateLockSet_Cmd()
131 // name_ is allocated in the lexer with strdup
139 int CC_CreateLockSet_Cmd::execute(void)
143 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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_
,
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
)),
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)
192 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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
=
207 catch (const CORBA::Exception
& ex
)
209 ex
._tao_print_exception ("CC_Lock_Cmd::execute(void)");
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
)),
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)
233 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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)");
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
)),
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)
275 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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_
);
294 ACE_DEBUG ((LM_DEBUG
,
295 "%C lock not held\n",
296 CC_TestUtils::get_lock_mode_name (mode_
)));
299 ACE_DEBUG ((LM_DEBUG
,
301 CC_TestUtils::get_lock_mode_name (mode_
)));
303 catch (const CORBA::Exception
& ex
)
305 ex
._tao_print_exception ("CC_TryLock_Cmd::execute(void)");
309 return 1; // CC_SUCCESS
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
),
320 // printf("CC_ChangeMode_Cmd::CC_ChangeMode_Cmd: lock set: %s, held mode: %s, new mode: %s\n",
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)
335 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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
)
364 CC_Sleep_Cmd::~CC_Sleep_Cmd()
368 int CC_Sleep_Cmd::execute(void)
372 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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
)
389 CC_Repeat_Cmd::~CC_Repeat_Cmd()
393 int CC_Repeat_Cmd::execute(void)
397 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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)
422 ACE_OS::printf ("Exception: %s\n", excep_
->_rep_id ());
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
456 // If there is an exception check that it's the expected one
457 if(ACE_OS::strcmp(excep_
->_rep_id (), ex_
)==0)
461 return 1; // CC_SUCCESS
465 ACE_OS::printf ("Exception: %s\n", excep_
->_rep_id ());
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)
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()
518 CC_Lookup_Cmd::execute(void)
522 ACE_OS::printf ("Exception: %s\n", excep_
->_rep_id ());
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 ("",
539 CosConcurrencyControl::LockSet_var ccls
=
540 CosConcurrencyControl::LockSet::_narrow (ccls_obj
.in ());
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)
568 CC_CommandElem::GetNext(void)
574 CC_CommandElem::SetNext(CC_CommandElem
*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
)
594 head_
= new CC_CommandElem(cmd
, 0);
599 CC_CommandElem
*tmp
= new CC_CommandElem(cmd
, 0);
607 CC_CommandList::execute(void)
609 CC_CommandElem
*current
= head_
;
611 for(int i
=0; i
<times_
; i
++)
616 if(current
->GetCommand()->execute()==0) // == CC_FAIL
620 current
= current
->GetNext();
623 return 1; // CC_SUCCESS
627 CC_CommandList::setrepeat(int times
)