- updated PoliciesGatekeeper to reflect change in design, it is still not usable at this time
git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@616 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
1e75fe91f1
commit
c5d78f3547
|
@ -143,6 +143,7 @@ src_backend_libbackend_la_SOURCES = \
|
||||||
src/backend/global_settings.cc \
|
src/backend/global_settings.cc \
|
||||||
src/backend/history.cc \
|
src/backend/history.cc \
|
||||||
src/backend/observed_subject.cc \
|
src/backend/observed_subject.cc \
|
||||||
|
src/backend/policies_gatekeeper.cc \
|
||||||
src/backend/policy.cc \
|
src/backend/policy.cc \
|
||||||
src/backend/policy_manager.cc \
|
src/backend/policy_manager.cc \
|
||||||
src/backend/policy_parameters.cc \
|
src/backend/policy_parameters.cc \
|
||||||
|
@ -161,6 +162,7 @@ pkginclude_HEADERS = \
|
||||||
src/backend/history.hh \
|
src/backend/history.hh \
|
||||||
src/backend/observed_subject.hh \
|
src/backend/observed_subject.hh \
|
||||||
src/backend/plugin.hh \
|
src/backend/plugin.hh \
|
||||||
|
src/backend/policies_gatekeeper.hh \
|
||||||
src/backend/policy.hh \
|
src/backend/policy.hh \
|
||||||
src/backend/policy_manager.hh \
|
src/backend/policy_manager.hh \
|
||||||
src/backend/policy_parameters.hh \
|
src/backend/policy_parameters.hh \
|
||||||
|
|
|
@ -76,6 +76,16 @@ namespace sgpem
|
||||||
|
|
||||||
policy_sorts_type wants() const throw(UserInterruptException);
|
policy_sorts_type wants() const throw(UserInterruptException);
|
||||||
|
|
||||||
|
void activate()
|
||||||
|
{
|
||||||
|
//FIXME write code for me
|
||||||
|
}
|
||||||
|
|
||||||
|
void deactivate()
|
||||||
|
{
|
||||||
|
//FIXME write code for me
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PythonPolicy(const PythonPolicy&);
|
PythonPolicy(const PythonPolicy&);
|
||||||
PythonPolicy& operator=(const PythonPolicy&);
|
PythonPolicy& operator=(const PythonPolicy&);
|
||||||
|
|
|
@ -58,6 +58,12 @@ namespace sgpem
|
||||||
*/
|
*/
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
std::vector<Policy*> get_avail_policies()
|
||||||
|
{
|
||||||
|
//FIXME write code for me
|
||||||
|
return std::vector<Policy*>();
|
||||||
|
}
|
||||||
|
|
||||||
/** \brief Returns the singleton instance of
|
/** \brief Returns the singleton instance of
|
||||||
* PythonPolicyManager.
|
* PythonPolicyManager.
|
||||||
*
|
*
|
||||||
|
|
|
@ -19,12 +19,21 @@
|
||||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
#include "policies_gatekeeper.hh"
|
#include "policies_gatekeeper.hh"
|
||||||
|
#include "policy_manager.hh"
|
||||||
|
#include "policy.hh"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
using std::map;
|
||||||
|
using std::find;
|
||||||
|
using std::runtime_error;
|
||||||
using namespace sgpem;
|
using namespace sgpem;
|
||||||
|
|
||||||
|
typedef vector<PolicyManager*>::iterator ManagerIterator;
|
||||||
|
typedef map<History*, Policy*>::iterator ActiveIterator;
|
||||||
|
|
||||||
PoliciesGatekeeper* PoliciesGatekeeper::_instance = NULL;
|
PoliciesGatekeeper* PoliciesGatekeeper::_instance = NULL;
|
||||||
|
|
||||||
PoliciesGatekeeper&
|
PoliciesGatekeeper&
|
||||||
|
@ -44,11 +53,9 @@ PoliciesGatekeeper::get_registered() const
|
||||||
void
|
void
|
||||||
PoliciesGatekeeper::register_manager(PolicyManager* manager)
|
PoliciesGatekeeper::register_manager(PolicyManager* manager)
|
||||||
{
|
{
|
||||||
using std::find;
|
|
||||||
|
|
||||||
assert(manager != NULL);
|
assert(manager != NULL);
|
||||||
|
|
||||||
end = _registered.end();
|
ManagerIterator end = _registered.end();
|
||||||
|
|
||||||
if(find(_registered.begin(), end, manager) != end)
|
if(find(_registered.begin(), end, manager) != end)
|
||||||
_registered.push_back(manager);
|
_registered.push_back(manager);
|
||||||
|
@ -57,31 +64,78 @@ PoliciesGatekeeper::register_manager(PolicyManager* manager)
|
||||||
void
|
void
|
||||||
PoliciesGatekeeper::unregister_manager(PolicyManager* manager)
|
PoliciesGatekeeper::unregister_manager(PolicyManager* manager)
|
||||||
{
|
{
|
||||||
using std::find;
|
|
||||||
|
|
||||||
assert(manager != NULL);
|
assert(manager != NULL);
|
||||||
|
|
||||||
end = _registered.end();
|
ManagerIterator end = _registered.end();
|
||||||
pos = find(_registered.begin(), end, manager);
|
ManagerIterator pos = find(_registered.begin(), end, manager);
|
||||||
|
|
||||||
if(pos != end)
|
if(pos != end)
|
||||||
|
{
|
||||||
|
deactivate_policies(*pos);
|
||||||
_registered.erase(pos);
|
_registered.erase(pos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Policy*
|
Policy*
|
||||||
PoliciesGatekeeper::get_current_policy()
|
PoliciesGatekeeper::get_current_policy(History* history) throw(runtime_error)
|
||||||
{
|
{
|
||||||
return _cur_policy;
|
assert(history != NULL);
|
||||||
|
|
||||||
|
ActiveIterator policy = _active_policies.find(history);
|
||||||
|
|
||||||
|
if(policy == _active_policies.end())
|
||||||
|
throw runtime_error("No active policy associated with this "
|
||||||
|
"history is available.");
|
||||||
|
|
||||||
|
return policy->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PoliciesGatekeeper::activate_policy(Policy* policy)
|
PoliciesGatekeeper::activate_policy(History *history, Policy* policy)
|
||||||
{
|
{
|
||||||
_cur_policy = policy;
|
assert(history != NULL && policy != NULL);
|
||||||
|
|
||||||
|
ActiveIterator end = _active_policies.end();
|
||||||
|
ActiveIterator pos = _active_policies.find(history);
|
||||||
|
|
||||||
|
if(pos != end && pos->second != policy)
|
||||||
|
_active_policies[history]->deactivate();
|
||||||
|
|
||||||
|
if(pos == end || pos->second != policy)
|
||||||
|
{
|
||||||
|
_active_policies[history] = policy;
|
||||||
|
_active_policies[history]->activate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PoliciesGatekeeper::PoliciesGatekeeper() :
|
PoliciesGatekeeper::PoliciesGatekeeper()
|
||||||
_cur_policy(NULL)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
PoliciesGatekeeper::deactivate_policies(PolicyManager* manager)
|
||||||
|
{
|
||||||
|
typedef vector<Policy*>::iterator PolicyIterator;
|
||||||
|
|
||||||
|
vector<Policy*> avail_policies = manager->get_avail_policies();
|
||||||
|
|
||||||
|
PolicyIterator avail_it = avail_policies.begin();
|
||||||
|
PolicyIterator avail_end = avail_policies.end();
|
||||||
|
|
||||||
|
for(; avail_it != avail_end; ++avail_it)
|
||||||
|
{
|
||||||
|
// TODO isn't there a way to write more compact code by using
|
||||||
|
// library utilities?
|
||||||
|
ActiveIterator act_it = _active_policies.begin();
|
||||||
|
|
||||||
|
for(; act_it != _active_policies.end(); ++act_it)
|
||||||
|
{
|
||||||
|
if(act_it->second == *avail_it)
|
||||||
|
{
|
||||||
|
act_it->second->deactivate();
|
||||||
|
_active_policies.erase(act_it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,11 +25,14 @@ namespace sgpem
|
||||||
{
|
{
|
||||||
class PolicyManager;
|
class PolicyManager;
|
||||||
class Policy;
|
class Policy;
|
||||||
|
class History;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace sgpem
|
namespace sgpem
|
||||||
{
|
{
|
||||||
|
@ -51,25 +54,23 @@ namespace sgpem
|
||||||
|
|
||||||
std::vector<PolicyManager*> get_registered() const;
|
std::vector<PolicyManager*> get_registered() const;
|
||||||
|
|
||||||
/** FIXME what to do when the manager was already registered? should we throw an exception?
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void register_manager(PolicyManager* manager);
|
void register_manager(PolicyManager* manager);
|
||||||
|
|
||||||
/** FIXME what to do when the manager wasn't already registered? should we throw an exception?
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void unregister_manager(PolicyManager* manager);
|
void unregister_manager(PolicyManager* manager);
|
||||||
|
|
||||||
Policy* get_current_policy();
|
Policy* get_current_policy(History* history) throw(std::runtime_error);
|
||||||
|
|
||||||
void activate_policy(Policy* policy);
|
void activate_policy(History* history, Policy* policy);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PoliciesGatekeeper(); //private constructor.
|
PoliciesGatekeeper(); //private constructor.
|
||||||
|
|
||||||
|
// Deactivates active policies managed by the specified manager.
|
||||||
|
void deactivate_policies(PolicyManager* manager);
|
||||||
|
|
||||||
static PoliciesGatekeeper* _instance;
|
static PoliciesGatekeeper* _instance;
|
||||||
std::vector<PolicyManager*> _registered;
|
std::vector<PolicyManager*> _registered;
|
||||||
Policy* _cur_policy;
|
std::map<History*, Policy*> _active_policies;
|
||||||
};
|
};
|
||||||
|
|
||||||
}//~ namespace sgpem
|
}//~ namespace sgpem
|
||||||
|
|
|
@ -112,6 +112,10 @@ namespace sgpem
|
||||||
composing the queue passed to the sort_queue method.
|
composing the queue passed to the sort_queue method.
|
||||||
*/
|
*/
|
||||||
virtual policy_sorts_type wants() const throw(UserInterruptException) = 0;
|
virtual policy_sorts_type wants() const throw(UserInterruptException) = 0;
|
||||||
|
|
||||||
|
virtual void activate() = 0;
|
||||||
|
|
||||||
|
virtual void deactivate() = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Gets the parameters related with this policy.
|
Gets the parameters related with this policy.
|
||||||
|
@ -121,7 +125,7 @@ namespace sgpem
|
||||||
\return The policy parameters.
|
\return The policy parameters.
|
||||||
*/
|
*/
|
||||||
PolicyParameters& get_parameters();
|
PolicyParameters& get_parameters();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
PolicyParameters _parameters;
|
PolicyParameters _parameters;
|
||||||
int _id;
|
int _id;
|
||||||
|
|
|
@ -61,6 +61,8 @@ namespace sgpem
|
||||||
*/
|
*/
|
||||||
virtual void init() = 0;
|
virtual void init() = 0;
|
||||||
|
|
||||||
|
virtual std::vector<Policy*> get_avail_policies() = 0;
|
||||||
|
|
||||||
/** \brief Get the registered manager instance
|
/** \brief Get the registered manager instance
|
||||||
*
|
*
|
||||||
* \return The registered policy manager instance.
|
* \return The registered policy manager instance.
|
||||||
|
|
Loading…
Reference in New Issue