diff --git a/src/Makefile.am b/src/Makefile.am index 1dc5b67..7482632 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -54,13 +54,17 @@ sgpemv2_SOURCES = \ main_window.cc \ observer.cc \ parse_opts.cc \ + simulation.cc \ + standard_io.cc \ start_gui.cc noinst_HEADERS = \ + graphical_terminal_io.hh \ io_manager.hh \ main.hh \ main_window.hh \ observer.hh \ - start_gui.hh \ - graphical_terminal_io.hh \ - parse_opts.hh + parse_opts.hh \ + simulation.hh \ + standard_io.hh \ + start_gui.hh diff --git a/src/backend/history.cc b/src/backend/history.cc index 0734993..45e7d3f 100644 --- a/src/backend/history.cc +++ b/src/backend/history.cc @@ -56,7 +56,7 @@ History::get_scheduled_at(int time) const //look for a runing entity smart_ptr p = get_simulation_status_at(time); - for (int i = 0; i < p->size(); i++) + for (uint i = 0; i < p->size(); i++) if (p->get_item_at(i)->get_state() == SchedulableStatus::state_running) return smart_ptr(new SchedulableStatus(*(p->get_item_at(i)))); @@ -138,8 +138,9 @@ History::truncate_at(int instant) //replaces the current vector with the "trimmed" one. _slices = vector(_slices.begin(),i); _total_time_elapsed = instant; - return; + break; } + notify(); } diff --git a/src/backend/policy.hh b/src/backend/policy.hh index f76f4fa..dc56cfc 100644 --- a/src/backend/policy.hh +++ b/src/backend/policy.hh @@ -49,6 +49,7 @@ namespace sgpem virtual bool is_pre_emptive() const = 0; virtual int get_time_slice() const = 0; virtual void set_time_slice(const int&) = 0; + const PolicyParameters& get_parameters() const; diff --git a/src/backend/python_policy.cc b/src/backend/python_policy.cc index 29b4b33..d443c49 100644 --- a/src/backend/python_policy.cc +++ b/src/backend/python_policy.cc @@ -24,17 +24,4 @@ using namespace std; PythonPolicy::PythonPolicy() { - // The following line is ugly, but necessary if we use - // non-standard installation directories. Theoretically, - // it should be up to the user to set correct - // environment variables. - // FIXME: find better way to achieve this. - //PyRun_SimpleString("import sys\n" - // "sys.path[:0] = [ '" MODDIR "', '" PYCDIR "' ]\n" - //"print '[II] Module search path is :', sys.path\n"); - - const char* filename = PYCDIR "/loadme.py"; - cout << filename; - cout.flush(); } - diff --git a/src/backend/python_policy_manager.cc b/src/backend/python_policy_manager.cc index 1f6d97a..c5d1f59 100644 --- a/src/backend/python_policy_manager.cc +++ b/src/backend/python_policy_manager.cc @@ -47,6 +47,20 @@ void PythonPolicyManager::init() { Py_Initialize(); + + // The following line is ugly, but necessary if we use + // non-standard installation directories. Theoretically, + // it should be up to the user to set correct + // environment variables. + // FIXME: find better way to achieve this. + + PyRun_SimpleString("import sys\n" + "sys.path[:0] = [ '" MODDIR "', '" PYCDIR "' ]\n" + "print '[II] Module search path is :', sys.path\n"); + + const char* filename = PYCDIR "/loadme.py"; + std::cout << "\n\n" << filename; + std::cout.flush(); } diff --git a/src/backend/scheduler.cc b/src/backend/scheduler.cc index dbf87c1..ceb074b 100644 --- a/src/backend/scheduler.cc +++ b/src/backend/scheduler.cc @@ -45,6 +45,7 @@ Scheduler::get_ready_queue() return &_ready_queue; } + /** \note E' fondamentale che questo metodo memorizzi localmente qualora la politica attuale sia a prerilascio o meno, e la durata del quanto di tempo, in quanto la politica e' libera di variare questi parametri a piacere durante l'esecuzione della simulazione @@ -57,6 +58,19 @@ Scheduler::reset_status() // restore the policy } +void +Scheduler::set_policy(Policy* p) +{ + _policy = p; +} + + +Policy* +Scheduler::get_policy() +{ + return _policy; +} + void Scheduler::step_forward() @@ -146,6 +160,3 @@ Scheduler::step_forward() - - - diff --git a/src/backend/scheduler.hh b/src/backend/scheduler.hh index 2104e80..41b26af 100644 --- a/src/backend/scheduler.hh +++ b/src/backend/scheduler.hh @@ -56,7 +56,10 @@ namespace sgpem SchedulableList* get_ready_queue(); void reset_status(); void step_forward(); + void set_policy(Policy*); + Policy* get_policy(); + private: Scheduler(int); //private constructor. The parameter is discarded static Scheduler _instance; diff --git a/src/graphical_terminal_io.cc b/src/graphical_terminal_io.cc index 5a4255b..8f6a9c4 100644 --- a/src/graphical_terminal_io.cc +++ b/src/graphical_terminal_io.cc @@ -61,7 +61,7 @@ GraphicalTerminalIO::GraphicalTerminalIO() GraphicalTerminalIO::~GraphicalTerminalIO() {} -GraphicalTerminalIO::size_type +uint GraphicalTerminalIO::write_buffer(const Glib::ustring& buffer) { _text_output.get_buffer()->insert_at_cursor(buffer); @@ -69,7 +69,7 @@ GraphicalTerminalIO::write_buffer(const Glib::ustring& buffer) } Glib::ustring -GraphicalTerminalIO::read_command() const +GraphicalTerminalIO::read_command() { using Glib::ustring; static const ustring whitespaces = " \r\b\n\t\a"; @@ -78,8 +78,8 @@ GraphicalTerminalIO::read_command() const ustring command = _text_input.get_text(); // trimming: - size_type f = command.find_first_not_of(whitespaces); - size_type l = command.find_last_not_of(whitespaces); + uint f = command.find_first_not_of(whitespaces); + uint l = command.find_last_not_of(whitespaces); if(f == ustring::npos) return 0; diff --git a/src/graphical_terminal_io.hh b/src/graphical_terminal_io.hh index 3d48e52..3f08b76 100644 --- a/src/graphical_terminal_io.hh +++ b/src/graphical_terminal_io.hh @@ -45,13 +45,13 @@ namespace sgpem { * ... long desc ... */ class GraphicalTerminalIO : public IOManager, public Gtk::Window { - typedef unsigned int size_type; + public: GraphicalTerminalIO(); virtual ~GraphicalTerminalIO(); - virtual size_type write_buffer(const Glib::ustring& buffer); - virtual Glib::ustring read_command() const; + virtual uint write_buffer(const Glib::ustring& buffer); + virtual Glib::ustring read_command(); private: Gtk::TextView _text_output; mutable Gtk::Entry _text_input; diff --git a/src/io_manager.hh b/src/io_manager.hh index 3fdb21b..2a5fe7b 100644 --- a/src/io_manager.hh +++ b/src/io_manager.hh @@ -28,23 +28,19 @@ namespace sgpem { - // --------------------------------------------- - + class IOManager; + + /** - // --------------------------------------------- - - /** \brief - * - * ... long desc ... */ + */ class IOManager - { - typedef unsigned int size_type; + { public: virtual ~IOManager() {} - virtual size_type write_buffer(const Glib::ustring& buffer) = 0; - virtual Glib::ustring read_command() const = 0; + virtual uint write_buffer(const Glib::ustring& buffer) = 0; + virtual Glib::ustring read_command() = 0; }; } diff --git a/src/main.cc b/src/main.cc index 97dd1b1..87f64cd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -36,6 +36,10 @@ #include "backend/policy_parameters.hh" #include "backend/python_policy.hh" #include "backend/python_policy_manager.hh" +#include "standard_io.hh" + +#include +#include #include #include @@ -66,10 +70,15 @@ main(int argc, char* argv[]) filenames.insert(filenames.begin(), a_ptr, a_ptr+a_count); } */ - start_gui(argc, argv); - - PythonPolicyManager p = PythonPolicyManager::get_instance(); + //start_gui(argc, argv); + PythonPolicyManager& p = PythonPolicyManager::get_instance(); + p.init(); + StandardIO io; + io.write_buffer("\nciao!!"); + ustring ss = io.read_command(); + cout << "\n\n\n" << ss; + //SMOKE-TEST for backend classes /* cout << "\n\n********************************"; Process p1("P1", 0,10,1); diff --git a/src/simulation.cc b/src/simulation.cc new file mode 100644 index 0000000..c61e362 --- /dev/null +++ b/src/simulation.cc @@ -0,0 +1,180 @@ +// src/backend/simulation.cc - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// SGPEMv2 is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "simulation.hh" + +using namespace std; +using namespace sgpem; +using namespace memory; +using Glib::usleep; + +Simulation::Simulation(): _state(state_paused), _mode(false), _timer_interval(1000) +{ +} + +void +Simulation::set_timer(const int& t) +{ + _timer_interval = t; +} + +int +Simulation::get_timer() const +{ + return _timer_interval; +} + +void +Simulation::set_mode(const bool& b) +{ + _mode = b; +} + +bool +Simulation::get_mode() const +{ + return _mode; +} + +void +Simulation::pause() +{ + _state = state_paused; +} + +void +Simulation::stop() +{ + _state = state_stopped; +} + +void +Simulation::reset() +{ + _state = state_paused; + History::get_instance().truncate_at(0); +} + +void +Simulation::run() +{ + History& h = History::get_instance(); + + if (_state == state_stopped) + h.truncate_at(0); + + _state = state_running; + + //******* CONTINUOUS TIME + if (_mode) + { + loop: + // chech for termination + bool all_term = true; + smart_ptr left = h.get_simulation_status_at(h.get_current_time()); + for(uint i = 0; i < left->size(); i++) + if (left->get_item_at(i)->get_state() != SchedulableStatus::state_terminated) + { + all_term = true; + break; + } + + //if there are no processes left the termination message has already been notified + //by the last execution of upadate() + if (all_term) + { + _state = state_paused; + return; + } + + //step forward + Scheduler::get_instance().step_forward(); + + //sleep + Glib::usleep(_timer_interval*1000); + + //check the state + if (_state == state_stopped || _state == state_paused) + return; + + goto loop; + } + + //******* STEP by STEP + else + { + // chech for termination + bool all_term = true; + smart_ptr left = h.get_simulation_status_at(h.get_current_time()); + for(uint i = 0; i < left->size(); i++) + if (left->get_item_at(i)->get_state() != SchedulableStatus::state_terminated) + { + all_term = true; + break; + } + + if (all_term) + //if there are no processes left the termination message has already been notified + //by the last execution of upadate() + _state = state_paused; + else + //step forward + Scheduler::get_instance().step_forward(); + } + +} + + +void +Simulation::jump_to(const uint& where) +{ + //jump to position 0 + reset(); + bool old = _mode; + _mode = false; + + // executes "where" steps + for (uint i=0; i < where; i++) + run(); + + _state = state_paused; + _mode = old; +} + +void +Simulation::set_policy(Policy* p) +{ + Scheduler::get_instance().set_policy(p); +} + + +Policy* +Simulation::get_policy() +{ + return Scheduler::get_instance().get_policy(); +} + +vector +Simulation::get_avaiable_policies() +{ + vector v; + v.push_back(Scheduler::get_instance().get_policy()); + return v; +} diff --git a/src/simulation.hh b/src/simulation.hh new file mode 100644 index 0000000..c25f39e --- /dev/null +++ b/src/simulation.hh @@ -0,0 +1,78 @@ +// src/frontend/simulation.hh - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// SGPEMv2 is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef SIMULATION_HH +#define SIMULATION_HH 1 + +#include "config.h" + +#include + +#include "observer.hh" +#include "backend/policy.hh" +#include "backend/history.hh" +#include "backend/scheduler.hh" + +namespace sgpem +{ + class Simulation; + + /** + + */ + class SG_DLLEXPORT Simulation : public Observer + { + public: + enum state + { + state_running, + state_paused, + state_stopped + }; + + Simulation(); + + void run(); + void pause(); + void stop(); + void reset(); + void jump_to(const uint&); + + void set_timer(const int&); + int get_timer() const; + void set_mode(const bool&); + bool get_mode() const; + + void set_policy(Policy*); + Policy* get_policy(); + std::vector get_avaiable_policies(); + + + private: + state _state; + bool _mode; + int _timer_interval; + + }; + +} + + +#endif diff --git a/src/standard_io.cc b/src/standard_io.cc new file mode 100644 index 0000000..b599502 --- /dev/null +++ b/src/standard_io.cc @@ -0,0 +1,43 @@ +// src/standard_io.cc - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// SGPEMv2 is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "standard_io.hh" + +using namespace std; +using namespace sgpem; +using Glib::ustring; + +uint +StandardIO::write_buffer(const ustring& buffer) +{ + cout << buffer; + if (cout.good()) + return buffer.length(); + else + return 0; +} + +ustring +StandardIO::read_command() +{ + ustring i; + cin >>i; + return i; +} diff --git a/src/standard_io.hh b/src/standard_io.hh new file mode 100644 index 0000000..6c67b8f --- /dev/null +++ b/src/standard_io.hh @@ -0,0 +1,50 @@ +// src/standard_io.hh - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// SGPEMv2 is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef STANDARD_IO_HH +#define STANDARD_IO_HH 1 + +#include "config.h" +#include "gettext.h" + +#include +#include + +#include "io_manager.hh" + +namespace sgpem { + + + class StandardIO; + + /** + + */ + class StandardIO : public IOManager + { + public: + + uint write_buffer(const Glib::ustring& buffer); + Glib::ustring read_command(); + }; + +} + +#endif diff --git a/src/text_simulation.hh b/src/text_simulation.hh new file mode 100644 index 0000000..fd8abdc --- /dev/null +++ b/src/text_simulation.hh @@ -0,0 +1,48 @@ +// src/backend/text_simulation.hh - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// SGPEMv2 is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef TEXT_SIMULATION_HH +#define TEXT_SIMULATION_HH 1 + +#include "config.h" + +#include "simulation.hh" + + + + +namespace sgpem +{ + class TextSimulation; + + /** + + */ + class SG_DLLEXPORT TextSimulation : public Simulation + { + public: + + + }; + +} + + +#endif \ No newline at end of file