diff options
author | Tadao Tanikawa <tanikawa.tadao@jp.panasonic.com> | 2018-12-13 01:01:33 +0000 |
---|---|---|
committer | Tadao Tanikawa <tanikawa.tadao@jp.panasonic.com> | 2018-12-13 01:01:53 +0000 |
commit | 7a4a02d048910615cb15c26fc5534ad471a8d5ac (patch) | |
tree | d2e9e0573215e215d54b8d66fb3a86d72360e4b0 | |
parent | e35c4bf9b16927829e1741b9d9c08294f0cbed29 (diff) |
POI: AGL LifeCycle Managementsandbox/ruke47/lifecycle
Improved internal management of observer's context.
Before: using appid as key and storing map
After: storing vector into context of afb_req
Change-Id: I0a4d2350e6be458dd048625fabb8a1ac84e8e848
Signed-off-by: Tadao Tanikawa <tanikawa.tadao@jp.panasonic.com>
-rw-r--r-- | src/activity_manager.hpp | 16 | ||||
-rw-r--r-- | src/main.cpp | 85 | ||||
-rw-r--r-- | src/window_manager.cpp | 99 |
3 files changed, 135 insertions, 65 deletions
diff --git a/src/activity_manager.hpp b/src/activity_manager.hpp index 0663003..ebd5762 100644 --- a/src/activity_manager.hpp +++ b/src/activity_manager.hpp @@ -57,28 +57,28 @@ struct _observer_context { using observer = struct _observer_context; -// map of <"id:appid", "context of observer"> -using observers = std::map<std::string, observer>; - class ActivityManager { public: explicit ActivityManager(); ~ActivityManager() = default; - void api_register_activity_observer (afb_req_t req); - void api_unregister_activity_observer (afb_req_t req); + void api_register_activity_observer (afb_req_t req, void* &obs_ctx); + void api_unregister_activity_observer (afb_req_t req, void* &obs_ctx); wm::result<json_object *> api_get_activity_status(const char *appid); - public: void emit_activity_status_changed(const char* appid, const char* state); + void lcm_clear_context (void* &lcm_ctx); - // map of <"id:target", "registered observers"> - std::map<std::string, observers> map_observers; + // map of <"string:target $appid", "vector of registered observers"> + std::map<std::string, std::vector<observer*>> map_observers; // map of <"id:target", "current_state"> std::map<std::string, int> states; const char *states_s[NUM_STATUS]; + + private: + void remove_observer (observer* obs, std::vector<observer*>& obs_v); }; } // namespace lcm diff --git a/src/main.cpp b/src/main.cpp index 8e37d10..ac3050b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,10 +31,16 @@ typedef struct WMClientCtxt { std::string name; std::string role; + void* lcm_obs_ctx; // POI: LCM + WMClientCtxt(const char *appName, const char* appRole) + : name(appName), role(appRole), lcm_obs_ctx(nullptr) {}; + WMClientCtxt() : lcm_obs_ctx(nullptr) {}; + + void set_name_role(const char *appName, const char* appRole) { - name = appName; - role = appRole; + name = appName; + role = appRole; } } WMClientCtxt; @@ -98,28 +104,45 @@ static void cbRemoveClientCtxt(void *data) { return; } - HMI_DEBUG("remove app %s", ctxt->name.c_str()); - // Policy Manager does not know this app was killed, - // so notify it by deactivate request. - g_afb_instance->wmgr.api_deactivate_window( - ctxt->name.c_str(), ctxt->role.c_str(), - [](const char *) {}); + if (!ctxt->name.empty()) { + HMI_DEBUG("remove app %s", ctxt->name.c_str()); + + // Policy Manager does not know this app was killed, + // so notify it by deactivate request. + g_afb_instance->wmgr.api_deactivate_window( + ctxt->name.c_str(), ctxt->role.c_str(), + [](const char *) {}); + + g_afb_instance->wmgr.removeClient(ctxt->name); + } + + // POI: LCM + g_afb_instance->wmgr.amgr.lcm_clear_context(ctxt->lcm_obs_ctx); - g_afb_instance->wmgr.removeClient(ctxt->name); delete ctxt; } -static void createSecurityContext(afb_req_t req, const char* appid, const char* role) +static WMClientCtxt* getWMClientContext(afb_req_t req) { - WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req); - if (!ctxt) - { + WMClientCtxt *ctx = static_cast<WMClientCtxt *>(afb_req_context_get(req)); + if (!ctx) { // Create Security Context at first time - WMClientCtxt *ctxt = new WMClientCtxt(appid, role); - HMI_DEBUG("create session for %s", ctxt->name.c_str()); + ctx = new WMClientCtxt(); + HMI_DEBUG("create session without name & role"); afb_req_session_set_LOA(req, 1); - afb_req_context_set(req, ctxt, cbRemoveClientCtxt); + afb_req_context_set(req, ctx, cbRemoveClientCtxt); + } + return ctx; +} + +static void createSecurityContext(afb_req_t req, const char* appid, const char* role) +{ + WMClientCtxt *ctx = getWMClientContext(req); + if (ctx && ctx->name.empty()) + { + HMI_DEBUG("create session for name=\"%s\" & role=\"role\"", appid, role); + ctx->set_name_role(appid, role); } } @@ -499,15 +522,19 @@ static void lcm_register_activity_observer (afb_req_t req) return; } - // register new activity observer - // { - // id: "app id of target to observe", - // type: "type of lifecycle, APP, HMI or GUI to observe", - // filter: { states which observer wants to know } - // } - g_afb_instance->wmgr.amgr.api_register_activity_observer(req); - - afb_req_success(req, NULL, "success"); + WMClientCtxt* wc_ctx = getWMClientContext(req); + if (wc_ctx == nullptr) { + afb_req_fail(req, "failed", "WMClientCtxt cannot create."); + } else { + // register new activity observer + // { + // id: "app id of target to observe", + // type: "type of lifecycle, APP, HMI or GUI to observe", + // filter: { states which observer wants to know } + // } + g_afb_instance->wmgr.amgr.api_register_activity_observer(req, wc_ctx->lcm_obs_ctx); + afb_req_success(req, NULL, "success"); + } } static void lcm_unregister_activity_observer (afb_req_t req) @@ -519,11 +546,17 @@ static void lcm_unregister_activity_observer (afb_req_t req) return; } + WMClientCtxt* wc_ctx = getWMClientContext(req); + if (wc_ctx == nullptr) { + afb_req_fail(req, "failed", "WMClientCtxt not exsit."); + return; + } + // unregister activity observer // { // id: "uniq id of observer" // } - g_afb_instance->wmgr.amgr.api_unregister_activity_observer(req); + g_afb_instance->wmgr.amgr.api_unregister_activity_observer(req, wc_ctx->lcm_obs_ctx); afb_req_success(req, NULL, "success"); } diff --git a/src/window_manager.cpp b/src/window_manager.cpp index 06f7793..bbfd08c 100644 --- a/src/window_manager.cpp +++ b/src/window_manager.cpp @@ -988,9 +988,10 @@ ActivityManager::ActivityManager (void) ; } -void ActivityManager::api_register_activity_observer (afb_req_t req) +void ActivityManager::api_register_activity_observer (afb_req_t req, void* &lcm_ctx) { - std::string observer = afb_req_get_application_id(req); + //std::string observer = afb_req_get_application_id(req); + observer* obs = static_cast<observer *>(lcm_ctx); json_object *j_data = afb_req_json(req); json_object *j_target; @@ -1000,39 +1001,53 @@ void ActivityManager::api_register_activity_observer (afb_req_t req) } std::string target(json_object_get_string(j_target)); - for (auto itr = this->map_observers.begin(); itr != this->map_observers.end(); ++itr) { - observers mp = itr->second; - if (mp.count(observer)) { - // Already exist - HMI_DEBUG("observer(%s) is already exist", observer.c_str()); - if (this->map_observers[target].count(observer)) { - // Aready registered - HMI_DEBUG("observer(%s->%s) is already registered", observer.c_str(), target.c_str()); - } else { - HMI_DEBUG("observer(%s->%s) is registered", observer.c_str(), target.c_str()); - - (this->map_observers[target])[observer] = mp[observer]; - } + if (obs == nullptr) { + // New observer + afb_event_t event = afb_daemon_make_event(LCM_EVENT_STATUS_CHANGED); + //afb_event_t event = afb_daemon_make_event(LCM_EVENT_STATUS_CHANGED); + obs = new observer({ + event, + std::bitset<NUM_STATUS>(ACTIVITY_FILTER_ALL_SET) + }); + + if (afb_req_subscribe(req, event) != 0) { + afb_req_fail(req, "failed", "cannot subscribe event"); return; } + HMI_DEBUG("observer(%p, target=\"%s\") is created.[NEW]", obs, target.c_str()); + + lcm_ctx = static_cast<observer *>(obs); } - // New observer - afb_event_t event = afb_daemon_make_event(LCM_EVENT_STATUS_CHANGED); - //afb_event_t event = afb_daemon_make_event(LCM_EVENT_STATUS_CHANGED); - (this->map_observers[target])[observer] = { - event, - std::bitset<NUM_STATUS>(ACTIVITY_FILTER_ALL_SET) - }; + if (this->map_observers.count(target)) { + // already registered + HMI_DEBUG("observer(%p, target=\"%s\") is already exist", obs, target.c_str()); + std::vector<observer*> obs_v = this->map_observers[target]; + auto result = std::find(obs_v.begin(), obs_v.end(), obs); + if (result != obs_v.end()) { + HMI_DEBUG("observer(%p, target=\"%s\") is already registered", obs, target.c_str()); + return; + } + } + HMI_DEBUG("observer(%p, target=\"%s\") is registered", obs, target.c_str()); + (this->map_observers[target]).push_back(obs); +} - if (afb_req_subscribe(req, event) != 0) { - HMI_ERROR("cannot subscribe event"); +void ActivityManager::remove_observer (observer *obs, std::vector<observer*>& obs_v) +{ + auto obs_itr = obs_v.begin(); + while (obs_itr != obs_v.end()) { + if (*obs_itr == obs) { + obs_itr = obs_v.erase(obs_itr); + } else { + obs_itr++; + } } } -void ActivityManager::api_unregister_activity_observer (afb_req_t req) +void ActivityManager::api_unregister_activity_observer (afb_req_t req, void* &lcm_ctx) { - std::string observer = afb_req_get_application_id(req); + observer *obs = static_cast<observer *>(lcm_ctx); json_object *j_data = afb_req_json(req); json_object *j_target; @@ -1043,7 +1058,24 @@ void ActivityManager::api_unregister_activity_observer (afb_req_t req) } std::string target(json_object_get_string(j_target)); - HMI_DEBUG("observer(%s->%s) is unregistered.", observer.c_str(), target.c_str()); + HMI_DEBUG("observer(%p->\"%s\") is unregistered.", obs, target.c_str()); + + if (this->map_observers.count(target) != 0) { + remove_observer(obs, this->map_observers[target]); + } +} + +void ActivityManager::lcm_clear_context (void* &lcm_ctx) +{ + observer *obs = static_cast<observer *>(lcm_ctx); + + for (auto itr = this->map_observers.begin(); itr != this->map_observers.end(); itr++) { + remove_observer(obs, itr->second); + } + + HMI_DEBUG("observer(%p) is removed.", obs); + + delete obs; } wm::result<json_object *> ActivityManager::api_get_activity_status (const char *appid) @@ -1074,18 +1106,23 @@ void ActivityManager::emit_activity_status_changed (const char* appid, const cha { // POI: AGL LifeCycle Management // E.g. statusChanged(CREATED->STARTED) + HMI_DEBUG("emit_activity_status_changed(%s, %s)", appid, state); + std::string id = appid; if (this->map_observers.count(id)) { - observers mp = this->map_observers[id]; - for (auto itr = mp.begin(); itr != mp.end(); ++itr) { - afb_event_t event = itr->second.event; + std::vector<observer*>& obs_v = this->map_observers[id]; + + for (auto itr = obs_v.begin(); itr != obs_v.end(); ++itr) { + auto obs = *itr; + afb_event_t event = obs->event; if (afb_event_is_valid(event)) { - HMI_DEBUG("emit_activity_status_changed(%s, %s)", appid, state); json_object *data = json_object_new_object(); json_object_object_add(data, LCM_STATE, json_object_new_string(state)); json_object_object_add(data, LCM_TARGET, json_object_new_string(appid)); + HMI_DEBUG("send Event_statusChanged(%s, %s)", appid, state); + afb_event_push(event, json_object_get(data)); json_object_put(data); } else { |