Policy migration to Message Queue
authorAndrey Zabolotnyi <a.zabolotnyi@samsung.com>
Fri, 7 Jul 2017 13:16:05 +0000 (16:16 +0300)
committerAndrey Zabolotnyi <a.zabolotnyi@samsung.com>
Fri, 7 Jul 2017 13:16:05 +0000 (16:16 +0300)
device_core/nmdaemon/main_thread.cpp
device_core/nmdaemon/policyhandler.cpp
device_core/nmdaemon/policyhandler.h
device_core/nmdaemon/rmi_thread.cpp

index 0bf9b44..92980f6 100644 (file)
@@ -148,21 +148,26 @@ void main_thread::routine()
         std::shared_ptr<ReportResource> report_hub_resorce;
         std::shared_ptr<PolicyResource> policy_hub_resource;
 
-        PolicyHandler policy_handler{iotivity, config.ssid};
+        PolicyHandler *policy_handler;
         ReportHandler *report_handler;
-        if(with_cloud) {
+        if(with_cloud)
+        {
             report_handler = new ReportHandlerMQ();
-        } else {
-            report_handler = new ReportHandlerRes(config.ssid);
+            policy_handler = new PolicyHandlerMQ();
         }
-
-        while (!policy_handler.findResource())
+        else
         {
-            write_log("[MAIN_THREADS] Policy resource not found\n");
-            std::this_thread::sleep_for(std::chrono::seconds(10));
+            report_handler = new ReportHandlerRes(config.ssid);
+            policy_handler = new PolicyHandler(config.ssid);
+            while (!policy_handler->findResource())
+            {
+                write_log("[MAIN_THREADS] Policy resource not found\n");
+                std::this_thread::sleep_for(std::chrono::seconds(10));
+            }
         }
 
 
+
         if (g_working_mode == WorkingMode::Hub)
         {
             proxy_thread = std::make_shared<ProxyThread>();
@@ -182,7 +187,7 @@ void main_thread::routine()
             report_hub_resorce = std::make_shared<ReportResource>(*report_handler, proxy_thread);
             report_hub_resorce->registerResource();
 
-            policy_hub_resource = std::make_shared<PolicyResource>(&policy_handler, iotivity->getDeviceID(), proxy_thread);
+            policy_hub_resource = std::make_shared<PolicyResource>(policy_handler, iotivity->getDeviceID(), proxy_thread);
             policy_hub_resource->registerResource();
         }
 
@@ -191,7 +196,7 @@ void main_thread::routine()
             iotivity->publishResources(rhandles);
         }
 
-        AgentPolicyService agent_policy_service(std::bind(&PolicyHandler::enforceCallback, &policy_handler, PH::_1, PH::_2));
+        AgentPolicyService agent_policy_service(std::bind(&PolicyHandler::enforceCallback, policy_handler, PH::_1, PH::_2));
         std::thread rmi_thread(&AgentPolicyService::run, &agent_policy_service);
 
         std::srand(time(NULL));
index 235dc09..cb26f7b 100644 (file)
@@ -17,9 +17,10 @@ using namespace OC;
 using namespace NetworkManager;
 namespace PH = std::placeholders;
 
-PolicyHandler::PolicyHandler(NetworkManager::IoTivity* iotivity, const std::string& server_id)
-    : iotivity(iotivity), sid(server_id)
+PolicyHandler::PolicyHandler(const std::string& server_id)
+    : sid(server_id)
 {
+    iotivity = NetworkManager::IoTivity::getInstance();
     device_id = iotivity->getDeviceID();
 }
 
@@ -47,6 +48,11 @@ void PolicyHandler::pass(const OCRepresentation& rep, const QueryParamsMap& para
     }
 }
 
+void PolicyHandlerMQ::pass(const OCRepresentation& rep, const QueryParamsMap& params)
+{
+    NetworkManager::IoTivity::getInstance()->getMqHandler()->publish("/srv/policy", rep);
+}
+
 void PolicyHandler::observeCallback(const HeaderOptions& head_options, const OCRepresentation& rep, const int& ecode, const int& seq_number)
 {
     if (ecode == OC_STACK_OK)
@@ -73,7 +79,7 @@ void PolicyHandler::observeCallback(const HeaderOptions& head_options, const OCR
 
 void PolicyHandler::enforceCallback(const std::string& agentId, const std::string& jsonData)
 {
-    LOG_D(TAG, "Enfore request from agent: %s\n%s", agentId.c_str(), jsonData.c_str());
+    LOG_D(TAG, "Enforce request from agent: %s\n%s", agentId.c_str(), jsonData.c_str());
 
     OCRepresentation rep;
     rep.setValue("policy", jsonData);
index eadd42f..a9c79a8 100644 (file)
@@ -17,7 +17,7 @@ public:
      * @brief PolicyHandler constructor
      * @param iotivity [in] pointer to iotivity instance
      */
-    PolicyHandler(NetworkManager::IoTivity* iotivity, const std::string& server_id = "");
+    PolicyHandler(const std::string& server_id = "");
 
     /**
      * @brief setObserver set proxy observe callback
@@ -35,7 +35,7 @@ public:
      * @param rep [in] represantation of policy
      * @param params [in] query parameters
      */
-    void pass(const OC::OCRepresentation& rep, const OC::QueryParamsMap& params);
+    virtual void pass(const OC::OCRepresentation& rep, const OC::QueryParamsMap& params);
 
     /**
      * @brief enforceCallback callback for agent eforced policies
@@ -50,6 +50,8 @@ public:
      */
     bool findResource();
 
+    virtual ~PolicyHandler() {};
+
 private:
     ObserverCallback callback;
     std::shared_ptr<OC::OCResource> resource;
@@ -68,4 +70,11 @@ private:
     void observeCallback(const OC::HeaderOptions& head_options, const OC::OCRepresentation& rep, const int& ecode, const int& seq_number);
 };
 
+class PolicyHandlerMQ : public PolicyHandler
+{
+public:
+    void pass(const OC::OCRepresentation& rep, const OC::QueryParamsMap& params) override;
+    ~PolicyHandlerMQ() override {};
+};
+
 #endif // POLICYHANDLER_H
index 4f4a155..44467c2 100644 (file)
@@ -29,7 +29,7 @@ void rmi_thread::routine()
     write_log("[RMI_THREADS] started\n");
     try
     {
-        PolicyHandler policy_proxy{iotivity};
+        PolicyHandler policy_proxy;
         std::shared_ptr<PolicyResource> policy_resource;
 
         while (!policy_proxy.findResource())