diff --git a/Makefile.am b/Makefile.am index 5870425..d6cea4d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -181,6 +181,7 @@ src_backend_libbackend_la_SOURCES = \ src/backend/schedulable.cc \ src/backend/scheduler.cc \ src/backend/serialize_visitor.cc \ + src/backend/serializers_gatekeeper.cc \ src/backend/simulation.cc \ src/backend/static_process.cc \ src/backend/static_request.cc \ @@ -221,7 +222,8 @@ pkginclude_HEADERS += \ src/backend/process.hh \ src/backend/schedulable.hh \ src/backend/scheduler.hh \ - src/backend/serialize_visitor.cc \ + src/backend/serialize_visitor.hh \ + src/backend/serializers_gatekeeper.hh \ src/backend/simulation.hh \ src/backend/sub_request.hh \ src/backend/thread.hh \ diff --git a/plugins/xmlsave/Makefile.am b/plugins/xmlsave/Makefile.am index b708596..ba3b9e4 100644 --- a/plugins/xmlsave/Makefile.am +++ b/plugins/xmlsave/Makefile.am @@ -88,9 +88,15 @@ libxmlsave_la_LDFLAGS = \ # Please keep this in sorted order: libxmlsave_la_SOURCES = \ - src/plugin.cc + src/plugin.cc \ + src/xml_serializer.cc \ + src/xml_serializer_factory.cc \ + src/xml_visitor.cc -noinst_HEADERS += +noinst_HEADERS += \ + src/xml_serializer_factory.hh \ + src/xml_serializer.hh \ + src/xml_visitor.hh # ############################################################ # diff --git a/plugins/xmlsave/src/plugin.cc b/plugins/xmlsave/src/plugin.cc index f488229..e5a421e 100644 --- a/plugins/xmlsave/src/plugin.cc +++ b/plugins/xmlsave/src/plugin.cc @@ -21,27 +21,30 @@ #include "config.h" #include "plugin.hh" +#include "xml_serializer.hh" -//static XMLSerializer* _serializer = NULL; +using namespace sgpem; + +sgpem::XMLSerializer* _serializer = NULL; void sgpem__Plugin__on_init() { - // if(_serializer == NULL) - // _serializer = new sgpem::XMLSerializer(); + if(_serializer == NULL) + _serializer = new sgpem::XMLSerializer(); } void sgpem__Plugin__on_exit() { - // delete _serializer; - // _serializer = NULL; + delete _serializer; + _serializer = NULL; } const char* sgpem__Plugin__describe() { - return ""; + return "This plugin saves the simulation to an XML file"; } const char* diff --git a/plugins/xmlsave/src/xml_serializer.cc b/plugins/xmlsave/src/xml_serializer.cc index 936fe4f..bccefb5 100644 --- a/plugins/xmlsave/src/xml_serializer.cc +++ b/plugins/xmlsave/src/xml_serializer.cc @@ -22,11 +22,11 @@ #include "xml_serializer_factory.hh" #include "xml_visitor.hh" -#include "backend/environment.hh" -#include "backend/history.hh" -#include "backend/process.hh" -#include "backend/serializer_error.hh" -#include "backend/string_utils.hh" +#include "environment.hh" +#include "history.hh" +#include "process.hh" +#include "serializer_error.hh" +#include "string_utils.hh" using namespace sgpem; @@ -40,7 +40,7 @@ XMLSerializer::XMLSerializer() -void XMLSerializer::save_snapshot(const Glib::ustring& filename, const History& hist) throw(SerializeError) +void XMLSerializer::save_snapshot(const Glib::ustring& filename, const History& hist) throw(SerializerError) { /* COMPAT: Do not genrate nodes for formatting spaces */ LIBXML_TEST_VERSION @@ -65,7 +65,7 @@ void XMLSerializer::save_snapshot(const Glib::ustring& filename, const History& xmlCleanupParser(); } -void XMLSerializer::restore_snapshot(const Glib::ustring& filename, History& hist) throw(SerializeError) +void XMLSerializer::restore_snapshot(const Glib::ustring& filename, History& hist) throw(SerializerError) { // TODO - all to do!! // DEBUG - remove me when finished @@ -101,7 +101,7 @@ void XMLSerializer::restore_snapshot(const Glib::ustring& filename, History& his /* * the library has been compiled without some of the old interfaces */ - throw SerializerError("ERROR: Compilation with SAX1 must be enabled! (?)"); + #error Compilation of LIBXML with SAX1 support must be enabled #endif /* LIBXML_SAX1_ENABLED */ } @@ -222,7 +222,7 @@ void XMLSerializer::clear_history(History& hist) } -void XMLSerializer::read_doc(xmlDocPtr doc, XMLSerializerFactory& fact) throw(SerializeError) +void XMLSerializer::read_doc(xmlDocPtr doc, XMLSerializerFactory& fact) throw(SerializerError) { /* * Check the document is of the right kind diff --git a/plugins/xmlsave/src/xml_serializer.hh b/plugins/xmlsave/src/xml_serializer.hh index 715ff41..de47820 100644 --- a/plugins/xmlsave/src/xml_serializer.hh +++ b/plugins/xmlsave/src/xml_serializer.hh @@ -61,7 +61,7 @@ namespace sgpem \throws backend::SerializerError on error */ - virtual void save_snapshot(const Glib::ustring& filename, const History& hist) throw(SerializeError); + virtual void save_snapshot(const Glib::ustring& filename, const History& hist) throw(SerializerError); /** \brief Re-initialize system status from a saved XML snapshot @@ -72,7 +72,7 @@ namespace sgpem \throws backend::SerializerError */ - virtual void restore_snapshot(const Glib::ustring& filename, History& hist) throw(SerializeError); + virtual void restore_snapshot(const Glib::ustring& filename, History& hist) throw(SerializerError); /** \return Constant string "xsgp" @@ -124,7 +124,7 @@ namespace sgpem Traverse the passed (previously readed) xml document and rebuild the correct image using the XMLSerializerFactory object. */ - void read_doc(xmlDocPtr doc, XMLSerializerFactory& fact) throw(SerializeError); + void read_doc(xmlDocPtr doc, XMLSerializerFactory& fact) throw(SerializerError); /** \brief Restore all the resources from the passed xml node diff --git a/plugins/xmlsave/src/xml_serializer_factory.cc b/plugins/xmlsave/src/xml_serializer_factory.cc index 06aec14..1a0ed3a 100644 --- a/plugins/xmlsave/src/xml_serializer_factory.cc +++ b/plugins/xmlsave/src/xml_serializer_factory.cc @@ -25,7 +25,6 @@ #include "history.hh" #include "resource.hh" // #include "backend/process.hh" -#include "backend/serializer_error.hh" using namespace sgpem; @@ -50,7 +49,7 @@ History* XMLSerializerFactory::get_history() } void -XMLSerializerFactory::factory_method(const Glib::ustring& class_name, Parameters& parameters) throw(SerializeError) +XMLSerializerFactory::factory_method(const Glib::ustring& class_name, Parameters& parameters) throw(SerializerError) { if(class_name == "Resource") { @@ -241,7 +240,7 @@ XMLSerializerFactory::create_subrequest(Parameters& parameters) int old_key=0; int lasts_for=0; - int places=1; + //int places=1; Parameters::iterator pos; resource_key_t new_key = 0; @@ -260,10 +259,10 @@ XMLSerializerFactory::create_subrequest(Parameters& parameters) } // read "priority" property - pos = parameters.find(Glib::ustring("how-many")); - if (pos != parameters.end()) { - string_to_int(pos->second, places); - } + //pos = parameters.find(Glib::ustring("how-many")); + //if (pos != parameters.end()) { + // string_to_int(pos->second, places); + //} // read "priority" property pos = parameters.find(Glib::ustring("lasts-for")); @@ -271,7 +270,8 @@ XMLSerializerFactory::create_subrequest(Parameters& parameters) string_to_int(pos->second, lasts_for); } - return _hist->add_subrequest(*_last_request, new_key, (History::time_t)lasts_for, (History::size_t)places); + //FIXME places? + return _hist->add_subrequest(*_last_request, new_key, (History::time_t)lasts_for/*, (History::size_t)places*/); } // add a sub request - Request, resource_key, duration, places } diff --git a/plugins/xmlsave/src/xml_serializer_factory.hh b/plugins/xmlsave/src/xml_serializer_factory.hh index 5935eb0..b0fa3a0 100644 --- a/plugins/xmlsave/src/xml_serializer_factory.hh +++ b/plugins/xmlsave/src/xml_serializer_factory.hh @@ -24,6 +24,7 @@ #include "config.h" #include "history.hh" #include "environment.hh" +#include "serializer_error.hh" #include #include @@ -76,7 +77,7 @@ namespace sgpem \throw SerializerError If not all necessary parameters for an object creation are provided */ - void factory_method(const Glib::ustring& class_name, Parameters& parameters) throw(SerializeError); + void factory_method(const Glib::ustring& class_name, Parameters& parameters) throw(SerializerError); protected: private: typedef Environment::resource_key_t resource_key_t; diff --git a/plugins/xmlsave/src/xml_visitor.cc b/plugins/xmlsave/src/xml_visitor.cc index 9eecf82..eb55add 100644 --- a/plugins/xmlsave/src/xml_visitor.cc +++ b/plugins/xmlsave/src/xml_visitor.cc @@ -300,15 +300,16 @@ void XMLVisitor::from_subrequest(xmlNodePtr parent, const SubRequest& obj) throw { Glib::ustring strResource; - Glib::ustring strHowMany; + //Glib::ustring strHowMany; Glib::ustring strLastsFor; int_to_string(obj.get_resource_key(), strResource); - int_to_string(obj.get_places(), strHowMany); + //FIXME places? + //int_to_string(obj.get_places(), strHowMany); int_to_string(obj.get_length(), strLastsFor); xmlNodePtr subrequest_node = xmlNewChild(parent, NULL, (const xmlChar *) "subrequest", NULL); xmlNewProp(subrequest_node, (const xmlChar *) "resource", (const xmlChar *) strResource.c_str()); - xmlNewProp(subrequest_node, (const xmlChar *) "how-many", (const xmlChar *) strHowMany.c_str()); + //xmlNewProp(subrequest_node, (const xmlChar *) "how-many", (const xmlChar *) strHowMany.c_str()); xmlNewProp(subrequest_node, (const xmlChar *) "lasts-for", (const xmlChar *) strLastsFor.c_str()); } else diff --git a/src/backend/dynamic_process.cc b/src/backend/dynamic_process.cc index 98e83a3..869cbce 100644 --- a/src/backend/dynamic_process.cc +++ b/src/backend/dynamic_process.cc @@ -122,6 +122,10 @@ DynamicProcess::get_state() const if (future > 0) // running == 0 && ready == 0 && blocked == 0 && terminated == 0 return state_future; + // I'm not sure if we can get here (maybe if there are no threads?), + // but I don't like this compiler warning: 'control reaches end of non-void function' + return state_future; + // Since premature optimization is the root of all evil, and the // following code was very fast but also very wrong, the coder // will be punished by allowing her to code in C++ just after diff --git a/src/backend/serializer.cc b/src/backend/serializer.cc index 2f61cc4..82037b3 100644 --- a/src/backend/serializer.cc +++ b/src/backend/serializer.cc @@ -22,6 +22,11 @@ using namespace sgpem; +Serializer::Serializer() +{ + SerializersGatekeeper::get_instance().register_serializer(this); +} + Serializer::~Serializer() {} diff --git a/src/backend/serializer.hh b/src/backend/serializer.hh index b08faba..be97132 100644 --- a/src/backend/serializer.hh +++ b/src/backend/serializer.hh @@ -23,6 +23,7 @@ #include "config.h" #include "history.hh" +#include "serializer_error.hh" #include @@ -33,10 +34,11 @@ namespace sgpem class Serializer { public: + Serializer(); virtual ~Serializer() = 0; - virtual void save_snapshot(const Glib::ustring& filename, const History& hist) throw(SerializeError) = 0; - virtual void restore_snapshot(const Glib::ustring& filename, History& hist) throw(SerializeError) = 0; + virtual void save_snapshot(const Glib::ustring& filename, const History& hist) throw(SerializerError) = 0; + virtual void restore_snapshot(const Glib::ustring& filename, History& hist) throw(SerializerError) = 0; virtual const Glib::ustring get_filename_extension() = 0; virtual const Glib::ustring get_filename_description() = 0; protected: diff --git a/src/testsuite/test-history.cc b/src/testsuite/test-history.cc index daf925a..5bbe74a 100644 --- a/src/testsuite/test-history.cc +++ b/src/testsuite/test-history.cc @@ -419,7 +419,7 @@ main(int argc, char** argv) } test << endl; - ThreadCreationData p1_3_d = { "p1_3", &p1, 3, 4 }; + ThreadCreationData p1_3_d = { "p1_3", &p1, 3, 4, 0 }; Thread& p1_3 = h.add_thread(p1_3_d.name, *p1_3_d.parent, diff --git a/src/text_simulation.cc b/src/text_simulation.cc index f526100..1f52c6f 100644 --- a/src/text_simulation.cc +++ b/src/text_simulation.cc @@ -23,6 +23,8 @@ #include "backend/cpu_policy_manager.hh" #include "backend/policy_parameters.hh" #include "backend/history.hh" +#include "backend/serializers_gatekeeper.hh" +#include "backend/serializer.hh" #include "backend/static_process.hh" #include "backend/static_resource.hh" #include "backend/static_thread.hh" @@ -1288,6 +1290,37 @@ TextSimulation::on_remove_subrequest(const Tokens& arguments) h.remove(*r); } +void +TextSimulation::on_save(const Tokens& arguments) +{ + if(!check_arguments_num(arguments, 1)) + return; + + ustring filename = arguments[0]; + + try + { + vector serializers = + SerializersGatekeeper::get_instance().get_registered(); + + Serializer& serializer = *serializers.at(0); + + const History& history = Simulation::get_instance().get_history(); + + serializer.save_snapshot(filename, history); + } + catch(out_of_range e) + { + p_stderr(_("ERROR: No registered serializer available\n")); + } + catch(SerializerError e) + { + string msg = _("ERROR: "); + + p_stderr(msg + e.what() + "\n"); + } +} + void TextSimulation::p_stdout(const ustring& str) { @@ -1327,6 +1360,7 @@ TextSimulation::parse_command(TextSimulation& sim, const ustring& str) command_handlers["SHOW"] = &TextSimulation::on_show; command_handlers["ADD"] = &TextSimulation::on_add; command_handlers["REMOVE"] = &TextSimulation::on_remove; + command_handlers["SAVE"] = &TextSimulation::on_save; command_handlers["QUIT"] = &TextSimulation::on_quit; Tokens arguments = tokenize(str); diff --git a/src/text_simulation.hh b/src/text_simulation.hh index e6141a9..9730592 100644 --- a/src/text_simulation.hh +++ b/src/text_simulation.hh @@ -106,6 +106,7 @@ namespace sgpem void on_remove_thread(const Tokens& arguments); void on_remove_request(const Tokens& arguments); void on_remove_subrequest(const Tokens& arguments); + void on_save(const Tokens& arguments); // FIXME This is a temporary replacement for the // to-be written I/O layer