Simplify PolicyManager by adding a map for subscription requests 12/76712/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 27 Jun 2016 04:29:06 +0000 (13:29 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 27 Jun 2016 04:29:06 +0000 (13:29 +0900)
Change-Id: Id0bde1e18f634c52683cb99c86064d3e5562a8a0
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/policy/PolicyManager.cpp
src/policy/PolicyManager.h

index ba15ef7..a75a6cf 100644 (file)
@@ -17,6 +17,7 @@
 #include <Types.h>
 #include <DBusTypes.h>
 #include <ProviderTypes.h>
+#include <SensorRecorderTypes.h>
 #include "../ContextManager.h"
 #include "PolicyRequest.h"
 #include "PolicyManager.h"
@@ -26,63 +27,45 @@ using namespace ctx;
 PolicyManager::PolicyManager(ContextManager *contextMgr) :
        __contextMgr(contextMgr)
 {
-       __init();
-}
-
-PolicyManager::~PolicyManager()
-{
-       __release();
-}
+       __subscribe(SUBJ_STATE_WIFI);
+       __subscribe(SUBJ_APP_LOGGER);
+       __subscribe(SUBJ_MEDIA_LOGGER);
 
-void PolicyManager::__init()
-{
-       /* TODO: WiFi API has multi-session support issue.
-          The issue should be fixed, or walkarouned first. */
-       __subscribe(SUBJ_STATE_WIFI, __ridWifiState);
-       __subscribe(SUBJ_APP_LOGGER, __ridAppLogging);
-       __subscribe(SUBJ_MEDIA_LOGGER, __ridMediaLogging);
+       __subscribe(SUBJ_SENSOR_PEDOMETER);
+       __subscribe(SUBJ_SENSOR_PRESSURE);
 
 #ifdef TRIGGER_SUPPORT
-       __subscribe(SUBJ_CUSTOM, __ridCustomManager);
+       __subscribe(SUBJ_CUSTOM);
 #endif
 
 #if 0
-       __subscribe(SUBJ_PLACE_DETECTION, __ridPlaceDetection);
+       __subscribe(SUBJ_PLACE_DETECTION);
 #endif
 }
 
-void PolicyManager::__release()
+PolicyManager::~PolicyManager()
 {
-       __unsubscribe(SUBJ_STATE_WIFI, __ridWifiState);
-       __unsubscribe(SUBJ_APP_LOGGER, __ridAppLogging);
-       __unsubscribe(SUBJ_MEDIA_LOGGER, __ridMediaLogging);
-
-#ifdef TRIGGER_SUPPORT
-       __unsubscribe(SUBJ_CUSTOM, __ridCustomManager);
-#endif
-
-#if 0
-       __unsubscribe(SUBJ_PLACE_DETECTION, __ridPlaceDetection);
-#endif
+       for (auto &it : __subscriptionMap) {
+               PolicyRequest *req = new(std::nothrow) PolicyRequest(REQ_UNSUBSCRIBE, it.first, it.second, NULL);
+               if (!req) {
+                       _E("Memory allocation failed");
+                       continue;
+               }
+               __contextMgr->assignRequest(req);
+       }
+
+       __subscriptionMap.clear();
 }
 
-void PolicyManager::__subscribe(const char *subject, int &reqId)
+void PolicyManager::__subscribe(const char *subject)
 {
        static int rid = 0;
        ++rid;
 
-       reqId = rid;
-
-       PolicyRequest *req = new(std::nothrow) PolicyRequest(REQ_SUBSCRIBE, reqId, subject, NULL);
+       PolicyRequest *req = new(std::nothrow) PolicyRequest(REQ_SUBSCRIBE, rid, subject, NULL);
        IF_FAIL_VOID_TAG(req, _E, "Memory allocation failed");
 
        __contextMgr->assignRequest(req);
-}
 
-void PolicyManager::__unsubscribe(const char *subject, int reqId)
-{
-       PolicyRequest *req = new(std::nothrow) PolicyRequest(REQ_UNSUBSCRIBE, reqId, subject, NULL);
-       IF_FAIL_VOID_TAG(req, _E, "Memory allocation failed");
-
-       __contextMgr->assignRequest(req);
+       __subscriptionMap[rid] = subject;
 }
index 7427943..ce9b315 100644 (file)
@@ -17,6 +17,8 @@
 #ifndef _CONTEXT_POLICY_MANAGER_H_
 #define _CONTEXT_POLICY_MANAGER_H_
 
+#include <map>
+
 namespace ctx {
 
        class ContextManger;
@@ -28,23 +30,14 @@ namespace ctx {
        private:
                PolicyManager(ContextManager *contextMgr);
 
-               void __init();
-               void __release();
-
-               void __subscribe(const char *subject, int &reqId);
-               void __unsubscribe(const char *subject, int reqId);
+               void __subscribe(const char *subject);
 
                ContextManager *__contextMgr;
-               int __ridWifiState;
-               int __ridAppLogging;
-               int __ridMediaLogging;
-               int __ridSocialLogging;
-               int __ridPlaceDetection;
-               int __ridCustomManager;
+               std::map<int, const char*> __subscriptionMap;
 
                friend class Server;
        };
 
 }      /* namespace ctx */
 
-#endif /* End of _CONTEXT_POLICY_MANAGER_H_ */
+#endif /* _CONTEXT_POLICY_MANAGER_H_ */