2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
3 * Copyright (C) 2020 Konsulko Group
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "hs-clientmanager.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)
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 ctxt = new HS_ClientCtxt(appid);
95 afb_req_session_set_LOA(req, 1);
96 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
98 appid2ctxt[appid] = ctxt;
114 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
116 return (client_list[appid] = new HS_Client(req, appid));
129 void HS_ClientManager::removeClient(std::string appid)
131 delete client_list[appid];
132 client_list.erase(appid);
136 * remove Client from list
139 * - data: HS_ClientCtxt pointer
145 void HS_ClientManager::removeClientCtxt(void *data)
147 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
150 AFB_WARNING( "data is nullptr");
154 AFB_INFO( "remove app %s", ctxt->id.c_str());
155 std::lock_guard<std::mutex> lock(this->mtx);
156 removeClient(ctxt->id);
157 delete appid2ctxt[ctxt->id];
158 appid2ctxt.erase(ctxt->id);
162 * handle homescreen request
165 * - request : the request
166 * - verb : the verb name
167 * - appid : to which application
174 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
176 AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
178 std::lock_guard<std::mutex> lock(this->mtx);
179 if(appid == nullptr) {
180 for(auto m : client_list) {
181 m.second->handleRequest(request, verb);
185 std::string id(appid);
186 auto ip = client_list.find(id);
187 if(ip != client_list.end()) {
188 // FIXME: do another verification here in case the application died
189 ret = ip->second->handleRequest(request, verb);
192 if(!strcasecmp(verb, "subscribe")) {
193 createClientCtxt(request, id);
194 HS_Client* client = addClient(request, id);
195 ret = client->handleRequest(request, "subscribe");
198 AFB_NOTICE("not exist session");
199 ret = AFB_REQ_NOT_STARTED_APPLICATION;
210 * - event : the event want to push
211 * - param : the parameter contents of event
212 * - appid : the destination application's id
219 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
221 if(event == nullptr) {
222 AFB_WARNING("event name is null.");
226 std::lock_guard<std::mutex> lock(this->mtx);
227 if(appid.empty()) { // broadcast event to clients who subscribed this event
228 for(auto m : client_list) {
229 m.second->pushEvent(event, param);
232 else { // push event to specific client
233 auto ip = client_list.find(appid);
234 if(ip != client_list.end()) {
235 ip->second->pushEvent(event, param);