2.0_alpha release commit
[framework/messaging/email-service.git] / email-ipc / email-ipc-proxy.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22 #include "email-ipc-build.h"
23 #include "email-ipc.h"
24 #include "email-ipc-api-info.h"
25 #include "email-ipc-param-list.h"
26 #include "email-ipc-socket.h"
27 #include "email-proxy-main.h"
28
29 #include "email-debug-log.h"
30 #include "email-api.h"
31 #include "email-types.h"
32 #include "email-internal-types.h"
33
34 pthread_mutex_t proxy_mutex = PTHREAD_MUTEX_INITIALIZER;
35
36 EXPORT_API int emipc_initialize_proxy()
37 {
38         EM_DEBUG_FUNC_BEGIN();
39
40         int err = emipc_initialize_proxy_main();
41
42         EM_DEBUG_FUNC_END();
43         return err;
44 }
45
46 EXPORT_API int emipc_finalize_proxy()
47 {
48         EM_DEBUG_FUNC_BEGIN();
49         return emipc_finalize_proxy_main();
50 }
51
52 EXPORT_API int emipc_execute_proxy_api(HIPC_API api)
53 {
54         EM_DEBUG_FUNC_BEGIN();
55         int ret;
56         int err = EMAIL_ERROR_NONE;
57         emipc_email_api_info *api_info = (emipc_email_api_info *)api;
58
59         EM_DEBUG_LOG("API [%p]", api_info);
60                 
61         if(api_info == NULL) {
62                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
63                 return EMAIL_ERROR_INVALID_PARAM;
64         }
65                 
66         EM_DEBUG_LOG("APIID [%s], ResponseID [%d], APPID[%d]",
67                                 EM_APIID_TO_STR(api_info->api_id), api_info->response_id, api_info->app_id);
68
69         ENTER_CRITICAL_SECTION(proxy_mutex);
70         ret = emipc_execute_api_of_proxy_main(api_info);
71
72         /* connection retry */
73         if (!ret) {
74                 EM_DEBUG_LOG("Connection retry");
75                 emipc_finalize_proxy();
76
77                 err = emipc_initialize_proxy();
78                 if (err != EMAIL_ERROR_NONE) {
79                         EM_DEBUG_EXCEPTION("Failed to open the socket : [%d]", err);
80                         err = EMAIL_ERROR_CONNECTION_FAILURE;
81                         goto FINISH_OFF;
82                 }
83
84                 ret = emipc_execute_api_of_proxy_main(api_info);
85                 if (!ret) {
86                         EM_DEBUG_EXCEPTION("emipc_proxy_main : emipc_execute_api failed [%d]", err);
87                         err = EMAIL_ERROR_CONNECTION_FAILURE;
88                         goto FINISH_OFF;
89                 }
90         }
91         
92 FINISH_OFF:
93         LEAVE_CRITICAL_SECTION(proxy_mutex);
94         EM_DEBUG_FUNC_END("err [%d]", err);
95         return err;     
96 }