X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fclient.c;h=ff64fa3558d489e595dfde7df2a273d45d601f91;hb=5bc13c89117dc2814b503dfce34516770816416c;hp=3a483fc042784c56bc2d9c976bd69ff12ed92823;hpb=74fec1ddff061b31f4a5cbb8f6b86d27f1c1af6b;p=platform%2Fframework%2Fweb%2Flivebox-viewer.git diff --git a/src/client.c b/src/client.c index 3a483fc..ff64fa3 100644 --- a/src/client.c +++ b/src/client.c @@ -1,11 +1,11 @@ /* * Copyright 2013 Samsung Electronics Co., Ltd * - * Licensed under the Flora License, Version 1.0 (the "License"); + * Licensed under the Flora License, Version 1.1 (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.tizenopensource.org/license + * http://floralicense.org/license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -19,14 +19,19 @@ #include #include #include +#include #include #include +#include +#include + #include #include #include #include +#include #include "debug.h" #include "client.h" @@ -38,15 +43,18 @@ #include "master_rpc.h" #include "conf.h" #include "critical_log.h" +#include "file_service.h" -static inline void make_connection(void); +int errno; static struct info { int fd; - guint reconnector; + guint timer_id; + char *client_addr; } s_info = { .fd = -1, - .reconnector = 0, + .timer_id = 0, + .client_addr = NULL, }; static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet) @@ -62,7 +70,33 @@ static struct packet *master_fault_package(pid_t pid, int handle, const struct p master_rpc_clear_fault_package(pkgname); lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function); - DbgPrint("%s(%s) is deactivated\n", pkgname, id); + return NULL; +} + +static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet) +{ + struct livebox *handler; + const char *pkgname; + const char *id; + int seize; + int ret; + + ret = packet_get(packet, "ssi", &pkgname, &id, &seize); + if (ret != 3) { + ErrPrint("Invalid argument\n"); + goto out; + } + + handler = lb_find_livebox(pkgname, id); + if (!handler) { + ErrPrint("Instance(%s) is not exists\n", id); + goto out; + } + + DbgPrint("HOLD: %s %d\n", id, seize); + lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL); + +out: return NULL; } @@ -74,9 +108,10 @@ static struct packet *master_pinup(pid_t pid, int handle, const struct packet *p struct livebox *handler; char *new_content; int ret; + int status; int pinup; - ret = packet_get(packet, "iisss", &ret, &pinup, &pkgname, &id, &content); + ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content); if (ret != 5) { ErrPrint("Invalid argument\n"); goto out; @@ -88,7 +123,7 @@ static struct packet *master_pinup(pid_t pid, int handle, const struct packet *p goto out; } - if (ret == 0) { + if (status == 0) { new_content = strdup(content); if (new_content) { free(handler->content); @@ -96,16 +131,23 @@ static struct packet *master_pinup(pid_t pid, int handle, const struct packet *p handler->is_pinned_up = pinup; } else { ErrPrint("Heap: %s\n", strerror(errno)); - ret = LB_STATUS_ERROR_MEMORY; + status = LB_STATUS_ERROR_MEMORY; } } if (handler->pinup_cb) { - handler->pinup_cb(handler, ret, handler->pinup_cbdata); + ret_cb_t cb; + void *cbdata; + + /* Make sure that user can call pinup API in its result callback */ + cb = handler->pinup_cb; + cbdata = handler->pinup_cbdata; - handler->pinup_cb = NULL; /*!< Reset pinup cb */ + handler->pinup_cb = NULL; handler->pinup_cbdata = NULL; - } else if (ret == 0) { + + cb(handler, status, cbdata); + } else if (status == 0) { lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED); } @@ -148,10 +190,12 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet } if (handler->created_cb) { + ret_cb_t cb; + void *cbdata; /*! * \note * - * "if (handler->id == NULL)" + * "if (handler->id == NULL) {" * * The instance is not created yet. * But the master forcely destroy it and send destroyed event to this @@ -161,32 +205,38 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet * before creating an instance successfully. */ if (handler->created_cb == handler->deleted_cb) { - if (handler->created_cbdata != handler->deleted_cbdata) + if (handler->created_cbdata != handler->deleted_cbdata) { DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id); + } handler->deleted_cb = NULL; handler->deleted_cbdata = NULL; } - DbgPrint("Call the created cb with LB_STATUS_ERROR_CANCEL\n"); - handler->created_cb(handler, LB_STATUS_ERROR_CANCEL, handler->created_cbdata); + cb = handler->created_cb; + cbdata = handler->created_cbdata; + handler->created_cb = NULL; handler->created_cbdata = NULL; + + cb(handler, LB_STATUS_ERROR_CANCEL, cbdata); } else if (handler->id) { if (handler->deleted_cb) { - DbgPrint("Call the deleted cb\n"); - handler->deleted_cb(handler, LB_STATUS_SUCCESS, handler->deleted_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->deleted_cb; + cbdata = handler->deleted_cbdata; handler->deleted_cb = NULL; handler->deleted_cbdata = NULL; + + cb(handler, LB_STATUS_SUCCESS, cbdata); } else { - DbgPrint("Call the lb,deleted\n"); lb_invoke_event_handler(handler, LB_EVENT_DELETED); } } - DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id); - /* Just try to delete it, if a user didn't remove it from the live box list */ lb_unref(handler); @@ -194,6 +244,215 @@ out: return NULL; } +static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet) +{ + struct livebox *handler; + const char *pkgname; + const char *id; + const char *content; + const char *title; + const char *fbfile; + double priority; + int ret; + + ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile); + if (ret != 6) { + ErrPrint("Invalid argument\n"); + goto out; + } + + handler = lb_find_livebox(pkgname, id); + if (!handler) { + ErrPrint("Instance[%s] is not exists\n", id); + goto out; + } + + if (handler->state != CREATE) { + ErrPrint("(%s) is not created\n", id); + goto out; + } + + lb_set_priority(handler, priority); + lb_set_content(handler, content); + lb_set_title(handler, title); + + /*! + * \NOTE + * Width & Height is not changed in this case. + * If the active update is began, the size should not be changed, + * And if the size is changed, the provider should finish the updating first. + * And then begin updating again after change its size. + */ + if (lb_get_lb_fb(handler)) { + lb_set_lb_fb(handler, fbfile); + + ret = fb_sync(lb_get_lb_fb(handler)); + if (ret != LB_STATUS_SUCCESS) { + ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret); + } else { + lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN); + } + } else { + ErrPrint("Invalid request[%s], %s\n", id, fbfile); + } + +out: + return NULL; +} + +static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet) +{ + struct livebox *handler; + const char *pkgname; + const char *id; + const char *fbfile; + int ret; + + ret = packet_get(packet, "sss", &pkgname, &id, &fbfile); + if (ret != 2) { + ErrPrint("Invalid argument\n"); + goto out; + } + + handler = lb_find_livebox(pkgname, id); + if (!handler) { + ErrPrint("Instance[%s] is not exists\n", id); + goto out; + } + + if (handler->state != CREATE) { + ErrPrint("[%s] is not created\n", id); + goto out; + } + + if (lb_get_pd_fb(handler)) { + lb_set_lb_fb(handler, fbfile); + + ret = fb_sync(lb_get_lb_fb(handler)); + if (ret != LB_STATUS_SUCCESS) { + ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret); + } else { + lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN); + } + } else { + ErrPrint("Invalid request[%s], %s\n", id, fbfile); + } + +out: + return NULL; +} + +static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet) +{ + struct livebox *handler; + const char *pkgname; + const char *id; + int ret; + + ret = packet_get(packet, "ss", &pkgname, &id); + if (ret != 2) { + ErrPrint("Invalid argument\n"); + goto out; + } + + handler = lb_find_livebox(pkgname, id); + if (!handler) { + ErrPrint("Instance[%s] is not exists\n", id); + goto out; + } + + if (handler->state != CREATE) { + ErrPrint("[%s] is not created\n", id); + goto out; + } + + if (lb_get_lb_fb(handler)) { + lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END); + } else { + ErrPrint("Invalid request[%s]\n", id); + } + +out: + return NULL; +} + +static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet) +{ + struct livebox *handler; + const char *pkgname; + const char *id; + int ret; + int status; + + ret = packet_get(packet, "ssi", &pkgname, &id, &status); + if (ret != 3) { + ErrPrint("Invalid argument\n"); + goto out; + } + + handler = lb_find_livebox(pkgname, id); + if (!handler) { + ErrPrint("Instance[%s] is not exists\n", id); + goto out; + } + + if (handler->state != CREATE) { + ErrPrint("[%s] is not created\n", id); + goto out; + } + + if (handler->access_event_cb) { + ret_cb_t cb; + void *cbdata; + + cb = handler->access_event_cb; + cbdata = handler->access_event_cbdata; + + handler->access_event_cb = NULL; + handler->access_event_cbdata = NULL; + + cb(handler, status, cbdata); + } else { + ErrPrint("Invalid event[%s]\n", id); + } +out: + return NULL; +} + +static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet) +{ + struct livebox *handler; + const char *pkgname; + const char *id; + int ret; + + ret = packet_get(packet, "ss", &pkgname, &id); + if (ret != 2) { + ErrPrint("Invalid argument\n"); + goto out; + } + + handler = lb_find_livebox(pkgname, id); + if (!handler) { + ErrPrint("Instance[%s] is not exists\n", id); + goto out; + } + + if (handler->state != CREATE) { + ErrPrint("[%s] is not created\n", id); + goto out; + } + + if (lb_get_lb_fb(handler)) { + lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END); + } else { + ErrPrint("Invalid request[%s]", id); + } + +out: + return NULL; +} + static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet) { const char *pkgname; @@ -229,16 +488,16 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack * Don't try to notice anything with this, Just ignore all events * Beacuse the user doesn't wants know about this anymore */ - DbgPrint("(%s) is not exists, but updated\n", id); + ErrPrint("(%s) is not exists, but updated\n", id); goto out; } lb_set_priority(handler, priority); lb_set_content(handler, content); lb_set_title(handler, title); + lb_set_size(handler, lb_w, lb_h); if (lb_text_lb(handler)) { - lb_set_size(handler, lb_w, lb_h); (void)parse_desc(handler, livebox_filename(handler), 0); /*! * \note @@ -247,18 +506,18 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack */ goto out; } else if (lb_get_lb_fb(handler)) { - lb_set_size(handler, lb_w, lb_h); lb_set_lb_fb(handler, fbfile); ret = fb_sync(lb_get_lb_fb(handler)); - if (ret < 0) + if (ret != LB_STATUS_SUCCESS) { ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret); + } } else { - lb_set_size(handler, lb_w, lb_h); - ret = 0; + ret = LB_STATUS_SUCCESS; } - if (ret == 0) + if (ret == LB_STATUS_SUCCESS) { lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED); + } out: return NULL; @@ -298,20 +557,31 @@ static struct packet *master_pd_created(pid_t pid, int handle, const struct pack } else { (void)lb_set_pd_fb(handler, buf_id); ret = fb_sync(lb_get_pd_fb(handler)); - if (ret < 0) + if (ret < 0) { ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id))); + } } handler->is_pd_created = (status == 0); if (handler->pd_created_cb) { - DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status); - handler->pd_created_cb(handler, status, handler->pd_created_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->pd_created_cb; + cbdata = handler->pd_created_cbdata; handler->pd_created_cb = NULL; handler->pd_created_cbdata = NULL; - } else if (status == 0) { - DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status); + + /*! + * Before call the Callback function, + * pd_create_cb must be reset. + * Because, in the create callback, user can call create_pd function again. + */ + DbgPrint("PERF_DBOX\n"); + cb(handler, status, cbdata); + } else if (handler->is_pd_created) { lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED); } @@ -347,13 +617,22 @@ static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct pa handler->is_pd_created = 0; if (handler->pd_destroyed_cb) { - DbgPrint("Invoke the PD Destroyed CB\n"); - handler->pd_destroyed_cb(handler, status, handler->pd_destroyed_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->pd_destroyed_cb; + cbdata = handler->pd_destroyed_cbdata; handler->pd_destroyed_cb = NULL; handler->pd_destroyed_cbdata = NULL; + + /*! + * Before call the Callback function, + * pd_destroyed_cb must be reset. + * Because, in the create callback, user can call destroy_pd function again. + */ + cb(handler, status, cbdata); } else if (status == 0) { - DbgPrint("Invoke the LB_EVENT_PD_DESTROYED event\n"); lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED); } @@ -406,10 +685,65 @@ static struct packet *master_pd_updated(pid_t pid, int handle, const struct pack (void)lb_set_pd_fb(handler, fbfile); ret = fb_sync(lb_get_pd_fb(handler)); - if (ret < 0) - ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id))); - else + if (ret < 0) { + ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret); + } else { lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED); + } + } + +out: + return NULL; +} + +static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet) +{ + struct livebox *handler; + const char *pkgname; + const char *id; + int active_mode; + int status; + int ret; + + if (!packet) { + ErrPrint("Invalid packet\n"); + goto out; + } + + ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode); + if (ret != 4) { + ErrPrint("Invalid argument\n"); + goto out; + } + + handler = lb_find_livebox(pkgname, id); + if (!handler) { + ErrPrint("Livebox(%s) is not found\n", id); + goto out; + } + + if (handler->state != CREATE) { + ErrPrint("Livebox(%s) is not created yet\n", id); + goto out; + } + + if (status == LB_STATUS_SUCCESS) { + lb_set_update_mode(handler, active_mode); + } + + if (handler->update_mode_cb) { + ret_cb_t cb; + void *cbdata; + + cb = handler->update_mode_cb; + cbdata = handler->update_mode_cbdata; + + handler->update_mode_cb = NULL; + handler->update_mode_cbdata = NULL; + + cb(handler, status, cbdata); + } else { + lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED); } out: @@ -421,6 +755,7 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa struct livebox *handler; const char *pkgname; const char *id; + const char *fbfile; int status; int ret; int w; @@ -432,14 +767,12 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa goto out; } - ret = packet_get(packet, "ssiiii", &pkgname, &id, &is_pd, &w, &h, &status); - if (ret != 6) { + ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status); + if (ret != 7) { ErrPrint("Invalid argument\n"); goto out; } - DbgPrint("Size is changed: %dx%d (%s)\n", w, h, id); - handler = lb_find_livebox(pkgname, id); if (!handler) { ErrPrint("Livebox(%s) is not found\n", id); @@ -452,7 +785,6 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa } if (is_pd) { - DbgPrint("PD is resized\n"); /*! * \NOTE * PD is not able to resized by the client. @@ -468,7 +800,6 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa } } else { if (status == 0) { - DbgPrint("LB is successfully resized (%dx%d)\n", w, h); lb_set_size(handler, w, h); /*! @@ -476,8 +807,16 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa * If there is a created LB FB, * Update it too. */ - if (lb_get_lb_fb(handler)) - (void)lb_set_lb_fb(handler, id); + if (lb_get_lb_fb(handler)) { + lb_set_lb_fb(handler, fbfile); + + ret = fb_sync(lb_get_lb_fb(handler)); + if (ret < 0) { + ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id))); + } + + /* Just update the size info only. */ + } /*! * \NOTE @@ -485,20 +824,31 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa * So I added some log before & after call the user callback. */ if (handler->size_changed_cb) { - handler->size_changed_cb(handler, status, handler->size_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->size_changed_cb; + cbdata = handler->size_cbdata; handler->size_changed_cb = NULL; handler->size_cbdata = NULL; + + cb(handler, status, cbdata); } else { lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED); } } else { - DbgPrint("LB is not resized: %dx%d (%d)\n", w, h, status); if (handler->size_changed_cb) { - handler->size_changed_cb(handler, status, handler->size_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->size_changed_cb; + cbdata = handler->size_cbdata; handler->size_changed_cb = NULL; handler->size_cbdata = NULL; + + cb(handler, status, cbdata); } } } @@ -533,15 +883,21 @@ static struct packet *master_period_changed(pid_t pid, int handle, const struct goto out; } - DbgPrint("Update period is changed? %lf (%d)\n", period, status); - if (status == 0) + if (status == 0) { lb_set_period(handler, period); + } if (handler->period_changed_cb) { - handler->period_changed_cb(handler, status, handler->group_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->period_changed_cb; + cbdata = handler->period_cbdata; handler->period_changed_cb = NULL; handler->period_cbdata = NULL; + + cb(handler, status, cbdata); } else if (status == 0) { lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED); } @@ -582,15 +938,21 @@ static struct packet *master_group_changed(pid_t pid, int handle, const struct p goto out; } - DbgPrint("Group is changed? [%s] / [%s] (%d)\n", cluster, category, status); - if (status == 0) + if (status == 0) { (void)lb_set_group(handler, cluster, category); + } if (handler->group_changed_cb) { - handler->group_changed_cb(handler, status, handler->group_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->group_changed_cb; + cbdata = handler->group_cbdata; handler->group_changed_cb = NULL; handler->group_cbdata = NULL; + + cb(handler, status, cbdata); } else if (status == 0) { lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED); } @@ -711,12 +1073,14 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet break; case _LB_TYPE_SCRIPT: case _LB_TYPE_BUFFER: - if (!strlen(lb_fname)) + if (!strlen(lb_fname)) { break; + } lb_set_lb_fb(handler, lb_fname); ret = fb_sync(lb_get_lb_fb(handler)); - if (ret < 0) + if (ret < 0) { ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id))); + } break; case _LB_TYPE_TEXT: lb_set_text_lb(handler); @@ -731,13 +1095,15 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet switch (pd_type) { case _PD_TYPE_SCRIPT: case _PD_TYPE_BUFFER: - if (!strlen(pd_fname)) + if (!strlen(pd_fname)) { break; + } lb_set_pd_fb(handler, pd_fname); ret = fb_sync(lb_get_pd_fb(handler)); - if (ret < 0) + if (ret < 0) { ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id))); + } break; case _PD_TYPE_TEXT: lb_set_text_pd(handler); @@ -771,20 +1137,23 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet */ if (handler->created_cb) { - DbgPrint("Invoke the created_cb\n"); - handler->created_cb(handler, ret, handler->created_cbdata); + ret_cb_t cb; + void *cbdata; + + cb = handler->created_cb; + cbdata = handler->created_cbdata; handler->created_cb = NULL; handler->created_cbdata = NULL; + + cb(handler, ret, cbdata); } else { - DbgPrint("Invoke the lb,created\n"); lb_invoke_event_handler(handler, LB_EVENT_CREATED); } } out: if (ret == 0 && old_state == DELETE) { - DbgPrint("Send the delete request\n"); lb_send_delete(handler, handler->created_cb, handler->created_cbdata); /*! @@ -848,6 +1217,39 @@ static struct method s_table[] = { .handler = master_pinup, }, { + .cmd = "scroll", + .handler = master_hold_scroll, + }, + + { + .cmd = "update_mode", + .handler = master_update_mode, + }, + + { + .cmd = "lb_update_begin", + .handler = master_lb_update_begin, + }, + { + .cmd = "lb_update_end", + .handler = master_lb_update_end, + }, + + { + .cmd = "pd_update_begin", + .handler = master_pd_update_begin, + }, + { + .cmd = "pd_update_end", + .handler = master_pd_update_end, + }, + + { + .cmd = "access_status", + .handler = master_access_status, + }, + + { .cmd = NULL, .handler = NULL, }, @@ -860,51 +1262,34 @@ static void acquire_cb(struct livebox *handler, const struct packet *result, voi } else { int ret; - if (packet_get(result, "i", &ret) != 1) + if (packet_get(result, "i", &ret) != 1) { ErrPrint("Invalid argument\n"); - else + } else { DbgPrint("Acquire returns: %d\n", ret); + } } return; } -static gboolean connector_cb(gpointer user_data) -{ - s_info.reconnector = 0; - - if (s_info.fd > 0) { - DbgPrint("Connection is already made\n"); - return FALSE; - } - - make_connection(); - return FALSE; -} - -static inline void make_connection(void) +static inline int make_connection(void) { struct packet *packet; int ret; DbgPrint("Let's making connection!\n"); - s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table); + s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table); if (s_info.fd < 0) { - /*!< After 10 secs later, try to connect again */ - s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL); - if (s_info.reconnector == 0) - ErrPrint("Failed to fire the reconnector\n"); - - ErrPrint("Try this again A sec later\n"); - return; + ErrPrint("Try this again later\n"); + return LB_STATUS_ERROR_IO; } packet = packet_create("acquire", "d", util_timestamp()); if (!packet) { com_core_packet_client_fini(s_info.fd); s_info.fd = -1; - return; + return LB_STATUS_ERROR_FAULT; } ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL); @@ -912,8 +1297,10 @@ static inline void make_connection(void) ErrPrint("Master RPC returns %d\n", ret); com_core_packet_client_fini(s_info.fd); s_info.fd = -1; + return LB_STATUS_ERROR_IO; } + return LB_STATUS_SUCCESS; } static int connected_cb(int handle, void *data) @@ -922,6 +1309,36 @@ static int connected_cb(int handle, void *data) return 0; } +static void master_started_cb(keynode_t *node, void *data) +{ + int state = 0; + + if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) { + ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED); + } + + DbgPrint("Master state: %d\n", state); + if (state == 1 && make_connection() == LB_STATUS_SUCCESS) { + int ret; + ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb); + DbgPrint("master_started vconf key de-registered [%d]\n", ret); + } +} + +static gboolean timeout_cb(gpointer data) +{ + if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) { + ErrPrint("Failed to add vconf for monitoring service state\n"); + } else { + DbgPrint("vconf event callback is registered\n"); + } + + master_started_cb(NULL, NULL); + + s_info.timer_id = 0; + return FALSE; +} + static int disconnected_cb(int handle, void *data) { if (s_info.fd != handle) { @@ -931,30 +1348,50 @@ static int disconnected_cb(int handle, void *data) s_info.fd = -1; /*!< Disconnected */ - if (s_info.reconnector > 0) { - DbgPrint("Reconnector already fired\n"); - return 0; - } - - /*!< After 10 secs later, try to connect again */ - s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL); - if (s_info.reconnector == 0) { - ErrPrint("Failed to fire the reconnector\n"); - make_connection(); - } - master_rpc_clear_all_request(); lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected"); lb_delete_all(); + + /* Try to reconnect after 1 sec later */ + if (!s_info.timer_id) { + DbgPrint("Reconnecting timer is added\n"); + s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL); + if (s_info.timer_id == 0) { + ErrPrint("Unable to add reconnecting timer\n"); + return 0; + } + } else { + ErrPrint("Reconnecting timer is already exists\n"); + } + return 0; } int client_init(void) { + s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR); + if (!s_info.client_addr) { + s_info.client_addr = strdup(CLIENT_SOCKET); + if (!s_info.client_addr) { + ErrPrint("Heap: %s\n", strerror(errno)); + return -ENOMEM; + } + } + + (void)file_service_init(); + + DbgPrint("Server Address: %s\n", s_info.client_addr); + com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL); com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL); - make_connection(); + if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) { + ErrPrint("Failed to add vconf for service state\n"); + } else { + DbgPrint("vconf event callback is registered\n"); + } + + master_started_cb(NULL, NULL); return 0; } @@ -965,16 +1402,27 @@ int client_fd(void) const char *client_addr(void) { - return CLIENT_SOCKET; + return s_info.client_addr; } int client_fini(void) { - com_core_packet_client_fini(s_info.fd); + int ret; + + (void)file_service_fini(); + + ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb); + if (ret < 0) { + DbgPrint("Ignore vconf key: %d\n", ret); + } + com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL); com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL); + com_core_packet_client_fini(s_info.fd); s_info.fd = -1; - return 0; + free(s_info.client_addr); + s_info.client_addr = NULL; + return LB_STATUS_SUCCESS; } /* End of a file */