From: Mu-Woong Date: Tue, 21 Jul 2015 06:33:05 +0000 (+0900) Subject: Integrate changes from Tizen 2.4 (Version 0.5.3) X-Git-Tag: submit/tizen_tv/20150721.114308^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cb0ed55359f3a3f86cf41d6c5590045d96f6d876;p=platform%2Fcore%2Fcontext%2Fcontext-service.git Integrate changes from Tizen 2.4 (Version 0.5.3) - Remove Vasum zone control code - Assign null to clips_h while initialization - Removed unnecessary logs and modified invalid log tag - Modified to construct clips handler instanece only when at least one enabled rule exists Change-Id: I6bf048441161aee5ee50d5d55f43bf4db9fe666e Signed-off-by: Mu-Woong --- diff --git a/packaging/context-service.spec b/packaging/context-service.spec index e054daa..ed4f119 100644 --- a/packaging/context-service.spec +++ b/packaging/context-service.spec @@ -1,6 +1,6 @@ Name: context-service Summary: Context-Service -Version: 0.5.0 +Version: 0.5.3 Release: 1 Group: System/Service License: Apache-2.0 diff --git a/packaging/context-service.xml b/packaging/context-service.xml index 61c5d87..dd71b93 100644 --- a/packaging/context-service.xml +++ b/packaging/context-service.xml @@ -2,6 +2,7 @@ @@ -17,6 +18,5 @@ http://tizen.org/privilege/location - http://tizen.org/privilege/packagemanager.info diff --git a/src/client_request.cpp b/src/client_request.cpp index b4c2753..f7205ca 100644 --- a/src/client_request.cpp +++ b/src/client_request.cpp @@ -19,7 +19,6 @@ #include #include #include -#include "zone_util_impl.h" #include "dbus_server_impl.h" #include "client_request.h" @@ -35,11 +34,10 @@ ctx::client_request::~client_request() g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT)); } -bool ctx::client_request::set_peer_creds(const char *smack_label, const char *zone) +bool ctx::client_request::set_peer_creds(const char *smack_label) { - IF_FAIL_RETURN_TAG(smack_label && zone, false, _E, "Invalid parameter"); + IF_FAIL_RETURN_TAG(smack_label, false, _E, "Invalid parameter"); client_app_id = smack_label; - _zone_name = zone; return true; } diff --git a/src/client_request.h b/src/client_request.h index bc7e8f3..ae16282 100644 --- a/src/client_request.h +++ b/src/client_request.h @@ -27,7 +27,7 @@ namespace ctx { client_request(int type, const char* client, int req_id, const char* subj, const char* desc, GDBusMethodInvocation *inv); ~client_request(); - bool set_peer_creds(const char *smack_label, const char *zone); + bool set_peer_creds(const char *smack_label); const char* get_app_id(); bool reply(int error); diff --git a/src/context_mgr_impl.cpp b/src/context_mgr_impl.cpp index 0837a82..18cec7b 100644 --- a/src/context_mgr_impl.cpp +++ b/src/context_mgr_impl.cpp @@ -22,7 +22,6 @@ #include #include "server.h" #include "context_mgr_impl.h" -#include "zone_util_impl.h" #include "access_control/privilege.h" /* Analyzer Headers */ @@ -139,17 +138,17 @@ void ctx::context_manager_impl::assign_request(ctx::request_info* request) } } -bool ctx::context_manager_impl::is_supported(const char* subject, const char* zone) +bool ctx::context_manager_impl::is_supported(const char* subject) { subject_provider_map_t::iterator it = subject_provider_map.find(subject); IF_FAIL_RETURN(it != subject_provider_map.end(), false); - return it->second->is_supported(subject, zone); + return it->second->is_supported(subject); } void ctx::context_manager_impl::is_supported(request_info *request) { - if (is_supported(request->get_subject(), request->get_zone_name())) + if (is_supported(request->get_subject())) request->reply(ERR_NONE); else request->reply(ERR_NOT_SUPPORTED); @@ -158,9 +157,9 @@ void ctx::context_manager_impl::is_supported(request_info *request) } ctx::context_manager_impl::request_list_t::iterator -ctx::context_manager_impl::find_request(request_list_t& r_list, std::string subject, json& option, const char* zone) +ctx::context_manager_impl::find_request(request_list_t& r_list, std::string subject, json& option) { - return find_request(r_list.begin(), r_list.end(), subject, option, zone); + return find_request(r_list.begin(), r_list.end(), subject, option); } ctx::context_manager_impl::request_list_t::iterator @@ -176,12 +175,12 @@ ctx::context_manager_impl::find_request(request_list_t& r_list, std::string clie } ctx::context_manager_impl::request_list_t::iterator -ctx::context_manager_impl::find_request(request_list_t::iterator begin, request_list_t::iterator end, std::string subject, json& option, const char* zone) +ctx::context_manager_impl::find_request(request_list_t::iterator begin, request_list_t::iterator end, std::string subject, json& option) { //TODO: Do we need to consider the case that the inparam option is a subset of the request description? request_list_t::iterator it; for (it = begin; it != end; ++it) { - if (subject == (*it)->get_subject() && option == (*it)->get_description() && STR_EQ(zone, (*it)->get_zone_name())) { + if (subject == (*it)->get_subject() && option == (*it)->get_description()) { break; } } @@ -214,8 +213,6 @@ bool ctx::context_manager_impl::check_permission(ctx::request_info* request) IF_FAIL_RETURN_TAG(!STR_EQ(app_id, TRIGGER_CLIENT_NAME), true, _D, "Skipping permission check for Trigger"); - scope_zone_joiner sz(request->get_zone_name()); - bool allowed = ctx::privilege_manager::is_allowed(app_id, request->get_subject()); if (!allowed) { _W("Permission denied"); @@ -229,14 +226,14 @@ bool ctx::context_manager_impl::check_permission(ctx::request_info* request) void ctx::context_manager_impl::subscribe(ctx::request_info *request) { - _I(CYAN("[%s] '%s' subscribes '%s' (RID-%d)"), request->get_zone_name(), request->get_client(), request->get_subject(), request->get_id()); + _I(CYAN("'%s' subscribes '%s' (RID-%d)"), request->get_client(), request->get_subject(), request->get_id()); IF_FAIL_VOID(check_permission(request)); context_provider_iface *provider = get_provider(request); IF_FAIL_VOID(provider); ctx::json request_result; - int error = provider->subscribe(request->get_subject(), request->get_description().str(), &request_result, request->get_zone_name()); + int error = provider->subscribe(request->get_subject(), request->get_description().str(), &request_result); _D("Analyzer returned %d", error); @@ -250,7 +247,7 @@ void ctx::context_manager_impl::subscribe(ctx::request_info *request) void ctx::context_manager_impl::unsubscribe(ctx::request_info *request) { - _I(CYAN("[%s] '%s' unsubscribes RID-%d"), request->get_zone_name(), request->get_client(), request->get_id()); + _I(CYAN("'%s' unsubscribes RID-%d"), request->get_client(), request->get_id()); // Search the subscribe request to be removed request_list_t::iterator target = find_request(subscribe_request_list, request->get_client(), request->get_id()); @@ -267,7 +264,7 @@ void ctx::context_manager_impl::unsubscribe(ctx::request_info *request) subscribe_request_list.erase(target); // Check if there exist the same requests - if (find_request(subscribe_request_list, req_found->get_subject(), req_found->get_description(), req_found->get_zone_name()) != subscribe_request_list.end()) { + if (find_request(subscribe_request_list, req_found->get_subject(), req_found->get_description()) != subscribe_request_list.end()) { // Do not stop detecting the subject _D("A same request from '%s' exists", req_found->get_client()); request->reply(ERR_NONE); @@ -286,7 +283,7 @@ void ctx::context_manager_impl::unsubscribe(ctx::request_info *request) } // Stop detecting the subject - int error = ca->second->unsubscribe(req_found->get_subject(), req_found->get_description(), req_found->get_zone_name()); + int error = ca->second->unsubscribe(req_found->get_subject(), req_found->get_description()); request->reply(error); delete request; delete req_found; @@ -294,14 +291,14 @@ void ctx::context_manager_impl::unsubscribe(ctx::request_info *request) void ctx::context_manager_impl::read(ctx::request_info *request) { - _I(CYAN("[%s] '%s' reads '%s' (RID-%d)"), request->get_zone_name(), request->get_client(), request->get_subject(), request->get_id()); + _I(CYAN("'%s' reads '%s' (RID-%d)"), request->get_client(), request->get_subject(), request->get_id()); IF_FAIL_VOID(check_permission(request)); context_provider_iface *provider = get_provider(request); IF_FAIL_VOID(provider); ctx::json request_result; - int error = provider->read(request->get_subject(), request->get_description().str(), &request_result, request->get_zone_name()); + int error = provider->read(request->get_subject(), request->get_description().str(), &request_result); _D("Analyzer returned %d", error); @@ -315,14 +312,14 @@ void ctx::context_manager_impl::read(ctx::request_info *request) void ctx::context_manager_impl::write(ctx::request_info *request) { - _I(CYAN("[%s] '%s' writes '%s' (RID-%d)"), request->get_zone_name(), request->get_client(), request->get_subject(), request->get_id()); + _I(CYAN("'%s' writes '%s' (RID-%d)"), request->get_client(), request->get_subject(), request->get_id()); IF_FAIL_VOID(check_permission(request)); context_provider_iface *provider = get_provider(request); IF_FAIL_VOID(provider); ctx::json request_result; - int error = provider->write(request->get_subject(), request->get_description(), &request_result, request->get_zone_name()); + int error = provider->write(request->get_subject(), request->get_description(), &request_result); _D("Analyzer returned %d", error); @@ -330,7 +327,7 @@ void ctx::context_manager_impl::write(ctx::request_info *request) delete request; } -bool ctx::context_manager_impl::_publish(const char* subject, ctx::json option, int error, ctx::json data_updated, const char* zone) +bool ctx::context_manager_impl::_publish(const char* subject, ctx::json option, int error, ctx::json data_updated) { IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter"); @@ -338,29 +335,28 @@ bool ctx::context_manager_impl::_publish(const char* subject, ctx::json option, _J("Option", option); request_list_t::iterator end = subscribe_request_list.end(); - request_list_t::iterator target = find_request(subscribe_request_list.begin(), end, subject, option, zone); + request_list_t::iterator target = find_request(subscribe_request_list.begin(), end, subject, option); while (target != end) { if (!(*target)->publish(error, data_updated)) { return false; } - target = find_request(++target, end, subject, option, zone); + target = find_request(++target, end, subject, option); } return true; } -bool ctx::context_manager_impl::_reply_to_read(const char* subject, ctx::json option, int error, ctx::json data_read, const char* zone) +bool ctx::context_manager_impl::_reply_to_read(const char* subject, ctx::json option, int error, ctx::json data_read) { IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter"); _I("Sending data of '%s'", subject); _J("Option", option); _J("Data", data_read); - _SI("Zone: '%s'", zone); request_list_t::iterator end = read_request_list.end(); - request_list_t::iterator target = find_request(read_request_list.begin(), end, subject, option, zone); + request_list_t::iterator target = find_request(read_request_list.begin(), end, subject, option); request_list_t::iterator prev; ctx::json dummy; @@ -368,7 +364,7 @@ bool ctx::context_manager_impl::_reply_to_read(const char* subject, ctx::json op while (target != end) { (*target)->reply(error, dummy, data_read); prev = target; - target = find_request(++target, end, subject, option, zone); + target = find_request(++target, end, subject, option); delete *prev; read_request_list.erase(prev); @@ -381,16 +377,14 @@ struct published_data_s { int type; ctx::context_manager_impl *mgr; std::string subject; - std::string zone; int error; ctx::json option; ctx::json data; - published_data_s(int t, ctx::context_manager_impl *m, const char* s, ctx::json& o, int e, ctx::json& d, const char* z) + published_data_s(int t, ctx::context_manager_impl *m, const char* s, ctx::json& o, int e, ctx::json& d) : type(t), mgr(m), subject(s), error(e) { option = o.str(); data = d.str(); - zone = z; } }; @@ -400,10 +394,10 @@ gboolean ctx::context_manager_impl::thread_switcher(gpointer data) switch (tuple->type) { case REQ_SUBSCRIBE: - tuple->mgr->_publish(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data, tuple->zone.c_str()); + tuple->mgr->_publish(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data); break; case REQ_READ: - tuple->mgr->_reply_to_read(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data, tuple->zone.c_str()); + tuple->mgr->_reply_to_read(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data); break; default: _W("Invalid type"); @@ -413,11 +407,11 @@ gboolean ctx::context_manager_impl::thread_switcher(gpointer data) return FALSE; } -bool ctx::context_manager_impl::publish(const char* subject, ctx::json& option, int error, ctx::json& data_updated, const char* zone) +bool ctx::context_manager_impl::publish(const char* subject, ctx::json& option, int error, ctx::json& data_updated) { - IF_FAIL_RETURN_TAG(subject && zone, false, _E, "Invalid parameter"); + IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter"); - published_data_s *tuple = new(std::nothrow) published_data_s(REQ_SUBSCRIBE, this, subject, option, error, data_updated, zone); + published_data_s *tuple = new(std::nothrow) published_data_s(REQ_SUBSCRIBE, this, subject, option, error, data_updated); IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed"); g_idle_add(thread_switcher, tuple); @@ -425,11 +419,11 @@ bool ctx::context_manager_impl::publish(const char* subject, ctx::json& option, return true; } -bool ctx::context_manager_impl::reply_to_read(const char* subject, ctx::json& option, int error, ctx::json& data_read, const char* zone) +bool ctx::context_manager_impl::reply_to_read(const char* subject, ctx::json& option, int error, ctx::json& data_read) { - IF_FAIL_RETURN_TAG(subject && zone, false, _E, "Invalid parameter"); + IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter"); - published_data_s *tuple = new(std::nothrow) published_data_s(REQ_READ, this, subject, option, error, data_read, zone); + published_data_s *tuple = new(std::nothrow) published_data_s(REQ_READ, this, subject, option, error, data_read); IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed"); g_idle_add(thread_switcher, tuple); diff --git a/src/context_mgr_impl.h b/src/context_mgr_impl.h index 9aa8305..c6026d3 100644 --- a/src/context_mgr_impl.h +++ b/src/context_mgr_impl.h @@ -38,12 +38,12 @@ namespace ctx { void release(); void assign_request(ctx::request_info *request); - bool is_supported(const char *subject, const char *zone); + bool is_supported(const char *subject); /* From the interface class */ bool register_provider(const char *subject, ctx::context_provider_iface *cp); - bool publish(const char *subject, ctx::json& option, int error, ctx::json& data_updated, const char *zone); - bool reply_to_read(const char *subject, ctx::json& option, int error, ctx::json& data_read, const char *zone); + bool publish(const char *subject, ctx::json& option, int error, ctx::json& data_updated); + bool reply_to_read(const char *subject, ctx::json& option, int error, ctx::json& data_read); private: typedef std::vector provider_list_t; @@ -66,12 +66,12 @@ namespace ctx { bool check_permission(request_info *request); static gboolean thread_switcher(gpointer data); - bool _publish(const char *subject, ctx::json option, int error, ctx::json data_updated, const char *zone); - bool _reply_to_read(const char *subject, ctx::json option, int error, ctx::json data_read, const char *zone); + bool _publish(const char *subject, ctx::json option, int error, ctx::json data_updated); + bool _reply_to_read(const char *subject, ctx::json option, int error, ctx::json data_read); - request_list_t::iterator find_request(request_list_t& r_list, std::string subject, json& option, const char *zone); + request_list_t::iterator find_request(request_list_t& r_list, std::string subject, json& option); request_list_t::iterator find_request(request_list_t& r_list, std::string client, int req_id); - request_list_t::iterator find_request(request_list_t::iterator begin, request_list_t::iterator end, std::string subject, json& option, const char *zone); + request_list_t::iterator find_request(request_list_t::iterator begin, request_list_t::iterator end, std::string subject, json& option); }; /* class context_manager_impl */ diff --git a/src/context_trigger/clips_handler.cpp b/src/context_trigger/clips_handler.cpp index 0fb6c71..2f2b749 100644 --- a/src/context_trigger/clips_handler.cpp +++ b/src/context_trigger/clips_handler.cpp @@ -31,34 +31,32 @@ static void* env = NULL; static ctx::rule_manager* rule_mgr = NULL; static int string_to_int(std::string str); -ctx::clips_handler::clips_handler() +ctx::clips_handler::clips_handler(ctx::rule_manager* rm) { + rule_mgr = rm; + init_environment(); + _D(YELLOW("Clips handler initialized")); } ctx::clips_handler::~clips_handler() { if (env) { DestroyEnvironment(env); + env = NULL; } -} - -bool ctx::clips_handler::init(ctx::rule_manager* rm) -{ - rule_mgr = rm; - int error = init_environment(env); - IF_FAIL_RETURN(error == ERR_NONE, false); - - bool ret = define_global_variable_string("zone", ""); - IF_FAIL_RETURN(ret, false); - - return true; + _D(YELLOW("Clips handler destroyed")); } -int ctx::clips_handler::init_environment(void* &environment) +int ctx::clips_handler::init_environment(void) { - environment = CreateEnvironment(); - if (!environment) { + if (env) { + _D("Clips environment already created"); + return ERR_NONE; + } + + env = CreateEnvironment(); + if (!env) { _E("Create environment failed"); return ERR_OPERATION_FAILED; } @@ -73,8 +71,7 @@ int ctx::clips_handler::init_environment(void* &environment) return ERR_OUT_OF_MEMORY; } - EnvDefineFunction2(environment, func_name, 'i', PTIEF execute_action, func_name, restrictions); - + EnvDefineFunction2(env, func_name, 'i', PTIEF execute_action, func_name, restrictions); free(func_name); free(restrictions); @@ -190,12 +187,9 @@ int ctx::clips_handler::unmake_instance(std::string& instance_name) return ERR_NONE; } -int ctx::clips_handler::execute_action() +int ctx::clips_handler::execute_action(void) { - if (!env) { - _E("Environment not created"); - return ERR_OPERATION_FAILED; - } + ASSERT_NOT_NULL(env); const char* result = EnvRtnLexeme(env, 1); if (!result) { @@ -214,6 +208,7 @@ int ctx::clips_handler::execute_action() bool ctx::clips_handler::find_instance(std::string& instance_name) { ASSERT_NOT_NULL(env); + if (find_instance_internal(instance_name)) { _D("[%s] already exists", instance_name.c_str()); return true; diff --git a/src/context_trigger/clips_handler.h b/src/context_trigger/clips_handler.h index 8d3f6a6..0ebb747 100644 --- a/src/context_trigger/clips_handler.h +++ b/src/context_trigger/clips_handler.h @@ -24,9 +24,8 @@ namespace ctx { class clips_handler { public: - clips_handler(); + clips_handler(ctx::rule_manager* rm); ~clips_handler(); - bool init(ctx::rule_manager* rm); int define_template(std::string& script); int define_class(std::string& script); @@ -36,13 +35,14 @@ namespace ctx { int route_string_command(std::string& fact); int make_instance(std::string& script); int unmake_instance(std::string& instance_name); - static int execute_action(); + static int execute_action(void); bool find_instance(std::string& instance_name); bool define_global_variable_string(std::string variable_name, std::string value); bool set_global_variable_string(std::string variable_name, std::string value); private: - int init_environment(void* &environment); + clips_handler(); + int init_environment(void); int env_build(std::string& script); void* find_instance_internal(std::string& instance_name); diff --git a/src/context_trigger/context_monitor.cpp b/src/context_trigger/context_monitor.cpp index cce3bab..ed9d6a7 100644 --- a/src/context_trigger/context_monitor.cpp +++ b/src/context_trigger/context_monitor.cpp @@ -22,8 +22,6 @@ #include "timer_types.h" static ctx::fact_reader *reader = NULL; -typedef std::map timer_map_t; -static timer_map_t timer_map; // ctx::context_monitor::context_monitor() { @@ -31,27 +29,30 @@ ctx::context_monitor::context_monitor() ctx::context_monitor::~context_monitor() { + delete timer; } bool ctx::context_monitor::init(ctx::fact_reader* fr, ctx::context_trigger* tr) { reader = fr; trigger = tr; + timer = new(std::nothrow) trigger_timer(trigger); + IF_FAIL_RETURN_TAG(timer, false, _E, "Memory allocation failed"); return true; } -int ctx::context_monitor::subscribe(int rule_id, std::string subject, ctx::json event, const char* zone) +int ctx::context_monitor::subscribe(int rule_id, std::string subject, ctx::json event) { if (subject.compare(TIMER_EVENT_SUBJECT) == 0) { // option is event json in case of ON_TIME - return subscribe_timer(event, zone); + return subscribe_timer(event); } ctx::json eoption = NULL; event.get(NULL, CT_RULE_EVENT_OPTION, &eoption); - int req_id = reader->subscribe(subject.c_str(), &eoption, zone, true); + int req_id = reader->subscribe(subject.c_str(), &eoption, true); IF_FAIL_RETURN_TAG(req_id > 0, ERR_OPERATION_FAILED, _E, "Subscribe event failed"); _D(YELLOW("Subscribe event(rule%d). req%d"), rule_id, req_id); request_map[rule_id] = req_id; @@ -60,10 +61,10 @@ int ctx::context_monitor::subscribe(int rule_id, std::string subject, ctx::json return ERR_NONE; } -int ctx::context_monitor::unsubscribe(int rule_id, std::string subject, ctx::json option, const char* zone) +int ctx::context_monitor::unsubscribe(int rule_id, std::string subject, ctx::json option) { if (subject.compare(TIMER_EVENT_SUBJECT) == 0) { - return unsubscribe_timer(option, zone); + return unsubscribe_timer(option); } _D(YELLOW("Unsubscribe event(rule%d). req%d"), rule_id, request_map[rule_id]); @@ -79,7 +80,7 @@ int ctx::context_monitor::unsubscribe(int rule_id, std::string subject, ctx::jso return ERR_NONE; } -int ctx::context_monitor::read_time(ctx::json* result, const char* zone) +int ctx::context_monitor::read_time(ctx::json* result) { int dom = ctx::trigger_timer::get_day_of_month(); (*result).set(NULL, TIMER_RESPONSE_KEY_DAY_OF_MONTH, dom); @@ -93,15 +94,15 @@ int ctx::context_monitor::read_time(ctx::json* result, const char* zone) return ERR_NONE; } -int ctx::context_monitor::read(std::string subject, json option, const char* zone, ctx::json* result) +int ctx::context_monitor::read(std::string subject, json option, ctx::json* result) { bool ret; if (subject.compare(TIMER_CONDITION_SUBJECT) == 0) { - return read_time(result, zone); + return read_time(result); } context_fact fact; - ret = reader->read(subject.c_str(), &option, zone, fact); + ret = reader->read(subject.c_str(), &option, fact); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Read fact failed"); *result = fact.get_data(); @@ -144,18 +145,7 @@ static int arrange_day_of_week(ctx::json day_info) return result; } -timer_map_t::iterator ctx::context_monitor::get_zone_timer(std::string zone) -{ - timer_map_t::iterator it = timer_map.find(zone); - - if (it == timer_map.end()) { - timer_map.insert(std::pair(zone, ctx::trigger_timer(trigger, zone))); - } - - return timer_map.find(zone); -} - -int ctx::context_monitor::subscribe_timer(ctx::json option, const char* zone) +int ctx::context_monitor::subscribe_timer(ctx::json option) { ctx::json day_info; ctx::json time_info; @@ -177,15 +167,14 @@ int ctx::context_monitor::subscribe_timer(ctx::json option, const char* zone) // Time option processing int time; // minute - timer_map_t::iterator timer = get_zone_timer(zone); for (int i = 0; time_info.get_array_elem(NULL, CT_RULE_DATA_VALUE_ARR, i, &time); i++) { - (timer->second).add(time, dow); + timer->add(time, dow); } return ERR_NONE; } -int ctx::context_monitor::unsubscribe_timer(ctx::json option, const char* zone) +int ctx::context_monitor::unsubscribe_timer(ctx::json option) { ctx::json day_info; ctx::json time_info; @@ -207,24 +196,19 @@ int ctx::context_monitor::unsubscribe_timer(ctx::json option, const char* zone) // Time option processing int time; // minute - timer_map_t::iterator timer = get_zone_timer(zone); for (int i = 0; time_info.get_array_elem(NULL, CT_RULE_DATA_VALUE_ARR, i, &time); i++) { - (timer->second).remove(time, dow); - } - - if ((timer->second).empty()) { - timer_map.erase(timer); + timer->remove(time, dow); } return ERR_NONE; } -bool ctx::context_monitor::is_supported(std::string subject, const char* zone) +bool ctx::context_monitor::is_supported(std::string subject) { if (subject.compare(TIMER_EVENT_SUBJECT) == 0 || subject.compare(TIMER_CONDITION_SUBJECT) == 0) { return true; } - return reader->is_supported(subject.c_str(), zone); + return reader->is_supported(subject.c_str()); } diff --git a/src/context_trigger/context_monitor.h b/src/context_trigger/context_monitor.h index 48d4f0f..678107a 100644 --- a/src/context_trigger/context_monitor.h +++ b/src/context_trigger/context_monitor.h @@ -27,28 +27,28 @@ namespace ctx { class context_fact; class context_monitor { - public: - context_monitor(); - ~context_monitor(); - bool init(ctx::fact_reader* fr, ctx::context_trigger* tr); - - int subscribe(int rule_id, std::string subject, ctx::json event, const char* zone); - int unsubscribe(int rule_id, std::string subject, ctx::json option, const char* zone); - int read(std::string subject, json option, const char* zone, ctx::json* result); - bool is_supported(std::string subject, const char* zone); - - private: - std::map::iterator get_zone_timer(std::string zone); - int subscribe_timer(ctx::json option, const char* zone); - int unsubscribe_timer(ctx::json option, const char* zone); - int read_day_of_month(ctx::json* result, const char* zone); - int read_day_of_week(ctx::json* result, const char* zone); - int read_time(ctx::json* result, const char* zone); - std::map request_map; // - std::map read_req_cnt_map; // - ctx::context_trigger* trigger; - - }; /* class context_monitor */ + public: + context_monitor(); + ~context_monitor(); + bool init(ctx::fact_reader* fr, ctx::context_trigger* tr); + + int subscribe(int rule_id, std::string subject, ctx::json event); + int unsubscribe(int rule_id, std::string subject, ctx::json option); + int read(std::string subject, json option, ctx::json* result); + bool is_supported(std::string subject); + + private: + int subscribe_timer(ctx::json option); + int unsubscribe_timer(ctx::json option); + int read_day_of_month(ctx::json* result); + int read_day_of_week(ctx::json* result); + int read_time(ctx::json* result); + std::map request_map; // + std::map read_req_cnt_map; // + ctx::context_trigger* trigger; + ctx::trigger_timer* timer; + + }; /* class context_monitor */ } /* namespace ctx */ diff --git a/src/context_trigger/fact.cpp b/src/context_trigger/fact.cpp index 85ee3da..960f59d 100644 --- a/src/context_trigger/fact.cpp +++ b/src/context_trigger/fact.cpp @@ -23,16 +23,13 @@ ctx::context_fact::context_fact() { } -ctx::context_fact::context_fact(int id, int err, const char* s, ctx::json& o, ctx::json& d, const char* z) +ctx::context_fact::context_fact(int id, int err, const char* s, ctx::json& o, ctx::json& d) : req_id(id) , error(err) , subject(s) , option(o) , data(d) { - if (z) { - zone_name = z; - } } ctx::context_fact::~context_fact() @@ -64,11 +61,6 @@ void ctx::context_fact::set_data(ctx::json& d) data = d; } -void ctx::context_fact::set_zone_name(const char* z) -{ - zone_name = z; -} - int ctx::context_fact::get_req_id() { return req_id; @@ -84,11 +76,6 @@ const char* ctx::context_fact::get_subject() return subject.c_str(); } -const char* ctx::context_fact::get_zone_name() -{ - return zone_name.c_str(); -} - ctx::json& ctx::context_fact::get_option() { return option; diff --git a/src/context_trigger/fact.h b/src/context_trigger/fact.h index 14ff95e..9411b68 100644 --- a/src/context_trigger/fact.h +++ b/src/context_trigger/fact.h @@ -29,24 +29,21 @@ namespace ctx { std::string subject; ctx::json option; ctx::json data; - std::string zone_name; public: context_fact(); - context_fact(int id, int err, const char* s, ctx::json& o, ctx::json& d, const char* z); + context_fact(int id, int err, const char* s, ctx::json& o, ctx::json& d); ~context_fact(); void set_req_id(int id); void set_error(int err); void set_subject(const char* s); void set_option(ctx::json& o); - void set_zone_name(const char* z); void set_data(ctx::json& d); int get_req_id(); int get_error(); const char* get_subject(); - const char* get_zone_name(); ctx::json& get_option(); ctx::json& get_data(); }; diff --git a/src/context_trigger/fact_reader.cpp b/src/context_trigger/fact_reader.cpp index cbce51d..f8935ea 100644 --- a/src/context_trigger/fact_reader.cpp +++ b/src/context_trigger/fact_reader.cpp @@ -59,7 +59,7 @@ ctx::fact_reader::~fact_reader() subscr_list.clear(); } -int ctx::fact_reader::find_sub(const char* subject, json* option, const char* zone) +int ctx::fact_reader::find_sub(const char* subject, json* option) { json opt_j; if (option) { @@ -67,7 +67,7 @@ int ctx::fact_reader::find_sub(const char* subject, json* option, const char* zo } for (subscr_list_t::iterator it = subscr_list.begin(); it != subscr_list.end(); ++it) { - if ((*it)->subject == subject && (*it)->option == opt_j && (*it)->zone_name == zone) { + if ((*it)->subject == subject && (*it)->option == opt_j) { return (*it)->sid; } } @@ -75,16 +75,16 @@ int ctx::fact_reader::find_sub(const char* subject, json* option, const char* zo return -1; } -bool ctx::fact_reader::add_sub(int sid, const char* subject, json* option, const char* zone) +bool ctx::fact_reader::add_sub(int sid, const char* subject, json* option) { - subscr_info_s *info = new(std::nothrow) subscr_info_s(sid, subject, option, zone); + subscr_info_s *info = new(std::nothrow) subscr_info_s(sid, subject, option); IF_FAIL_RETURN_TAG(info, false, _E, "Memory allocation failed"); subscr_list.push_back(info); return true; } -void ctx::fact_reader::remove_sub(const char* subject, json* option, const char* zone) +void ctx::fact_reader::remove_sub(const char* subject, json* option) { json opt_j; if (option) { @@ -92,7 +92,7 @@ void ctx::fact_reader::remove_sub(const char* subject, json* option, const char* } for (subscr_list_t::iterator it = subscr_list.begin(); it != subscr_list.end(); ++it) { - if ((*it)->subject == subject && (*it)->option == opt_j && (*it)->zone_name == zone) { + if ((*it)->subject == subject && (*it)->option == opt_j) { delete *it; subscr_list.erase(it); return; @@ -118,20 +118,18 @@ gboolean ctx::fact_reader::send_request(gpointer data) return FALSE; } -bool ctx::fact_reader::is_supported(const char* subject, const char* zone) +bool ctx::fact_reader::is_supported(const char* subject) { - IF_FAIL_RETURN_TAG(zone, false, _E, "'zone' cannot be NULL"); - return _context_mgr->is_supported(subject, zone); + return _context_mgr->is_supported(subject); } -int ctx::fact_reader::subscribe(const char* subject, json* option, const char* zone, bool wait_response) +int ctx::fact_reader::subscribe(const char* subject, json* option, bool wait_response) { - IF_FAIL_RETURN_TAG(zone, ERR_INVALID_PARAMETER, _E, "'zone' cannot be NULL"); IF_FAIL_RETURN(subject, ERR_INVALID_PARAMETER); ctx::scope_mutex sm(&request_mutex); - int rid = find_sub(subject, option, zone); + int rid = find_sub(subject, option); if (rid > 0) { _D("Duplicated request for %s", subject); return rid; @@ -140,11 +138,11 @@ int ctx::fact_reader::subscribe(const char* subject, json* option, const char* z rid = generate_req_id(); fact_request *req = new(std::nothrow) fact_request(REQ_SUBSCRIBE, CLIENT_NAME, - rid, subject, option ? option->str().c_str() : NULL, zone, wait_response ? this : NULL); + rid, subject, option ? option->str().c_str() : NULL, wait_response ? this : NULL); IF_FAIL_RETURN_TAG(req, -1, _E, "Memory allocation failed"); g_idle_add(send_request, req); - add_sub(rid, subject, option, zone); + add_sub(rid, subject, option); IF_FAIL_RETURN_TAG(wait_response, rid, _D, "Ignoring response for %s", subject); @@ -166,14 +164,13 @@ int ctx::fact_reader::subscribe(const char* subject, json* option, const char* z return rid; } -void ctx::fact_reader::unsubscribe(const char* subject, json* option, const char* zone) +void ctx::fact_reader::unsubscribe(const char* subject, json* option) { - IF_FAIL_VOID_TAG(zone, _E, "'zone' cannot be NULL"); IF_FAIL_VOID(subject); ctx::scope_mutex sm(&request_mutex); - int rid = find_sub(subject, option, zone); + int rid = find_sub(subject, option); IF_FAIL_VOID_TAG(rid > 0, _W, "Unknown subscription for %s", subject); unsubscribe(rid); @@ -181,16 +178,15 @@ void ctx::fact_reader::unsubscribe(const char* subject, json* option, const char void ctx::fact_reader::unsubscribe(int subscription_id) { - fact_request *req = new(std::nothrow) fact_request(REQ_UNSUBSCRIBE, CLIENT_NAME, subscription_id, "", NULL, NULL, NULL); + fact_request *req = new(std::nothrow) fact_request(REQ_UNSUBSCRIBE, CLIENT_NAME, subscription_id, "", NULL, NULL); IF_FAIL_VOID_TAG(req, _E, "Memory allocation failed"); g_idle_add(send_request, req); remove_sub(subscription_id); } -bool ctx::fact_reader::read(const char* subject, json* option, const char* zone, context_fact& fact) +bool ctx::fact_reader::read(const char* subject, json* option, context_fact& fact) { - IF_FAIL_RETURN_TAG(zone, false, _E, "'zone' cannot be NULL"); IF_FAIL_RETURN(subject, false); ctx::scope_mutex sm(&request_mutex); @@ -198,7 +194,7 @@ bool ctx::fact_reader::read(const char* subject, json* option, const char* zone, int rid = generate_req_id(); fact_request *req = new(std::nothrow) fact_request(REQ_READ_SYNC, CLIENT_NAME, - rid, subject, option ? option->str().c_str() : NULL, zone, this); + rid, subject, option ? option->str().c_str() : NULL, this); IF_FAIL_RETURN_TAG(req, false, _E, "Memory allocation failed"); g_idle_add(send_request, req); @@ -219,16 +215,12 @@ bool ctx::fact_reader::read(const char* subject, json* option, const char* zone, fact.set_req_id(rid); fact.set_subject(subject); fact.set_data(last_data_read); - if (zone) { - fact.set_zone_name(zone); - } - last_data_read = EMPTY_JSON_OBJECT; return true; } -void ctx::fact_reader::reply_result(int req_id, int error, const char* zone, json* request_result, json* fact) +void ctx::fact_reader::reply_result(int req_id, int error, json* request_result, json* fact) { ctx::scope_mutex sm(&request_mutex); @@ -239,7 +231,7 @@ void ctx::fact_reader::reply_result(int req_id, int error, const char* zone, jso g_cond_signal(&request_cond); } -void ctx::fact_reader::publish_fact(int req_id, int error, const char* zone, const char* subject, json* option, json* fact) +void ctx::fact_reader::publish_fact(int req_id, int error, const char* subject, json* option, json* fact) { - _trigger->push_fact(req_id, error, subject, *option, *fact, zone); + _trigger->push_fact(req_id, error, subject, *option, *fact); } diff --git a/src/context_trigger/fact_reader.h b/src/context_trigger/fact_reader.h index 2a5192d..e52b86e 100644 --- a/src/context_trigger/fact_reader.h +++ b/src/context_trigger/fact_reader.h @@ -31,15 +31,15 @@ namespace ctx { fact_reader(context_manager_impl* mgr, context_trigger* trigger); ~fact_reader(); - bool is_supported(const char* subject, const char* zone); + bool is_supported(const char* subject); - int subscribe(const char* subject, json* option, const char* zone, bool wait_response = false); - void unsubscribe(const char* subject, json* option, const char* zone); + int subscribe(const char* subject, json* option, bool wait_response = false); + void unsubscribe(const char* subject, json* option); void unsubscribe(int subscription_id); - bool read(const char* subject, json* option, const char* zone, context_fact& fact); + bool read(const char* subject, json* option, context_fact& fact); - void reply_result(int req_id, int error, const char* zone, json* request_result = NULL, json* fact = NULL); - void publish_fact(int req_id, int error, const char* zone, const char* subject, json* option, json* fact); + void reply_result(int req_id, int error, json* request_result = NULL, json* fact = NULL); + void publish_fact(int req_id, int error, const char* subject, json* option, json* fact); private: static context_manager_impl* _context_mgr; @@ -49,24 +49,20 @@ namespace ctx { int sid; std::string subject; ctx::json option; - std::string zone_name; - subscr_info_s(int id, const char* subj, ctx::json* opt, const char* zone) + subscr_info_s(int id, const char* subj, ctx::json* opt) : sid(id), subject(subj) { if (opt) option = *opt; - - if (zone) - zone_name = zone; } }; typedef std::list subscr_list_t; subscr_list_t subscr_list; - int find_sub(const char* subject, json* option, const char* zone); - bool add_sub(int sid, const char* subject, json* option, const char* zone); - void remove_sub(const char* subject, json* option, const char* zone); + int find_sub(const char* subject, json* option); + bool add_sub(int sid, const char* subject, json* option); + void remove_sub(const char* subject, json* option); void remove_sub(int sid); static gboolean send_request(gpointer data); diff --git a/src/context_trigger/fact_request.cpp b/src/context_trigger/fact_request.cpp index ec074b2..b4017bd 100644 --- a/src/context_trigger/fact_request.cpp +++ b/src/context_trigger/fact_request.cpp @@ -17,13 +17,11 @@ #include #include "fact_request.h" -ctx::fact_request::fact_request(int type, const char* client, int req_id, const char* subj, const char* desc, const char* zone, fact_reader* reader) +ctx::fact_request::fact_request(int type, const char* client, int req_id, const char* subj, const char* desc, fact_reader* reader) : request_info(type, client, req_id, subj, desc) , _reader(reader) , replied(false) { - if (zone) - _zone_name = zone; } ctx::fact_request::~fact_request() @@ -34,7 +32,7 @@ ctx::fact_request::~fact_request() bool ctx::fact_request::reply(int error) { IF_FAIL_RETURN(!replied && _reader, true); - _reader->reply_result(_req_id, error, _zone_name.c_str()); + _reader->reply_result(_req_id, error); return (replied = true); } @@ -42,26 +40,20 @@ bool ctx::fact_request::reply(int error, ctx::json& request_result) { IF_FAIL_RETURN(!replied && _reader, true); IF_FAIL_RETURN(_type != REQ_READ_SYNC, true); - _reader->reply_result(_req_id, error, _zone_name.c_str(), &request_result); + _reader->reply_result(_req_id, error, &request_result); return (replied = true); } bool ctx::fact_request::reply(int error, ctx::json& request_result, ctx::json& data_read) { IF_FAIL_RETURN(!replied && _reader, true); - _reader->reply_result(_req_id, error, _zone_name.c_str(), &request_result, &data_read); + _reader->reply_result(_req_id, error, &request_result, &data_read); return (replied = true); } bool ctx::fact_request::publish(int error, ctx::json& data) { IF_FAIL_RETURN(_reader, true); - _reader->publish_fact(_req_id, error, _zone_name.c_str(), _subject.c_str(), &get_description(), &data); + _reader->publish_fact(_req_id, error, _subject.c_str(), &get_description(), &data); return true; } - -void ctx::fact_request::set_zone_name(const char* zone_name) -{ - if (zone_name) - _zone_name = zone_name; -} diff --git a/src/context_trigger/fact_request.h b/src/context_trigger/fact_request.h index 7832072..6ad52c5 100644 --- a/src/context_trigger/fact_request.h +++ b/src/context_trigger/fact_request.h @@ -24,10 +24,9 @@ namespace ctx { class fact_request : public request_info { public: - fact_request(int type, const char* client, int req_id, const char* subj, const char* desc, const char* zone, fact_reader* reader); + fact_request(int type, const char* client, int req_id, const char* subj, const char* desc, fact_reader* reader); ~fact_request(); - void set_zone_name(const char* zone_name); bool reply(int error); bool reply(int error, ctx::json& request_result); bool reply(int error, ctx::json& request_result, ctx::json& data_read); diff --git a/src/context_trigger/rule_manager.cpp b/src/context_trigger/rule_manager.cpp index ceecc85..4fd214f 100644 --- a/src/context_trigger/rule_manager.cpp +++ b/src/context_trigger/rule_manager.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include "../access_control/privilege.h" #include "rule_manager.h" #include "script_generator.h" @@ -40,9 +39,9 @@ #define CONDITION_TABLE "context_trigger_condition" #define TEMPLATE_TABLE "context_trigger_template" -#define RULE_TABLE_COLUMNS "enabled INTEGER DEFAULT 0 NOT NULL, creator TEXT DEFAULT '' NOT NULL, description TEXT DEFAULT '', details TEXT DEFAULT '' NOT NULL, zone TEXT DEFAULT '' NOT NULL" -#define EVENT_TABLE_COLUMNS "rule_id INTEGER references context_trigger_rule(row_id) ON DELETE CASCADE NOT NULL, name TEXT DEFAULT '' NOT NULL, instance_name TEXT DEFAULT '', zone TEXT DEFAULT '' NOT NULL" -#define CONDITION_TABLE_COLUMNS "rule_id INTEGER references context_trigger_rule(row_id) ON DELETE CASCADE NOT NULL, name TEXT DEFAULT '' NOT NULL, option TEXT DEFAULT '', instance_name TEXT DEFAULT '', zone TEXT DEFAULT '' NOT NULL" +#define RULE_TABLE_COLUMNS "enabled INTEGER DEFAULT 0 NOT NULL, creator TEXT DEFAULT '' NOT NULL, description TEXT DEFAULT '', details TEXT DEFAULT '' NOT NULL" +#define EVENT_TABLE_COLUMNS "rule_id INTEGER references context_trigger_rule(row_id) ON DELETE CASCADE NOT NULL, name TEXT DEFAULT '' NOT NULL, instance_name TEXT DEFAULT ''" +#define CONDITION_TABLE_COLUMNS "rule_id INTEGER references context_trigger_rule(row_id) ON DELETE CASCADE NOT NULL, name TEXT DEFAULT '' NOT NULL, option TEXT DEFAULT '', instance_name TEXT DEFAULT ''" #define CREATE_TEMPLATE_TABLE "CREATE TABLE IF NOT EXISTS context_trigger_template (name TEXT DEFAULT '' NOT NULL PRIMARY KEY, j_template TEXT DEFAULT '' NOT NULL)" #define QUERY_TEMPLATE_TABLE "SELECT j_template FROM context_trigger_template" #define FOREIGN_KEYS_ON "PRAGMA foreign_keys = ON" @@ -51,7 +50,7 @@ #define UPDATE_RULE_DISABLED_STATEMENT "UPDATE context_trigger_rule SET enabled = 0 WHERE row_id = " #define QUERY_NAME_INSTANCE_NAME_AND_TEMPLATE_BY_RULE_ID_STATEMENT "SELECT context_trigger_condition.name, instance_name, j_template as templates FROM context_trigger_condition JOIN context_trigger_template ON (context_trigger_condition.name = context_trigger_template.name) WHERE rule_id = " #define QUERY_CONDITION_TEMPLATES_OF_INVOKED_EVENT_STATEMENT "SELECT DISTINCT context_trigger_condition.name, instance_name, option, j_template FROM context_trigger_condition JOIN context_trigger_template ON (context_trigger_condition.name = context_trigger_template.name) WHERE rule_id IN (SELECT row_id FROM context_trigger_rule WHERE enabled = 1 AND row_id IN (SELECT rule_id FROM context_trigger_event WHERE context_trigger_event.instance_name = '" -#define QUERY_RULE_AND_ZONE_BY_RULE_ID "SELECT details, zone FROM context_trigger_rule WHERE row_id = " +#define QUERY_RULE_BY_RULE_ID "SELECT details FROM context_trigger_rule WHERE row_id = " #define QUERY_EVENT_TEMPLATE_BY_RULE_ID "SELECT j_template FROM context_trigger_template WHERE name IN (SELECT name FROM context_trigger_event WHERE rule_id = " #define QUERY_CONDITION_BY_RULE_ID "SELECT name, option FROM context_trigger_condition WHERE rule_id = " @@ -59,6 +58,7 @@ #define EVENT_KEY_PREFIX "?" static ctx::context_trigger* trigger = NULL; +static int enb_rule_cnt = 0; static int string_to_int(std::string str) { @@ -85,19 +85,17 @@ ctx::rule_manager::rule_manager() ctx::rule_manager::~rule_manager() { + destroy_clips(); } bool ctx::rule_manager::init(ctx::context_trigger* tr, ctx::fact_reader* fr) { - int error; bool ret; + clips_h = NULL; trigger = tr; - - ret = clips_h.init(this); - IF_FAIL_RETURN_TAG(ret, false, _E, "CLIPS handler initialization failed"); - ret = c_monitor.init(fr, tr); + IF_FAIL_RETURN_TAG(ret, false, _E, "Context monitor initialization failed"); // Create tables into db (rule, event, condition, action, template) ret = db_manager::create_table(1, RULE_TABLE, RULE_TABLE_COLUMNS, NULL, NULL); @@ -112,9 +110,29 @@ bool ctx::rule_manager::init(ctx::context_trigger* tr, ctx::fact_reader* fr) ret = db_manager::execute(4, CREATE_TEMPLATE_TABLE, NULL); IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed"); + // Foreign keys on + std::vector record; + ret = db_manager::execute_sync(FOREIGN_KEYS_ON, &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Foreign keys on failed"); + + ret = reenable_rule(); + + return ret; +} + +bool ctx::rule_manager::initialize_clips(void) +{ + if (clips_h) { + _D("CLIPS handler already initialized"); + return true; + } + + clips_h = new(std::nothrow) clips_handler(this); + IF_FAIL_RETURN_TAG(clips_h, false, _E, "CLIPS handler initialization failed"); + // Load all templates from DB std::vector record; - ret = db_manager::execute_sync(QUERY_TEMPLATE_TABLE, &record); + bool ret = db_manager::execute_sync(QUERY_TEMPLATE_TABLE, &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Query template table failed"); // Make scripts for deftemplate, defclass, make-instance and load them to clips @@ -126,25 +144,26 @@ bool ctx::rule_manager::init(ctx::context_trigger* tr, ctx::fact_reader* fr) ctx::json tmpl = tmpl_str; std::string deftemplate_str = script_generator::generate_deftemplate(&tmpl); - error = clips_h.define_template(deftemplate_str); + int error = clips_h->define_template(deftemplate_str); IF_FAIL_RETURN_TAG(error == ERR_NONE, false, _E, "Deftemplate failed"); std::string defclass_str = script_generator::generate_defclass(&tmpl); - error = clips_h.define_class(defclass_str); + error = clips_h->define_class(defclass_str); IF_FAIL_RETURN_TAG(error == ERR_NONE, false, _E, "Defclass failed"); std::string makeinstance_str = script_generator::generate_makeinstance(&tmpl); - error = clips_h.make_instance(makeinstance_str); + error = clips_h->make_instance(makeinstance_str); IF_FAIL_RETURN_TAG(error == ERR_NONE, false, _E, "Makeinstance failed"); } - // Foreign keys on - ret = db_manager::execute_sync(FOREIGN_KEYS_ON, &record); - IF_FAIL_RETURN_TAG(ret, false, _E, "Foreign keys on failed"); - - ret = reenable_rule(); + _D(YELLOW("Deftemplate, Defclass, Make-instance completed")); + return true; +} - return ret; +void ctx::rule_manager::destroy_clips(void) +{ + delete clips_h; + clips_h = NULL; } bool ctx::rule_manager::reenable_rule(void) @@ -166,7 +185,7 @@ bool ctx::rule_manager::reenable_rule(void) if (error != ERR_NONE) { _E("Re-enable rule%d failed(%d)", row_id, error); } else { - _E("Re-enable rule%d succeeded", row_id); + _D("Re-enable rule%d succeeded", row_id); } } @@ -327,12 +346,10 @@ bool ctx::rule_manager::rule_equals(ctx::json& lrule, ctx::json& rrule) return true; } -int64_t ctx::rule_manager::get_duplicated_rule(std::string creator, std::string zone, ctx::json& rule) +int64_t ctx::rule_manager::get_duplicated_rule(std::string creator, ctx::json& rule) { std::string q = "SELECT row_id, details FROM context_trigger_rule WHERE creator = '"; q += creator; - q += "' AND zone ='"; - q += zone; q += "'"; std::vector record; @@ -361,7 +378,7 @@ int64_t ctx::rule_manager::get_duplicated_rule(std::string creator, std::string return -1; } -int ctx::rule_manager::verify_rule(ctx::json& rule, const char* app_id, const char* zone) +int ctx::rule_manager::verify_rule(ctx::json& rule, const char* app_id) { ctx::json details; rule.get(NULL, CT_RULE_DETAILS, &details); @@ -369,7 +386,7 @@ int ctx::rule_manager::verify_rule(ctx::json& rule, const char* app_id, const ch std::string e_name; rule.get(CT_RULE_DETAILS "." CT_RULE_EVENT, CT_RULE_EVENT_ITEM, &e_name); - IF_FAIL_RETURN_TAG(c_monitor.is_supported(e_name, zone), ERR_NOT_SUPPORTED, _I, "Event(%s) is not supported", e_name.c_str()); + IF_FAIL_RETURN_TAG(c_monitor.is_supported(e_name), ERR_NOT_SUPPORTED, _I, "Event(%s) is not supported", e_name.c_str()); if (app_id) { if (!ctx::privilege_manager::is_allowed(app_id, e_name.c_str())) { @@ -383,7 +400,7 @@ int ctx::rule_manager::verify_rule(ctx::json& rule, const char* app_id, const ch std::string c_name; it.get(NULL, CT_RULE_CONDITION_ITEM, &c_name); - IF_FAIL_RETURN_TAG(c_monitor.is_supported(c_name, zone), ERR_NOT_SUPPORTED, _I, "Condition(%s) is not supported", c_name.c_str()); + IF_FAIL_RETURN_TAG(c_monitor.is_supported(c_name), ERR_NOT_SUPPORTED, _I, "Condition(%s) is not supported", c_name.c_str()); if (!ctx::privilege_manager::is_allowed(app_id, c_name.c_str())) { _W("Permission denied for '%s'", c_name.c_str()); @@ -394,18 +411,18 @@ int ctx::rule_manager::verify_rule(ctx::json& rule, const char* app_id, const ch return ERR_NONE; } -int ctx::rule_manager::add_rule(std::string creator, ctx::json rule, std::string zone, ctx::json* rule_id) +int ctx::rule_manager::add_rule(std::string creator, ctx::json rule, ctx::json* rule_id) { // * Insert rule to DB bool ret; int64_t rid; // Check if all items are supported && allowed to access - int err = verify_rule(rule, creator.c_str(), zone.c_str()); + int err = verify_rule(rule, creator.c_str()); IF_FAIL_RETURN(err==ERR_NONE, err); // Check if duplicated rule exits - if ((rid = get_duplicated_rule(creator, zone, rule)) > 0) { + if ((rid = get_duplicated_rule(creator, rule)) > 0) { // Save rule id rule_id->set(NULL, CT_RULE_ID, rid); return ERR_NONE; @@ -420,7 +437,6 @@ int ctx::rule_manager::add_rule(std::string creator, ctx::json rule, std::string r_record.set(NULL, "creator", creator); r_record.set(NULL, "description", description); r_record.set(NULL, "details", details.str()); - r_record.set(NULL, "zone", zone); ret = db_manager::insert_sync(RULE_TABLE, r_record, &rid); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Insert rule to db failed"); @@ -440,7 +456,6 @@ int ctx::rule_manager::add_rule(std::string creator, ctx::json rule, std::string e_record.set(NULL, "rule_id", rid); e_record.set(NULL, "name", e_name); e_record.set(NULL, "instance_name", e_inst); - e_record.set(NULL, "zone", zone); ret = db_manager::insert(1, EVENT_TABLE, e_record, NULL); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Insert event to db failed"); @@ -463,7 +478,6 @@ int ctx::rule_manager::add_rule(std::string creator, ctx::json rule, std::string c_record.set(NULL, "name", c_name); c_record.set(NULL, "option", (c_option_str)? c_option_str : ""); c_record.set(NULL, "instance_name", c_inst); - c_record.set(NULL, "zone", zone); ret = db_manager::insert(2, CONDITION_TABLE, c_record, NULL); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Insert conditions to db failed"); @@ -493,45 +507,57 @@ int ctx::rule_manager::remove_rule(int rule_id) int ctx::rule_manager::enable_rule(int rule_id) { + if (enb_rule_cnt == 0) { + initialize_clips(); + } + // Subscribe event - bool ret; int error; + std::string query; + std::string ename; + std::string script; + std::string r_details; + std::string etmpl_str; + + ctx::json jrule; + ctx::json jetemplate; + ctx::json jevent; + ctx::json inst_names; + + std::vector rule_record; + std::vector etemplate_record; + std::vector cond_record; + std::vector record; + std::vector::iterator vec_end; + std::string id_str = int_to_string(rule_id); // Get rule json by rule id; - std::string q1 = QUERY_RULE_AND_ZONE_BY_RULE_ID; - q1 += int_to_string(rule_id); - std::vector rule_record; - ret = db_manager::execute_sync(q1.c_str(), &rule_record); - IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query rule by rule id failed"); - std::string r1; - std::string zone; - rule_record[0].get(NULL, "details", &r1); - rule_record[0].get(NULL, "zone", &zone); - ctx::json rule = r1; - ctx::json event; - rule.get(NULL, CT_RULE_EVENT, &event); + query = QUERY_RULE_BY_RULE_ID; + query += int_to_string(rule_id); + error = (db_manager::execute_sync(query.c_str(), &rule_record))? ERR_NONE : ERR_OPERATION_FAILED; + IF_FAIL_CATCH_TAG(error == ERR_NONE, _E, "Query rule by rule id failed"); + + rule_record[0].get(NULL, "details", &r_details); + jrule = r_details; + jrule.get(NULL, CT_RULE_EVENT, &jevent); // Get event template by rule id - std::string q2 = QUERY_EVENT_TEMPLATE_BY_RULE_ID; - q2 += int_to_string(rule_id); - q2 += ")"; - std::vector etemplate_record; - ret = db_manager::execute_sync(q2.c_str(), &etemplate_record); - IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query event template by rule id failed"); - std::string r2; - etemplate_record[0].get(NULL, "j_template", &r2); - ctx::json etemplate = r2; + query = QUERY_EVENT_TEMPLATE_BY_RULE_ID; + query += int_to_string(rule_id); + query += ")"; + error = (db_manager::execute_sync(query.c_str(), &etemplate_record))? ERR_NONE : ERR_OPERATION_FAILED; + IF_FAIL_CATCH_TAG(error == ERR_NONE, _E, "Query event template by rule id failed"); + etemplate_record[0].get(NULL, "j_template", &etmpl_str); + jetemplate = etmpl_str; // Query name, instance name & template for conditions of the rule - std::string q3 = QUERY_NAME_INSTANCE_NAME_AND_TEMPLATE_BY_RULE_ID_STATEMENT; - q3 += id_str; - std::vector cond_record; - ret = db_manager::execute_sync(q3.c_str(), &cond_record); - IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query condition's names, instance names, templates by rule id failed"); + query = QUERY_NAME_INSTANCE_NAME_AND_TEMPLATE_BY_RULE_ID_STATEMENT; + query += id_str; + error = (db_manager::execute_sync(query.c_str(), &cond_record))? ERR_NONE : ERR_OPERATION_FAILED; + IF_FAIL_CATCH_TAG(error == ERR_NONE, _E, "Query condition's names, instance names, templates by rule id failed"); - ctx::json inst_names; - std::vector::iterator vec_end = cond_record.end(); + vec_end = cond_record.end(); for (std::vector::iterator vec_pos = cond_record.begin(); vec_pos != vec_end; ++vec_pos) { ctx::json elem = *vec_pos; @@ -543,16 +569,17 @@ int ctx::rule_manager::enable_rule(int rule_id) elem.get(NULL, "templates", &temp); ctx::json ctemplate = temp; ctemplate.set(NULL, "instance_name", ciname); - // TODO ctemplate (name, instance_name, attributes) now - it is based on form using when templates are inserted into db when rebooting // For defrule script generation inst_names.set(NULL, cname.c_str(), ciname); if (cname.compare(ciname) != 0) { - if (!clips_h.find_instance(ciname)) { + if (!clips_h->find_instance(ciname)) { std::string makeinst_script = script_generator::generate_makeinstance(&ctemplate); - error = clips_h.make_instance(makeinst_script); - IF_FAIL_RETURN_TAG(error == ERR_NONE, ERR_OPERATION_FAILED, _E, "Add condition instance([%s]) failed", ciname.c_str()); + error = (makeinst_script.length() > 0)? ERR_NONE : ERR_OPERATION_FAILED; + IF_FAIL_CATCH_TAG(error == ERR_NONE, _E, "Make instance script generation failed"); + error = clips_h->make_instance(makeinst_script); + IF_FAIL_CATCH_TAG(error == ERR_NONE, _E, "Add condition instance([%s]) failed", ciname.c_str()); cond_cnt_map[ciname] = 1; } else { @@ -562,28 +589,33 @@ int ctx::rule_manager::enable_rule(int rule_id) } // Subscribe event - std::string ename; - etemplate.get(NULL, "name", &ename); - - //TODO: set the proper zone - error = c_monitor.subscribe(rule_id, ename, event, zone.c_str()); - IF_FAIL_RETURN(error == ERR_NONE, ERR_OPERATION_FAILED); + jetemplate.get(NULL, "name", &ename); + error = c_monitor.subscribe(rule_id, ename, jevent); + IF_FAIL_CATCH(error == ERR_NONE); // Generate defrule script and execute it - std::string script = script_generator::generate_defrule(id_str, etemplate, rule, &inst_names, zone); - error = clips_h.define_rule(script); - IF_FAIL_RETURN_TAG(error == ERR_NONE, ERR_OPERATION_FAILED, _E, "Defrule failed"); + script = script_generator::generate_defrule(id_str, jetemplate, jrule, &inst_names); + error = clips_h->define_rule(script); + IF_FAIL_CATCH_TAG(error == ERR_NONE, _E, "Defrule failed"); // Update db to set 'enabled' - std::string q4 = UPDATE_RULE_ENABLED_STATEMENT; - q4 += id_str; - std::vector record; - ret = db_manager::execute_sync(q4.c_str(), &record); - IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Update db failed"); + query = UPDATE_RULE_ENABLED_STATEMENT; + query += id_str; + error = (db_manager::execute_sync(query.c_str(), &record))? ERR_NONE : ERR_OPERATION_FAILED; + IF_FAIL_CATCH_TAG(error == ERR_NONE, _E, "Update db failed"); + enb_rule_cnt++; _D(YELLOW("Enable Rule%d succeeded"), rule_id); return ERR_NONE; + +CATCH: + if (enb_rule_cnt <= 0) { + enb_rule_cnt = 0; + destroy_clips(); + } + + return error; } std::string ctx::rule_manager::get_instance_name(std::string name, ctx::json& option) @@ -626,15 +658,13 @@ int ctx::rule_manager::disable_rule(int rule_id) // For event with options // Get rule json by rule id; - std::string q1 = QUERY_RULE_AND_ZONE_BY_RULE_ID; + std::string q1 = QUERY_RULE_BY_RULE_ID; q1 += int_to_string(rule_id); std::vector rule_record; ret = db_manager::execute_sync(q1.c_str(), &rule_record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query rule by rule id failed"); std::string r1; - std::string zone; rule_record[0].get(NULL, "details", &r1); - rule_record[0].get(NULL, "zone", &zone); ctx::json rule = r1; ctx::json event; rule.get(NULL, CT_RULE_EVENT, &event); @@ -642,13 +672,13 @@ int ctx::rule_manager::disable_rule(int rule_id) event.get(NULL, CT_RULE_EVENT_ITEM, &ename); // Unsubscribe event - error = c_monitor.unsubscribe(rule_id, ename, event, zone.c_str()); + error = c_monitor.unsubscribe(rule_id, ename, event); IF_FAIL_RETURN(error == ERR_NONE, ERR_OPERATION_FAILED); // Undef rule in clips std::string id_str = int_to_string(rule_id); std::string script = script_generator::generate_undefrule(id_str); - error = clips_h.route_string_command(script); + error = clips_h->route_string_command(script); IF_FAIL_RETURN_TAG(error == ERR_NONE, ERR_OPERATION_FAILED, _E, "Undefrule failed"); // Update db to set 'disabled' @@ -658,7 +688,7 @@ int ctx::rule_manager::disable_rule(int rule_id) ret = db_manager::execute_sync(q2.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Update db failed"); - // Remove condition instances TODO + // Remove condition instances std::string q3 = "SELECT name, instance_name FROM context_trigger_condition WHERE rule_id = "; q3 += id_str; std::vector name_record; @@ -678,7 +708,7 @@ int ctx::rule_manager::disable_rule(int rule_id) cond_cnt_map[ciname]--; if (cond_cnt_map[ciname] == 0) { - error = clips_h.unmake_instance(ciname); + error = clips_h->unmake_instance(ciname); IF_FAIL_RETURN(error == ERR_NONE, error); cond_cnt_map.erase(ciname); @@ -686,6 +716,10 @@ int ctx::rule_manager::disable_rule(int rule_id) } } + if (--enb_rule_cnt <= 0) { + enb_rule_cnt = 0; + destroy_clips(); + } return ERR_NONE; } @@ -710,19 +744,13 @@ void ctx::rule_manager::make_condition_option_based_on_event_data(ctx::json& cte } } -void ctx::rule_manager::on_event_received(std::string item, ctx::json option, ctx::json data, std::string zone) +void ctx::rule_manager::on_event_received(std::string item, ctx::json option, ctx::json data) { _D(YELLOW("Event(%s(%s) - %s) is invoked."), item.c_str(), option.str().c_str(), data.str().c_str()); // TODO: Check permission of an event(item), if permission denied, return int err; bool ret; - // Set the zone - ret = clips_h.set_global_variable_string("zone", zone); - if (!ret) { - _E("Set clips zone to %s failed", zone.c_str()); - return; - } // Generate event fact script std::string q1 = "SELECT j_template FROM context_trigger_template WHERE name = '"; @@ -740,8 +768,6 @@ void ctx::rule_manager::on_event_received(std::string item, ctx::json option, ct std::string e_inst = get_instance_name(item, option); std::string query = QUERY_CONDITION_TEMPLATES_OF_INVOKED_EVENT_STATEMENT; query += e_inst; - query += "' AND zone = '"; - query += zone; query += "'))"; std::vector conds; ret = db_manager::execute_sync(query.c_str(), &conds); @@ -774,9 +800,9 @@ void ctx::rule_manager::on_event_received(std::string item, ctx::json option, ct // TODO: Check permission of a condition(cname), if permission granted, read condition data. (or, condition data should be empty json) - // Get Context Data and Set the proper zone. + // Get Context Data ctx::json condition_data; - err = c_monitor.read(cname, coption, zone.c_str(), &condition_data); + err = c_monitor.read(cname, coption, &condition_data); if (err != ERR_NONE) return; _D(YELLOW("Condition(%s(%s) - %s)."), cname.c_str(), coption.str().c_str(), condition_data.str().c_str()); @@ -784,24 +810,24 @@ void ctx::rule_manager::on_event_received(std::string item, ctx::json option, ct // Generate ModifyInstance script std::string modifyinst_script = script_generator::generate_modifyinstance(ciname, ctemplate, condition_data); - err = clips_h.route_string_command(modifyinst_script); + err = clips_h->route_string_command(modifyinst_script); IF_FAIL_VOID_TAG(err == ERR_NONE, _E, "Modify condition instance failed"); } // Add fact and Run environment - err = clips_h.add_fact(eventfact_str); + err = clips_h->add_fact(eventfact_str); IF_FAIL_VOID_TAG(err == ERR_NONE, _E, "Assert event fact failed"); - err = clips_h.run_environment(); + err = clips_h->run_environment(); IF_FAIL_VOID_TAG(err == ERR_NONE, _E, "Run environment failed"); // Retract event fact std::string retract_command = "(retract *)"; - err = clips_h.route_string_command(retract_command); + err = clips_h->route_string_command(retract_command); IF_FAIL_VOID_TAG(err == ERR_NONE, _E, "Retract event fact failed"); } -static void trigger_action_app_control(ctx::json& action, std::string zone) +static void trigger_action_app_control(ctx::json& action) { int error; std::string appctl_str; @@ -820,16 +846,6 @@ static void trigger_action_app_control(ctx::json& action, std::string zone) app_control_create(&app); app_control_import_from_bundle(app, appctl_bundle); -#if _ZONE_ENABLED_ - char* op; - app_control_get_operation(app, &op); - app_control_set_operation(app, APP_SVC_OPERATION_JUMP); - app_control_add_extra_data(app, APP_SVC_K_JUMP_ZONE_NAME, zone.c_str()); - if (op != NULL) { - app_control_add_extra_data(app, APP_SVC_K_JUMP_ORIGIN_OPERATION, op); - } -#endif - error = app_control_send_launch_request(app, NULL, NULL); if (error != APP_CONTROL_ERROR_NONE) { _E("Launch request failed(%d)", error); @@ -841,7 +857,7 @@ static void trigger_action_app_control(ctx::json& action, std::string zone) app_control_destroy(app); } -static void trigger_action_notification(ctx::json& action, std::string creator, std::string zone) +static void trigger_action_notification(ctx::json& action, std::string creator) { int error; notification_h notification = notification_create(NOTIFICATION_TYPE_NOTI); @@ -899,8 +915,6 @@ static void trigger_action_notification(ctx::json& action, std::string creator, _E("Set pkgname(%s) failed(%d)", creator.c_str(), error); } - ctx::scope_zone_joiner sz(zone.c_str()); - error = notification_post(notification); if (error != NOTIFICATION_ERROR_NONE) { _E("Post notification failed(%d)", error); @@ -920,7 +934,7 @@ void ctx::rule_manager::on_rule_triggered(int rule_id) { _D(YELLOW("Rule%d is triggered"), rule_id); - std::string q = "SELECT details, creator, zone FROM context_trigger_rule WHERE row_id ="; + std::string q = "SELECT details, creator FROM context_trigger_rule WHERE row_id ="; q += int_to_string(rule_id); std::vector record; @@ -931,17 +945,15 @@ void ctx::rule_manager::on_rule_triggered(int rule_id) ctx::json details = details_str; ctx::json action; details.get(NULL, CT_RULE_ACTION, &action); - std::string zone; - record[0].get(NULL, "zone", &zone); std::string type; if (action.get(NULL, CT_RULE_ACTION_TYPE, &type)) { if (type.compare(CT_RULE_ACTION_TYPE_APP_CONTROL) == 0) { - trigger_action_app_control(action, zone); + trigger_action_app_control(action); } else if (type.compare(CT_RULE_ACTION_TYPE_NOTIFICATION) == 0) { std::string creator; record[0].get(NULL, "creator", &creator); - trigger_action_notification(action, creator, zone); + trigger_action_notification(action, creator); } } } @@ -990,7 +1002,6 @@ bool ctx::rule_manager::is_rule_enabled(int rule_id) int ctx::rule_manager::get_rule_by_id(std::string creator, int rule_id, ctx::json* request_result) { -// std::string q = "SELECT description, details FROM context_trigger_rule WHERE (creator = '"; std::string q = "SELECT description FROM context_trigger_rule WHERE (creator = '"; q += creator; q += "') and (row_id = "; @@ -1008,13 +1019,10 @@ int ctx::rule_manager::get_rule_by_id(std::string creator, int rule_id, ctx::jso } std::string description; -// std::string details; record[0].get(NULL, "description", &description); -// record[0].get(NULL, "details", &details); (*request_result).set(NULL, CT_RULE_ID, rule_id); (*request_result).set(NULL, CT_RULE_DESCRIPTION, description); -// (*request_result).set(NULL, CT_RULE_DETAILS, details); return ERR_NONE; } diff --git a/src/context_trigger/rule_manager.h b/src/context_trigger/rule_manager.h index 11da157..b64a658 100644 --- a/src/context_trigger/rule_manager.h +++ b/src/context_trigger/rule_manager.h @@ -31,7 +31,7 @@ namespace ctx { rule_manager(); ~rule_manager(); bool init(ctx::context_trigger* tr, ctx::fact_reader* fr); - int add_rule(std::string creator, ctx::json rule, std::string zone, ctx::json* rule_id); + int add_rule(std::string creator, ctx::json rule, ctx::json* rule_id); int remove_rule(int rule_id); int enable_rule(int rule_id); int disable_rule(int rule_id); @@ -40,24 +40,25 @@ namespace ctx { int check_rule(std::string creator, int rule_id); bool is_rule_enabled(int rule_id); - void on_event_received(std::string item, ctx::json option, ctx::json data, std::string zone); + void on_event_received(std::string item, ctx::json option, ctx::json data); void on_rule_triggered(int rule_id); private: - clips_handler clips_h; + clips_handler* clips_h; context_monitor c_monitor; bool reenable_rule(void); - int verify_rule(ctx::json& rule, const char* app_id, const char* zone); - int64_t get_duplicated_rule(std::string creator, std::string zone, ctx::json& rule); + int verify_rule(ctx::json& rule, const char* app_id); + int64_t get_duplicated_rule(std::string creator, ctx::json& rule); bool rule_data_arr_elem_equals(ctx::json& lelem, ctx::json& relem); bool rule_item_equals(ctx::json& litem, ctx::json& ritem); bool rule_equals(ctx::json& lrule, ctx::json& rrule); std::string get_instance_name(std::string name, ctx::json& condition); void make_condition_option_based_on_event_data(ctx::json& ctemplate, ctx::json& edata, ctx::json* coption); + bool initialize_clips(void); + void destroy_clips(void); std::map cond_cnt_map; // - }; /* class rule_manager */ } /* namespace ctx */ diff --git a/src/context_trigger/script_generator.cpp b/src/context_trigger/script_generator.cpp index d567988..3c8653b 100644 --- a/src/context_trigger/script_generator.cpp +++ b/src/context_trigger/script_generator.cpp @@ -46,7 +46,7 @@ static std::string convert_condition_weekday_weekend(std::string inst_name, std: std::string buf = (day.compare(TIMER_WEEKDAY) == 0)? CONDITION_WEEKDAY : CONDITION_WEEKEND; size_t pos = 0; - while((pos = buf.find("%s", pos)) != std::string::npos) { + while ((pos = buf.find("%s", pos)) != std::string::npos) { buf.replace(pos, 2, inst_name); pos += inst_name.length(); } @@ -83,10 +83,7 @@ std::string ctx::script_generator::generate_deftemplate(ctx::json* item) script += *it; script += " (default null))"; } - script += ")"; - -// _D("Deftemplate script is generated: %s", script.c_str()); - script += "\n"; + script += ")\n"; return script; } @@ -109,10 +106,7 @@ std::string ctx::script_generator::generate_defclass(ctx::json* item) script += attr_name; script += " (create-accessor read-write))"; } - script += ")"; - -// _D("Defclass script is generated: %s", script.c_str()); - script += "\n"; + script += ")\n"; return script; } @@ -141,10 +135,7 @@ std::string ctx::script_generator::generate_makeinstance(ctx::json* item) script += attr_name; script += " 0)"; } - script += ")"; - -// _D("Makeinstance script is generated: %s", script.c_str()); - script += "\n"; + script += ")\n"; return script; } @@ -159,16 +150,13 @@ std::string ctx::script_generator::generate_undefrule(std::string rule_id) return script; } -std::string ctx::script_generator::generate_defrule(std::string rule_id, ctx::json event_template, ctx::json rule, ctx::json* inst_names, std::string zone) +std::string ctx::script_generator::generate_defrule(std::string rule_id, ctx::json event_template, ctx::json rule, ctx::json* inst_names) { std::string script; ctx::json option = NULL; rule.get(CT_RULE_EVENT, CT_RULE_EVENT_OPTION, &option); - script = "(defrule rule"; - script += rule_id; - script += " "; - + script = "(defrule rule" + rule_id + " "; script += generate_initial_fact(event_template, option); script += " => "; @@ -177,13 +165,12 @@ std::string ctx::script_generator::generate_defrule(std::string rule_id, ctx::js if (condition_num > 0) { // case1: condition exists - script += "(if (and (eq ?*zone* \""; - script += zone; - script += "\") "; + script += "(if "; if (eventdata_num > 0) { ctx::json event; rule.get(NULL, CT_RULE_EVENT, &event); + script += "(and "; script += generate_event_data(event); } @@ -194,31 +181,21 @@ std::string ctx::script_generator::generate_defrule(std::string rule_id, ctx::js rule.get(NULL, CT_RULE_OPERATOR, &rule_op); script += generate_condition_data(rule_id, conditions, rule_op, inst_names); - script += ")"; - script += " then "; - script += " (execute_action rule"; - script += rule_id; - script += "))"; + if (eventdata_num > 0) + script += ")"; + + script = script + " then (execute_action rule" + rule_id + "))"; } else if (eventdata_num > 0) { // case2: no conditions, but event data ctx::json event; rule.get(NULL, CT_RULE_EVENT, &event); - script += "(if (and (eq ?*zone* \""; - script += zone; - script += "\") "; + script += "(if "; script += generate_event_data(event); - script += ") then "; - script += " (execute_action rule"; - script += rule_id; - script += "))"; + script = script + " then (execute_action rule" + rule_id + "))"; } else { // case3: only action - script += "if (eq ?*zone* \""; - script += zone; - script += "\") then (execute_action rule"; - script += rule_id; - script += ")"; + script = script + " (execute_action rule" + rule_id + ")"; } script += ")"; @@ -267,7 +244,6 @@ std::string generate_initial_fact(ctx::json event_template, ctx::json option) } script += ")"; -// _D("Initial event fact is %s", script.c_str()); return script; } @@ -505,7 +481,6 @@ std::string ctx::script_generator::generate_modifyinstance(std::string instance_ } else if (data.get(NULL, key.c_str(), &value)) { // integer type data script += "(" + key + " " + int_to_string(value) + ")"; } - // else continue; } script += ")"; diff --git a/src/context_trigger/script_generator.h b/src/context_trigger/script_generator.h index 4185ade..3b1a4c6 100644 --- a/src/context_trigger/script_generator.h +++ b/src/context_trigger/script_generator.h @@ -25,7 +25,7 @@ namespace ctx { std::string generate_defclass(ctx::json* item); std::string generate_makeinstance(ctx::json* item); std::string generate_undefrule(std::string rule_id); - std::string generate_defrule(std::string rule_id, ctx::json event_template, ctx::json rule, ctx::json* inst_names, std::string zone); + std::string generate_defrule(std::string rule_id, ctx::json event_template, ctx::json rule, ctx::json* inst_names); std::string generate_fact(std::string item_name, ctx::json event_template, ctx::json option, ctx::json data); std::string generate_modifyinstance(std::string instance_name, ctx::json condition_template, ctx::json data); diff --git a/src/context_trigger/timer.cpp b/src/context_trigger/timer.cpp index de19e77..152af58 100644 --- a/src/context_trigger/timer.cpp +++ b/src/context_trigger/timer.cpp @@ -34,9 +34,8 @@ ctx::trigger_timer::ref_count_array_s::ref_count_array_s() memset(count, 0, sizeof(int) * MAX_DAY); } -ctx::trigger_timer::trigger_timer(ctx::context_trigger* tr, std::string z) +ctx::trigger_timer::trigger_timer(ctx::context_trigger* tr) : trigger(tr) - , zone(z) { } @@ -167,14 +166,14 @@ bool ctx::trigger_timer::on_timer_expired(int timer_id, void* user_data) void ctx::trigger_timer::on_timer_expired(int hour, int min, int day_of_week) { - _I("[Timer-%s] Time: %02d:%02d, Day of Week: %#x", zone.c_str(), hour, min, day_of_week); + _I("Time: %02d:%02d, Day of Week: %#x", hour, min, day_of_week); ctx::json result; result.set(NULL, TIMER_RESPONSE_KEY_TIME_OF_DAY, hour * 60 + min); result.set(NULL, TIMER_RESPONSE_KEY_DAY_OF_WEEK, convert_day_of_week_to_string(day_of_week)); ctx::json dummy = NULL; - trigger->push_fact(TIMER_EVENT_REQ_ID, ERR_NONE, TIMER_EVENT_SUBJECT, dummy, result, zone.c_str()); + trigger->push_fact(TIMER_EVENT_REQ_ID, ERR_NONE, TIMER_EVENT_SUBJECT, dummy, result); } int ctx::trigger_timer::get_day_of_month() diff --git a/src/context_trigger/timer.h b/src/context_trigger/timer.h index f1b9787..8914bb7 100644 --- a/src/context_trigger/timer.h +++ b/src/context_trigger/timer.h @@ -43,7 +43,6 @@ namespace ctx { typedef std::map timer_state_map_t; ctx::context_trigger *trigger; - std::string zone; ref_count_map_t ref_count_map; timer_state_map_t timer_state_map; @@ -55,7 +54,7 @@ namespace ctx { bool on_timer_expired(int timer_id, void *user_data); public: - trigger_timer(ctx::context_trigger *tr, std::string z); + trigger_timer(ctx::context_trigger *tr); ~trigger_timer(); bool add(int minute, int day_of_week); diff --git a/src/context_trigger/trigger.cpp b/src/context_trigger/trigger.cpp index 568ca7c..32eb2f4 100644 --- a/src/context_trigger/trigger.cpp +++ b/src/context_trigger/trigger.cpp @@ -53,6 +53,9 @@ void ctx::context_trigger::release() // This function is called from the main thread during the service termination process. delete _reader; _reader = NULL; + + delete rule_mgr; + rule_mgr = NULL; } void ctx::context_trigger::on_thread_event_popped(int type, void* data) @@ -138,9 +141,9 @@ void ctx::context_trigger::process_request(ctx::request_info* request) } } -void ctx::context_trigger::push_fact(int req_id, int error, const char* subject, ctx::json& option, ctx::json& data, const char* zone) +void ctx::context_trigger::push_fact(int req_id, int error, const char* subject, ctx::json& option, ctx::json& data) { - context_fact *fact = new(std::nothrow) context_fact(req_id, error, subject, option, data, zone); + context_fact *fact = new(std::nothrow) context_fact(req_id, error, subject, option, data); IF_FAIL_VOID_TAG(fact, _E, "Memory allocation failed"); push_thread_event(ETYPE_FACT, fact); @@ -149,7 +152,7 @@ void ctx::context_trigger::push_fact(int req_id, int error, const char* subject, void ctx::context_trigger::process_fact(ctx::context_fact* fact) { // Process the context fact. - rule_mgr->on_event_received(fact->get_subject(), fact->get_option(), fact->get_data(), fact->get_zone_name()); + rule_mgr->on_event_received(fact->get_subject(), fact->get_option(), fact->get_data()); } void ctx::context_trigger::process_initialize(void) @@ -174,13 +177,7 @@ void ctx::context_trigger::add_rule(ctx::request_info* request) return; } - const char* zone_name = request->get_zone_name(); - if (zone_name == NULL) { - request->reply(ERR_OPERATION_FAILED); - return; - } - - int error = rule_mgr->add_rule(app_id, request->get_description(), zone_name, &rule_id); + int error = rule_mgr->add_rule(app_id, request->get_description(), &rule_id); _I("'%s' adds a rule (Error: %#x)", request->get_client(), error); request->reply(error, rule_id); diff --git a/src/context_trigger/trigger.h b/src/context_trigger/trigger.h index c1a7173..1d4ba2a 100644 --- a/src/context_trigger/trigger.h +++ b/src/context_trigger/trigger.h @@ -36,10 +36,7 @@ namespace ctx { void release(); bool assign_request(ctx::request_info* request); - void push_fact(int req_id, int error, const char* subject, ctx::json& option, ctx::json& data, const char* zone); - bool publish(int rule_id, int error, ctx::json data); - bool _publish(int rule_id, int error, ctx::json data); - + void push_fact(int req_id, int error, const char* subject, ctx::json& option, ctx::json& data); private: enum event_type_e { diff --git a/src/dbus_server_impl.cpp b/src/dbus_server_impl.cpp index 1674986..23f20e4 100644 --- a/src/dbus_server_impl.cpp +++ b/src/dbus_server_impl.cpp @@ -23,7 +23,6 @@ #include "server.h" #include "client_request.h" #include "access_control/peer_creds.h" -#include "zone_util_impl.h" #include "dbus_server_impl.h" static bool conn_acquired = false; @@ -94,8 +93,7 @@ static void handle_request(const char *sender, GVariant *param, GDBusMethodInvoc std::string smack_label; pid_t pid; - if (ctx::peer_creds::get(dbus_connection, sender, smack_label, pid) && - request->set_peer_creds(smack_label.c_str(), ctx::zone_util::get_name_by_pid(pid))) { + if (ctx::peer_creds::get(dbus_connection, sender, smack_label, pid) && request->set_peer_creds(smack_label.c_str())) { ctx::server::send_request(request); return; } diff --git a/src/request.cpp b/src/request.cpp index 9215302..6b9e51c 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -41,11 +41,6 @@ int ctx::request_info::get_id() return _req_id; } -const char* ctx::request_info::get_zone_name() -{ - return _zone_name.c_str(); -} - const char* ctx::request_info::get_client() { return _client.c_str(); diff --git a/src/request.h b/src/request.h index f0e6586..a11487f 100644 --- a/src/request.h +++ b/src/request.h @@ -29,7 +29,6 @@ namespace ctx { int get_type(); int get_id(); - const char* get_zone_name(); const char* get_client(); const char* get_subject(); ctx::json& get_description(); @@ -43,7 +42,6 @@ namespace ctx { protected: int _type; int _req_id; - std::string _zone_name; std::string _client; std::string _subject; ctx::json _description; diff --git a/src/server.cpp b/src/server.cpp index 385e76a..82432fe 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -24,7 +24,6 @@ #include "db_mgr_impl.h" #include "timer_mgr_impl.h" #include "context_mgr_impl.h" -#include "zone_util_impl.h" #include "access_control/privilege.h" #include "context_trigger/trigger.h" #include "server.h" @@ -75,10 +74,6 @@ void ctx::server::activate() bool result = false; - _I("Init vasum context"); - result = ctx::zone_util::init(); - IF_FAIL_CATCH_TAG(result, _E, "Vasum context initialization failed"); - _I("Init access control configuration"); result = ctx::privilege_manager::init(); IF_FAIL_CATCH_TAG(result, _E, "Access controller initialization failed"); @@ -151,9 +146,6 @@ void ctx::server::release() _I("Release Access control configuration"); ctx::privilege_manager::release(); - _I("Release Vasum context"); - ctx::zone_util::release(); - #ifdef _USE_ECORE_MAIN_LOOP_ ecore_shutdown(); #else diff --git a/src/zone_util_impl.cpp b/src/zone_util_impl.cpp deleted file mode 100644 index aa0636b..0000000 --- a/src/zone_util_impl.cpp +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#ifdef _ZONE_ENABLED_ -#include -#endif -#include -#include -#include "zone_util_impl.h" - -#define HOST_NAME "" - -#if _ZONE_ENABLED_ -static bool container_enabled = false; -static vsm_context_h _vsm_ctx = NULL; -#endif -static ctx::zone_util_impl* _instance = NULL; - -const char* ctx::zone_util_impl::default_zone() -{ -#if _ZONE_ENABLED_ - if (container_enabled) - return VSM_DEFAULT_ZONE; -#endif - - return HOST_NAME; -} - -void* ctx::zone_util_impl::join_by_name(const char* name) -{ -#if _ZONE_ENABLED_ - IF_FAIL_RETURN(container_enabled, NULL); - IF_FAIL_RETURN_TAG(_vsm_ctx, NULL, _E, "Not initialized"); - - if (name == NULL) { - _D("NULL zone name. The default zone will be used."); - name = VSM_DEFAULT_ZONE; - } - - vsm_zone_h target_zone = vsm_lookup_zone_by_name(_vsm_ctx, name); - IF_FAIL_RETURN_TAG(target_zone, NULL, _E, RED("Zone lookup failed")); - - vsm_zone_h current_zone = vsm_lookup_zone_by_pid(_vsm_ctx, getpid()); - IF_FAIL_RETURN_TAG(current_zone, NULL, _E, RED("Zone lookup failed")); - IF_FAIL_RETURN_TAG(target_zone != current_zone, NULL, _I, YELLOW("Already in the target zone %s"), name); - - _I(YELLOW("Joining to '%s'"), name); - return vsm_join_zone(target_zone); -#else - return NULL; -#endif -} - -void* ctx::zone_util_impl::join_to_zone(void* zone) -{ -#if _ZONE_ENABLED_ - IF_FAIL_RETURN(container_enabled, NULL); - IF_FAIL_RETURN_TAG(_vsm_ctx, NULL, _E, "Not initialized"); - IF_FAIL_RETURN(zone, NULL); - vsm_zone_h target = static_cast(zone); - _I(YELLOW("Joining to '%s'"), vsm_get_zone_name(target)); - return vsm_join_zone(target); -#else - return NULL; -#endif -} - -bool ctx::zone_util::init() -{ -#if _ZONE_ENABLED_ - system_info_get_platform_bool("tizen.org/feature/container", &container_enabled); - IF_FAIL_RETURN_TAG(_instance == NULL, true, _W, "Re-initialization"); -#endif - - _instance = new(std::nothrow) zone_util_impl(); - IF_FAIL_RETURN_TAG(_instance, false, _E, "Memory allocation failed"); - -#if _ZONE_ENABLED_ - if (container_enabled) { - _vsm_ctx = vsm_create_context(); - if (!_vsm_ctx) { - delete _instance; - _E("Memory allocation failed"); - return false; - } - } -#endif - - zone_util::set_instance(_instance); - return true; -} - -void ctx::zone_util::release() -{ - zone_util::set_instance(NULL); - - delete _instance; - _instance = NULL; - -#if _ZONE_ENABLED_ - if (_vsm_ctx) - vsm_cleanup_context(_vsm_ctx); - - _vsm_ctx = NULL; -#endif -} - -const char* ctx::zone_util::get_name_by_pid(pid_t pid) -{ -#if _ZONE_ENABLED_ - IF_FAIL_RETURN(container_enabled, HOST_NAME); - IF_FAIL_RETURN_TAG(_vsm_ctx, NULL, _E, "Not initialized"); - vsm_zone_h zn = vsm_lookup_zone_by_pid(_vsm_ctx, pid); - return vsm_get_zone_name(zn); -#else - return HOST_NAME; -#endif -} diff --git a/src/zone_util_impl.h b/src/zone_util_impl.h deleted file mode 100644 index 4ae9bcb..0000000 --- a/src/zone_util_impl.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __CONTEXT_VASUM_ZONE_UTIL_IMPL_H__ -#define __CONTEXT_VASUM_ZONE_UTIL_IMPL_H__ - -#include -#include -#include - -namespace ctx { - - class zone_util_impl : public zone_util_iface { - public: - zone_util_impl() {} - ~zone_util_impl() {} - void* join_by_name(const char* name); - void* join_to_zone(void* zone); - const char* default_zone(); - }; - - namespace zone_util { - bool init(); - void release(); - const char* get_name_by_pid(pid_t pid); - } - -} /* namespace ctx */ - -#endif /* End of __CONTEXT_VASUM_ZONE_UTIL_IMPL_H__ */