Name: context-service
Summary: Context-Service
-Version: 0.5.0
+Version: 0.5.3
Release: 1
Group: System/Service
License: Apache-2.0
<manifest xmlns="http://tizen.org/ns/packages"
package="context-service"
version="0.0.0"
+ api-version="3.0"
install-location="internal-only"
type="rpm">
<label>Context Service</label>
</service-application>
<privileges>
<privilege>http://tizen.org/privilege/location</privilege>
- <privilege>http://tizen.org/privilege/packagemanager.info</privilege>
</privileges>
</manifest>
#include <app_manager.h>
#include <types_internal.h>
#include <dbus_server.h>
-#include "zone_util_impl.h"
#include "dbus_server_impl.h"
#include "client_request.h"
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;
}
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);
#include <provider_iface.h>
#include "server.h"
#include "context_mgr_impl.h"
-#include "zone_util_impl.h"
#include "access_control/privilege.h"
/* Analyzer Headers */
}
}
-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);
}
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
}
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;
}
}
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");
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);
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());
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);
}
// 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;
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);
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);
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");
_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;
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);
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;
}
};
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");
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);
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);
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<context_provider_iface*> provider_list_t;
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 */
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;
}
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);
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) {
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;
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);
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);
#include "timer_types.h"
static ctx::fact_reader *reader = NULL;
-typedef std::map<std::string, ctx::trigger_timer> timer_map_t;
-static timer_map_t timer_map; // <zone_name, timer>
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;
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]);
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);
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();
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<std::string, ctx::trigger_timer>(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;
// 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;
// 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());
}
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<std::string, ctx::trigger_timer>::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<int, int> request_map; // <rule_id, fact_read_req_id>
- std::map<int, int> read_req_cnt_map; // <fact_read_req_id, count>
- 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<int, int> request_map; // <rule_id, fact_read_req_id>
+ std::map<int, int> read_req_cnt_map; // <fact_read_req_id, count>
+ ctx::context_trigger* trigger;
+ ctx::trigger_timer* timer;
+
+ }; /* class context_monitor */
} /* namespace ctx */
{
}
-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()
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;
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;
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();
};
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) {
}
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;
}
}
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) {
}
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;
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;
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);
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);
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);
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);
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);
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);
}
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;
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_info_s*> 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);
#include <types_internal.h>
#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()
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);
}
{
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;
-}
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);
#include <context_trigger_types_internal.h>
#include <context_trigger.h>
#include <db_mgr.h>
-#include <zone_util.h>
#include "../access_control/privilege.h"
#include "rule_manager.h"
#include "script_generator.h"
#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"
#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 = "
#define EVENT_KEY_PREFIX "?"
static ctx::context_trigger* trigger = NULL;
+static int enb_rule_cnt = 0;
static int string_to_int(std::string str)
{
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);
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<json> 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<json> 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
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)
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);
}
}
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<json> record;
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);
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())) {
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());
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;
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");
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");
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");
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<json> rule_record;
+ std::vector<json> etemplate_record;
+ std::vector<json> cond_record;
+ std::vector<json> record;
+ std::vector<json>::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<json> 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<json> 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<json> 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<json>::iterator vec_end = cond_record.end();
+ vec_end = cond_record.end();
for (std::vector<json>::iterator vec_pos = cond_record.begin(); vec_pos != vec_end; ++vec_pos) {
ctx::json elem = *vec_pos;
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 {
}
// 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<json> 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)
// 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<json> 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);
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'
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<json> name_record;
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);
}
}
+ if (--enb_rule_cnt <= 0) {
+ enb_rule_cnt = 0;
+ destroy_clips();
+ }
return ERR_NONE;
}
}
}
-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 = '";
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<json> conds;
ret = db_manager::execute_sync(query.c_str(), &conds);
// 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());
// 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;
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);
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);
_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);
{
_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<json> record;
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);
}
}
}
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 = ";
}
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;
}
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);
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<std::string, int> cond_cnt_map; // <condition instance name, count>
-
}; /* class rule_manager */
} /* namespace ctx */
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();
}
script += *it;
script += " (default null))";
}
- script += ")";
-
-// _D("Deftemplate script is generated: %s", script.c_str());
- script += "\n";
+ script += ")\n";
return script;
}
script += attr_name;
script += " (create-accessor read-write))";
}
- script += ")";
-
-// _D("Defclass script is generated: %s", script.c_str());
- script += "\n";
+ script += ")\n";
return script;
}
script += attr_name;
script += " 0)";
}
- script += ")";
-
-// _D("Makeinstance script is generated: %s", script.c_str());
- script += "\n";
+ script += ")\n";
return script;
}
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 += " => ";
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);
}
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 += ")";
}
script += ")";
-// _D("Initial event fact is %s", script.c_str());
return script;
}
} else if (data.get(NULL, key.c_str(), &value)) { // integer type data
script += "(" + key + " " + int_to_string(value) + ")";
}
- // else continue;
}
script += ")";
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);
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)
{
}
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()
typedef std::map<int, timer_state_s> 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;
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);
// 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)
}
}
-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);
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)
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);
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 {
#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;
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;
}
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();
int get_type();
int get_id();
- const char* get_zone_name();
const char* get_client();
const char* get_subject();
ctx::json& get_description();
protected:
int _type;
int _req_id;
- std::string _zone_name;
std::string _client;
std::string _subject;
ctx::json _description;
#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"
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");
_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
+++ /dev/null
-/*
- * 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 <unistd.h>
-#ifdef _ZONE_ENABLED_
-#include <vasum.h>
-#endif
-#include <system_info.h>
-#include <types_internal.h>
-#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<vsm_zone_h>(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
-}
+++ /dev/null
-/*
- * 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 <sys/types.h>
-#include <zone_util.h>
-#include <zone_util_iface.h>
-
-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__ */