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.
17 #ifndef HOMESCREEN_CLIENTMANAGER_H
18 #define HOMESCREEN_CLIENTMANAGER_H
23 #include <unordered_map>
26 #include "hs-helper.h"
27 #include "hs-client.h"
29 extern const std::string _listen_all;
31 class listener_interface {
33 listener_interface(std::string uid, std::set<std::string> listen_appid = std::set<std::string>()) : m_uid(uid), m_listen_appid(listen_appid) {}
34 virtual ~listener_interface() {}
35 virtual void notify(afb_api_t api, std::string appid = "") = 0;
36 std::string myUid(void) {return m_uid;}
37 std::set<std::string> listenAppSet(void) {return m_listen_appid;}
38 void addListenAppId(std::string appid) {m_listen_appid.insert(appid);}
39 bool isListenAppId(std::string &appid) {
40 auto it = m_listen_appid.find(appid);
41 return it != m_listen_appid.end() ? true : false;
43 bool listenAppEmpty(void) {return m_listen_appid.empty();}
44 void clearListenAppSet(void) {m_listen_appid.clear();}
47 std::set<std::string> m_listen_appid;
50 typedef struct HS_ClientCtxt
53 HS_ClientCtxt(const char *appid)
60 class HS_ClientManager {
63 ~HS_ClientManager() = default;
64 HS_ClientManager(HS_ClientManager const &) = delete;
65 HS_ClientManager &operator=(HS_ClientManager const &) = delete;
66 HS_ClientManager(HS_ClientManager &&) = delete;
67 HS_ClientManager &operator=(HS_ClientManager &&) = delete;
69 static HS_ClientManager* instance(void);
71 int handleRequest(afb_req_t request, const char *verb, const char *appid = nullptr);
72 int pushEvent(const char *event, struct json_object *param, std::string appid = "");
73 void removeClientCtxt(void *data); // don't use, internal only
74 void setStartupAppid(const std::string &appid) {startup_appid = appid;}
75 bool isAppStarted(const std::string &appid);
76 void addListener(listener_interface* listener);
77 void removeListener(listener_interface* listener);
80 HS_ClientCtxt* createClientCtxt(afb_req_t req, std::string appid);
81 HS_Client* addClient(afb_req_t req, std::string appid);
82 void removeClient(std::string appid);
83 bool checkRegisterApp(afb_api_t api, const std::string &appid);
84 void notifyListener(afb_api_t api, const std::string &appid);
87 static HS_ClientManager* me;
88 std::unordered_map<std::string, std::list<listener_interface*>> listener_list;
89 std::unordered_map<std::string, HS_Client*> client_list;
90 std::unordered_map<std::string, HS_ClientCtxt*> appid2ctxt;
92 std::string startup_appid;
95 #endif // HOMESCREEN_CLIENTMANAGER_H