2.0_alpha release commit
[framework/messaging/email-service.git] / email-ipc / email-proxy / email-proxy-main.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
23 #include <unistd.h>
24 #include <pthread.h>
25 #include <malloc.h>
26
27 #include "email-ipc.h"
28 #include "email-ipc-build.h"
29 #include "email-proxy-main.h"
30 #include "email-proxy-socket.h"
31 #include "email-proxy-callback-info.h"
32
33 #include "email-debug-log.h"
34
35 EXPORT_API int emipc_initialize_proxy_main()
36 {
37         EM_DEBUG_FUNC_BEGIN();
38         int sock_fd = 0;
39
40         sock_fd = emipc_get_proxy_socket_id();
41         
42         if (sock_fd) {
43                 EM_DEBUG_LOG("Socket already initialized");
44                 return EMAIL_ERROR_IPC_ALREADY_INITIALIZED;
45         }
46         
47         if (!emipc_start_proxy_socket()) {
48                 EM_DEBUG_EXCEPTION("Socket start failed");
49                 return EMAIL_ERROR_IPC_CONNECTION_FAILURE;
50         }
51         
52         EM_DEBUG_LOG("Socket ID : %d", emipc_get_proxy_socket_id());
53         EM_DEBUG_FUNC_END();
54         return EMAIL_ERROR_NONE; 
55 }
56
57 EXPORT_API int emipc_finalize_proxy_main()
58 {
59         EM_DEBUG_FUNC_BEGIN();
60         if (!emipc_end_proxy_socket()) {
61                 EM_DEBUG_EXCEPTION("emipc_finalize_proxy_main failed");
62                 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
63         }
64
65         EM_DEBUG_FUNC_END();
66         return EMAIL_ERROR_NONE;
67 }
68
69 EXPORT_API bool emipc_execute_api_of_proxy_main(emipc_email_api_info *api_info)
70 {
71         EM_DEBUG_FUNC_BEGIN();
72         
73         int ret;
74         unsigned char *in_stream = NULL;
75         int length = 0;
76         bool result = false;
77         int sending_bytes;
78
79         if (!api_info) {
80                 EM_DEBUG_EXCEPTION("Invalid Parameter");
81                 return false;
82         }
83
84         in_stream = emipc_serialize_api_info(api_info, ePARAMETER_IN, &length);
85         if( !in_stream ) {
86                 EM_DEBUG_EXCEPTION("NULL stream");
87                 return false;
88         }
89
90         sending_bytes = emipc_send_proxy_socket(in_stream, length);
91
92         EM_DEBUG_LOG("Proxy=>stub sending %d byte.", sending_bytes);
93
94         if (sending_bytes > 0) {
95 #ifdef IPCLIB_STREAM_TRACE_ON
96                 int index = 0;
97                 for (index=0;index<length;index++) 
98                         EM_DEBUG_LOG("in_stream[index] : [%x]", in_stream[index]);
99 #endif
100                 char *ipc_buf = NULL;
101
102                 ret = emipc_recv_proxy_socket(&ipc_buf);
103         
104                 EM_DEBUG_LOG("Recv length : %d", ret);
105
106                 if (ret > 0)
107                         result = emipc_deserialize_api_info(api_info, ePARAMETER_OUT, ipc_buf);
108                 else
109                         result = false; 
110
111                 EM_SAFE_FREE(ipc_buf);
112         }
113         
114         EM_DEBUG_FUNC_END();
115         return result;          
116 }