* limitations under the License.
*/
-#include "api/action.h"
+#include <string>
-#include "connector.hh"
+#include "api/action.h"
+#include "api/connector.hh"
+#include "api/tizen_action_service_proxy.h"
#include "common/action_model.h"
#include "common/action_schema.h"
#include "common/action_parameter.h"
: cb_(cb), user_data_(user_data) {}
void OnReceived(int execution_id, std::string result) {
- LOG(WARNING) << "@@@ reply OnReceived : " << result;
-
- cb_(100, result.c_str(), user_data_);
+ cb_(execution_id, result.c_str(), user_data_);
}
private:
void* user_data_;
};
-class ActionClient {
- public:
- ActionClient(std::unique_ptr<api::Connector> connector)
- : connector_(std::move(connector)) {}
-
- api::rpc::ActionSchema GetAction(std::string name) {
- LOG(WARNING) << "ActionClient GetAction";
-
- auto* conn = connector_->GetConnection();
- if (conn == nullptr) {
- LOG(WARNING) << "get action conn null";
- throw std::invalid_argument("connection is nullptr");
- }
-
- auto action_schema = conn->GetAction(name);
-
- return action_schema;
- }
-
- std::vector<api::rpc::ActionSchema> ListActions() {
- LOG(WARNING) << "ActionClient ListActions";
-
- auto* conn = connector_->GetConnection();
- if (conn == nullptr) {
- LOG(WARNING) << "foreach action conn null";
- throw std::invalid_argument("connection is nullptr");
- }
-
- auto actions = conn->ListActions();
-
- return actions;
- };
-
- int Execute(std::string param, action_result_cb cb, void* user_data) {
- LOG(WARNING) << "ActionClient Execute";
-
- auto* conn = connector_->GetConnection();
- if (conn == nullptr) {
- LOG(WARNING) << "get action conn null";
- throw std::invalid_argument("connection is nullptr");
- }
-
- common::ActionModel model_(std::move(param));
- api::rpc::ActionModel action;
- action.Setaction_id(model_.GetId());
- action.Setjson(model_.GetJson());
-
- cb_id_ = conn->RegisterActionReplyCb(std::make_unique<Reply>(cb, user_data));
- int ret = conn->Execute(action);
-
- LOG(WARNING) << "execute cb : " << std::to_string(cb_id_) << ", ret : " << std::to_string(ret);
-
- return ret;
- }
-
- void Dispose() {
- LOG(WARNING) << "ActionClient Dispose";
-
- if (cb_id_ > -1) {
- LOG(WARNING) << "unregister replycb";
-
- auto* conn = connector_->GetConnection();
- if (conn == nullptr) {
- LOG(WARNING) << "get action conn null";
- throw std::invalid_argument("connection is nullptr");
- }
-
- conn->UnregisterActionReplyCb(cb_id_);
- }
- }
-
- private:
- std::unique_ptr<api::Connector> connector_;
- int cb_id_ = -1;
-};
-
}
API int action_client_create(action_client_h* client) {
- LOG(WARNING) << "action_client_create";
-
if (client == nullptr) {
- LOG(WARNING) << "Invalid Parameter";
+ LOG(ERROR) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- auto connector = std::make_unique<api::Connector>();
- ActionClient* client_ = new (std::nothrow) ActionClient(std::move(connector));
- if (client_ == nullptr) {
- LOG(WARNING) << "Out-of-memory";
+ auto con = new (std::nothrow) api::Connector();
+ if (!con) {
+ LOG(ERROR) << "Out-of-memory";
return ACTION_ERROR_OUT_OF_MEMORY;
}
- *client = static_cast<action_client_h>(client_);
+ if (!con->Connect()) {
+ LOG(ERROR) << "Failed to connect to action service";
+ delete con;
+ return ACTION_ERROR_IO_ERROR;
+ }
+
+ *client = static_cast<action_client_h>(con);
return ACTION_ERROR_NONE;
}
API int action_client_destroy(action_client_h client) {
- LOG(WARNING) << "action_client_destroy";
-
if (client == nullptr) {
- LOG(WARNING) << "Invalid Parameter";
+ LOG(ERROR) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- ActionClient* client_ = static_cast<ActionClient*>(client);
- client_->Dispose();
- delete client_;
+ auto con = static_cast<api::Connector*>(client);
+ // TODO: need to unregister all callbacks
+ // auto proxy = con->GetProxy();
+ // proxy->UnregisterAll();
+ delete con;
return ACTION_ERROR_NONE;
}
API int action_client_get_action(action_client_h client,
const char* name, action_h* action) {
- LOG(WARNING) << "action_client_get_action";
-
if (client == nullptr || name == nullptr || action == nullptr) {
LOG(WARNING) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- ActionClient* client_ = static_cast<ActionClient*>(client);
- try {
- auto action_schema = client_->GetAction(name);
-
- common::ActionSchema* schema = new (std::nothrow) common::ActionSchema(action_schema.Getjson());
+ auto con = static_cast<api::Connector*>(client);
+ auto proxy = con->GetProxy();
+ auto action_schema = proxy->GetAction(name);
+ // TODO: check action_schema is exist
+ // return ACTION_ERROR_NO_SUCH_ACTION
- *action = static_cast<action_h>(schema);
- } catch (...) {
- LOG(WARNING) << "get action exception";
- return -1;
+ common::ActionSchema* schema =
+ new (std::nothrow) common::ActionSchema(action_schema.Getjson());
+ if (!schema) {
+ LOG(ERROR) << "Out-of-memory";
+ return ACTION_ERROR_OUT_OF_MEMORY;
}
+ *action = static_cast<action_h>(schema);
+
return ACTION_ERROR_NONE;
}
API int action_client_foreach_action(action_client_h client,
action_foreach_action_cb cb, void* user_data) {
- LOG(WARNING) << "action_client_foreach_action";
-
if (client == nullptr || cb == nullptr) {
- LOG(WARNING) << "Invalid Parameter";
+ LOG(ERROR) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- ActionClient* client_ = static_cast<ActionClient*>(client);
- try {
- auto actions = client_->ListActions();
-
- for (auto action : actions) {
- common::ActionSchema schema(action.Getjson());
-
- if (!cb(static_cast<action_h>(&schema), user_data)) {
- LOG(WARNING) << "callback returns false";
- break;
- }
- }
- } catch (...) {
- LOG(WARNING) << "exception foreach action";
- return -1;
+ auto con = static_cast<api::Connector*>(client);
+ auto proxy = con->GetProxy();
+ auto actions = proxy->ListActions();
+
+ for (auto action : actions) {
+ common::ActionSchema schema(action.Getjson());
+ if (!cb(static_cast<action_h>(&schema), user_data))
+ break;
}
return ACTION_ERROR_NONE;
API int action_client_execute(action_client_h client, const char* param,
action_result_cb cb, void* user_data, int* execution_id) {
- LOG(WARNING) << "action_client_execute";
if (client == nullptr || param == nullptr) {
LOG(WARNING) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- ActionClient* client_ = static_cast<ActionClient*>(client);
- try {
- client_->Execute(param, cb, user_data);
- } catch (...) {
- LOG(WARNING) << "execute exception3";
- return -1;
+ auto con = static_cast<api::Connector*>(client);
+ auto proxy = con->GetProxy();
+
+ common::ActionModel model_(std::move(param));
+ api::rpc::ActionModel action;
+ // TODO: remove Setaction_id, Setjson
+ action.Setaction_id(model_.GetId());
+ action.Setjson(model_.GetJson());
+
+ // TODO: unregister cb using returned cb_id
+ proxy->RegisterActionReplyCb(std::make_unique<Reply>(cb, user_data));
+
+ *execution_id = proxy->Execute(action);
+ if (*execution_id) {
+ LOG(ERROR) << "Failed to execute action";
+ return ACTION_ERROR_IO_ERROR;
}
return ACTION_ERROR_NONE;
}
API int action_get_name(action_h action, const char** name) {
- LOG(WARNING) << "action_get_name";
if (action == nullptr || name == nullptr) {
LOG(WARNING) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- common::ActionSchema* action_schema = static_cast<common::ActionSchema*>(action);
+ auto action_schema = static_cast<common::ActionSchema*>(action);
*name = strdup(action_schema->GetActionId().c_str());
return ACTION_ERROR_NONE;
}
API int action_get_schema(action_h action, const char** json_schema) {
- LOG(WARNING) << "action_get_schema";
if (action == nullptr || json_schema == nullptr) {
LOG(WARNING) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- common::ActionSchema* action_schema = static_cast<common::ActionSchema*>(action);
+ auto action_schema = static_cast<common::ActionSchema*>(action);
*json_schema = strdup(action_schema->GetJsonString().c_str());
return ACTION_ERROR_NONE;
}
API int action_destroy(action_h action) {
- LOG(WARNING) << "action_destroy";
if (action == nullptr) {
LOG(WARNING) << "Invalid Parameter";
return ACTION_ERROR_INVALID_PARAMETER;
}
- common::ActionSchema* action_schema = static_cast<common::ActionSchema*>(action);
+ auto action_schema = static_cast<common::ActionSchema*>(action);
delete action_schema;
return ACTION_ERROR_NONE;