- Implemented PythonPolicyManager::collect_policies()

- Integrated PythonPolicyManager with PoliciesGatekeeper

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@620 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-06-12 13:16:10 +00:00
parent c5d78f3547
commit 65ed285807
5 changed files with 79 additions and 12 deletions

View File

@ -24,15 +24,20 @@
#include <Python.h>
#include <glibmm/ustring.h>
#include <glibmm/timer.h>
#include <glibmm/fileutils.h>
#include <glibmm/pattern.h>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <string>
#include <iostream>
#include <unistd.h>
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<PythonPolicy>(new PythonPolicy(policy_name));
collect_policies();
}
vector<Policy*>
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;
}
}
}
}

View File

@ -58,11 +58,7 @@ namespace sgpem
*/
void init();
std::vector<Policy*> get_avail_policies()
{
//FIXME write code for me
return std::vector<Policy*>();
}
std::vector<Policy*> 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<PythonPolicy> _python_policy;
~PythonPolicyManager();
void collect_policies();
PythonPolicy* _python_policy;
private:
PythonPolicyManager(const PythonPolicyManager&);

View File

@ -43,7 +43,7 @@ class TestPythonPolicyManager : public PythonPolicyManager {
public:
void test_init(const char* policy_name) {
init();
_python_policy = std::auto_ptr<PythonPolicy>(new PythonPolicy(policy_name));
_python_policy = new PythonPolicy(policy_name);
}
};

View File

@ -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;
}

View File

@ -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<Policy*> 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<Policy*> _policies;
private:
/** A pointer to the registered instance */
static PolicyManager* _registered;