ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION during disconnect_on_thread
[platform/core/pim/contacts-service.git] / client / ctsvc_client_handle.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <glib.h>
21 #include <unistd.h>
22
23 #include "ctsvc_internal.h"
24 #include "ctsvc_handle.h"
25 #include "ctsvc_mutex.h"
26 #include "ctsvc_client_utils.h"
27 #include "ctsvc_client_handle.h"
28
29 static GHashTable *_ctsvc_handle_table = NULL;
30
31 static int _ctsvc_client_handle_get_key(unsigned int id, char *key, int key_len)
32 {
33         int ret;
34         int len;
35
36         ret = gethostname(key, key_len);
37         RETVM_IF(0 != ret, CONTACTS_ERROR_SYSTEM, "gethostname() Failed(%d)", errno);
38
39         len = strlen(key);
40         snprintf(key+len, key_len-len, ":%u", id);
41
42         return CONTACTS_ERROR_NONE;
43 }
44
45 int ctsvc_client_handle_get_p_with_id(unsigned int id, contacts_h *p_contact)
46 {
47         int ret;
48         char key[CTSVC_STR_SHORT_LEN] = {0};
49         contacts_h contact = NULL;
50
51         RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA, "_ctsvc_handle_table is NULL");
52
53         ret = _ctsvc_client_handle_get_key(id, key, sizeof(key));
54         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
55
56         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
57         contact = g_hash_table_lookup(_ctsvc_handle_table, key);
58         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
59
60         if (NULL == contact) {
61                 CTS_ERR("g_hash_table_lookup() return NULL");
62                 return CONTACTS_ERROR_NO_DATA;
63         }
64
65         *p_contact = contact;
66         return CONTACTS_ERROR_NONE;
67 }
68
69 int ctsvc_client_handle_get_p(contacts_h *p_contact)
70 {
71         int ret;
72         char key[CTSVC_STR_SHORT_LEN] = {0};
73         contacts_h contact = NULL;
74
75         RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA, "_ctsvc_handle_table is NULL");
76
77         ret = _ctsvc_client_handle_get_key(ctsvc_client_get_tid(), key, sizeof(key));
78         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
79
80         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
81         contact = g_hash_table_lookup(_ctsvc_handle_table, key);
82         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
83
84         if (NULL == contact) {
85                 ret = _ctsvc_client_handle_get_key(ctsvc_client_get_pid(), key, sizeof(key));
86                 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
87
88                 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
89                 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
90                 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
91
92                 RETVM_IF(NULL == contact, CONTACTS_ERROR_NO_DATA, "g_hash_table_lookup() return NULL");
93         }
94         *p_contact = contact;
95         return CONTACTS_ERROR_NONE;
96 }
97
98 int ctsvc_client_handle_create(unsigned int id, contacts_h *p_contact)
99 {
100         int ret;
101         char handle_key[CTSVC_STR_SHORT_LEN] = {0};
102         contacts_h contact = NULL;
103
104         RETVM_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER, "p_contact is NULL");
105         *p_contact = NULL;
106
107         ret = _ctsvc_client_handle_get_key(id, handle_key, sizeof(handle_key));
108         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
109
110         ret = ctsvc_handle_create(&contact);
111         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_handle_create() Fail(%d)", ret);
112
113         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
114         if (NULL == _ctsvc_handle_table)
115                 _ctsvc_handle_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
116
117         g_hash_table_insert(_ctsvc_handle_table, strdup(handle_key), contact);
118         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
119
120         *p_contact = contact;
121         return CONTACTS_ERROR_NONE;
122 }
123
124 int ctsvc_client_handle_remove(unsigned int id, contacts_h contact)
125 {
126         int ret;
127         char key[CTSVC_STR_SHORT_LEN] = {0};
128         RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NONE, "_ctsvc_handle_table is NULL");
129
130         ret = _ctsvc_client_handle_get_key(id, key, sizeof(key));
131         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
132
133         ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
134         if (false == g_hash_table_remove(_ctsvc_handle_table, key))
135                 CTS_ERR("g_hash_table_remove() Fail. key:%s", key);
136
137         if (0 == g_hash_table_size(_ctsvc_handle_table)) {
138                 g_hash_table_destroy(_ctsvc_handle_table);
139                 _ctsvc_handle_table = NULL;
140         }
141         ctsvc_handle_destroy(contact);
142         ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
143
144         return CONTACTS_ERROR_NONE;
145 }
146
147