#include <pwd.h>
#include <json-glib/json-glib.h>
#include <vconf.h>
+#include <queue>
#include "ft.h"
#include "log.h"
#include "sticker_info.h"
#include "../inc/sticker_data.h"
+#include "sticker_request.h"
#include "message.h"
#include "sync_alarm.h"
using namespace std;
-typedef enum {
- REQUEST_TYPE_SYNC,
- REQUEST_TYPE_FEATURE_REQ
-} request_type;
-
enum {
SYNC_START_RSP_SUCCESS = 1000,
SYNC_START_RSP_NO_STICKER = 1001
sap_file_transaction_h file_socket;
};
-struct request {
- request_type req_type;
- string mode;
- string category;
- string type;
-};
-
static struct sap_info_s priv_data = { 0 };
static struct sticker_info sticker_data;
-static struct request pending_request, current_request;
+static queue<StickerRequest> ReqQueue;
+static StickerRequest current_request;
gboolean file_on_progress = 0;
static string incoming_file_name;
static int t_id = 0;
-static int rec_file_cnt = 0;
-static int total_file_count = 0;
+static int rec_file_cnt_in_group = 0;
+static int total_file_count_in_group = 0;
+static int sync_success_cnt = 0;
static gboolean _send_json_data(JsonObject *obj)
{
static void notify_sync_progress(unsigned int file_progress)
{
- if (total_file_count == 0)
+ if (total_file_count_in_group == 0)
return;
- double total_progress = (((double)rec_file_cnt / (double)total_file_count) + (1.0/(double)total_file_count*file_progress/100))*100;
-
- LOGI("(%2d / %2d), file progress : %3u%%, total progress : %3u%%", rec_file_cnt, total_file_count, file_progress, (unsigned int)total_progress);
- char progress_str[32];
- snprintf(progress_str, sizeof(progress_str), "%u", (unsigned int)total_progress);
- send_message("sync_progress", progress_str);
+ LOGI("(%2d / %2d), file progress : %3u%%", rec_file_cnt_in_group+1, total_file_count_in_group, file_progress);
+ send_message("sync_progress", NULL);
}
static void _on_send_completed(sap_file_transaction_h file_transaction,
}
}
- rec_file_cnt++;
+ rec_file_cnt_in_group++;
} else {
switch (result) {
case (SAP_FT_TRANSFER_FAIL_CHANNEL_IO): {
file_on_progress = 1;
}
-void request_sticker_data(const char *mode, const char *category, const char *type)
+bool request_sticker_data(const char *mode, const char *category, const char *type)
{
- JsonObject *j_object = json_object_new();
+ bool result = false;
+ JsonObject *j_object = NULL;
+
+ if (!priv_data.socket) {
+ StickerRequest pending_request;
+ pending_request.req_type = REQUEST_TYPE_SYNC;
+ pending_request.mode = string(mode ? mode : "manual");
+ pending_request.category = string(category ? category : "arsticker");
+ pending_request.type = string(type ? type : "input");
+
+ ReqQueue.push(pending_request);
+ LOGI("Push sync request");
+
+ return false;
+ }
+
+ j_object = json_object_new();
if (j_object == NULL) {
LOGE("json object create error");
- return;
+ return false;
}
json_object_set_string_member(j_object, "msgId", STICKER_SYNC_START_REQ);
json_object_set_string_member(j_object, "type", type);
if (_send_json_data(j_object) == FALSE) {
- pending_request.req_type = REQUEST_TYPE_SYNC;
- pending_request.mode = string(mode ? mode : "manual");
- pending_request.category = string(category? category : "arsticker");
- pending_request.type = string(type ? type : "input");
-
- LOGI("Push sync request");
+ LOGE("Failed to send STICKER_SYNC_START_REQ");
+ result = false;
}
else {
current_request.req_type = REQUEST_TYPE_SYNC;
current_request.mode = string(mode ? mode : "manual");
- current_request.category = string(category? category : "arsticker");
+ current_request.category = string(category ? category : "arsticker");
current_request.type = string(type ? type : "input");
+ result = true;
}
json_object_unref(j_object);
+
+ return result;
}
void request_sticker_feature()
{
- JsonObject *j_object = json_object_new();
+ JsonObject *j_object = NULL;
+
+ if (!priv_data.socket) {
+ StickerRequest pending_request;
+ pending_request.req_type = REQUEST_TYPE_FEATURE_REQ;
+ ReqQueue.push(pending_request);
+ LOGI("Push sync feature request");
+ return;
+ }
+
+ j_object = json_object_new();
if (j_object == NULL) {
LOGE("json object create error");
return;
json_object_set_int_member(j_object, "tID", ++t_id);
if (_send_json_data(j_object) == FALSE) {
- pending_request.req_type = REQUEST_TYPE_FEATURE_REQ;
LOGE("Failed to send STICKER_SYNC_FEATURE_REQ");
}
service_app_exit();
}
+static bool process_request_queue()
+{
+ if (ReqQueue.empty())
+ return false;
+
+ StickerRequest request = ReqQueue.front();
+
+ if (request.req_type == REQUEST_TYPE_FEATURE_REQ) {
+ LOGD("[Request feature exchange]");
+ request_sticker_feature();
+ ReqQueue.pop();
+ }
+ else if (request.req_type == REQUEST_TYPE_SYNC) {
+ LOGD("[Request to sync sticker] mode: %s, category: %s, type : %s", request.mode.c_str(),
+ request.category.c_str(),
+ request.type.c_str());
+
+ if (request_sticker_data(request.mode.c_str(), request.category.c_str(), request.type.c_str()))
+ ReqQueue.pop();
+ }
+
+ return true;
+}
void
on_data_received(sap_socket_h socket, unsigned short int channel_id, unsigned int payload_length, void *buffer,
LOGW("Fail to set supported feature");
#endif
- service_app_exit();
+ if (!process_request_queue()) {
+ service_app_exit();
+ }
} else if (msg_id == STICKER_SYNC_START_RSP) {
LOGD("msg : %s", msg_id.c_str());
const char *json_result = json_object_get_string_member(root_obj, "result");
send_message("sync_start_response", response_to_app.c_str());
- if (result_code != SYNC_START_RSP_SUCCESS)
- service_app_exit();
+ if (result_code != SYNC_START_RSP_SUCCESS) {
+ if (!process_request_queue()) {
+ service_app_exit();
+ }
+ }
} else if (msg_id == STICKER_SEND_START_REQ) {
LOGD("msg : %s", msg_id.c_str());
- total_file_count = 0;
- rec_file_cnt = 0;
+ total_file_count_in_group = 0;
+ rec_file_cnt_in_group = 0;
t_id = json_object_get_int_member(root_obj, "tID");
JsonArray *file_list = json_object_get_array_member(root_obj, "list");
if (file_list) {
if (file_len > 0) {
LOGD("Add file : %s, len : %d", file_name.c_str(), file_len);
- total_file_count++;
+ total_file_count_in_group++;
} else {
LOGD("Delete file : %s, len : %d", file_name.c_str(), file_len);
if (create_sticker_provider_handle() == STICKER_ERROR_NONE) {
json_object_set_string_member(j_object, "msgId", STICKER_SEND_STOP_RSP);
json_object_set_int_member(j_object, "tID", t_id);
- if (reason == "complete" && rec_file_cnt != file_len)
+ if (reason == "complete" && rec_file_cnt_in_group != file_len) {
json_object_set_string_member(j_object, "result", "failure");
+ }
else {
int complete_flags = 0;
if (vconf_get_int(VCONFKEY_STICKER_SYNC_COMPLETE, &complete_flags) == 0) {
}
json_object_set_string_member(j_object, "result", "success");
-
- if (current_request.mode == string("manual")) {
- if (!sync_alarm_exist())
- sync_alarm_register(APP_CONTROL_OPERATION_SYNC_ALARM, SYNC_ALARM_DELAY, SYNC_ALARM_INTERVAL);
- }
+ sync_success_cnt++;
}
if (_send_json_data(j_object) == FALSE)
LOGE("Failed to send message");
+ json_object_unref(j_object);
+
send_message("sync_stop_result", reason.c_str());
- json_object_unref(j_object);
+ if (!process_request_queue()) {
+ if (sync_success_cnt > 0 && current_request.mode == string("manual")) {
+ if (!sync_alarm_exist())
+ sync_alarm_register(APP_CONTROL_OPERATION_SYNC_ALARM, SYNC_ALARM_DELAY, SYNC_ALARM_INTERVAL);
+ }
+
+ sync_success_cnt = 0;
+
+ service_app_exit();
+ }
current_request.mode.clear();
current_request.category.clear();
current_request.type.clear();
-
- service_app_exit();
} else
LOGW("unknown msg id : %s", msg_id.c_str());
priv->socket = socket;
LOGI("Connection Established");
- LOGD("pending_request : %d", pending_request.req_type);
-
- if (pending_request.req_type == REQUEST_TYPE_FEATURE_REQ) {
- LOGD("[Request feature exchange]");
- request_sticker_feature();
- }
- else if (pending_request.req_type == REQUEST_TYPE_SYNC) {
- if (!pending_request.mode.empty()) {
- LOGD("[Request to sync sticker] mode: %s, category: %s, type : %s", pending_request.mode.c_str(),
- pending_request.category.c_str(),
- pending_request.type.c_str());
-
- request_sticker_data(pending_request.mode.c_str(), pending_request.category.c_str(), pending_request.type.c_str());
-
- pending_request.mode.clear();
- pending_request.category.clear();
- pending_request.type.clear();
- }
- }
+ process_request_queue();
break;