Modified cotext monitor as singleton 99/56599/1
authorSomin Kim <somin926.kim@samsung.com>
Mon, 11 Jan 2016 09:36:03 +0000 (18:36 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Mon, 11 Jan 2016 09:36:03 +0000 (18:36 +0900)
Change-Id: I80745645ccab43e619bd23854de34be1449bfad7
Signed-off-by: Somin Kim <somin926.kim@samsung.com>
src/context_trigger/context_monitor.cpp
src/context_trigger/context_monitor.h
src/context_trigger/rule.cpp
src/context_trigger/rule.h
src/context_trigger/rule_manager.cpp
src/context_trigger/rule_manager.h
src/context_trigger/trigger.cpp

index c48cc6788c3fa8e6dadabff4d4b04b266a5bfd3a..e1b79c931ba7200fd82498f4a2daf4d4f4db25f2 100644 (file)
@@ -24,6 +24,7 @@
 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()
@@ -46,11 +47,29 @@ ctx::context_monitor::~context_monitor()
 {
 }
 
-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)
index 8ef7a8bceddcc95e7a3edbf8125b1987adb4499a..19de83a8e635a6ce28fc925aab5dfa8e07a4fda1 100644 (file)
@@ -29,9 +29,9 @@ namespace ctx {
 
        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);
@@ -46,12 +46,17 @@ namespace ctx {
                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 {
index 6afcab0f854fd01bdf9628b295f2b9224261e7d4..deae787e39f1c2ec58bec50f0f7b1de032c70469 100644 (file)
@@ -28,9 +28,8 @@ ctx::trigger_rule::trigger_rule()
 {
 }
 
-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)
 {
@@ -73,7 +72,7 @@ ctx::trigger_rule::~trigger_rule()
 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;
@@ -82,7 +81,7 @@ int ctx::trigger_rule::start(void)
 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;
@@ -157,7 +156,7 @@ void ctx::trigger_rule::on_event_received(std::string name, ctx::json option, ct
                        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");
        }
 
index 62ab80a4adf288d80e290c3b5ba74549ef827000..cbbc66af3c44b21d080dd5f5e67b7feeecd74e6d 100644 (file)
@@ -23,7 +23,6 @@
 
 namespace ctx {
 
-       class context_monitor;
        class rule_manager;
 
        class trigger_rule : public context_listener_iface {
@@ -49,7 +48,6 @@ namespace ctx {
                        ctx::json action;
                        ctx::json result;
 
-                       context_monitor* ctx_monitor;
                        static rule_manager* rule_mgr;
 
                        void clear_result(void);
@@ -63,7 +61,7 @@ namespace ctx {
                        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);
index 1689064960f4dcf822608088f680cba099698230..dc1c37331a155fdc5ba6f3cebf57555bdc4d2311 100644 (file)
@@ -53,14 +53,11 @@ ctx::rule_manager::~rule_manager()
 {
 }
 
-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 '',"
@@ -95,7 +92,10 @@ void ctx::rule_manager::apply_templates(void)
        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);
@@ -110,8 +110,7 @@ void ctx::rule_manager::apply_templates(void)
        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");
@@ -447,10 +446,13 @@ int ctx::rule_manager::verify_rule(ctx::json& rule, const char* creator)
        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;
                }
@@ -461,9 +463,9 @@ int ctx::rule_manager::verify_rule(ctx::json& rule, const char* creator)
                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;
                }
@@ -557,7 +559,7 @@ int ctx::rule_manager::enable_rule(int rule_id)
        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
index 446b5055007b7bfd3ade263edceac96055b5a69a..ad2b450053b729f659ec73050c49f619857f9f42 100644 (file)
 
 #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);
@@ -46,8 +44,6 @@ namespace ctx {
                        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);
index 1caffe2ecdd7599beb5c6f66c6b5b377d7c3b38b..ded892b354dd74df070d2a64a3c51be75ca710cf 100644 (file)
@@ -18,6 +18,7 @@
 #include <context_trigger_types_internal.h>
 #include "trigger.h"
 #include "rule_manager.h"
+#include "context_monitor.h"
 
 ctx::context_trigger::context_trigger()
 {
@@ -42,9 +43,10 @@ void ctx::context_trigger::release()
        // 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)
@@ -86,10 +88,12 @@ void ctx::context_trigger::process_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);