diff --git a/src/backend/simulation.hh b/src/backend/simulation.hh index fcc79ec..a1925d2 100644 --- a/src/backend/simulation.hh +++ b/src/backend/simulation.hh @@ -32,6 +32,7 @@ namespace sgpem #include "config.h" #include "history.hh" #include "singleton.hh" +#include "cpu_policy_exception.hh" #include "user_interrupt_exception.hh" #include "null_policy_exception.hh" #include "malformed_policy_exception.hh" @@ -138,7 +139,7 @@ namespace sgpem /** \brief Setup the CPU policy to be used by the system. */ - virtual void set_policy(CPUPolicy*) = 0; + virtual void set_policy(CPUPolicy*) throw(CPUPolicyException) = 0; /** \brief Setup the resource policy to be used by the system. diff --git a/src/gui_builder.cc b/src/gui_builder.cc index 9273a54..0961b27 100644 --- a/src/gui_builder.cc +++ b/src/gui_builder.cc @@ -28,16 +28,16 @@ #include "simulation_widget.hh" #include "resources_widget.hh" +#include "sequences.tcc" + #include "backend/cpu_policy_exception.hh" #include "backend/malformed_policy_exception.hh" #include "backend/null_policy_exception.hh" #include "backend/user_interrupt_exception.hh" #include "backend/cpu_policies_gatekeeper.hh" -#include "backend/cpu_policy.hh" #include "backend/cpu_policy_manager.hh" #include "backend/resource_policies_gatekeeper.hh" #include "backend/resource_policy_manager.hh" -#include "backend/resource_policy.hh" #include "backend/history.hh" #include "backend/policy_parameters.hh" #include "backend/simulation.hh" @@ -53,7 +53,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -64,7 +66,6 @@ using namespace sgpem; using Gnome::Glade::Xml; - void GuiBuilder::on_edit_preferences_activate() { @@ -246,6 +247,121 @@ GuiBuilder::on_configure_resource_policy() } +void +GuiBuilder::populate_with_cpu_policies(Gtk::Menu& menu) +{ + using namespace Gtk; + + // NOTE: Please note that this code relies on the fact that a given + // policy never "disappears" at runtime. A *GatekeeperObserver should + // be needed to avoid dangling pointers if this behaviour changes. + + typedef std::vector Managers; + typedef std::vector Policies; + + RadioButtonGroup group; + + CPUPoliciesGatekeeper& pgk = CPUPoliciesGatekeeper::get_instance(); + const Managers& managers = pgk.get_registered(); + + for(Iseq m_it = const_iseq(managers); m_it; ++m_it) + { + const Policies& policies = (*m_it)->get_avail_policies(); + for(Iseq p_it = const_iseq(policies); p_it; ++p_it) + { + RadioMenuItem& menuitem = *manage(new RadioMenuItem(group, (*p_it)->get_name())); + menuitem.signal_activate().connect(sigc::bind(sigc::mem_fun(*this, &GuiBuilder::on_selected_cpu_policy), *p_it)); + menu.append(menuitem); + menuitem.show(); + } + } + + // Activate the first policy available if possible + Menu::MenuList& items = menu.items(); + if(!items.empty()) + menu.activate_item(items.front()); +} + + +void +GuiBuilder::on_selected_cpu_policy(CPUPolicy* pol) +{ + using namespace Gtk; + + Statusbar* sbar; + _refXml->get_widget("MainStatusBar", sbar); + + try + { + Simulation::get_instance().set_policy(pol); + if(pol != NULL) { + sbar->push(_("Selected CPU policy ") + pol->get_name()); + return; + } + } + catch(CPUPolicyException& e) + { + Simulation::get_instance().set_policy(NULL); + MessageDialog error(get_initial_window(), + Glib::ustring(_("Impossible to select this CPU Policy.\n")) + e.what(), + true, MESSAGE_ERROR, BUTTONS_OK, true); + error.run(); + } + + // If we got here, no policy is selected. + sbar->push(_("No CPU policy selected. Please select one.")); +} + + +void +GuiBuilder::populate_with_resource_policies(Gtk::Menu& menu) +{ + using namespace Gtk; + + // NOTE: Please note that this code relies on the fact that a given + // policy never "disappears" at runtime. A *GatekeeperObserver should + // be needed to avoid dangling pointers if this behaviour changes. + + typedef std::vector Managers; + typedef std::vector Policies; + + RadioButtonGroup group; + + ResourcePoliciesGatekeeper& pgk = ResourcePoliciesGatekeeper::get_instance(); + const Managers& managers = pgk.get_registered(); + + for(Iseq m_it = const_iseq(managers); m_it; ++m_it) + { + const Policies& policies = (*m_it)->get_avail_policies(); + for(Iseq p_it = const_iseq(policies); p_it; ++p_it) + { + RadioMenuItem& menuitem = *manage(new RadioMenuItem(group, (*p_it)->get_name())); + menuitem.signal_activate().connect(sigc::bind(sigc::mem_fun(*this, &GuiBuilder::on_selected_resource_policy), *p_it)); + menu.append(menuitem); + menuitem.show(); + } + } + + // Activate the first policy available if possible + Menu::MenuList& items = menu.items(); + if(!items.empty()) + menu.activate_item(items.front()); +} + + +void +GuiBuilder::on_selected_resource_policy(ResourcePolicy* pol) +{ + using namespace Gtk; + Simulation::get_instance().set_resource_policy(pol); + Statusbar* sbar; + _refXml->get_widget("MainStatusBar", sbar); + if(pol == NULL) + sbar->push(_("Current resource policy deselected.")); + else + sbar->push(_("Selected resource policy ") + pol->get_name()); +} + GuiBuilder::GuiBuilder(const std::string& gladefile) : _refXml(Xml::create(gladefile)), _controller(Simulation::get_instance(), _refXml) @@ -291,11 +407,16 @@ GuiBuilder::GuiBuilder(const std::string& gladefile) MenuToolButton* cpu_policies_tb_menu; _refXml->get_widget("ToolBar.PolicySelector", cpu_policies_tb_menu); cpu_policies_tb_menu->signal_clicked().connect(sigc::mem_fun(*this, &GuiBuilder::on_configure_cpu_policy)); + cpu_policies_tb_menu->set_menu(*manage(new Menu())); + populate_with_cpu_policies(*cpu_policies_tb_menu->get_menu()); + // Configure Resource Policy MenuToolButton* res_policies_tb_menu; _refXml->get_widget("ToolBar.ResourceScheduling", res_policies_tb_menu); res_policies_tb_menu->signal_clicked().connect(sigc::mem_fun(*this, &GuiBuilder::on_configure_resource_policy)); + res_policies_tb_menu->set_menu(*manage(new Menu())); + populate_with_resource_policies(*res_policies_tb_menu->get_menu()); // --------------------------------------------------- @@ -330,23 +451,7 @@ GuiBuilder::GuiBuilder(const std::string& gladefile) _simulation_widget = new SimulationWidget(Simulation::get_instance()); SimulationWidget* simulation_widget = manage(_simulation_widget); simulation_window->add(*simulation_widget); - 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); - - - // FIXME: dedicate a function to set resource policy - // which includes the following default one - ResourcePolicyManager & rpm = *ResourcePoliciesGatekeeper::get_instance().get_registered().at(0); - Simulation::get_instance().set_resource_policy(rpm.get_avail_policies().at(0)); - // end of include - + simulation_widget->show(); } diff --git a/src/gui_builder.hh b/src/gui_builder.hh index ea75051..045ce27 100644 --- a/src/gui_builder.hh +++ b/src/gui_builder.hh @@ -24,8 +24,11 @@ #include "config.h" #include "simulation_widget.hh" +#include "backend/cpu_policy.hh" +#include "backend/resource_policy.hh" #include "backend/simulation_observer.hh" +#include #include #include #include @@ -59,8 +62,8 @@ namespace sgpem bool run_simulation_adaptor(); - Simulation& _sim; - bool _break_requested; + Simulation& _sim; + bool _break_requested; Gtk::ToolButton* _toolbt_start; Gtk::ToolButton* _toolbt_pause; @@ -83,6 +86,11 @@ namespace sgpem void on_file_saveas_activate(); void on_configure_cpu_policy(); void on_configure_resource_policy(); + void on_selected_cpu_policy(CPUPolicy* pol); + void on_selected_resource_policy(ResourcePolicy* pol); + + void populate_with_cpu_policies(Gtk::Menu& menu); + void populate_with_resource_policies(Gtk::Menu& menu); private: Glib::RefPtr _refXml;