[IoTivity Simulator]Resolved JIRA issues IOT-893 and IOT-870.
authorG S Senthil Kumar <senthil.gs@samsung.com>
Tue, 29 Dec 2015 13:50:11 +0000 (19:20 +0530)
committerUze Choi <uzchoi@samsung.com>
Thu, 31 Dec 2015 05:24:38 +0000 (05:24 +0000)
IOT-893 is about the time delay after stopping the automation.
IOT-870 is about the server crash while notifying the observers.
Updated the code to allow errors when notifying the observers to be
propagated till application.

Managing thread objects using shared pointers
and detaching them for de-allocation of thread resources.

Change-Id: I06e33926a79fd241bf4089b8c4760742edab8290
Signed-off-by: G S Senthil Kumar <senthil.gs@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4721
Reviewed-by: Radha Bhavani <radha.p@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/simulator/src/server/resource_update_automation.cpp
service/simulator/src/server/resource_update_automation.h
service/simulator/src/server/resource_update_automation_mngr.cpp

index 6c6f863..07ea8de 100644 (file)
@@ -51,14 +51,13 @@ AttributeUpdateAutomation::AttributeUpdateAutomation(int id, SimulatorSingleReso
 
 void AttributeUpdateAutomation::start()
 {
-    m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this);
+    m_thread = std::make_shared<std::thread>(&AttributeUpdateAutomation::updateAttribute, this);
+    m_thread->detach();
 }
 
 void AttributeUpdateAutomation::stop()
 {
     m_stopRequested = true;
-    if (m_thread)
-        m_thread->join();
 }
 
 void AttributeUpdateAutomation::updateAttribute()
@@ -76,11 +75,16 @@ void AttributeUpdateAutomation::updateAttribute()
             SimulatorResourceModel::Attribute attribute;
             while (!m_stopRequested && true == m_attributeGen.next(attribute))
             {
-                if (false == m_resource->updateAttributeValue(attribute))
+                try
                 {
-                    OC_LOG_V(ERROR, ATAG, "Failed to update the attribute![%s]", attribute.getName().c_str());
-                    continue;
+                    if (false == m_resource->updateAttributeValue(attribute))
+                    {
+                        OC_LOG_V(ERROR, ATAG, "Failed to update the attribute![%s]", attribute.getName().c_str());
+                        continue;
+                    }
                 }
+                catch(SimulatorException &e) {}
+
                 resourceImpl->notifyApp();
 
                 SLEEP_FOR(m_updateInterval);
@@ -105,7 +109,7 @@ void AttributeUpdateAutomation::updateAttribute()
     if (m_callback)
         m_callback(m_resource->getURI(), m_id);
 
-    if (m_finishedCallback && !m_stopRequested)
+    if (m_finishedCallback)
         m_finishedCallback(m_id);
 }
 
@@ -135,14 +139,13 @@ void ResourceUpdateAutomation::start()
         throw SimulatorException(SIMULATOR_ERROR, "Resource has zero attributes!");
     }
 
-    m_thread = new std::thread(&ResourceUpdateAutomation::updateAttributes, this, attributes);
+    m_thread = std::make_shared<std::thread>(&ResourceUpdateAutomation::updateAttributes, this, attributes);
+    m_thread->detach();
 }
 
 void ResourceUpdateAutomation::stop()
 {
     m_stopRequested = true;
-    if (m_thread)
-        m_thread->join();
 }
 
 void ResourceUpdateAutomation::updateAttributes(
@@ -162,7 +165,11 @@ void ResourceUpdateAutomation::updateAttributes(
         {
             for (auto &attributeEntry : resModel.getAttributes())
             {
-                resourceImpl->updateAttributeValue(attributeEntry.second);
+                try
+                {
+                    resourceImpl->updateAttributeValue(attributeEntry.second);
+                }
+                catch(SimulatorException &e) {}
             }
 
             resourceImpl->notifyApp();
@@ -180,7 +187,7 @@ void ResourceUpdateAutomation::updateAttributes(
     if (m_callback)
         m_callback(m_resource->getURI(), m_id);
 
-    if (m_finishedCallback && !m_stopRequested)
+    if (m_finishedCallback)
         m_finishedCallback(m_id);
 }
 
index a3dc6d1..54263bf 100644 (file)
@@ -50,7 +50,7 @@ class AttributeUpdateAutomation
         AttributeGenerator m_attributeGen;
         updateCompleteCallback m_callback;
         std::function<void (const int)> m_finishedCallback;
-        std::thread *m_thread;
+        std::shared_ptr<std::thread> m_thread;
 };
 
 typedef std::shared_ptr<AttributeUpdateAutomation> AttributeUpdateAutomationSP;
@@ -77,7 +77,7 @@ class ResourceUpdateAutomation
         int m_updateInterval;
         updateCompleteCallback m_callback;
         std::function<void (const int)> m_finishedCallback;
-        std::thread *m_thread;
+        std::shared_ptr<std::thread> m_thread;
 };
 
 typedef std::shared_ptr<ResourceUpdateAutomation> ResourceUpdateAutomationSP;
index 1b52c70..972b176 100644 (file)
@@ -112,13 +112,11 @@ void UpdateAutomationMngr::stop(int id)
     if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
     {
         m_resourceUpdationList[id]->stop();
-        m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
         return;
     }
     else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
     {
         m_attrUpdationList[id]->stop();
-        m_attrUpdationList.erase(m_attrUpdationList.find(id));
         return;
     }
 }