From: Mu-Woong Lee Date: Fri, 20 May 2016 11:04:49 +0000 (+0900) Subject: Remove request_handler from the API library implementation X-Git-Tag: submit/tizen/20160523.074005^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e251949c58ca4312ac4fa4794e8949337c74c95d;p=platform%2Fcore%2Fapi%2Fcontext.git Remove request_handler from the API library implementation API implementations use DBusClient directly, and request_handler is moved to the testsuite. Change-Id: Ib897e2c3a335a2f6587139b9780bcf31c1c6dc6f Signed-off-by: Mu-Woong Lee --- diff --git a/src/context_history.cpp b/src/context_history.cpp index 8db3ba6..3f90f42 100644 --- a/src/context_history.cpp +++ b/src/context_history.cpp @@ -18,7 +18,7 @@ #include #include #include -#include "request_handler.h" +#include "DBusClient.h" #define TYPE_INT 0 #define TYPE_STRING 1 @@ -153,7 +153,9 @@ SO_EXPORT int context_history_get_list(context_history_h handle, context_history int req_id; ctx::Json tmp_list; - int err = ctx::request_handler::read_sync(data_type_str.c_str(), (filter)? &filter->jfilter : NULL, &req_id, &tmp_list); + ctx::DBusClient dbusClient; + + int err = dbusClient.readSync(data_type_str, filter ? filter->jfilter : NULL, &req_id, &tmp_list); IF_FAIL_RETURN_TAG(err == ERR_NONE, err, _E, "Getting list failed"); _J("Read response", tmp_list); diff --git a/src/context_trigger.cpp b/src/context_trigger.cpp index 7ad7df5..2cc7262 100644 --- a/src/context_trigger.cpp +++ b/src/context_trigger.cpp @@ -26,7 +26,7 @@ #include #include #include -#include "request_handler.h" +#include "DBusClient.h" #include "rule_validator.h" #define INITIAL_RULE "{ \"ID\" : -1, \"DESCRIPTION\" : \"\", \"DETAILS\" : { } }" @@ -35,6 +35,8 @@ #define EVENT_DATA_KEY_PREFIX_STR std::string("?") //#define DOUBLE_PRECISION 2 +static ctx::DBusClient __dbusClient; + static int context_trigger_rule_event_create_internal(const char* event_item, context_trigger_logical_type_e logical_type, context_trigger_rule_entry_h* entry, bool is_custom = false); static int context_trigger_rule_condition_create_internal(const char* condition_item, context_trigger_logical_type_e logical_type, context_trigger_rule_entry_h* entry, bool is_custom = false); static std::string convert_event_to_string(context_trigger_event_e item); @@ -86,7 +88,7 @@ SO_EXPORT int context_trigger_add_rule(context_trigger_rule_h rule, int* rule_id return CONTEXT_TRIGGER_ERROR_INVALID_RULE; ctx::Json jrule_id; - int error = ctx::request_handler::write_with_reply(CONTEXT_TRIGGER_SUBJECT_ADD, &(rule->jrule), &jrule_id); + int error = __dbusClient.write(CONTEXT_TRIGGER_SUBJECT_ADD, rule->jrule, &jrule_id); if (error == ERR_NONE) { jrule_id.get(NULL, CT_RULE_ID, rule_id); @@ -105,7 +107,7 @@ SO_EXPORT int context_trigger_remove_rule(int rule_id) ctx::Json jrule_id; jrule_id.set(NULL, CT_RULE_ID, rule_id); - int error = ctx::request_handler::write_with_reply(CONTEXT_TRIGGER_SUBJECT_REMOVE, &jrule_id, NULL); + int error = __dbusClient.write(CONTEXT_TRIGGER_SUBJECT_REMOVE, jrule_id, NULL); if (error == ERR_ALREADY_STARTED) { // Rule is still enabled. return CONTEXT_TRIGGER_ERROR_RULE_ENABLED; @@ -127,7 +129,7 @@ SO_EXPORT int context_trigger_enable_rule(int rule_id) jrule_id.set(NULL, CT_RULE_ID, rule_id); int req_id; // Useless in context_trigger - int error = ctx::request_handler::subscribe(CONTEXT_TRIGGER_SUBJECT_ENABLE, &jrule_id, &req_id, NULL); + int error = __dbusClient.subscribe(CONTEXT_TRIGGER_SUBJECT_ENABLE, jrule_id, &req_id, NULL); if (error == ERR_NO_DATA) { return CONTEXT_TRIGGER_ERROR_RULE_NOT_EXIST; @@ -145,7 +147,7 @@ SO_EXPORT int context_trigger_disable_rule(int rule_id) ctx::Json jrule_id; jrule_id.set(NULL, CT_RULE_ID, rule_id); - int error = ctx::request_handler::write_with_reply(CONTEXT_TRIGGER_SUBJECT_DISABLE, &jrule_id, NULL); + int error = __dbusClient.write(CONTEXT_TRIGGER_SUBJECT_DISABLE, jrule_id, NULL); if (error == ERR_NO_DATA) { return CONTEXT_TRIGGER_ERROR_RULE_NOT_EXIST; @@ -161,7 +163,7 @@ SO_EXPORT int context_trigger_get_own_rule_ids(int** enabled_rule_ids, int* enab int req_id; ctx::Json data_read; - int error = ctx::request_handler::read_sync(CONTEXT_TRIGGER_SUBJECT_GET_RULE_IDS, NULL, &req_id, &data_read); + int error = __dbusClient.readSync(CONTEXT_TRIGGER_SUBJECT_GET_RULE_IDS, NULL, &req_id, &data_read); if (error != ERR_NONE) { return error; @@ -212,7 +214,7 @@ SO_EXPORT int context_trigger_get_rule_by_id(int rule_id, context_trigger_rule_h int req_id; ctx::Json data_read; - int error = ctx::request_handler::read_sync(CONTEXT_TRIGGER_SUBJECT_GET, &option, &req_id, &data_read); + int error = __dbusClient.readSync(CONTEXT_TRIGGER_SUBJECT_GET, option, &req_id, &data_read); if (error == ERR_NO_DATA) { return CONTEXT_TRIGGER_ERROR_RULE_NOT_EXIST; @@ -352,11 +354,11 @@ SO_EXPORT int context_trigger_rule_set_action_app_control(context_trigger_rule_h int error; // Privilege check - error = ctx::request_handler::call(METHOD_CHK_PRIV_APPLAUNCH); + error = __dbusClient.call(METHOD_CHK_PRIV_APPLAUNCH); IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Privilege checking failed (%#x)", error); if (is_call_operation(app_control)) { - error = ctx::request_handler::call(METHOD_CHK_PRIV_CALL); + error = __dbusClient.call(METHOD_CHK_PRIV_CALL); IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Privilege checking failed (%#x)", error); } @@ -414,7 +416,7 @@ SO_EXPORT int context_trigger_rule_set_action_notification(context_trigger_rule_ ASSERT_NOT_NULL(rule && title && content); // Privilege check - int error = ctx::request_handler::call(METHOD_CHK_PRIV_NOTIFICATION); + int error = __dbusClient.call(METHOD_CHK_PRIV_NOTIFICATION); IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Privilege checking failed (%#x)", error); // if action arleady exists @@ -582,7 +584,7 @@ SO_EXPORT int context_trigger_rule_event_is_supported(context_trigger_event_e ev return CONTEXT_TRIGGER_ERROR_INVALID_PARAMETER; } - int error = ctx::request_handler::is_supported(eitem_str.c_str()); + int error = __dbusClient.isSupported(eitem_str); if (error == ERR_NONE) *supported = true; @@ -657,7 +659,7 @@ SO_EXPORT int context_trigger_rule_condition_is_supported(context_trigger_condit return CONTEXT_TRIGGER_ERROR_INVALID_PARAMETER; } - int error = ctx::request_handler::is_supported(citem_str.c_str()); + int error = __dbusClient.isSupported(citem_str); if (error == ERR_NONE) *supported = true; @@ -929,7 +931,7 @@ SO_EXPORT int context_trigger_add_custom_item(const char* name, const char* attr data.set(NULL, CT_CUSTOM_NAME, name); data.set(NULL, CT_CUSTOM_ATTRIBUTES, jattr_template); - int error = ctx::request_handler::write_with_reply(CONTEXT_TRIGGER_SUBJECT_CUSTOM_ADD, &data, NULL); + int error = __dbusClient.write(CONTEXT_TRIGGER_SUBJECT_CUSTOM_ADD, data, NULL); IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Failed to add custom item: %#x", error); return error; @@ -944,7 +946,7 @@ SO_EXPORT int context_trigger_remove_custom_item(const char* name) data.set(NULL, CT_CUSTOM_NAME, name); ctx::Json subj; - int error = ctx::request_handler::write_with_reply(CONTEXT_TRIGGER_SUBJECT_CUSTOM_REMOVE, &data, &subj); + int error = __dbusClient.write(CONTEXT_TRIGGER_SUBJECT_CUSTOM_REMOVE, data, &subj); IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Failed to remove custom item: %#x", error); std::string subject; @@ -967,7 +969,7 @@ SO_EXPORT int context_trigger_publish_custom_item(const char* name, const char* data.set(NULL, CT_CUSTOM_NAME, name); data.set(NULL, CT_CUSTOM_FACT, jfact); - int error = ctx::request_handler::write_with_reply(CONTEXT_TRIGGER_SUBJECT_CUSTOM_PUBLISH, &data, NULL); + int error = __dbusClient.write(CONTEXT_TRIGGER_SUBJECT_CUSTOM_PUBLISH, data, NULL); IF_FAIL_RETURN_TAG(error == ERR_NONE, error, _E, "Failed to publish custom data"); return error; diff --git a/src/request_handler.cpp b/src/request_handler.cpp deleted file mode 100644 index b827d98..0000000 --- a/src/request_handler.cpp +++ /dev/null @@ -1,99 +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. - */ - -/* TODO: Remove this request_handler. All APIs and the testsuite should use DBusClient directly. */ - -#include -#include - -#include -#include -#include "DBusClient.h" -#include "request_handler.h" - -using namespace ctx; - -class DBusClientListenerImpl : public IDBusClientListener { -public: - DBusClientListenerImpl() {} - ~DBusClientListenerImpl() {} - void setCb(std::string subject, request_handler::subject_response_cb cb); - void onPublish(std::string subject, int reqId, int error, Json event); -private: - std::map __callbackMap; -}; - -void DBusClientListenerImpl::setCb(std::string subject, request_handler::subject_response_cb cb) -{ - __callbackMap[subject] = cb; -} - -void DBusClientListenerImpl::onPublish(std::string subject, int reqId, int error, Json event) -{ - auto it = __callbackMap.find(subject); - IF_FAIL_VOID_TAG(it != __callbackMap.end(), _W, "Unregistered subject"); - it->second(subject.c_str(), reqId, error, event); -} - -static DBusClientListenerImpl __dbusListener; -static DBusClient __dbusClient; - -SO_EXPORT int ctx::request_handler::subscribe(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result) -{ - return __dbusClient.subscribe(subject, option ? *option : NULL, req_id, request_result); -} - -SO_EXPORT int ctx::request_handler::unsubscribe(const char* subject, int req_id) -{ - return __dbusClient.unsubscribe(subject, req_id); -} - -SO_EXPORT int ctx::request_handler::read(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result) -{ - return __dbusClient.read(subject, option ? *option : NULL, req_id, request_result); -} - -SO_EXPORT int ctx::request_handler::read_sync(const char* subject, ctx::Json* option, int* req_id, ctx::Json* data_read) -{ - return __dbusClient.readSync(subject, option ? *option : NULL, req_id, data_read); -} - -SO_EXPORT int ctx::request_handler::write(const char* subject, ctx::Json* data) -{ - return __dbusClient.write(subject, *data); -} - -SO_EXPORT int ctx::request_handler::write_with_reply(const char* subject, ctx::Json* data, ctx::Json* request_result) -{ - return __dbusClient.write(subject, *data, request_result); -} - -SO_EXPORT int ctx::request_handler::is_supported(const char* subject) -{ - return __dbusClient.isSupported(subject); -} - -SO_EXPORT int ctx::request_handler::call(const char* method) -{ - return __dbusClient.call(method); -} - -SO_EXPORT bool ctx::request_handler::register_callback(const char* subject, subject_response_cb callback) -{ - __dbusListener.setCb(subject, callback); - __dbusClient.addListener(subject, &__dbusListener); - return true; -} diff --git a/src/request_handler.h b/src/request_handler.h deleted file mode 100644 index 3d7fa41..0000000 --- a/src/request_handler.h +++ /dev/null @@ -1,64 +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_LIB_REQUEST_HANDLER_H__ -#define __CONTEXT_LIB_REQUEST_HANDLER_H__ - -#include - -namespace ctx { namespace request_handler { - - /* - */ - typedef void(* subject_response_cb)(const char* subject, int req_id, int error, ctx::Json response); - - /* - */ - bool register_callback(const char* subject, subject_response_cb callback); - - /* - */ - int subscribe(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result = NULL); - - /* - */ - int unsubscribe(const char* subject, int req_id); - - /* - */ - int read(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result = NULL); - - /* - */ - int read_sync(const char* subject, ctx::Json* option, int* req_id, ctx::Json* data_read); - - /* - */ - int write(const char* subject, ctx::Json* data); - - /* - */ - int write_with_reply(const char* subject, ctx::Json* data, ctx::Json* request_result = NULL); - - /* - */ - int is_supported(const char* subject); - - int call(const char* method); - -} } /* namespace ctx::request_handler */ - -#endif // __CONTEXT_LIB_REQUEST_HANDLER_H__ diff --git a/src/rule_validator.cpp b/src/rule_validator.cpp index 09aa833..dfee6fe 100644 --- a/src/rule_validator.cpp +++ b/src/rule_validator.cpp @@ -20,7 +20,7 @@ #include #include #include -#include "request_handler.h" +#include "DBusClient.h" #include "rule_validator.h" #define KEY_TEMPLATE "templates" @@ -72,7 +72,8 @@ int ctx::rule_validator::request_template(std::string name, bool mandatory) int req_id; ctx::Json tmpl; - int error = ctx::request_handler::read_sync(CONTEXT_TRIGGER_SUBJECT_GET_TEMPLATE, &request, &req_id, &tmpl); + ctx::DBusClient dbusClient; + int error = dbusClient.readSync(CONTEXT_TRIGGER_SUBJECT_GET_TEMPLATE, request, &req_id, &tmpl); if (error == ERR_NOT_SUPPORTED) { template_map.erase(name); _E("Failed to get request: not supported"); diff --git a/testsuite/CMakeLists.txt b/testsuite/CMakeLists.txt index 30e85d2..514be5f 100644 --- a/testsuite/CMakeLists.txt +++ b/testsuite/CMakeLists.txt @@ -4,6 +4,7 @@ SET(TEST_EXEC context-test) # Source List FILE(GLOB_RECURSE TEST_SRCS src/*.cpp) +SET(TEST_SRCS ${TEST_SRCS} ../src/DBusClient.cpp) INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src ) diff --git a/testsuite/src/trigger/condition.cpp b/testsuite/src/trigger/condition.cpp index 4cadf47..5ee470f 100644 --- a/testsuite/src/trigger/condition.cpp +++ b/testsuite/src/trigger/condition.cpp @@ -14,12 +14,12 @@ * limitations under the License. */ -#include #include #include #include #include "../shared.h" +#include "request_handler.h" #include "condition.h" using namespace ctx::test; diff --git a/testsuite/src/trigger/event.cpp b/testsuite/src/trigger/event.cpp index 9d843c8..0dee837 100644 --- a/testsuite/src/trigger/event.cpp +++ b/testsuite/src/trigger/event.cpp @@ -15,11 +15,11 @@ */ #include -#include #include #include #include "../shared.h" +#include "request_handler.h" #include "event.h" using namespace ctx::test; diff --git a/testsuite/src/trigger/request_handler.cpp b/testsuite/src/trigger/request_handler.cpp new file mode 100644 index 0000000..6969aff --- /dev/null +++ b/testsuite/src/trigger/request_handler.cpp @@ -0,0 +1,99 @@ +/* + * 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. + */ + +/* TODO: Remove this request_handler. All APIs and the testsuite should use DBusClient directly. */ + +#include +#include + +#include +#include +#include +#include "request_handler.h" + +using namespace ctx; + +class DBusClientListenerImpl : public IDBusClientListener { +public: + DBusClientListenerImpl() {} + ~DBusClientListenerImpl() {} + void setCb(std::string subject, request_handler::subject_response_cb cb); + void onPublish(std::string subject, int reqId, int error, Json event); +private: + std::map __callbackMap; +}; + +void DBusClientListenerImpl::setCb(std::string subject, request_handler::subject_response_cb cb) +{ + __callbackMap[subject] = cb; +} + +void DBusClientListenerImpl::onPublish(std::string subject, int reqId, int error, Json event) +{ + auto it = __callbackMap.find(subject); + IF_FAIL_VOID_TAG(it != __callbackMap.end(), _W, "Unregistered subject"); + it->second(subject.c_str(), reqId, error, event); +} + +static DBusClientListenerImpl __dbusListener; +static DBusClient __dbusClient; + +int ctx::request_handler::subscribe(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result) +{ + return __dbusClient.subscribe(subject, option ? *option : NULL, req_id, request_result); +} + +int ctx::request_handler::unsubscribe(const char* subject, int req_id) +{ + return __dbusClient.unsubscribe(subject, req_id); +} + +int ctx::request_handler::read(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result) +{ + return __dbusClient.read(subject, option ? *option : NULL, req_id, request_result); +} + +int ctx::request_handler::read_sync(const char* subject, ctx::Json* option, int* req_id, ctx::Json* data_read) +{ + return __dbusClient.readSync(subject, option ? *option : NULL, req_id, data_read); +} + +int ctx::request_handler::write(const char* subject, ctx::Json* data) +{ + return __dbusClient.write(subject, *data); +} + +int ctx::request_handler::write_with_reply(const char* subject, ctx::Json* data, ctx::Json* request_result) +{ + return __dbusClient.write(subject, *data, request_result); +} + +int ctx::request_handler::is_supported(const char* subject) +{ + return __dbusClient.isSupported(subject); +} + +int ctx::request_handler::call(const char* method) +{ + return __dbusClient.call(method); +} + +bool ctx::request_handler::register_callback(const char* subject, subject_response_cb callback) +{ + __dbusListener.setCb(subject, callback); + __dbusClient.addListener(subject, &__dbusListener); + return true; +} diff --git a/testsuite/src/trigger/request_handler.h b/testsuite/src/trigger/request_handler.h new file mode 100644 index 0000000..3d7fa41 --- /dev/null +++ b/testsuite/src/trigger/request_handler.h @@ -0,0 +1,64 @@ +/* + * 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_LIB_REQUEST_HANDLER_H__ +#define __CONTEXT_LIB_REQUEST_HANDLER_H__ + +#include + +namespace ctx { namespace request_handler { + + /* + */ + typedef void(* subject_response_cb)(const char* subject, int req_id, int error, ctx::Json response); + + /* + */ + bool register_callback(const char* subject, subject_response_cb callback); + + /* + */ + int subscribe(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result = NULL); + + /* + */ + int unsubscribe(const char* subject, int req_id); + + /* + */ + int read(const char* subject, ctx::Json* option, int* req_id, ctx::Json* request_result = NULL); + + /* + */ + int read_sync(const char* subject, ctx::Json* option, int* req_id, ctx::Json* data_read); + + /* + */ + int write(const char* subject, ctx::Json* data); + + /* + */ + int write_with_reply(const char* subject, ctx::Json* data, ctx::Json* request_result = NULL); + + /* + */ + int is_supported(const char* subject); + + int call(const char* method); + +} } /* namespace ctx::request_handler */ + +#endif // __CONTEXT_LIB_REQUEST_HANDLER_H__