fix handshake bug
[apps/agl-service-homescreen.git] / src / hs-clientmanager.cpp
1 /*
2  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <cstring>
18 #include <algorithm>
19 #include "hs-clientmanager.h"
20 #include "hs-apprecover.h"
21
22 static const char _homescreen[] = "homescreen";
23
24 HS_ClientManager* HS_ClientManager::me = nullptr;
25
26 static void cbRemoveClientCtxt(void *data)
27 {
28     HS_ClientManager::instance()->removeClientCtxt(data);
29 }
30
31 /**
32  * HS_ClientManager construction function
33  *
34  * #### Parameters
35  *  - Nothing
36  *
37  * #### Return
38  * None
39  *
40  */
41 HS_ClientManager::HS_ClientManager()
42 {
43 }
44
45 /**
46  * get instance
47  *
48  * #### Parameters
49  *  - Nothing
50  *
51  * #### Return
52  * HS_ClientManager instance pointer
53  *
54  */
55 HS_ClientManager* HS_ClientManager::instance(void)
56 {
57     if(me == nullptr)
58         me = new HS_ClientManager();
59
60     return me;
61 }
62
63 /**
64  * HS_ClientManager init function
65  *
66  * #### Parameters
67  *  - Nothing
68  *
69  * #### Return
70  * init result
71  *
72  */
73 int HS_ClientManager::init(void)
74 {
75     AFB_NOTICE("called.");
76 }
77
78 /**
79  * create client's afb_req_context
80  *
81  * #### Parameters
82  *  - appid: app's id
83  *
84  * #### Return
85  * HS_ClientCtxt pointer
86  *
87  */
88 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
89 {
90     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
91     if (!ctxt)
92     {
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);
97     }
98     return ctxt;
99 }
100
101 /**
102  * add Client
103  *
104  * #### Parameters
105  *  - ctxt: app's id
106  *
107  * #### Return
108  * HS_Client pointer
109  *
110  */
111 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
112 {
113     return (client_list[appid] = new HS_Client(req, appid));
114 }
115
116 /**
117  * remove Client
118  *
119  * #### Parameters
120  *  - appid: app's id
121  *
122  * #### Return
123  * None
124  *
125  */
126 void HS_ClientManager::removeClient(std::string appid)
127 {
128     delete client_list[appid];
129     client_list.erase(appid);
130 }
131
132 /**
133  * remove Client from list
134  *
135  * #### Parameters
136  *  - data: HS_ClientCtxt pointer
137  *
138  * #### Return
139  * None
140  *
141  */
142 void HS_ClientManager::removeClientCtxt(void *data)
143 {
144     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
145     if(ctxt == nullptr)
146     {
147         AFB_WARNING( "data is nullptr");
148         return;
149     }
150
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);
156 }
157
158 /**
159  * handle homescreen request
160  *
161  * #### Parameters
162  *  - request : the request
163  *  - verb : the verb name
164  *  - appid : to which application
165  *
166  * #### Return
167  * 0 : success
168  * others : fail
169  *
170  */
171 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
172 {
173     AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
174     int ret = 0;
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);
180         }
181     }
182     else {
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);
187         }
188         else {
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;
194             }
195             else {
196                 AFB_NOTICE("not exist session");
197                 ret = AFB_REQ_NOT_STARTED_APPLICATION;
198             }
199         }
200     }
201     if(isRegisterApp) {
202         checkRegisterApp(request->api, std::string(appid));
203     }
204     return ret;
205 }
206
207 /**
208  * push event
209  *
210  * #### Parameters
211  *  - event : the event want to push
212  *  - param : the parameter contents of event
213  *  - appid : the destination application's id
214  *
215  * #### Return
216  * 0 : success
217  * others : fail
218  *
219  */
220 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
221 {
222     AFB_INFO("event=[%s], appid=[%s].", event, appid.c_str());
223     if(event == nullptr) {
224         AFB_WARNING("event name is null.");
225         return -1;
226     }
227
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);
232         }
233     }
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);
238         }
239     }
240
241     return 0;
242 }
243
244 /**
245  * check register application
246  *
247  * #### Parameters
248  *  - api : the api
249  *  - appid : register application's id
250  *
251  * #### Return
252  * None
253  *
254  */
255 void HS_ClientManager::checkRegisterApp(afb_api_t api, const std::string &appid)
256 {
257     if(HS_AppRecover::instance()->registerRecoveredApp(api, appid)) {
258         AFB_INFO("register recover application %s.", appid.c_str());
259         return;
260     }
261
262     if(startup_appid == appid) {
263         startup_appid.clear();
264         pushEvent("showWindow", nullptr, appid);
265     }
266 }