2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "hs-clientmanager.h"
20 #include "hs-apprecover.h"
22 static const char _homescreen[] = "homescreen";
23 static const char _area[] = "area";
24 static const char _parameter[] = "parameter";
26 HS_ClientManager* HS_ClientManager::me = nullptr;
28 static void cbRemoveClientCtxt(void *data)
30 HS_ClientManager::instance()->removeClientCtxt(data);
34 * HS_ClientManager construction function
43 HS_ClientManager::HS_ClientManager()
54 * HS_ClientManager instance pointer
57 HS_ClientManager* HS_ClientManager::instance(void)
60 me = new HS_ClientManager();
66 * HS_ClientManager init function
75 int HS_ClientManager::init(void)
77 listener_list.clear();
81 * create client's afb_req_context
87 * HS_ClientCtxt pointer
90 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
92 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
95 AFB_INFO( "create new session for %s", appid.c_str());
96 HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
97 afb_req_session_set_LOA(req, 1);
98 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
113 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
115 return (client_list[appid] = new HS_Client(req, appid));
128 void HS_ClientManager::removeClient(std::string appid)
130 delete client_list[appid];
131 client_list.erase(appid);
135 * remove Client from list
138 * - data: HS_ClientCtxt pointer
144 void HS_ClientManager::removeClientCtxt(void *data)
146 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
149 AFB_WARNING( "data is nullptr");
153 AFB_INFO( "remove app %s", ctxt->id.c_str());
154 std::lock_guard<std::mutex> lock(this->mtx);
155 removeClient(ctxt->id);
156 delete appid2ctxt[ctxt->id];
157 appid2ctxt.erase(ctxt->id);
161 * handle homescreen request
164 * - request : the request
165 * - verb : the verb name
166 * - appid : to which application
173 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
175 AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
177 bool isRegisterApp = false;
178 if(appid == nullptr) {
179 std::lock_guard<std::mutex> lock(this->mtx);
180 for(auto m : client_list) {
181 m.second->handleRequest(request, verb);
185 std::lock_guard<std::mutex> lock(this->mtx);
186 auto ip = client_list.find(std::string(appid));
187 if(ip != client_list.end()) {
188 ret = ip->second->handleRequest(request, verb);
191 if(!strcasecmp(verb, "subscribe")) {
192 appid2ctxt[appid] = createClientCtxt(request, appid);
193 HS_Client* client = addClient(request, appid);
194 ret = client->handleRequest(request, "subscribe");
195 isRegisterApp = true;
198 AFB_NOTICE("not exist session");
199 ret = AFB_REQ_NOT_STARTED_APPLICATION;
204 notifyListener(request->api, std::string(appid));
213 * - event : the event want to push
214 * - param : the parameter contents of event
215 * - appid : the destination application's id
222 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
224 AFB_INFO("event=[%s], appid=[%s].", event, appid.c_str());
225 if(event == nullptr) {
226 AFB_WARNING("event name is null.");
230 std::lock_guard<std::mutex> lock(this->mtx);
231 if(appid.empty()) { // broadcast event to clients who subscribed this event
232 for(auto m : client_list) {
233 m.second->pushEvent(event, param);
236 else { // push event to specific client
237 auto ip = client_list.find(appid);
238 if(ip != client_list.end()) {
239 ip->second->pushEvent(event, param);
247 * check whether application was started
250 * - appid : application's id
257 bool HS_ClientManager::isAppStarted(const std::string &appid)
259 auto it = client_list.find(appid);
260 return it != client_list.end() ? true : false;
264 * add app register listener
267 * - listener_interface : listener interface
273 void HS_ClientManager::addListener(listener_interface* listener)
275 listener_list[listener->myUid()] = listener;
279 * remove app register listener
282 * - listener_interface : listener interface
288 void HS_ClientManager::removeListener(listener_interface* listener)
290 listener_list.erase(listener->myUid());
298 * - appid : register application's id
304 void HS_ClientManager::notifyListener(afb_api_t api, const std::string &appid)
306 AFB_INFO("listen %s, notified", appid.c_str());
307 for(auto &it : listener_list) {
308 if(it.second->isListenAppId(appid)) {
309 it.second->notify(api, appid);
313 if(startup_appid == appid) {
314 struct json_object* json_param = json_object_new_object();
315 json_object_object_add(json_param, _area, json_object_new_string(startup_area.c_str()));
316 startup_area.clear();
317 startup_appid.clear();
318 pushEvent("showWindow", json_param, appid);