- Partially written the code for the SHOW command.

- Reorganized source to avoid duplicated code

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@753 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-07-11 22:50:41 +00:00
parent d4beb67d0e
commit 3612d20ae0
2 changed files with 171 additions and 60 deletions

View File

@ -23,7 +23,9 @@
#include "backend/policy_manager.hh"
#include "backend/policy_parameters.hh"
#include "backend/history.hh"
#include "backend/dynamic_process.hh"
#include "backend/dynamic_resource.hh"
#include "backend/dynamic_thread.hh"
#include "text_simulation.hh"
@ -55,17 +57,40 @@ TextSimulation::add_io_device(smart_ptr<IOManager> io)
// Thread::create( sigc::bind(&TextSimulation::_io_loop, p), true);
}
void
TextSimulation::arguments_ignored(const Tokens& arguments)
bool
TextSimulation::check_arguments_num(const Tokens& arguments, unsigned int num)
{
if(arguments.size() != 0)
if(arguments.size() < num)
{
ostringstream oss;
oss << _("\nERROR: this command requires at least ") << num << _(" arguments\n");
p_stderr(oss.str());
return false;
}
else if(arguments.size() > num)
p_stderr(_("\nWARNING: some arguments will be ignored"));
return true;
}
template <typename T>
void
TextSimulation::show(const vector<T>& entities)
{
ostringstream oss;
for(unsigned int i = 0; i < entities.size(); ++i)
{
oss << i + 1 << ". " << entities[i]->get_core().get_name() << endl;
p_stdout(oss.str());
}
}
void
TextSimulation::on_run(const Tokens& arguments)
{
arguments_ignored(arguments);
check_arguments_num(arguments, 0);
try
{
@ -82,7 +107,7 @@ TextSimulation::on_run(const Tokens& arguments)
void
TextSimulation::on_pause(const Tokens& arguments)
{
arguments_ignored(arguments);
check_arguments_num(arguments, 0);
pause();
}
@ -90,7 +115,7 @@ TextSimulation::on_pause(const Tokens& arguments)
void
TextSimulation::on_stop(const Tokens& arguments)
{
arguments_ignored(arguments);
check_arguments_num(arguments, 0);
stop();
}
@ -98,7 +123,7 @@ TextSimulation::on_stop(const Tokens& arguments)
void
TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
{
arguments_ignored(arguments);
check_arguments_num(arguments, 0);
// FIXME we should use the current policy to obtain parmaters, this code
// is only for testing purposes
@ -290,17 +315,14 @@ void
TextSimulation::on_help(const Tokens& arguments)
{
ustring command;
//make a local copy which we'll probably modify
Tokens args = arguments;
if(args.size() > 0)
if(arguments.size() > 0)
{
command = args[0].uppercase();
args.erase(args.begin());
command = arguments[0].uppercase();
// print warning if necessary
check_arguments_num(arguments, 1);
}
arguments_ignored(args);
if(command.size() == 0)
p_stdout(_("\nAvaiable commands:\nRUN\nPAUSE\nSTOP\nQUIT\nHELP"
"\nGET\nSET\nSHOW\nADD\nREMOVE\nCONFIGURE-CPU-POLICY"
@ -350,7 +372,7 @@ TextSimulation::on_help(const Tokens& arguments)
void
TextSimulation::on_quit(const Tokens& arguments)
{
arguments_ignored(arguments);
check_arguments_num(arguments, 0);
p_stdout(_("\n\n*** Thank you for using SGPEM by Sirius Cybernetics Corporation ***\n\n"));
@ -361,23 +383,10 @@ TextSimulation::on_quit(const Tokens& arguments)
void
TextSimulation::on_get(const Tokens& arguments)
{
//make a local copy which we'll probably modify
Tokens args = arguments;
ustring attr;
if(args.size() == 0)
{
p_stderr("\nERROR: you must provide an argument\n");
if(!check_arguments_num(arguments, 1))
return;
}
else
{
attr = args[0].uppercase();
args.erase(args.begin());
}
arguments_ignored(args);
ustring attr = arguments[0].uppercase();
if(attr == "SIMULATION_TICK")
{
@ -386,37 +395,28 @@ TextSimulation::on_get(const Tokens& arguments)
p_stdout(oss.str());
}
else
p_stderr("\nERROR: invalid attribute. Accepted are: simulation_tick\n");
p_stderr(_("\nERROR: invalid attribute. Accepted are: simulation_tick\n"));
}
void
TextSimulation::on_set(const Tokens& arguments)
{
//make a local copy which we'll probably modify
Tokens args = arguments;
ustring attr;
ustring value;
if(args.size() < 2)
{
p_stderr("\nERROR: you must provide an attribute name and a value\n");
// handle the optional "=' (I knew that I was buying myself a problem when I
// decided to support the assigment operator!)
if(arguments.size() >= 3)
check_arguments_num(arguments, 3);
else if(!check_arguments_num(arguments, 2))
return;
}
ustring attr = arguments[0].uppercase();
ustring value;
if(arguments[1] == "=")
value = arguments[2];
else
{
attr = args[0].uppercase();
args.erase(args.begin());
if(args[0] == "=")
args.erase(args.begin());
value = args[0].uppercase();
args.erase(args.begin());
}
arguments_ignored(args);
value = arguments[1];
if(attr == "SIMULATION_TICK")
{
try
@ -425,16 +425,119 @@ TextSimulation::on_set(const Tokens& arguments)
}
catch(domain_error e)
{
p_stderr("\nERROR: you must provide a valid integer value\n");
p_stderr(_("\nERROR: you must provide a valid integer value\n"));
}
}
else
p_stderr("\nERROR: invalid attribute. Accepted are: simulation_tick\n");
p_stderr(_("\nERROR: invalid attribute. Accepted are: simulation_tick\n"));
}
void
TextSimulation::on_show(const Tokens& arguments)
{
if(!check_arguments_num(arguments, 1))
return;
//make a local copy which we'll probably modify
Tokens args = arguments;
ustring entities = args[0].uppercase();
args.erase(args.begin());
typedef void (TextSimulation::*f_ptr)(const Tokens&);
map<ustring, f_ptr> entities_handlers;
entities_handlers["PROCESSES"] = &TextSimulation::on_show_processes;
entities_handlers["RESOURCES"] = &TextSimulation::on_show_resources;
entities_handlers["THREADS"] = &TextSimulation::on_show_threads;
entities_handlers["REQUESTS"] = &TextSimulation::on_show_requests;
entities_handlers["SUBREQUESTS"] = &TextSimulation::on_show_subrequests;
entities_handlers["CPU-POLICIES"] = &TextSimulation::on_show_cpu_policies;
entities_handlers["RESOURCE-POLICIES"] = &TextSimulation::on_show_resource_policies;
if(entities_handlers.find(entities) == entities_handlers.end())
p_stderr(_("\nERROR: invalid argument\n"));
else
(this->*(entities_handlers[entities]))(args);
}
void
TextSimulation::on_show_processes(const Tokens& arguments)
{
check_arguments_num(arguments, 0);
// FIXME need to get the true process array. i think it's
// still not possible at the actual stage of development
vector<DynamicProcess*> processes;
show(processes);
}
void
TextSimulation::on_show_resources(const Tokens& arguments)
{
check_arguments_num(arguments, 0);
// FIXME need to get the true resource array. i think it's
// still not possible at the actual stage of development
vector<DynamicResource*> resources;
show(resources);
}
void
TextSimulation::on_show_threads(const Tokens& arguments)
{
if(!check_arguments_num(arguments, 1))
return;
ustring process = arguments[0];
// FIXME need to get the true arrays. i think it's
// still not possible at the actual stage of development
vector<DynamicProcess*> processes;
vector<DynamicThread*> threads;
try
{
unsigned int pid = string_to<int>(process);
threads = processes.at(pid - 1)->get_dynamic_threads();
}
catch(domain_error e)
{
p_stderr(_("ERROR: provided process identifier is not a valid integer\n"));
return;
}
catch(out_of_range e)
{
p_stderr(_("ERROR: this process identifier does not belong to an existing process\n"));
}
show(threads);
}
void
TextSimulation::on_show_requests(const Tokens& arguments)
{
p_stderr(_("\nFIXME: Not implemented\n"));
}
void
TextSimulation::on_show_subrequests(const Tokens& arguments)
{
p_stderr(_("\nFIXME: Not implemented\n"));
}
void
TextSimulation::on_show_cpu_policies(const Tokens& arguments)
{
p_stderr(_("\nFIXME: Not implemented\n"));
}
void
TextSimulation::on_show_resource_policies(const Tokens& arguments)
{
p_stderr(_("\nFIXME: Not implemented\n"));
}

View File

@ -122,8 +122,9 @@ namespace sgpem
void update();
private:
void arguments_ignored(const Tokens& arguments);
bool check_arguments_num(const Tokens& arguments, unsigned int num);
template <typename T>
void show(const std::vector<T>& entities);
void on_run(const Tokens& arguments);
void on_pause(const Tokens& arguments);
void on_stop(const Tokens& arguments);
@ -133,6 +134,13 @@ namespace sgpem
void on_get(const Tokens& arguments);
void on_set(const Tokens& arguments);
void on_show(const Tokens& arguments);
void on_show_processes(const Tokens& arguments);
void on_show_resources(const Tokens& arguments);
void on_show_threads(const Tokens& arguments);
void on_show_requests(const Tokens& arguments);
void on_show_subrequests(const Tokens& arguments);
void on_show_cpu_policies(const Tokens& arguments);
void on_show_resource_policies(const Tokens& arguments);
void on_add(const Tokens& arguments);
void on_remove(const Tokens& arguments);