Change log macro to AFB_XXX
[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 }
75
76 /**
77  * create client's afb_req_context
78  *
79  * #### Parameters
80  *  - appid: app's id
81  *
82  * #### Return
83  * HS_ClientCtxt pointer
84  *
85  */
86 HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
87 {
88     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
89     if (!ctxt)
90     {
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);
95     }
96     return ctxt;
97 }
98
99 /**
100  * add Client
101  *
102  * #### Parameters
103  *  - ctxt: app's id
104  *
105  * #### Return
106  * HS_Client pointer
107  *
108  */
109 HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
110 {
111     return (client_list[appid] = new HS_Client(req, appid));
112 }
113
114 /**
115  * remove Client
116  *
117  * #### Parameters
118  *  - appid: app's id
119  *
120  * #### Return
121  * None
122  *
123  */
124 void HS_ClientManager::removeClient(std::string appid)
125 {
126     delete client_list[appid];
127     client_list.erase(appid);
128 }
129
130 /**
131  * remove Client from list
132  *
133  * #### Parameters
134  *  - data: HS_ClientCtxt pointer
135  *
136  * #### Return
137  * None
138  *
139  */
140 void HS_ClientManager::removeClientCtxt(void *data)
141 {
142     HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
143     if(ctxt == nullptr)
144     {
145         AFB_WARNING( "data is nullptr");
146         return;
147     }
148
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);
154 }
155
156 /**
157  * handle homescreen request
158  *
159  * #### Parameters
160  *  - request : the request
161  *  - verb : the verb name
162  *  - appid : to which application
163  *
164  * #### Return
165  * 0 : success
166  * others : fail
167  *
168  */
169 int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const char *appid)
170 {
171     AFB_INFO("verb=[%s],appid=[%s].", verb, appid);
172     int ret = 0;
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);
177         }
178     }
179     else {
180         auto ip = client_list.find(std::string(appid));
181         if(ip != client_list.end()) {
182             ret = ip->second->handleRequest(request, verb);
183         }
184         else {
185             if(!strcasecmp(verb, "subscribe")) {
186                 appid2ctxt[appid] = createClientCtxt(request, appid);
187                 HS_Client* client = addClient(request, appid);
188                 ret = client->handleRequest(request, "subscribe");
189             }
190             else {
191                 AFB_NOTICE("not exist session");
192                 ret = AFB_REQ_NOT_STARTED_APPLICATION;
193             }
194         }
195     }
196     return ret;
197 }
198
199 /**
200  * push event
201  *
202  * #### Parameters
203  *  - event : the event want to push
204  *  - param : the parameter contents of event
205  *  - appid : the destination application's id
206  *
207  * #### Return
208  * 0 : success
209  * others : fail
210  *
211  */
212 int HS_ClientManager::pushEvent(const char *event, struct json_object *param, std::string appid)
213 {
214     if(event == nullptr) {
215         AFB_WARNING("event name is null.");
216         return -1;
217     }
218
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);
223         }
224     }
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);
229         }
230     }
231
232     return 0;
233 }