change hs_recoer
[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
21 static const char _homescreen[] = "homescreen";
22
23 HS_ClientManager* HS_ClientManager::me = nullptr;
24
25 static void cbRemoveClientCtxt(void *data)
26 {
27     HS_ClientManager::instance()->removeClientCtxt(data);
28 }
29
30 /**
31  * HS_ClientManager construction function
32  *
33  * #### Parameters
34  *  - Nothing
35  *
36  * #### Return
37  * None
38  *
39  */
40 HS_ClientManager::HS_ClientManager()
41 {
42 }
43
44 /**
45  * get instance
46  *
47  * #### Parameters
48  *  - Nothing
49  *
50  * #### Return
51  * HS_ClientManager instance pointer
52  *
53  */
54 HS_ClientManager* HS_ClientManager::instance(void)
55 {
56     if(me == nullptr)
57         me = new HS_ClientManager();
58
59     return me;
60 }
61
62 /**
63  * HS_ClientManager init function
64  *
65  * #### Parameters
66  *  - Nothing
67  *
68  * #### Return
69  * init result
70  *
71  */
72 int HS_ClientManager::init(void)
73 {
74     AFB_NOTICE("called.");
75 }
76
77 /**
78  * create client's afb_req_context
79  *
80  * #### Parameters
81  *  - appid: app's id
82  *
83  * #### Return
84  * HS_ClientCtxt pointer
85  *
86  */
87 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
88 {
89     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
90     if (!ctxt)
91     {
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);
96     }
97     return ctxt;
98 }
99
100 /**
101  * add Client
102  *
103  * #### Parameters
104  *  - ctxt: app's id
105  *
106  * #### Return
107  * HS_Client pointer
108  *
109  */
110 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
111 {
112     return (client_list[appid] = new HS_Client(req, appid));
113 }
114
115 /**
116  * remove Client
117  *
118  * #### Parameters
119  *  - appid: app's id
120  *
121  * #### Return
122  * None
123  *
124  */
125 void HS_ClientManager::removeClient(std::string appid)
126 {
127     delete client_list[appid];
128     client_list.erase(appid);
129 }
130
131 /**
132  * remove Client from list
133  *
134  * #### Parameters
135  *  - data: HS_ClientCtxt pointer
136  *
137  * #### Return
138  * None
139  *
140  */
141 void HS_ClientManager::removeClientCtxt(void *data)
142 {
143     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
144     if(ctxt == nullptr)
145     {
146         AFB_WARNING( "data is nullptr");
147         return;
148     }
149
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);
155 }
156
157 /**
158  * handle homescreen request
159  *
160  * #### Parameters
161  *  - request : the request
162  *  - verb : the verb name
163  *  - appid : to which application
164  *
165  * #### Return
166  * 0 : success
167  * others : fail
168  *
169  */
170 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
171 {
172     AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
173     int ret = 0;
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);
178         }
179     }
180     else {
181         auto ip = client_list.find(std::string(appid));
182         if(ip != client_list.end()) {
183             ret = ip->second->handleRequest(request, verb);
184         }
185         else {
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);
191             }
192             else {
193                 AFB_NOTICE("not exist session");
194                 ret = AFB_REQ_NOT_STARTED_APPLICATION;
195             }
196         }
197     }
198     return ret;
199 }
200
201 /**
202  * push event
203  *
204  * #### Parameters
205  *  - event : the event want to push
206  *  - param : the parameter contents of event
207  *  - appid : the destination application's id
208  *
209  * #### Return
210  * 0 : success
211  * others : fail
212  *
213  */
214 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
215 {
216     if(event == nullptr) {
217         AFB_WARNING("event name is null.");
218         return -1;
219     }
220
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);
225         }
226     }
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);
231         }
232     }
233
234     return 0;
235 }
236
237 /**
238  * register recovered application
239  *
240  * #### Parameters
241  *  - appid : application id
242  *
243  * #### Return
244  * None
245  *
246  */
247 void HS_ClientManager::registerApplication(std::string appid)
248 {
249
250 }