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";
24 HS_ClientManager* HS_ClientManager::me = nullptr;
26 static void cbRemoveClientCtxt(void *data)
28 HS_ClientManager::instance()->removeClientCtxt(data);
32 * HS_ClientManager construction function
41 HS_ClientManager::HS_ClientManager()
52 * HS_ClientManager instance pointer
55 HS_ClientManager* HS_ClientManager::instance(void)
58 me = new HS_ClientManager();
64 * HS_ClientManager init function
73 int HS_ClientManager::init(void)
75 AFB_NOTICE("called.");
79 * create client's afb_req_context
85 * HS_ClientCtxt pointer
88 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
90 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
93 AFB_INFO( "create new session for %s", appid.c_str());
94 HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
95 afb_req_session_set_LOA(req, 1);
96 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
111 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
113 return (client_list[appid] = new HS_Client(req, appid));
126 void HS_ClientManager::removeClient(std::string appid)
128 delete client_list[appid];
129 client_list.erase(appid);
133 * remove Client from list
136 * - data: HS_ClientCtxt pointer
142 void HS_ClientManager::removeClientCtxt(void *data)
144 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
147 AFB_WARNING( "data is nullptr");
151 AFB_INFO( "remove app %s", ctxt->id.c_str());
152 std::lock_guard<std::mutex> lock(this->mtx);
153 removeClient(ctxt->id);
154 delete appid2ctxt[ctxt->id];
155 appid2ctxt.erase(ctxt->id);
159 * handle homescreen request
162 * - request : the request
163 * - verb : the verb name
164 * - appid : to which application
171 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
173 AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
175 bool isRegisterApp = false;
176 if(appid == nullptr) {
177 std::lock_guard<std::mutex> lock(this->mtx);
178 for(auto m : client_list) {
179 m.second->handleRequest(request, verb);
183 std::lock_guard<std::mutex> lock(this->mtx);
184 auto ip = client_list.find(std::string(appid));
185 if(ip != client_list.end()) {
186 ret = ip->second->handleRequest(request, verb);
189 if(!strcasecmp(verb, "subscribe")) {
190 appid2ctxt[appid] = createClientCtxt(request, appid);
191 HS_Client* client = addClient(request, appid);
192 ret = client->handleRequest(request, "subscribe");
193 isRegisterApp = true;
196 AFB_NOTICE("not exist session");
197 ret = AFB_REQ_NOT_STARTED_APPLICATION;
202 checkRegisterApp(request->api, std::string(appid));
211 * - event : the event want to push
212 * - param : the parameter contents of event
213 * - appid : the destination application's id
220 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
222 AFB_INFO("event=[%s], appid=[%s].", event, appid.c_str());
223 if(event == nullptr) {
224 AFB_WARNING("event name is null.");
228 std::lock_guard<std::mutex> lock(this->mtx);
229 if(appid.empty()) { // broadcast event to clients who subscribed this event
230 for(auto m : client_list) {
231 m.second->pushEvent(event, param);
234 else { // push event to specific client
235 auto ip = client_list.find(appid);
236 if(ip != client_list.end()) {
237 ip->second->pushEvent(event, param);
245 * check register application
249 * - appid : register application's id
255 void HS_ClientManager::checkRegisterApp(afb_api_t api, const std::string &appid)
257 if(HS_AppRecover::instance()->registerRecoveredApp(api, appid)) {
258 AFB_INFO("register recover application.");
262 if(startup_appid == appid) {
263 startup_appid.clear();
264 pushEvent("showWindow", nullptr, appid);