2.0_alpha release commit
[framework/messaging/email-service.git] / email-ipc / email-proxy / email-proxy-socket.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
24 #include <unistd.h>
25 #include <sys/select.h>
26
27 #include "email-ipc-build.h"
28 #include "email-proxy-socket.h"
29 #include "email-ipc-socket.h"
30
31 #include "email-debug-log.h"
32 #include <errno.h>
33
34 static int proxy_socket_fd = 0;
35
36 EXPORT_API bool emipc_start_proxy_socket()
37 {
38         EM_DEBUG_FUNC_BEGIN();
39         int ret = true;
40
41         ret = emipc_init_email_socket(&proxy_socket_fd);
42         if (!ret) {
43                 return false;
44         }
45
46         ret = emipc_connect_email_socket(proxy_socket_fd);
47
48         return ret;
49 }
50
51 EXPORT_API bool emipc_end_proxy_socket()
52 {
53         EM_DEBUG_FUNC_BEGIN();
54         EM_DEBUG_LOG("[IPCLib] emipc_end_proxy_socket_fd");
55         
56         if (proxy_socket_fd) {
57                 emipc_close_email_socket(&proxy_socket_fd);
58         }
59
60         return true;
61 }
62
63 /* return result of emipc_send_email_socket
64  * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection */
65 EXPORT_API int emipc_send_proxy_socket(unsigned char *data, int len)
66 {
67         EM_DEBUG_FUNC_BEGIN();
68         if (!proxy_socket_fd) {
69                 EM_DEBUG_EXCEPTION("[IPCLib] emipc_send_proxy_socket_fd not connect");
70                 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
71         }
72         int send_len = emipc_send_email_socket(proxy_socket_fd, data, len);
73         if (send_len == 0) {
74                 EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", proxy_socket_fd);
75                 emipc_close_email_socket(&proxy_socket_fd);
76         }
77         return send_len;
78 }
79
80 EXPORT_API int emipc_get_proxy_socket_id()
81 {
82         EM_DEBUG_FUNC_BEGIN();
83         return proxy_socket_fd;
84 }
85
86 /* return true, when event occurred
87  * false, when select error
88  */
89 static bool wait_for_reply (int fd)
90 {
91         fd_set fds;
92
93         if (fd == 0) {
94                 EM_DEBUG_EXCEPTION("Invalid file description : [%d]", fd);
95                 return false;
96         }
97
98         FD_ZERO(&fds);
99         FD_SET(fd, &fds);
100
101         if (select(fd + 1, &fds, NULL, NULL, NULL) == -1) {
102                 EM_DEBUG_EXCEPTION("[IPCLib] select: %s", strerror(errno) );
103                 return false;
104         }
105
106         if (FD_ISSET(fd, &fds)) return true;
107
108         return false;
109 }
110
111
112 /* return result of emipc_recv_email_socket
113  * EMAIL_ERROR_IPC_SOCKET_FAILURE, when no IPC connection, or wrong fd */
114 EXPORT_API int emipc_recv_proxy_socket(char **data)
115 {
116         EM_DEBUG_FUNC_BEGIN();
117
118         if (!proxy_socket_fd) {
119                 EM_DEBUG_EXCEPTION("[IPCLib] proxy_socket_fd[%p] is not available or disconnected", proxy_socket_fd);
120                 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
121         }
122
123         if( !wait_for_reply(proxy_socket_fd) ) {
124
125                 return EMAIL_ERROR_IPC_SOCKET_FAILURE;
126         }
127
128         int recv_len = emipc_recv_email_socket(proxy_socket_fd, data);
129         if (recv_len == 0) {
130                 EM_DEBUG_EXCEPTION("[IPCLib] server closed connection %x", proxy_socket_fd);
131                 emipc_close_email_socket(&proxy_socket_fd);
132         }
133
134         return recv_len;
135 }
136