+const char _keyData[] = "data";
+const char _keyId[] = "id";
+const char _parameter[] = "parameter";
+const char _area[] = "area";
+
+struct hs_handshake {
+ hs_handshake(int times, int sleep) : m_times(times), m_sleep(sleep) {}
+ int start(afb_api_t api);
+ void handshake_loop(afb_api_t api, int times, int sleeps);
+
+ enum HandshakeStatus {
+ Handshake_Idle = 0,
+ Handshake_Subscribing,
+ Handshake_Subscribe_Fail,
+ Handshake_WaitEvent,
+ Handshake_Over
+ };
+ static int hs_sts;
+
+private:
+ const std::string sub_event = "windowmanager/handshake";
+ const int m_times;
+ const int m_sleep;
+};
+
+int hs_handshake::hs_sts = hs_handshake::Handshake_Idle;
+
+/**
+ * handshake callback function
+ *
+ * #### Parameters
+ * - obj : reply json object
+ * - error : api_call error
+ * - info : api_call information
+ *
+ * #### Return
+ * None
+ *
+ */
+void handshake_subscribe_callback(struct json_object *obj, const char *error, const char *info)
+{
+ AFB_NOTICE("subscribe handshake reply: obj=%s, error=%s, info=%s", json_object_to_json_string(obj), error, info);
+ if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
+ return;
+ }
+ if(error == nullptr) {
+ hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent;
+ }
+ else {
+ hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail;
+ }
+}
+
+/**
+ * handshake event function
+ *
+ * #### Parameters
+ * - api : the api
+ * - event : received event name
+ * - object : received json object
+ *
+ * #### Return
+ * 0 : event can transfer to others
+ * 1 : event not transfer to others
+ */
+int on_handshake_event(afb_api_t api, const char *event, struct json_object *object)
+{
+ AFB_NOTICE("received handshake event from windowmanager.");
+ hs_handshake::hs_sts = hs_handshake::Handshake_Over;
+ return 1;
+}
+
+/**
+ * start handshake function
+ *
+ * #### Parameters
+ * - api : the api
+ *
+ * #### Return
+ * 0 : handshake success
+ * other : handshake fail
+ *
+ */
+int hs_handshake::start(afb_api_t api)
+{
+ AFB_NOTICE("start handshake with windowmanager.");
+ setEventHook(sub_event.c_str(), on_handshake_event);
+
+ std::thread th(&hs_handshake::handshake_loop, this, api, m_times, m_sleep);
+ th.detach();
+ return 0;
+}
+
+/**
+ * handshake loop
+ *
+ * #### Parameters
+ * - api : the api
+ *
+ * #### Return
+ * None
+ *
+ */
+void hs_handshake::handshake_loop(afb_api_t api, int times, int sleeps)
+{
+ int count = 0;
+ do {
+ // try to subscribe handshake event
+ if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle
+ || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) {
+ hs_handshake::hs_sts = Handshake_Subscribing;
+ HS_WmProxy wm_proxy;
+ wm_proxy.subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback);
+ }
+
+ // wait handshake event
+ if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
+ break;
+ }
+
+ ++count;
+ usleep(sleeps*1000);
+ } while(count < times);
+ AFB_NOTICE("handshake over, m_times=%d, m_sleep=%d, count=%d.", times, sleeps, count);
+ HS_AppRecover::instance()->startRecovery(api);
+}
+
+struct hs_instance {
+ HS_ClientManager *client_manager; // the connection session manager
+ HS_AppInfo *app_info; // application info
+ HS_AppRecover *app_recover;
+
+ hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()) {}
+ int init(afb_api_t api);
+ void setEventHook(const char *event, const event_hook_func f);
+ void onEvent(afb_api_t api, const char *event, struct json_object *object);
+private:
+ std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
+};
+
+static struct hs_instance *g_hs_instance;
+
+/**
+ * init function
+ *
+ * #### Parameters
+ * - api : the api serving the request
+ *
+ * #### Return
+ * 0 : init success
+ * 1 : init fail
+ *
+ */
+int hs_instance::init(afb_api_t api)
+{
+ if(client_manager == nullptr) {
+ AFB_ERROR("client_manager is nullptr.");
+ return -1;
+ }
+ client_manager->init();
+
+ if(app_info == nullptr) {
+ AFB_ERROR("app_info is nullptr.");
+ return -1;
+ }
+ app_info->init(api);
+
+ HS_Config hs_config;
+ if(hs_config.readConfig() < 0) {
+ AFB_ERROR("read config file failed.");
+ return -1;
+ }
+
+ if(app_recover == nullptr) {
+ AFB_ERROR("app_recover is nullptr.");
+ return -1;
+ }
+ app_recover->init(api);
+ app_recover->setRecoverMap(hs_config.getRecoverMap());
+
+ const struct handshake_info *h = hs_config.getHandshakeInfo();
+ struct hs_handshake handshake(h->times, h->sleep);
+ if(handshake.start(api) < 0) {
+ AFB_ERROR("handshake with windowmanager failed.");
+ return -1;
+ }
+
+ return 0;
+}
+
+/**
+ * set event hook
+ *
+ * #### Parameters
+ * - event : event name
+ * - f : hook function
+ *
+ * #### Return
+ * Nothing
+ */
+void hs_instance::setEventHook(const char *event, const event_hook_func f)
+{
+ if(event == nullptr || f == nullptr) {
+ AFB_WARNING("argument is null.");
+ return;
+ }
+
+ std::string ev(event);
+ auto it = event_hook_list.find(ev);
+ if(it != event_hook_list.end()) {
+ it->second.push_back(f);
+ }
+ else {
+ std::list<event_hook_func> l;
+ l.push_back(f);
+ event_hook_list[ev] = std::move(l);
+ }
+}
+
+/**
+ * onEvent function
+ *
+ * #### Parameters
+ * - api : the api serving the request
+ * - event : event name
+ * - object : event json object
+ *
+ * #### Return
+ * Nothing
+ */
+void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
+{
+ std::string ev(event);
+ auto it = event_hook_list.find(ev);
+ if(it != event_hook_list.end()) {
+ for(auto &ref : it->second) {
+ if(ref(api, event, object))
+ break;
+ }
+ }
+}
+
+/**
+ * set event hook
+ *
+ * #### Parameters
+ * - event : event name
+ * - f : hook function pointer
+ *
+ * #### Return
+ * Nothing
+ */
+void setEventHook(const char *event, const event_hook_func f)
+{
+ if(g_hs_instance == nullptr) {
+ AFB_ERROR("g_hs_instance is null.");
+ return;
+ }