- First implementation of a SimulationController in the GUI

that uses a callback to run the simulation in continuous mode.
It isn't finished yet.


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@948 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-08-28 20:15:05 +00:00
parent 1cdd2a6a9e
commit 3689424217
5 changed files with 213 additions and 59 deletions

View File

@ -37,11 +37,9 @@ using namespace sgpem;
using namespace memory; using namespace memory;
ConcreteSimulation::ConcreteSimulation() : ConcreteSimulation::ConcreteSimulation() :
_state(state_stopped), _mode(mode_continuous), _policy(NULL) Simulation(), _state(state_stopped),
{ _mode(mode_continuous), _policy(NULL)
_notify = false; {}
_front = 0;
}
void void
ConcreteSimulation::set_mode(const mode new_mode) ConcreteSimulation::set_mode(const mode new_mode)
@ -61,16 +59,20 @@ ConcreteSimulation::jump_to(History::position p)
switch (_state) switch (_state)
{ {
case state_running: case state_running:
pause(); // pauses the simulation (see below)
break; break;
case state_stopped: case state_stopped:
_history.reset(true); _history.reset(true);
_front = 0; _front = 0;
break; break;
default: default:
; break;
} }
// Disable momentarily updates for registered observers on
// sgpem::Simulation too.
// See History for an example of how to implement this.
set_notify_enabled(false);
pause(); pause();
// Disable updates to registered observers // Disable updates to registered observers
@ -87,6 +89,10 @@ ConcreteSimulation::jump_to(History::position p)
// Reenables updates to registered observers. // Reenables updates to registered observers.
// Calls _history.notify_change() on reactivation. // Calls _history.notify_change() on reactivation.
_history.set_notify_enabled(true); _history.set_notify_enabled(true);
// Do the same for notifications on the state
// of Simulation
set_notify_enabled(true);
} }
@ -94,16 +100,20 @@ void
ConcreteSimulation::pause() ConcreteSimulation::pause()
{ {
if(_state != state_paused) if(_state != state_paused)
notify_change(); {
_state = state_paused; _state = state_paused;
notify_change();
}
} }
void void
ConcreteSimulation::stop() ConcreteSimulation::stop()
{ {
if(_state != state_stopped) if(_state != state_stopped)
notify_change(); {
_state = state_stopped; _state = state_stopped;
notify_change();
}
} }
void void
@ -111,14 +121,12 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
{ {
switch (_state) switch (_state)
{ {
case state_running:
return;
case state_stopped: case state_stopped:
_history.reset(true); _history.reset(true);
_front = 0; _front = 0;
break; break;
default: default:
; break;
} }
_state = state_running; _state = state_running;
@ -126,8 +134,20 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
//step forward //step forward
bool yet_to_finish = step(); bool yet_to_finish = step();
if (yet_to_finish) if (yet_to_finish)
{
if(_mode == mode_step_by_step)
pause(); pause();
else else
// We remain in running state, and we notify everybody!
// This is non-trivial, and we must do so since we
// put the state to running inconditionally. Don't
// touch this if you don't provide another way to tell
// a SimulationObserver that the simulation advanced
// and *yet* it is in running state!
notify_change();
}
else
// Simulation ended
stop(); stop();
} }

View File

