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";
25 const std::string _listen_all("all");
27 HS_ClientManager* HS_ClientManager::me = nullptr;
29 static void cbRemoveClientCtxt(void *data)
31 HS_ClientManager::instance()->removeClientCtxt(data);
35 * HS_ClientManager construction function
44 HS_ClientManager::HS_ClientManager()
55 * HS_ClientManager instance pointer
58 HS_ClientManager* HS_ClientManager::instance(void)
61 me = new HS_ClientManager();
67 * HS_ClientManager init function
76 int HS_ClientManager::init(void)
78 listener_list.clear();
79 std::list<listener_interface*> interface_list;
80 listener_list[_listen_all] = std::move(interface_list);
84 * create client's afb_req_context
90 * HS_ClientCtxt pointer
93 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
95 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
98 AFB_INFO( "create new session for %s", appid.c_str());
99 HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
100 afb_req_session_set_LOA(req, 1);
101 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
116 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
118 return (client_list[appid] = new HS_Client(req, appid));
131 void HS_ClientManager::removeClient(std::string appid)
133 delete client_list[appid];
134 client_list.erase(appid);
138 * remove Client from list
141 * - data: HS_ClientCtxt pointer
147 void HS_ClientManager::removeClientCtxt(void *data)
149 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
152 AFB_WARNING( "data is nullptr");
156 AFB_INFO( "remove app %s", ctxt->id.c_str());
157 std::lock_guard<std::mutex> lock(this->mtx);
158 removeClient(ctxt->id);
159 delete appid2ctxt[ctxt->id];
160 appid2ctxt.erase(ctxt->id);
164 * handle homescreen request
167 * - request : the request
168 * - verb : the verb name
169 * - appid : to which application
176 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
178 AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
180 bool isRegisterApp = false;
181 if(appid == nullptr) {
182 std::lock_guard<std::mutex> lock(this->mtx);
183 for(auto m : client_list) {
184 m.second->handleRequest(request, verb);
188 std::lock_guard<std::mutex> lock(this->mtx);
189 auto ip = client_list.find(std::string(appid));
190 if(ip != client_list.end()) {
191 ret = ip->second->handleRequest(request, verb);
194 if(!strcasecmp(verb, "subscribe")) {
195 appid2ctxt[appid] = createClientCtxt(request, appid);
196 HS_Client* client = addClient(request, appid);
197 ret = client->handleRequest(request, "subscribe");
198 isRegisterApp = true;
201 AFB_NOTICE("not exist session");
202 ret = AFB_REQ_NOT_STARTED_APPLICATION;
207 notifyListener(request->api, std::string(appid));
216 * - event : the event want to push
217 * - param : the parameter contents of event
218 * - appid : the destination application's id
225 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
227 AFB_INFO("event=[%s], appid=[%s].", event, appid.c_str());
228 if(event == nullptr) {
229 AFB_WARNING("event name is null.");
233 std::lock_guard<std::mutex> lock(this->mtx);
234 if(appid.empty()) { // broadcast event to clients who subscribed this event
235 for(auto m : client_list) {
236 m.second->pushEvent(event, param);
239 else { // push event to specific client
240 auto ip = client_list.find(appid);
241 if(ip != client_list.end()) {
242 ip->second->pushEvent(event, param);
250 * check register application
254 * - appid : register application's id
258 * false : not checked
261 bool HS_ClientManager::checkRegisterApp(afb_api_t api, const std::string &appid)
264 auto &ip = listener_list[_listen_all];
267 it->notify(api, appid);
270 else if(startup_appid == appid) {
271 startup_appid.clear();
272 struct json_object* json_param = json_object_new_object();
273 json_object_object_add(json_param, _area, json_object_new_string(startup_area.c_str()));
274 pushEvent("showWindow", json_param, appid);
275 startup_area.clear();
284 * check whether application was started
287 * - appid : application's id
294 bool HS_ClientManager::isAppStarted(const std::string &appid)
296 auto it = client_list.find(appid);
297 return it != client_list.end() ? true : false;
301 * add app register listener
304 * - listener_interface : listener interface
310 void HS_ClientManager::addListener(listener_interface* listener)
312 for (auto &it : listener->listenAppSet()) {
313 auto ip = listener_list.find(it);
314 if(ip != listener_list.end()) {
315 ip->second.push_back(listener);
318 std::list<listener_interface*> lst;
319 lst.push_back(listener);
320 listener_list[it] = std::move(lst);
326 * remove app register listener
329 * - listener_interface : listener interface
335 void HS_ClientManager::removeListener(listener_interface* listener)
337 for (auto &iter : listener->listenAppSet()) {
338 auto it = listener_list.find(iter);
339 if(it != listener_list.end()) {
340 auto ip = it->second.begin();
341 for(; ip != it->second.end(); ++ip) {
342 if(listener->myUid() == (*ip)->myUid()) {
346 it->second.erase(ip);
347 if(it->second.empty()) {
348 listener_list.erase(it->first);
359 * - appid : register application's id
365 void HS_ClientManager::notifyListener(afb_api_t api, const std::string &appid)
367 if (checkRegisterApp(api, appid)) {
371 AFB_INFO("listen %s, notified", appid.c_str());
372 std::list<listener_interface*> interface_list;
373 auto ip = listener_list.find(appid);
374 if(ip != listener_list.end()) {
375 if(!ip->second.empty()) {
376 interface_list = ip->second;
379 AFB_WARNING("listener is null.");
384 for(auto &it : interface_list) {
385 it->notify(api, appid);