4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 static GHashTable *_ctsvc_handle_table = NULL;
31 static int _ctsvc_client_handle_get_key(unsigned int id, char *key, int key_len)
36 ret = gethostname(key, key_len);
37 RETVM_IF(0 != ret, CONTACTS_ERROR_SYSTEM, "gethostname() Failed(%d)", errno);
40 snprintf(key+len, key_len-len, ":%u", id);
42 return CONTACTS_ERROR_NONE;
45 int ctsvc_client_handle_get_p_with_id(unsigned int id, contacts_h *p_contact)
48 char key[CTSVC_STR_SHORT_LEN] = {0};
49 contacts_h contact = NULL;
51 RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA, "_ctsvc_handle_table is NULL");
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);
56 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
57 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
58 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
61 return CONTACTS_ERROR_NONE;
64 int ctsvc_client_handle_get_p(contacts_h *p_contact)
67 char key[CTSVC_STR_SHORT_LEN] = {0};
68 contacts_h contact = NULL;
70 RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NO_DATA, "_ctsvc_handle_table is NULL");
72 ret = _ctsvc_client_handle_get_key(ctsvc_client_get_tid(), key, sizeof(key));
73 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
75 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
76 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
77 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
79 if (NULL == contact) {
80 ret = _ctsvc_client_handle_get_key(ctsvc_client_get_pid(), key, sizeof(key));
81 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
83 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
84 contact = g_hash_table_lookup(_ctsvc_handle_table, key);
85 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
87 RETVM_IF(NULL == contact, CONTACTS_ERROR_NO_DATA, "g_hash_table_lookup() return NULL");
90 return CONTACTS_ERROR_NONE;
93 int ctsvc_client_handle_create(unsigned int id, contacts_h *p_contact)
96 char handle_key[CTSVC_STR_SHORT_LEN] = {0};
97 contacts_h contact = NULL;
99 RETVM_IF(NULL == p_contact, CONTACTS_ERROR_INVALID_PARAMETER, "p_contact is NULL");
102 ret = _ctsvc_client_handle_get_key(id, handle_key, sizeof(handle_key));
103 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
105 ret = ctsvc_handle_create(&contact);
106 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_handle_create() Fail(%d)", ret);
108 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
109 if (NULL == _ctsvc_handle_table)
110 _ctsvc_handle_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
112 g_hash_table_insert(_ctsvc_handle_table, strdup(handle_key), contact);
113 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
115 *p_contact = contact;
116 return CONTACTS_ERROR_NONE;
119 int ctsvc_client_handle_remove(unsigned int id, contacts_h contact)
122 char key[CTSVC_STR_SHORT_LEN] = {0};
123 RETVM_IF(NULL == _ctsvc_handle_table, CONTACTS_ERROR_NONE, "_ctsvc_handle_table is NULL");
125 ret = _ctsvc_client_handle_get_key(id, key, sizeof(key));
126 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "_ctsvc_client_handle_get_key() Fail(%d)", ret);
128 ctsvc_mutex_lock(CTS_MUTEX_HANDLE);
129 g_hash_table_remove(_ctsvc_handle_table, key);
130 if (0 == g_hash_table_size(_ctsvc_handle_table)) {
131 g_hash_table_destroy(_ctsvc_handle_table);
132 _ctsvc_handle_table = NULL;
134 ctsvc_mutex_unlock(CTS_MUTEX_HANDLE);
135 ctsvc_handle_destroy(contact);
137 return CONTACTS_ERROR_NONE;