sgpemv2/src/schedulables_tree_widget.cc

405 lines
11 KiB
C++

// src/schedulables_tree_widget.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 "schedulables_tree_widget.hh"
#include "templates/sequences.tcc"
#include "backend/history.hh"
#include "backend/simulation.hh"
#include "backend/process.hh"
#include "backend/thread.hh"
#include "backend/resource.hh"
#include "add_request_dialog.hh"
#include <cassert>
#include <iostream>
#include <sstream>
#include <gtk/gtk.h>
#include <gtkmm/entry.h>
#include <gtkmm/spinbutton.h>
using namespace sgpem;
using namespace Gtk;
using namespace Glib;
using Gnome::Glade::Xml;
SchedulablesTreeWidget::SchedulablesTreeWidget() :
_add_process_dialog_glade(Xml::create(GLADEDIR "/add-process-dialog.glade")),
_add_thread_dialog_glade(Xml::create(GLADEDIR "/add-thread-dialog.glade")),
_add_request_dialog_glade(Xml::create(GLADEDIR "/add-request-dialog.glade"))
{
_columns.add(_main_column);
_columns.add(_types_column);
_columns.add(_handles_column);
_model = TreeStore::create(_columns);
set_model(_model);
append_column("schedulables", _main_column);
//invisible
// append_column("handles", _types_column);
// append_column("handles", _handles_column);
/** DIALOGS **/
_add_process_dialog_glade->get_widget("AddProcessDialog", _add_process_dialog);
_add_thread_dialog_glade->get_widget("AddThreadDialog", _add_thread_dialog);
// NOTE This is *not* reflective programming! AddRequestDialog is the name of
// the base widget in the glade file.
_add_request_dialog_glade->get_widget_derived("AddRequestDialog", _add_request_dialog);
set_headers_visible(false);
Simulation::get_instance().get_history().attach(*this);
}
SchedulablesTreeWidget::~SchedulablesTreeWidget()
{
Simulation::get_instance().get_history().detach(*this);
}
template <typename T>
bool
SchedulablesTreeWidget::check_type(SchedulablesTreeWidget::HandleType type)
{
return false;
}
namespace sgpem
{
template <>
bool
SchedulablesTreeWidget::check_type<Process>(HandleType type)
{
return type == htype_process;
}
template <>
bool
SchedulablesTreeWidget::check_type<Thread>(HandleType type)
{
return type == htype_thread;
}
template <>
bool
SchedulablesTreeWidget::check_type<Request>(HandleType type)
{
return type == htype_request;
}
template <>
bool
SchedulablesTreeWidget::check_type<SubRequest>(HandleType type)
{
return type == htype_subrequest;
}
}
template <typename T>
T*
SchedulablesTreeWidget::get_selected()
{
TreeModel::iterator sel = get_selection()->get_selected();
if(!sel)
return NULL;
const void* p_handle = (*sel)[_handles_column];
HandleType type = (*sel)[_types_column];
if(!check_type<T>(type))
return NULL;
return reinterpret_cast<T*>(const_cast<void*>(p_handle));
}
SchedulablesTreeWidget::HandleType
SchedulablesTreeWidget::get_selection_type()
{
TreeModel::iterator sel = get_selection()->get_selected();
if(!sel)
return htype_undefined;
else
return (*sel)[_types_column];
}
bool
SchedulablesTreeWidget::on_button_press_event(GdkEventButton* event)
{
TreeView::on_button_press_event(event);
if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) )
{
RefPtr<ActionGroup> action_group = Gtk::ActionGroup::create();
action_group->add( Gtk::Action::create("AddProcess", "Add Process"),
sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_add_process) );
action_group->add( Gtk::Action::create("AddThread", "Add Thread"),
sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_add_thread) );
action_group->add( Gtk::Action::create("AddRequest", "Add Request"),
sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_add_request) );
action_group->add( Gtk::Action::create("RemoveProcess", "Remove Process"),
sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_process) );
action_group->add( Gtk::Action::create("RemoveThread", "Remove Thread"),
sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_thread) );
action_group->add( Gtk::Action::create("RemoveRequest", "Remove Request"),
sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_request) );
action_group->add( Gtk::Action::create("RemoveSubrequest", "Remove Subrequest"),
sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_subrequest) );
RefPtr<UIManager> UIManager = Gtk::UIManager::create();
UIManager->insert_action_group(action_group);
const HandleType selection_type = get_selection_type();
Glib::ustring ui_info =
"<ui>"
" <popup name='PopupMenu'>";
if(selection_type == htype_process)
ui_info +=
" <menuitem action='AddThread'/>";
else if(selection_type == htype_thread)
ui_info +=
" <menuitem action='AddRequest'/>";
ui_info +=
" <menuitem action='AddProcess'/>";
if(selection_type != htype_undefined)
ui_info +=
" <separator/>";
switch(selection_type)
{
case htype_process:
ui_info +=
" <menuitem action='RemoveProcess'/>";
break;
case htype_thread:
ui_info +=
" <menuitem action='RemoveThread'/>";
break;
case htype_request:
ui_info +=
" <menuitem action='RemoveRequest'/>";
break;
case htype_subrequest:
ui_info +=
" <menuitem action='RemoveSubrequest'/>";
}
ui_info +=
" </popup>"
"</ui>";
UIManager->add_ui_from_string(ui_info);
Gtk::Menu* menu = dynamic_cast<Gtk::Menu*>(UIManager->get_widget("/PopupMenu"));
menu->popup(event->button, event->time);
return true; //It has been handled.
}
else
return false;
}
void
SchedulablesTreeWidget::update(const History& history)
{
const Environment::Processes& processes =
Simulation::get_instance().get_history().get_last_environment().get_processes();
_model->clear();
// TODO use the new sequence iterator, it's been made for
// something!!!
for(unsigned int pi = 0; pi < processes.size(); ++pi)
{
Process& p = *processes[pi];
TreeModel::Row prow = *(_model->append());
prow[_main_column] = p.get_name();
prow[_types_column] = htype_process;
prow[_handles_column] = &p;
std::vector<Thread*> threads = p.get_threads();
for(unsigned int ti = 0; ti < threads.size(); ++ti)
{
Thread& t = *threads[ti];
TreeModel::Row trow = *(_model->append(prow.children()));
trow[_main_column] = t.get_name();
trow[_types_column] = htype_thread;
trow[_handles_column] = &t;
std::vector<Request*> requests = t.get_requests();
for(unsigned int ri = 0; ri < requests.size(); ++ri)
{
Request& r = *requests[ri];
TreeModel::Row rrow = *(_model->append(trow.children()));
std::ostringstream oss;
oss << "request " << ri + 1;
rrow[_main_column] = oss.str();
rrow[_types_column] = htype_request;
rrow[_handles_column] = &r;
std::vector<SubRequest*> subrequests = r.get_subrequests();
for(unsigned int sri = 0; sri < subrequests.size(); ++sri)
{
SubRequest& sr = *subrequests[sri];
TreeModel::Row srrow = *(_model->append(rrow.children()));
// we are sure the key is valid, or no?
Resource& res = *(history.get_last_environment().get_resources().find(sr.get_resource_key())->second);
srrow[_main_column] = res.get_name();
srrow[_types_column] = htype_subrequest;
srrow[_handles_column] = &sr;
}
}
}
}
}
void
SchedulablesTreeWidget::_on_add_process()
{
if(_add_process_dialog->run() == RESPONSE_OK)
{
Entry* name_entry;
SpinButton* arrival_time_spin;
SpinButton* base_priority_spin;
_add_process_dialog_glade->get_widget("Name.Entry", name_entry);
_add_process_dialog_glade->get_widget("ArrivalTime.Spin", arrival_time_spin);
_add_process_dialog_glade->get_widget("BasePriority.Spin", base_priority_spin);
Simulation::get_instance().get_history().add_process(name_entry->get_text(),
arrival_time_spin->get_value_as_int(),
base_priority_spin->get_value_as_int());
}
_add_process_dialog->hide();
}
void
SchedulablesTreeWidget::_on_add_thread()
{
Process* p = get_selected<Process>();
if(p == NULL)
return;
if(_add_thread_dialog->run() == RESPONSE_OK)
{
Entry* name_entry;
SpinButton* cpu_time_spin;
SpinButton* arrival_time_spin;
SpinButton* base_priority_spin;
_add_thread_dialog_glade->get_widget("Name.Entry", name_entry);
_add_thread_dialog_glade->get_widget("CpuTime.Spin", cpu_time_spin);
_add_thread_dialog_glade->get_widget("ArrivalTime.Spin", arrival_time_spin);
_add_thread_dialog_glade->get_widget("BasePriority.Spin", base_priority_spin);
Simulation::get_instance().get_history().add_thread(name_entry->get_text(),
*p,
cpu_time_spin->get_value_as_int(),
arrival_time_spin->get_value_as_int(),
base_priority_spin->get_value_as_int());
}
_add_thread_dialog->hide();
}
void
SchedulablesTreeWidget::_on_add_request()
{
Thread* t = get_selected<Thread>();
if(t == NULL)
return;
if(_add_request_dialog->run() == RESPONSE_OK)
_add_request_dialog->construct_request(*t);
_add_request_dialog->hide();
}
void
SchedulablesTreeWidget::_on_remove_process()
{
Process* p = get_selected<Process>();
assert(p != NULL);
Simulation::get_instance().get_history().remove(*p);
}
void
SchedulablesTreeWidget::_on_remove_thread()
{
Thread* t = get_selected<Thread>();
assert(t != NULL);
Simulation::get_instance().get_history().remove(*t);
}
void
SchedulablesTreeWidget::_on_remove_request()
{
Request* r = get_selected<Request>();
assert(r != NULL);
Simulation::get_instance().get_history().remove(*r);
}
void
SchedulablesTreeWidget::_on_remove_subrequest()
{
SubRequest* sr = get_selected<SubRequest>();
assert(sr != NULL);
Request& owner = sr->get_request();
Simulation::get_instance().get_history().remove(*sr);
// empty requests are COMPLETELY useless with the current GUI
if(owner.get_subrequests().empty())
Simulation::get_instance().get_history().remove(owner);
}