@ -35,6 +35,11 @@ using namespace sgpem;
// Explicit template instantiation to allow to export symbols from the DSO. // Explicit template instantiation to allow to export symbols from the DSO.
template class SG_DLLEXPORT Singleton<ConcreteSimulation>; template class SG_DLLEXPORT Singleton<ConcreteSimulation>;
Simulation::Simulation()
: _notify(true), _front(0)
{
}
Simulation::~Simulation() Simulation::~Simulation()
{} {}
@ -68,7 +73,7 @@ unsigned int Simulation::get_front() const
void void
Simulation::notify_change() Simulation::notify_change()
{ {
//if (!_notify) return; // what's the purpose of this? if (!_notify) return;
for (RegisteredObservers::iterator it = _observers.begin(); for (RegisteredObservers::iterator it = _observers.begin();
it != _observers.end(); it++) it != _observers.end(); it++)

View File

@ -67,9 +67,9 @@ namespace sgpem
public: public:
enum state enum state
{ {
state_running, state_running = 0xdeafd0d0,
state_paused, state_paused = 0xbaddcafe,
state_stopped state_stopped = 0xdeadbeef
}; };
enum mode enum mode
@ -78,7 +78,7 @@ namespace sgpem
mode_continuous = 1 mode_continuous = 1
}; };
virtual ~Simulation(); virtual ~Simulation() = 0;
/** /**
\brief Runs the simulation. \brief Runs the simulation.
@ -176,13 +176,14 @@ namespace sgpem
RegisteredObservers _observers; RegisteredObservers _observers;
// since no constructor is available, these fields must be defined in concrete subclasses. // since no constructor is available, these fields must be defined in concrete subclasses.
bool _notify;
History::position _front; History::position _front;
Simulation(); // Constructor
virtual void notify_change(); virtual void notify_change();
private: private:
bool _notify;
}; };
} }

View File

@ -38,6 +38,7 @@
#include "backend/serializer.hh" #include "backend/serializer.hh"
#include <gdkmm/pixbuf.h> #include <gdkmm/pixbuf.h>
#include <glibmm/timer.h>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <gtkmm/aboutdialog.h> #include <gtkmm/aboutdialog.h>
#include <gtkmm/messagedialog.h> #include <gtkmm/messagedialog.h>
@ -46,8 +47,8 @@
#include <gtkmm/menuitem.h> #include <gtkmm/menuitem.h>
#include <gtkmm/scrolledwindow.h> #include <gtkmm/scrolledwindow.h>
#include <gtkmm/stock.h> #include <gtkmm/stock.h>
#include <gtkmm/toolbutton.h>
#include <cassert>
#include <iostream> #include <iostream>
using namespace sgpem; using namespace sgpem;
@ -189,7 +190,7 @@ GuiBuilder::on_file_saveas_activate()
} }
GuiBuilder::GuiBuilder(const std::string& gladefile) GuiBuilder::GuiBuilder(const std::string& gladefile)
: _refXml(Xml::create(gladefile)) : _refXml(Xml::create(gladefile)), _controller(Simulation::get_instance(), _refXml)
{ {
using namespace Gtk; using namespace Gtk;
@ -211,37 +212,6 @@ GuiBuilder::GuiBuilder(const std::string& gladefile)
file_saveas->signal_activate().connect(sigc::mem_fun(*this, &GuiBuilder::on_file_saveas_activate)); file_saveas->signal_activate().connect(sigc::mem_fun(*this, &GuiBuilder::on_file_saveas_activate));
// Start, pause and stop simulation from the toolbar
// TODO: can we use action groups instead of this?
Simulation& sim = Simulation::get_instance();
// FIXME: just to try; a proper way to select a policy has to be installed
CPUPoliciesGatekeeper& pgk = CPUPoliciesGatekeeper::get_instance();
CPUPolicy* policy = pgk.get_registered()[0]->get_avail_policies()[1]; // RoundRobin at the moment of writing
std::cout << "Selected policy : " << policy->get_name() << std::endl;
pgk.activate_policy(&sim.get_history(), policy);
sim.set_policy(policy);
ToolButton *toolbt_start, *toolbt_pause, *toolbt_stop;
_refXml->get_widget("ToolBar.Play", toolbt_start);
_refXml->get_widget("ToolBar.Pause", toolbt_pause);
_refXml->get_widget("ToolBar.Stop", toolbt_stop);
toolbt_start->signal_clicked().connect(sigc::mem_fun(sim, &Simulation::run));
toolbt_pause->signal_clicked().connect(sigc::mem_fun(sim, &Simulation::pause));
toolbt_stop->signal_clicked().connect(sigc::mem_fun(sim, &Simulation::stop));
// Sensitivities
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_stop, &ToolButton::set_sensitive), true));
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), true));
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), false));
toolbt_pause->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), true));
toolbt_pause->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), false));
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_stop, &ToolButton::set_sensitive), false));
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), false));
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), true));
// Connect extra signals (decide where to do this... // Connect extra signals (decide where to do this...
// here -- ugly -- derive widgets and then use // here -- ugly -- derive widgets and then use
// Glade::Xml::get_widget_derived -- better --) // Glade::Xml::get_widget_derived -- better --)
@ -287,6 +257,13 @@ GuiBuilder::GuiBuilder(const std::string& gladefile)
simulation_window->add(*simulation_widget); simulation_window->add(*simulation_widget);
simulation_widget->show(); simulation_widget->show();
// ** FIXME: just to try; a proper way to select a policy has to be installed
Simulation& sim = Simulation::get_instance();
CPUPoliciesGatekeeper& pgk = CPUPoliciesGatekeeper::get_instance();
CPUPolicy* policy = pgk.get_registered()[0]->get_avail_policies()[1]; // RoundRobin at the moment of writing
std::cout << "Selected policy : " << policy->get_name() << std::endl;
sim.set_policy(policy);
} }
@ -311,3 +288,118 @@ GuiBuilder::open_file(const std::string& filename)
std::cout << _("Filename to open: ") << filename << std::endl; std::cout << _("Filename to open: ") << filename << std::endl;
} }
// ---------------------------------
SimulationController::SimulationController(Simulation& simulation, Glib::RefPtr<Xml> main_window)
: _sim(simulation), _refXml(main_window), _break_requested(false)
{
using namespace Gtk;
_sim.attach(*this);
// Start, pause and stop simulation from the toolbar
// TODO: can we use action groups instead of this?
_refXml->get_widget("ToolBar.Play", _toolbt_start);
_refXml->get_widget("ToolBar.Pause", _toolbt_pause);
_refXml->get_widget("ToolBar.Stop", _toolbt_stop);
_toolbt_start->signal_clicked().connect(sigc::mem_fun(*this, &SimulationController::on_simulation_run));
_toolbt_pause->signal_clicked().connect(sigc::mem_fun(*this, &SimulationController::on_simulation_pause));
_toolbt_stop->signal_clicked().connect(sigc::mem_fun(*this, &SimulationController::on_simulation_stop));
}
SimulationController::~SimulationController()
{
_sim.detach(*this);
}
void
SimulationController::on_simulation_run()
{
// Sensitivities
_toolbt_start->set_sensitive(false);
_toolbt_pause->set_sensitive(true);
_toolbt_stop->set_sensitive(true);
_break_requested = false;
_sim.run();
}
void
SimulationController::on_simulation_pause()
{
// Sensitivities
_toolbt_start->set_sensitive(true);
_toolbt_pause->set_sensitive(false);
_toolbt_stop->set_sensitive(true);
_break_requested = true;
_sim.pause();
}
void
SimulationController::on_simulation_stop()
{
// Sensitivities
_toolbt_start->set_sensitive(true);
_toolbt_pause->set_sensitive(false);
_toolbt_stop->set_sensitive(false);
_break_requested = true;
_sim.stop();
}
void
SimulationController::update(const Simulation& simulation)
{
std::cerr << "SimulationController::update(), simulation state == " << std::hex << simulation.get_state() << std::endl;
if(_break_requested)
return;
switch(simulation.get_state())
{
case Simulation::state_stopped:
on_simulation_stop();
return;
case Simulation::state_paused:
on_simulation_pause();
return;
case Simulation::state_running:
// Go on with the rest of the method...
break;
}
// We should never enter here, if the code is correct:
// a step by step simulation should always be in paused
// or stopped state after performing a single step
assert(simulation.get_mode() != Simulation::mode_step_by_step);
switch(simulation.get_mode())
{
case Simulation::mode_continuous:
{
int timeout = GlobalPreferences::get_instance().get_speed();
Glib::signal_timeout().connect(sigc::mem_fun(*this, &SimulationController::run_simulation_adaptor), timeout);
}
break;
default:
// Never gets here.
break;
}
}
bool
SimulationController::run_simulation_adaptor()
{
if(!_break_requested)
_sim.run();
return true;
}

View File

@ -24,6 +24,9 @@
#include "config.h" #include "config.h"
#include "simulation_widget.hh" #include "simulation_widget.hh"
#include "backend/simulation_observer.hh"
#include <gtkmm/toolbutton.h>
#include <gtkmm/window.h> #include <gtkmm/window.h>
#include <libglademm/xml.h> #include <libglademm/xml.h>
@ -35,6 +38,38 @@
namespace sgpem namespace sgpem
{ {
class GuiBuilder;
class SimulationController;
// Helper class for controlling the simulation via callbacks
// TODO: move me to another file.
class SimulationController : public SimulationObserver
{
public:
SimulationController(Simulation& simulation, Glib::RefPtr<Gnome::Glade::Xml> main_window);
virtual ~SimulationController();
void update(const Simulation& simulation);
private:
void on_simulation_run();
void on_simulation_pause();
void on_simulation_stop();
bool run_simulation_adaptor();
Simulation& _sim;
Glib::RefPtr<Gnome::Glade::Xml> _refXml;
bool _break_requested;
Gtk::ToolButton* _toolbt_start;
Gtk::ToolButton* _toolbt_pause;
Gtk::ToolButton* _toolbt_stop;
};
class GuiBuilder class GuiBuilder
{ {
public: public:
@ -51,6 +86,7 @@ namespace sgpem
private: private:
Glib::RefPtr<Gnome::Glade::Xml> _refXml; Glib::RefPtr<Gnome::Glade::Xml> _refXml;
SimulationWidget* _simulation_widget; SimulationWidget* _simulation_widget;
SimulationController _controller;
}; };
} //~ namespace sgpem } //~ namespace sgpem