- Fixed some bugs in command processing, added the configure-cpu-policy command

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@748 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-07-10 22:28:51 +00:00
parent 1a6805afc4
commit 6150af3d30
8 changed files with 196 additions and 77 deletions

View File

@ -21,7 +21,7 @@
from Policy import Policy from Policy import Policy
import sys import sys
from sgpem import policy_sorts_processes #from sgpem import policy_sorts_processes
class fcfs(Policy) : class fcfs(Policy) :
def __init__(self): def __init__(self):
@ -36,6 +36,11 @@ class fcfs(Policy) :
def get_time_slice(self): def get_time_slice(self):
return -2 return -2
# FIXME incorrect, but must be implemented to allow
# loading from the policy manager
def wants(self):
return 0
def sort_queue(self, queue): def sort_queue(self, queue):
cmpf = lambda a, b: \ cmpf = lambda a, b: \
a.get_arrival_time() < \ a.get_arrival_time() < \

View File

@ -21,7 +21,7 @@
from Policy import Policy from Policy import Policy
import sys import sys
from sgpem import policy_sorts_processes #from sgpem import policy_sorts_processes
class sjf(Policy) : class sjf(Policy) :
def __init__(self): def __init__(self):
@ -36,6 +36,11 @@ class sjf(Policy) :
def get_time_slice(self): def get_time_slice(self):
return -1 return -1
# FIXME incorrect, but must be implemented to allow
# loading from the policy manager
def wants(self):
return 0
def sort_queue(self, queue): def sort_queue(self, queue):
cmpf = lambda a, b: \ cmpf = lambda a, b: \
a.get_remaining_time() < \ a.get_remaining_time() < \

View File

@ -72,8 +72,9 @@ string_to_int(const ustring& str, int& num)
return true; return true;
} }
int template <typename T>
string_to_int(const ustring& str) throw(domain_error) T
string_to(const ustring& str) throw(domain_error)
{ {
if(tokenize(str).size() != 1) if(tokenize(str).size() != 1)
throw domain_error("too few or too many tokens"); throw domain_error("too few or too many tokens");
@ -82,17 +83,24 @@ string_to_int(const ustring& str) throw(domain_error)
iss.exceptions(ios_base::failbit | ios_base::badbit); iss.exceptions(ios_base::failbit | ios_base::badbit);
int value; T value;
try try
{ {
iss >> value; iss >> value;
} }
catch(ios_base::failure& e) catch(ios_base::failure e)
{ {
throw domain_error(e.what()); throw domain_error(e.what());
} }
// disable exceptions, otherwise peek() will throw them!
// how useless!!!
iss.exceptions(ios_base::goodbit);
if(iss.peek() != istringstream::traits_type::eof())
throw domain_error("incorrect number format");
return value; return value;
} }
@ -187,4 +195,7 @@ tokenize(const ustring& str)
return tokens; return tokens;
} }
template int string_to<int>(const Glib::ustring&);
template float string_to<float>(const Glib::ustring&);
} }

View File

