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()
27 int CC_Command::execute()
32 CC_Command::CC_Command()
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()
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()
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()");
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()
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()");
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()
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_
);
248 catch (const CORBA::Exception
& ex
)
250 ex
._tao_print_exception ("CC_UnLock_Cmd::execute()");
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
)),
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()
274 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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_
);
293 ACE_DEBUG ((LM_DEBUG
,
294 "%C lock not held\n",
295 CC_TestUtils::get_lock_mode_name (mode_
)));
298 ACE_DEBUG ((LM_DEBUG
,
300 CC_TestUtils::get_lock_mode_name (mode_
)));
302 catch (const CORBA::Exception
& ex
)
304 ex
._tao_print_exception ("CC_TryLock_Cmd::execute()");
308 return 1; // CC_SUCCESS
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
),
319 // printf("CC_ChangeMode_Cmd::CC_ChangeMode_Cmd: lock set: %s, held mode: %s, new mode: %s\n",
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()
334 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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
)
362 CC_Sleep_Cmd::~CC_Sleep_Cmd()
366 int CC_Sleep_Cmd::execute()
370 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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
)
387 CC_Repeat_Cmd::~CC_Repeat_Cmd()
391 int CC_Repeat_Cmd::execute()
395 ACE_OS::printf("Exception: %s\n", excep_
->_rep_id ());
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()
420 ACE_OS::printf ("Exception: %s\n", excep_
->_rep_id ());
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
454 // If there is an exception check that it's the expected one
455 if(ACE_OS::strcmp(excep_
->_rep_id (), ex_
)==0)
459 return 1; // CC_SUCCESS
463 ACE_OS::printf ("Exception: %s\n", excep_
->_rep_id ());
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()
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()
516 CC_Lookup_Cmd::execute()
520 ACE_OS::printf ("Exception: %s\n", excep_
->_rep_id ());
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 ("",
537 CosConcurrencyControl::LockSet_var ccls
=
538 CosConcurrencyControl::LockSet::_narrow (ccls_obj
.in ());
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()
565 CC_CommandElem::GetNext()
571 CC_CommandElem::SetNext(CC_CommandElem
*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
)
591 head_
= new CC_CommandElem(cmd
, 0);
596 CC_CommandElem
*tmp
= new CC_CommandElem(cmd
, 0);
604 CC_CommandList::execute()
606 CC_CommandElem
*current
= head_
;
608 for(int i
=0; i
<times_
; i
++)
613 if(current
->GetCommand()->execute()==0) // == CC_FAIL
617 current
= current
->GetNext();
620 return 1; // CC_SUCCESS
624 CC_CommandList::setrepeat(int times
)