hs-proxy: Keep track of clientCtx and client when starting application
[apps/agl-service-homescreen.git] / src / hs-clientmanager.cpp
1 /*
2  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
3  * Copyright (C) 2020 Konsulko Group
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <cstring>
19 #include <algorithm>
20 #include "hs-clientmanager.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     return 0;
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         ctxt = new HS_ClientCtxt(appid);
95         afb_req_session_set_LOA(req, 1);
96         afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
97     }
98
99     appid2ctxt[appid] = ctxt;
100     return ctxt;
101 }
102
103 /**
104  * add Client
105  *
106  * #### Parameters
107  *  - ctxt: app's id
108  *
109  * #### Return
110  * HS_Client pointer
111  *
112  */
113 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
114 {
115     return (client_list[appid] = new HS_Client(req, appid));
116 }
117
118 /**
119  * remove Client
120  *
121  * #### Parameters
122  *  - appid: app's id
123  *
124  * #### Return
125  * None
126  *
127  */
128 void HS_ClientManager::removeClient(std::string appid)
129 {
130     delete client_list[appid];
131     client_list.erase(appid);
132 }
133
134 /**
135  * remove Client from list
136  *
137  * #### Parameters
138  *  - data: HS_ClientCtxt pointer
139  *
140  * #### Return
141  * None
142  *
143  */
144 void HS_ClientManager::removeClientCtxt(void *data)
145 {
146     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
147     if(ctxt == nullptr)
148     {
149         AFB_WARNING( "data is nullptr");
150         return;
151     }
152
153     AFB_INFO( "remove app %s", ctxt->id.c_str());
154     std::lock_guard<std::mutex> lock(this->mtx);
155     removeClient(ctxt->id);
156     delete appid2ctxt[ctxt->id];
157     appid2ctxt.erase(ctxt->id);
158 }
159
160 /**
161  * handle homescreen request
162  *
163  * #### Parameters
164  *  - request : the request
165  *  - verb : the verb name
166  *  - appid : to which application
167  *
168  * #### Return
169  * 0 : success
170  * others : fail
171  *
172  */
173 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
174 {
175     AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
176     int ret = 0;
177     std::lock_guard<std::mutex> lock(this->mtx);
178     if(appid == nullptr) {
179         for(auto m : client_list) {
180             m.second->handleRequest(request, verb);
181         }
182     }
183     else {
184         std::string id(appid);
185         auto ip = client_list.find(id);
186         if(ip != client_list.end()) {
187             // FIXME: do another verification here in case the application died
188             ret = ip->second->handleRequest(request, verb);
189         }
190         else {
191             if(!strcasecmp(verb, "subscribe")) {
192                 createClientCtxt(request, id);
193                 HS_Client* client = addClient(request, id);
194                 ret = client->handleRequest(request, "subscribe");
195             }
196             else {
197                 AFB_NOTICE("not exist session");
198                 ret = AFB_REQ_NOT_STARTED_APPLICATION;
199             }
200         }
201     }
202     return ret;
203 }
204
205 /**
206  * push event
207  *
208  * #### Parameters
209  *  - event : the event want to push
210  *  - param : the parameter contents of event
211  *  - appid : the destination application's id
212  *
213  * #### Return
214  * 0 : success
215  * others : fail
216  *
217  */
218 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
219 {
220     if(event == nullptr) {
221         AFB_WARNING("event name is null.");
222         return -1;
223     }
224
225     std::lock_guard<std::mutex> lock(this->mtx);
226     if(appid.empty()) { // broadcast event to clients who subscribed this event
227         for(auto m : client_list) {
228             m.second->pushEvent(event, param);
229         }
230     }
231     else {  // push event to specific client
232         auto ip = client_list.find(appid);
233         if(ip != client_list.end()) {
234             ip->second->pushEvent(event, param);
235         }
236     }
237
238     return 0;
239 }