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)
74 AFB_NOTICE("called.");
78 * create client's afb_req_context
84 * HS_ClientCtxt pointer
87 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
89 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
92 AFB_INFO( "create new session for %s", appid.c_str());
93 HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
94 afb_req_session_set_LOA(req, 1);
95 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
110 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
112 return (client_list[appid] = new HS_Client(req, appid));
125 void HS_ClientManager::removeClient(std::string appid)
127 delete client_list[appid];
128 client_list.erase(appid);
132 * remove Client from list
135 * - data: HS_ClientCtxt pointer
141 void HS_ClientManager::removeClientCtxt(void *data)
143 HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
146 AFB_WARNING( "data is nullptr");
150 AFB_INFO( "remove app %s", ctxt->id.c_str());
151 std::lock_guard<std::mutex> lock(this->mtx);
152 removeClient(ctxt->id);
153 delete appid2ctxt[ctxt->id];
154 appid2ctxt.erase(ctxt->id);
158 * handle homescreen request
161 * - request : the request
162 * - verb : the verb name
163 * - appid : to which application
170 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
172 AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
174 std::lock_guard<std::mutex> lock(this->mtx);
175 if(appid == nullptr) {
176 for(auto m : client_list) {
177 m.second->handleRequest(request, verb);
181 auto ip = client_list.find(std::string(appid));
182 if(ip != client_list.end()) {
183 ret = ip->second->handleRequest(request, verb);
186 if(!strcasecmp(verb, "subscribe")) {
187 appid2ctxt[appid] = createClientCtxt(request, appid);
188 HS_Client* client = addClient(request, appid);
189 ret = client->handleRequest(request, "subscribe");
190 registerApplication(appid);
193 AFB_NOTICE("not exist session");
194 ret = AFB_REQ_NOT_STARTED_APPLICATION;
205 * - event : the event want to push
206 * - param : the parameter contents of event
207 * - appid : the destination application's id
214 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
216 if(event == nullptr) {
217 AFB_WARNING("event name is null.");
221 std::lock_guard<std::mutex> lock(this->mtx);
222 if(appid.empty()) { // broadcast event to clients who subscribed this event
223 for(auto m : client_list) {
224 m.second->pushEvent(event, param);
227 else { // push event to specific client
228 auto ip = client_list.find(appid);
229 if(ip != client_list.end()) {
230 ip->second->pushEvent(event, param);
238 * register recovered application
241 * - appid : application id
247 void HS_ClientManager::registerApplication(std::string appid)