diff --git a/plugins/pyloader/src/python_policy_manager.cc b/plugins/pyloader/src/python_policy_manager.cc index 634a858..a4dfca2 100644 --- a/plugins/pyloader/src/python_policy_manager.cc +++ b/plugins/pyloader/src/python_policy_manager.cc @@ -24,15 +24,20 @@ #include #include #include +#include +#include #include #include #include #include #include +#include #include using namespace sgpem; - +using std::vector; +using std::cout; +using std::endl; // Concatenate a string with all the policies directories @@ -63,6 +68,11 @@ PythonPolicyManager::PythonPolicyManager() } +PythonPolicyManager::~PythonPolicyManager() +{ + for_each(_policies.begin(), _policies.end(), ptr_fun(operator delete)); +} + PythonPolicyManager* const PythonPolicyManager::get_instance() { @@ -108,8 +118,49 @@ PythonPolicyManager::init() // Okay, here we go. // Black magic at work. - // FIXME : Hardcoded policy name - char* policy_name = "fcfs"; - //char* policy_name = "sjf"; - _python_policy = std::auto_ptr(new PythonPolicy(policy_name)); + collect_policies(); } + +vector +PythonPolicyManager::get_avail_policies() +{ + return _policies; +} + +void +PythonPolicyManager::collect_policies() +{ + GlobalSettings::dir_iterator dir_it = GlobalSettings::instance().policies_dir_begin(); + GlobalSettings::dir_iterator dir_end = GlobalSettings::instance().policies_dir_end(); + + for(; dir_it != dir_end; ++dir_it) + { + Glib::Dir dir(dir_it->c_str()); + + cout << "Opening directory " << *dir_it << "..." << endl; + + for(Glib::DirIterator file_it = dir.begin(); file_it != dir.end(); ++file_it) + { + cout << "\tChecking if " << *file_it << " is a Python script... " << endl; + + Glib::PatternSpec dot_py("*.py"); + + if(dot_py.match(*file_it)) + { + cout << "\t\tIt is.\n"; + + //strip extension + std::string policy_name = (*file_it).substr(0, (*file_it).size() - 3); + + PythonPolicy *pypolicy = new PythonPolicy(policy_name.c_str()); + + _policies.push_back(pypolicy); + + //FIXME remove me when get_policy is dropped + if(policy_name == "fcfs") + _python_policy = pypolicy; + } + } + } +} + diff --git a/plugins/pyloader/src/python_policy_manager.hh b/plugins/pyloader/src/python_policy_manager.hh index 1dbccbd..0655824 100644 --- a/plugins/pyloader/src/python_policy_manager.hh +++ b/plugins/pyloader/src/python_policy_manager.hh @@ -58,11 +58,7 @@ namespace sgpem */ void init(); - std::vector get_avail_policies() - { - //FIXME write code for me - return std::vector(); - } + std::vector get_avail_policies(); /** \brief Returns the singleton instance of * PythonPolicyManager. @@ -76,7 +72,11 @@ namespace sgpem protected: /** The selected and active PyhonPolicy object. */ PythonPolicyManager(); - std::auto_ptr _python_policy; + ~PythonPolicyManager(); + + void collect_policies(); + + PythonPolicy* _python_policy; private: PythonPolicyManager(const PythonPolicyManager&); diff --git a/plugins/pyloader/src/testsuite/test-python_loader.cc b/plugins/pyloader/src/testsuite/test-python_loader.cc index 3c608f2..1ee4117 100644 --- a/plugins/pyloader/src/testsuite/test-python_loader.cc +++ b/plugins/pyloader/src/testsuite/test-python_loader.cc @@ -43,7 +43,7 @@ class TestPythonPolicyManager : public PythonPolicyManager { public: void test_init(const char* policy_name) { init(); - _python_policy = std::auto_ptr(new PythonPolicy(policy_name)); + _python_policy = new PythonPolicy(policy_name); } }; diff --git a/src/backend/policy_manager.cc b/src/backend/policy_manager.cc index 30b6763..04e98f5 100644 --- a/src/backend/policy_manager.cc +++ b/src/backend/policy_manager.cc @@ -20,20 +20,27 @@ #include "policy_manager.hh" +#include "policies_gatekeeper.hh" PolicyManager* PolicyManager::_registered = NULL; PolicyManager::PolicyManager() { + //FIXME remove this when get_registered_manager is dropped _registered = this; + + PoliciesGatekeeper::get_instance().register_manager(this); } PolicyManager::~PolicyManager() { // This check is necessary: + //FIXME remove this when get_registered_manager is dropped if(_registered == this) _registered = NULL; + + PoliciesGatekeeper::get_instance().unregister_manager(this); } PolicyManager& @@ -41,3 +48,4 @@ PolicyManager::get_registered_manager() { return *_registered; } + diff --git a/src/backend/policy_manager.hh b/src/backend/policy_manager.hh index 9353cbe..3c6cce5 100644 --- a/src/backend/policy_manager.hh +++ b/src/backend/policy_manager.hh @@ -53,22 +53,30 @@ namespace sgpem Gets THE policy (the only today) used. Next versions will implement some other kind. \return A reference to the policy. + FIXME deprecated */ virtual Policy& get_policy() = 0; /** Init (or reset if yet initialized) the manager. + FIXME deprecated */ virtual void init() = 0; virtual std::vector get_avail_policies() = 0; /** \brief Get the registered manager instance + * FIXME deprecated * * \return The registered policy manager instance. */ static PolicyManager& get_registered_manager(); + protected: + virtual void collect_policies() = 0; + + std::vector _policies; + private: /** A pointer to the registered instance */ static PolicyManager* _registered;