static int last_rid;
static int last_err;
+ctx::context_monitor *ctx::context_monitor::_instance = NULL;
ctx::context_manager_impl *ctx::context_monitor::_context_mgr = NULL;
static int generate_req_id()
{
}
-bool ctx::context_monitor::init(ctx::context_manager_impl* ctx_mgr)
+void ctx::context_monitor::set_context_manager(ctx::context_manager_impl* ctx_mgr)
{
_context_mgr = ctx_mgr;
+}
- return true;
+ctx::context_monitor* ctx::context_monitor::get_instance()
+{
+ IF_FAIL_RETURN_TAG(_context_mgr, NULL, _E, "Context manager is needed");
+
+ IF_FAIL_RETURN(!_instance, _instance);
+
+ _instance = new(std::nothrow) context_monitor();
+ IF_FAIL_RETURN_TAG(_instance, NULL, _E, "Memory alllocation failed");
+
+ return _instance;
+}
+
+void ctx::context_monitor::destroy()
+{
+ if (_instance) {
+ delete _instance;
+ _instance = NULL;
+ }
}
int ctx::context_monitor::subscribe(int rule_id, std::string subject, ctx::json option, context_listener_iface* listener)
class context_monitor {
public:
- context_monitor();
- ~context_monitor();
- bool init(ctx::context_manager_impl* ctx_mgr);
+ static context_monitor* get_instance();
+ static void set_context_manager(ctx::context_manager_impl* ctx_mgr);
+ static void destroy();
int subscribe(int rule_id, std::string subject, ctx::json option, context_listener_iface* listener);
int unsubscribe(int rule_id, std::string subject, ctx::json option, context_listener_iface* listener);
bool get_fact_definition(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options);
private:
+ context_monitor();
+ context_monitor(const context_monitor& other);
+ ~context_monitor();
+
+ static context_monitor *_instance;
+ static context_manager_impl *_context_mgr;
+
int _subscribe(const char* subject, ctx::json* option, context_listener_iface* listener);
void _unsubscribe(const char *subject, int subscription_id);
int _read(const char *subject, ctx::json *option, context_listener_iface* listener);
- static context_manager_impl *_context_mgr;
-
typedef std::list<context_listener_iface*> listener_list_t;
struct subscr_info_s {
{
}
-ctx::trigger_rule::trigger_rule(int i, ctx::json& d, const char* cr, context_monitor* cm, rule_manager* rm)
+ctx::trigger_rule::trigger_rule(int i, ctx::json& d, const char* cr, rule_manager* rm)
: result(EMPTY_JSON_OBJECT)
- , ctx_monitor(cm)
, id(i)
, creator(cr)
{
int ctx::trigger_rule::start(void)
{
// Subscribe event
- int error = ctx_monitor->subscribe(id, event->name, event->option, this);
+ int error = ctx::context_monitor::get_instance()->subscribe(id, event->name, event->option, this);
IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Failed to start rule%d", id);
return error;
int ctx::trigger_rule::stop(void)
{
// Unsubscribe event
- int error = ctx_monitor->unsubscribe(id, event->name, event->option, this);
+ int error = ctx::context_monitor::get_instance()->unsubscribe(id, event->name, event->option, this);
IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Failed to stop rule%d", id);
return error;
return;
}
- int error = ctx_monitor->read((*it)->name.c_str(), cond_option, this);
+ int error = ctx::context_monitor::get_instance()->read((*it)->name.c_str(), cond_option, this);
IF_FAIL_VOID_TAG(error == ERR_NONE, _E, "Failed to read condition");
}
namespace ctx {
- class context_monitor;
class rule_manager;
class trigger_rule : public context_listener_iface {
ctx::json action;
ctx::json result;
- context_monitor* ctx_monitor;
static rule_manager* rule_mgr;
void clear_result(void);
std::string creator;
trigger_rule();
- trigger_rule(int i, ctx::json& d, const char* c, context_monitor* cm, rule_manager* rm);
+ trigger_rule(int i, ctx::json& d, const char* c, rule_manager* rm);
~trigger_rule();
int start(void);
{
}
-bool ctx::rule_manager::init(ctx::context_manager_impl* ctx_mgr)
+bool ctx::rule_manager::init()
{
bool ret;
int error;
- ret = ctx_monitor.init(ctx_mgr);
- IF_FAIL_RETURN_TAG(ret, false, _E, "Context monitor initialization failed");
-
// Create tables into db (rule, event, condition, action, template)
std::string q1 = std::string("enabled INTEGER DEFAULT 0 NOT NULL, creator TEXT DEFAULT '' NOT NULL,")
+ "creator_app_id TEXT DEFAULT '' NOT NULL, description TEXT DEFAULT '',"
std::string q_update;
std::string q_insert = "INSERT OR IGNORE INTO context_trigger_template (name, operation, attributes, options) VALUES";
- while (ctx_monitor.get_fact_definition(subject, operation, attributes, options)) {
+ ctx::context_monitor* ctx_monitor = ctx::context_monitor::get_instance();
+ IF_FAIL_VOID_TAG(ctx_monitor, _E, "Memory allocation failed");
+
+ while (ctx_monitor->get_fact_definition(subject, operation, attributes, options)) {
_D("Subject: %s, Ops: %d", subject.c_str(), operation);
_J("Attr", attributes);
_J("Opt", options);
q_insert += ";";
bool ret = db_manager::execute(5, q_update.c_str(), NULL);
- if (!ret)
- _E("Update item definition failed");
+ IF_FAIL_VOID_TAG(ret, _E, "Update item definition failed");
ret = db_manager::execute(6, q_insert.c_str(), NULL);
IF_FAIL_VOID_TAG(ret, _E, "Insert item definition failed");
std::string e_name;
rule.get(CT_RULE_DETAILS "." CT_RULE_EVENT, CT_RULE_EVENT_ITEM, &e_name);
- IF_FAIL_RETURN_TAG(ctx_monitor.is_supported(e_name), ERR_NOT_SUPPORTED, _I, "Event(%s) is not supported", e_name.c_str());
+ ctx::context_monitor* ctx_monitor = ctx::context_monitor::get_instance();
+ IF_FAIL_RETURN_TAG(ctx_monitor, ERR_OUT_OF_MEMORY, _E, "Memory allocation failed");
+
+ IF_FAIL_RETURN_TAG(ctx_monitor->is_supported(e_name), ERR_NOT_SUPPORTED, _I, "Event(%s) is not supported", e_name.c_str());
if (creator) {
- if (!ctx_monitor.is_allowed(creator, e_name.c_str())) {
+ if (!ctx_monitor->is_allowed(creator, e_name.c_str())) {
_W("Permission denied for '%s'", e_name.c_str());
return ERR_PERMISSION_DENIED;
}
std::string c_name;
it.get(NULL, CT_RULE_CONDITION_ITEM, &c_name);
- IF_FAIL_RETURN_TAG(ctx_monitor.is_supported(c_name), ERR_NOT_SUPPORTED, _I, "Condition(%s) is not supported", c_name.c_str());
+ IF_FAIL_RETURN_TAG(ctx_monitor->is_supported(c_name), ERR_NOT_SUPPORTED, _I, "Condition(%s) is not supported", c_name.c_str());
- if (!ctx_monitor.is_allowed(creator, c_name.c_str())) {
+ if (!ctx_monitor->is_allowed(creator, c_name.c_str())) {
_W("Permission denied for '%s'", c_name.c_str());
return ERR_PERMISSION_DENIED;
}
rule_record[0].get(NULL, "creator_app_id", &creator_app_id);
// Create a rule instance
- rule = new(std::nothrow) trigger_rule(rule_id, jrule, creator_app_id.c_str(), &ctx_monitor, this);
+ rule = new(std::nothrow) trigger_rule(rule_id, jrule, creator_app_id.c_str(), this);
IF_FAIL_RETURN_TAG(rule, ERR_OUT_OF_MEMORY, _E, "Failed to create rule instance");
// Start the rule
#include <set>
#include <map>
-#include "context_monitor.h"
namespace ctx {
class json;
class context_trigger;
class context_manager_impl;
- class context_monitor;
class trigger_rule;
class rule_manager {
public:
rule_manager();
~rule_manager();
- bool init(ctx::context_manager_impl* ctx_mgr);
+ bool init();
int add_rule(std::string creator, const char* app_id, ctx::json rule, ctx::json* rule_id);
int remove_rule(int rule_id);
int enable_rule(int rule_id);
static bool is_uninstalled_package(std::string app_id);
private:
- ctx::context_monitor ctx_monitor;
-
void apply_templates(void);
bool reenable_rule(void);
int verify_rule(ctx::json& rule, const char* app_id);
#include <context_trigger_types_internal.h>
#include "trigger.h"
#include "rule_manager.h"
+#include "context_monitor.h"
ctx::context_trigger::context_trigger()
{
// Release the occupied resources.
// This function is called from the main thread during the service termination process.
_D("Context Trigger Release");
-
delete rule_mgr;
rule_mgr = NULL;
+
+ ctx::context_monitor::destroy();
}
bool ctx::context_trigger::assign_request(ctx::request_info* request)
void ctx::context_trigger::process_initialize(ctx::context_manager_impl* mgr)
{
+ ctx::context_monitor::set_context_manager(mgr);
+
rule_mgr = new(std::nothrow) rule_manager();
IF_FAIL_VOID_TAG(rule_mgr, _E, "Memory allocation failed");
- bool ret = rule_mgr->init(mgr);
+ bool ret = rule_mgr->init();
if (!ret) {
_E("Context trigger initialization failed.");
raise(SIGTERM);