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"
21 static const char _homescreen[] = "homescreen";
23 HS_ClientManager* HS_ClientManager::me = nullptr;
25 static void cbRemoveClientCtxt(void *data)
27 HS_ClientManager::instance()->removeClientCtxt(data);
31 * HS_ClientManager construction function
40 HS_ClientManager::HS_ClientManager()
51 * HS_ClientManager instance pointer
54 HS_ClientManager* HS_ClientManager::instance(void)
57 me = new HS_ClientManager();
63 * HS_ClientManager init function
72 int HS_ClientManager::init(void)
77 * create client's afb_req_context
83 * HS_ClientCtxt pointer
86 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
88 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
91 AFB_INFO( "create new session for %s", appid.c_str());
92 HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
93 afb_req_session_set_LOA(req, 1);
94 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
109 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
111 return (client_list[appid] = new HS_Client(req, appid));
124 void HS_ClientManager::removeClient(std::string appid)
126 delete client_list[appid];
127 client_list.erase(appid);
131 * remove Client from list
134 * - data: HS_ClientCtxt pointer
140 void HS_ClientManager::removeClientCtxt(void *data)
142 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
145 AFB_WARNING( "data is nullptr");
149 AFB_INFO( "remove app %s", ctxt->id.c_str());
150 std::lock_guard<std::mutex> lock(this->mtx);
151 removeClient(ctxt->id);
152 delete appid2ctxt[ctxt->id];
153 appid2ctxt.erase(ctxt->id);
157 * handle homescreen request
160 * - request : the request
161 * - verb : the verb name
162 * - appid : to which application
169 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
171 AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
173 std::lock_guard<std::mutex> lock(this->mtx);
174 if(appid == nullptr) {
175 for(auto m : client_list) {
176 m.second->handleRequest(request, verb);
180 auto ip = client_list.find(std::string(appid));
181 if(ip != client_list.end()) {
182 ret = ip->second->handleRequest(request, verb);
185 if(!strcasecmp(verb, "subscribe")) {
186 appid2ctxt[appid] = createClientCtxt(request, appid);
187 HS_Client* client = addClient(request, appid);
188 ret = client->handleRequest(request, "subscribe");
191 AFB_NOTICE("not exist session");
192 ret = AFB_REQ_NOT_STARTED_APPLICATION;
203 * - event : the event want to push
204 * - param : the parameter contents of event
205 * - appid : the destination application's id
212 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
214 if(event == nullptr) {
215 AFB_WARNING("event name is null.");
219 std::lock_guard<std::mutex> lock(this->mtx);
220 if(appid.empty()) { // broadcast event to clients who subscribed this event
221 for(auto m : client_list) {
222 m.second->pushEvent(event, param);
225 else { // push event to specific client
226 auto ip = client_list.find(appid);
227 if(ip != client_list.end()) {
228 ip->second->pushEvent(event, param);