2 * Copyright 2020 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "client_manager.h"
18 #include "service_common.h"
20 int CClientManager::set_application_manager(IApplicationManager* manager)
22 mApplicationManager = manager;
26 int CClientManager::create_client(pid_t pid, std::string appid, std::string sender_info)
28 ma_client_s* data = NULL;
31 data = new ma_client_s;
32 } catch(const std::bad_alloc& e) {
33 MAS_LOGE("[ERROR] Fail to allocate memory : %s", e.what());
34 return -1;// MA_ERROR_OUT_OF_MEMORY;
40 data->sender_info = sender_info;
41 mClientList = g_slist_append(mClientList, data);
42 MAS_LOGI("Create client : %s %d", appid.c_str(), pid);
44 MAS_LOGE("[ERROR] data is NULL");
45 return -1;// MA_ERROR_OUT_OF_MEMORY;
51 int CClientManager::destroy_client(ma_client_s* client)
53 if (nullptr == client) {
54 MAS_LOGE("Input parameter is NULL");
55 return -1;// MA_ERROR_OPERATION_FAILED;
58 mClientList = g_slist_remove(mClientList, client);
65 int CClientManager::destroy_client_by_pid(pid_t pid)
67 ma_client_s* client = find_client_by_pid(pid);
68 return destroy_client(client);
71 int CClientManager::destroy_client_by_appid(std::string appid)
73 ma_client_s* client = find_client_by_appid(appid);
74 return destroy_client(client);
77 ma_client_s* CClientManager::find_client_by_appid(std::string appid)
79 ma_client_s *data = NULL;
81 int count = g_slist_length(mClientList);
84 for (i = 0; i < count; i++) {
85 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
88 if (0 == appid.compare(data->appid)) {
94 MAS_LOGE("[ERROR] client Not found : %s", appid.c_str());
99 ma_client_s* CClientManager::find_client_by_pid(pid_t pid)
101 ma_client_s *data = NULL;
103 int count = g_slist_length(mClientList);
106 for (i = 0; i < count; i++) {
107 data = static_cast<ma_client_s*>(g_slist_nth_data(mClientList, i));
110 if (data->pid == pid) {
116 MAS_LOGE("[ERROR] client Not found");
121 int CClientManager::get_client_num()
123 return g_slist_length(mClientList);
126 ma_client_s* CClientManager::get_client_by_index(unsigned int index)
128 return static_cast<ma_client_s*>(g_slist_nth_data(mClientList, index));
131 pid_t CClientManager::find_client_pid_by_index(unsigned int index)
134 ma_client_s* client = get_client_by_index(index);
141 pid_t CClientManager::find_client_pid_by_appid(std::string appid)
145 if (nullptr == mApplicationManager) {
146 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
150 ma_client_s* client = find_client_by_appid(appid);
152 bool running = mApplicationManager->is_application_running(client->pid);
153 if (false == running) {
154 MAS_LOGE("The PID for %s was %d, but it seems to be terminated",
155 appid.c_str(), client->pid);
161 MAS_LOGE("PID lookup failed for : %s", appid.c_str());
166 std::string CClientManager::find_client_appid_by_pid(pid_t pid)
170 if (nullptr == mApplicationManager) {
171 MAS_LOGE("ApplicationManager is NULL, can't check if app is running or not");
175 ma_client_s* client = find_client_by_pid(pid);
177 bool running = mApplicationManager->is_application_running(pid);
178 if (false == running) {
179 MAS_LOGE("The appid for %d was %s, but it seems to be terminated",
180 pid, client->appid.c_str());
182 appid = client->appid;
188 std::string CClientManager::find_sender_info_by_pid(pid_t pid)
191 ma_client_s* client = find_client_by_pid(pid);
193 ret = client->sender_info;
198 bool CClientManager::check_client_validity_by_appid(std::string appid)
200 pid_t pid = find_client_pid_by_appid(appid);
201 if (0 < pid) return true;
205 bool CClientManager::check_sender_info_by_pid(pid_t pid, std::string sender_info)
208 ma_client_s* client = find_client_by_pid(pid);
210 if (client->sender_info.compare(sender_info) == 0) {
215 MAS_LOGE("SENDER INFO does not match : [%s] [%s]",
216 (client ? client->sender_info.c_str() : ""),
217 sender_info.c_str());