@ -45,8 +45,8 @@ namespace sgpem
*/ */
bool SG_DLLEXPORT string_to_int(const Glib::ustring&, int&); bool SG_DLLEXPORT string_to_int(const Glib::ustring&, int&);
template <typename T>
int SG_DLLEXPORT string_to_int(const Glib::ustring&) throw(std::domain_error); T SG_DLLEXPORT string_to(const Glib::ustring&) throw(std::domain_error);
/**\brief This function converts an integer value into a string. /**\brief This function converts an integer value into a string.

View File

@ -89,7 +89,8 @@ GraphicalTerminalIO::onSend()
pair<TextSimulation*, IOManager*>(_sim, this), pair<TextSimulation*, IOManager*>(_sim, this),
read_command()); read_command());
Thread::create(sigc::bind(&TextSimulation::parse_command, p), true); //No way it will work with a graphical terminal at the moment
//Thread::create(sigc::bind(&TextSimulation::parse_command, p), true);
} }
uint uint

View File

@ -24,6 +24,8 @@
#include "main.hh" #include "main.hh"
#include "parse_opts.hh" #include "parse_opts.hh"
#include "text_simulation.hh"
#include "io_manager.hh"
#include <glibmm/module.h> #include <glibmm/module.h>
#include <glibmm/thread.h> #include <glibmm/thread.h>
@ -54,5 +56,16 @@ main(int argc, char* argv[])
// filenames of documents to be opened // filenames of documents to be opened
parse_options(argc, argv); parse_options(argc, argv);
TextSimulation sim;
while(1)
{
string str;
getline(cin, str);
TextSimulation::parse_command(sim, str);
}
return 0; return 0;
} }

View File

@ -24,6 +24,7 @@
#include "backend/policy_parameters.hh" #include "backend/policy_parameters.hh"
#include "backend/history.hh" #include "backend/history.hh"
#include "text_simulation.hh" #include "text_simulation.hh"
#include <sstream> #include <sstream>
@ -99,17 +100,31 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
{ {
arguments_ignored(arguments); arguments_ignored(arguments);
PolicyParameters& parameters = get_policy()->get_parameters(); // FIXME we should use the current policy to obtain parmaters, this code
// is only for testing purposes
PolicyParameters parameters;
p_stdout(_("\nPlease provide a value for each attribute")); parameters.register_int("a", -50, 50, false, 10);
p_stdout(_("\nMandatory arguments are marked with an asterisk (*)")); parameters.register_int("b", -2, INT_MAX, true);
parameters.register_int("c", INT_MIN, 2, true);
parameters.register_float("d", -FLT_MAX, FLT_MAX, true);
parameters.register_float("e", -5.0f, 50.0f, false, 10);
parameters.register_float("f", -FLT_MAX, 2.0f, true);
parameters.register_string("g", false);
parameters.register_string("h", true);
parameters.register_string("g", true);
p_stdout(_("\nPlease provide a value for each attribute:"));
p_stdout(_("\nMandatory arguments are marked with an asterisk (*)\n"));
p_stdout(_("\nInteger arguments:\n"));
typedef map<ustring, PolicyParameters::Parameter<int> > IntParams; typedef map<ustring, PolicyParameters::Parameter<int> > IntParams;
typedef map<ustring, PolicyParameters::Parameter<float> > FloatParams; typedef map<ustring, PolicyParameters::Parameter<float> > FloatParams;
typedef map<ustring, PolicyParameters::Parameter<ustring> > StringParams; typedef map<ustring, PolicyParameters::Parameter<ustring> > StringParams;
// TODO for int and double this code is easily replaced with a call to a
// template function. for strings we'll need to specialize it...
IntParams int_params = parameters.get_registered_int_parameters(); IntParams int_params = parameters.get_registered_int_parameters();
for(IntParams::iterator it = int_params.begin(); it != int_params.end();) for(IntParams::iterator it = int_params.begin(); it != int_params.end();)
@ -127,7 +142,6 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
p_stdout(buf.str()); p_stdout(buf.str());
bool arg_provided = false;
bool correct = true; bool correct = true;
ustring input = readline(); ustring input = readline();
@ -142,7 +156,13 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
{ {
try try
{ {
value = string_to_int(input); value = string_to<int>(input);
if(value > p.get_upper_bound() || value < p.get_lower_bound())
{
p_stderr(_("\nERROR: Provided value is out of range"));
correct = false;
}
} }
catch(domain_error e) catch(domain_error e)
{ {
@ -150,14 +170,8 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
correct = false; correct = false;
} }
if(value > p.get_upper_bound() || value < p.get_lower_bound())
{
p_stderr(_("\nERROR: Provided value is out of range"));
correct = false;
}
if(correct) if(correct)
p.set_value(value); parameters.set_int(p.get_name(), value);
} }
else if(p.is_required()) else if(p.is_required())
{ {
@ -170,41 +184,109 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
++it; ++it;
} }
//FIXME code for float arguments p_stdout(_("\nFloating-point arguments:\n"));
// NOTE this piece code is a verbatim copy of the one above.
// I tried solving this issue by using templates, but to make
// it work will require adding to PolicyParameters a member template
// method with 2 specializations...
FloatParams float_params = parameters.get_registered_float_parameters();
for(FloatParams::iterator it = float_params.begin(); it != float_params.end();)
{
PolicyParameters::Parameter<float> &p = it->second;
ostringstream buf;
buf << "\n";
if(p.is_required())
buf << "*";
buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
p_stdout(buf.str());
bool correct = true;
ustring input = readline();
float value;
// FIXME semi-hack, it's a bit overkill to tokenize the string
// to find if it's only composed of white spaces...
// Indedeed there's a pro: by using extensively tokenize() we are more sure
// it's correct ;-)
if(tokenize(input).size() > 0)
{
try
{
value = string_to<float>(input);
if(value > p.get_upper_bound() || value < p.get_lower_bound())
{
p_stderr(_("\nERROR: Provided value is out of range"));
correct = false;
}
}
catch(domain_error e)
{
p_stderr(_("\nERROR: Please provide a valid floating-point value"));
correct = false;
}
if(correct)
parameters.set_float(p.get_name(), value);
}
else if(p.is_required())
{
p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!"));
correct = false;
}
if(correct)
++it;
}
p_stdout(_("\nString arguments:\n"));
StringParams string_params = parameters.get_registered_string_parameters();
for(StringParams::iterator it = string_params.begin(); it != string_params.end();)
{
PolicyParameters::Parameter<ustring> &p = it->second;
ustring buf;
buf = "\n";
if(p.is_required())
buf += "*";
p_stdout(buf + p.get_name() + " = ");
buf = readline();
// FIXME semi-hack, it's a bit overkill to tokenize the string
// to find if it's only composed of white spaces...
// Indedeed there's a pro: by using extensively tokenize() we are more sure
// it's correct ;-)
Tokens tokens = tokenize(buf);
if(tokens.size() == 0 && p.is_required())
p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!"));
else
{
// FIXME should we insert the entire line here or just a token?
parameters.set_string(p.get_name(), buf);
++it;
}
}
// StringParams string_params = parameters->get_registered_string_parameters();
//
// for(StringParams::iterator it = int_params.begin(); it != int_params.end();)
// {
// Parameter<ustring> &p = *it;
// ustring buf;
//
// buf = "\n";
//
// if(p.is_required())
// buf += "*";
//
// p_stdout(buf + p.get_name() + " = ");
//
// buf = read();
//
// // FIXME semi-hack, it's a bit overkill to tokenize the string
// // to find if it's only composed of white spaces...
// // Indedeed there's a pro: by using extensively tokenize() we are more sure
// // it's correct ;-)
// Tokens tokens = tokenize(input);
//
// if(tokens.size() == 0 && p.is_required())
// p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!"));
// else
// {
// p.set_value(tokens[0]);
// ++it;
// }
//
// }
} }
void void
TextSimulation::on_help(const Tokens& arguments) TextSimulation::on_help(const Tokens& arguments)
{ {
@ -212,7 +294,7 @@ TextSimulation::on_help(const Tokens& arguments)
//make a local copy which we'll probably modify //make a local copy which we'll probably modify
Tokens args = arguments; Tokens args = arguments;
if(args.size() > 1) if(args.size() > 0)
{ {
command = args[0].uppercase(); command = args[0].uppercase();
args.erase(args.begin()); args.erase(args.begin());
@ -222,9 +304,9 @@ TextSimulation::on_help(const Tokens& arguments)
if(command.size() == 0) if(command.size() == 0)
p_stdout(_("\nAvaiable commands:\nRUN\nPAUSE\nSTOP\nQUIT\nHELP" p_stdout(_("\nAvaiable commands:\nRUN\nPAUSE\nSTOP\nQUIT\nHELP"
"\nGET\nSET\nSHOW\nADD\nREMOVE" "\nGET\nSET\nSHOW\nADD\nREMOVE\nCONFIGURE-CPU-POLICY"
"\n\nHELP followed by a command shows help about it." "\n\nHELP followed by a command shows help about it."
"\n ex. HELP RUN shows help about the command RUN")); "\n ex. HELP RUN shows help about the command RUN\n"));
else if(command == "RUN") else if(command == "RUN")
p_stdout(_("\n-- RUN COMMAND --\nStarts the simulation. It can be " p_stdout(_("\n-- RUN COMMAND --\nStarts the simulation. It can be "
"continuous or step-by-step depending on the mode configured with " "continuous or step-by-step depending on the mode configured with "
@ -278,31 +360,31 @@ TextSimulation::on_quit(const Tokens& arguments)
void void
TextSimulation::on_get(const Tokens& arguments) TextSimulation::on_get(const Tokens& arguments)
{ {
p_stderr(_("\nFIXME: Not implemented")); p_stderr(_("\nFIXME: Not implemented\n"));
} }
void void
TextSimulation::on_set(const Tokens& arguments) TextSimulation::on_set(const Tokens& arguments)
{ {
p_stderr(_("\nFIXME: Not implemented")); p_stderr(_("\nFIXME: Not implemented\n"));
} }
void void
TextSimulation::on_show(const Tokens& arguments) TextSimulation::on_show(const Tokens& arguments)
{ {
p_stderr(_("\nFIXME: Not implemented")); p_stderr(_("\nFIXME: Not implemented\n"));
} }
void void
TextSimulation::on_add(const Tokens& arguments) TextSimulation::on_add(const Tokens& arguments)
{ {
p_stderr(_("\nFIXME: Not implemented")); p_stderr(_("\nFIXME: Not implemented\n"));
} }
void void
TextSimulation::on_remove(const Tokens& arguments) TextSimulation::on_remove(const Tokens& arguments)
{ {
p_stderr(_("\nFIXME: Not implemented")); p_stderr(_("\nFIXME: Not implemented\n"));
} }
void void
@ -326,7 +408,7 @@ TextSimulation::readline()
} }
void void
TextSimulation::parse_command(pair< pair<TextSimulation*, IOManager*>, const ustring > p) TextSimulation::parse_command(TextSimulation& sim, const ustring& str)
{ {
typedef void (TextSimulation::*f_ptr)(const Tokens&); typedef void (TextSimulation::*f_ptr)(const Tokens&);
map<ustring, f_ptr> command_handlers; map<ustring, f_ptr> command_handlers;
@ -343,25 +425,22 @@ TextSimulation::parse_command(pair< pair<TextSimulation*, IOManager*>, const us
command_handlers["REMOVE"] = &TextSimulation::on_remove; command_handlers["REMOVE"] = &TextSimulation::on_remove;
command_handlers["QUIT"] = &TextSimulation::on_quit; command_handlers["QUIT"] = &TextSimulation::on_quit;
ustring str = p.second;
Tokens arguments = tokenize(str); Tokens arguments = tokenize(str);
if(arguments.size() == 0) if(arguments.size() == 0)
return; return;
if(command_handlers.find(arguments[0]) == command_handlers.end()) ustring key = arguments[0].uppercase();
if(command_handlers.find(key) == command_handlers.end())
{ {
p_stderr(_("\nERROR: command not supported")); p_stderr(_("\nERROR: command not supported\n"));
return; return;
} }
ustring key = arguments[0].uppercase();
arguments.erase(arguments.begin()); arguments.erase(arguments.begin());
TextSimulation* obj = p.first.first; (sim.*(command_handlers[key]))(arguments);
(obj->*(command_handlers[key]))(arguments);
} }
// ** Please do NOT delete this code, I still use it as a reference ** // ** Please do NOT delete this code, I still use it as a reference **
@ -842,7 +921,10 @@ TextSimulation::_io_loop(pair<TextSimulation* , int > pun)
//Thread::create( sigc::bind(&TextSimulation::parse_command, p), true); //Thread::create( sigc::bind(&TextSimulation::parse_command, p), true);
//else //else
//no read is possible: only write //no read is possible: only write
pun.first->parse_command(p);
// FIXME what was the purpose of this (_io_loop()) method
// need to comment out to make the code compile
//pun.first->parse_command(p);
} }
} }

View File

@ -35,6 +35,7 @@
#include <glibmm/thread.h> #include <glibmm/thread.h>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <utility> #include <utility>
#include <map>
namespace sgpem namespace sgpem
@ -107,7 +108,7 @@ namespace sgpem
\li getpolicyattributes <br> \li getpolicyattributes <br>
Prints the name and the value of the policy's attributes Prints the name and the value of the policy's attributes
*/ */
static void parse_command(std::pair< std::pair<TextSimulation*, IOManager*>, const Glib::ustring >); static void parse_command(TextSimulation &sim, const Glib::ustring& str);
/** /**
Adds an available I/O device. Adds an available I/O device.
@ -122,6 +123,7 @@ namespace sgpem
private: private:
void arguments_ignored(const Tokens& arguments); void arguments_ignored(const Tokens& arguments);
void on_run(const Tokens& arguments); void on_run(const Tokens& arguments);
void on_pause(const Tokens& arguments); void on_pause(const Tokens& arguments);
void on_stop(const Tokens& arguments); void on_stop(const Tokens& arguments);