Merge branch 'resource-container'
[platform/upstream/iotivity.git] / service / resource-container / src / BundleResource.cpp
index 396c674..2a779ba 100644 (file)
@@ -22,6 +22,9 @@
 
 #include <list>
 #include <string.h>
+#include <iostream>
+#include <boost/thread.hpp>
+#include "NotificationReceiver.h"
 
 #include "InternalTypes.h"
 
@@ -29,9 +32,9 @@ namespace OIC
 {
     namespace Service
     {
-        BundleResource::BundleResource()
+        BundleResource::BundleResource() : m_pNotiReceiver(nullptr)
         {
-            m_pNotiReceiver = nullptr;
+
         }
 
         BundleResource::~BundleResource()
@@ -47,61 +50,96 @@ namespace OIC
         std::list< std::string > BundleResource::getAttributeNames()
         {
             std::list< std::string > ret;
-            for (RCSResourceAttributes::iterator it = m_resourceAttributes.begin();
-                 it != m_resourceAttributes.end(); ++it)
-            {
-                ret.push_back(it->key());
+
+            for (auto &it : m_resourceAttributes){
+                ret.push_back(it.key());
             }
+
             return ret;
         }
 
-        RCSResourceAttributes &BundleResource::getAttributes()
+        const RCSResourceAttributes BundleResource::getAttributes()
+        {
+            std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
+            return RCSResourceAttributes(m_resourceAttributes);
+        }
+
+        void BundleResource::setAttributes(const RCSResourceAttributes &attrs)
         {
-            return m_resourceAttributes;
+            setAttributes(attrs, true);
         }
 
-        void BundleResource::setAttributes(RCSResourceAttributes &attrs, bool notify)
+        void BundleResource::setAttributes(const RCSResourceAttributes &attrs, bool notify)
         {
-            for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
-            {
-                OC_LOG_V(INFO, "BUNDLE_RESOUCE", "set attribute \(%s)'",
-                         std::string(it->key() + "\', with " + it->value().toString()).c_str());
+            std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
+
+            for (auto &it : m_resourceAttributes){
+                OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'",
+                           std::string(it.key() + "\', with " + it.value().toString()).c_str());
 
-                m_resourceAttributes[it->key()] = it->value();
+                m_resourceAttributes[it.key()] = it.value();
             }
 
-            if (notify && m_pNotiReceiver){
-                OC_LOG_V(INFO, "BUNDLE_RESOUCE", "Notifying receiver");
-                m_pNotiReceiver->onNotificationReceived(m_uri);
+            if(notify){
+                // asynchronous notification
+                auto notifyFunc = [](NotificationReceiver *notificationReceiver,
+                                        std::string uri)
+                {
+                    if (notificationReceiver){
+                        notificationReceiver->onNotificationReceived(uri);
+                    }
+                };
+                auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
+                boost::thread notifyThread(f);
             }
-        }
 
-        void BundleResource::setAttributes(RCSResourceAttributes &attrs)
-        {
-            setAttributes(attrs, false);
         }
 
         void BundleResource::setAttribute(const std::string &key,
                                           RCSResourceAttributes::Value &&value, bool notify)
         {
-            OC_LOG_V(INFO, "BUNDLE_RESOUCE", "set attribute \(%s)'", std::string(key + "\', with " +
+            OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'", std::string(key + "\', with " +
                      value.toString()).c_str());
+            std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
+            m_resourceAttributes[key] = std::move(value);
+
+            if(notify){
+                // asynchronous notification
+                auto notifyFunc = [](NotificationReceiver *notificationReceiver,
+                                        std::string uri)
+                {
+                    if (notificationReceiver){
+                        notificationReceiver->onNotificationReceived(uri);
+                    }
+                };
+                auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
+                boost::thread notifyThread(f);
+            }
 
-            m_resourceAttributes[key] = value;
+        }
 
-            if (notify && m_pNotiReceiver)
-                m_pNotiReceiver->onNotificationReceived(m_uri);
+        void BundleResource::setAttribute(const std::string &key,
+                                                 RCSResourceAttributes::Value &value, bool notify)
+        {
+            setAttribute(key, RCSResourceAttributes::Value(value), notify);
         }
 
-        void BundleResource::setAttribute(const std::string &key, RCSResourceAttributes::Value &&value)
+        void BundleResource::setAttribute(const std::string &key,
+                RCSResourceAttributes::Value &&value)
         {
             setAttribute(key, std::move(value), true);
         }
 
-        RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
+        void BundleResource::setAttribute(const std::string &key,
+                        RCSResourceAttributes::Value &value)
         {
-            OC_LOG_V(INFO, "BUNDLE_RESOUCE", "get attribute \'(%s)" , std::string(key + "\'").c_str());
+            setAttribute(key, value, true);
+        }
 
+        RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
+        {
+            OIC_LOG_V(INFO, CONTAINER_TAG, "get attribute \'(%s)" , std::string(key + "\'").c_str());
+            std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
             return m_resourceAttributes.at(key);
         }
     }