1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "BundleResource.h"
26 #include <boost/thread.hpp>
27 #include "NotificationReceiver.h"
29 #include "InternalTypes.h"
35 BundleResource::BundleResource() : m_pNotiReceiver(nullptr)
40 BundleResource::~BundleResource()
42 m_pNotiReceiver = nullptr;
45 void BundleResource::registerObserver(NotificationReceiver *pNotiReceiver)
47 m_pNotiReceiver = pNotiReceiver;
50 std::list< std::string > BundleResource::getAttributeNames()
52 std::list< std::string > ret;
54 for (auto &it : m_resourceAttributes){
55 ret.push_back(it.key());
61 const RCSResourceAttributes BundleResource::getAttributes()
63 std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
64 return RCSResourceAttributes(m_resourceAttributes);
67 void BundleResource::setAttributes(const RCSResourceAttributes &attrs)
69 setAttributes(attrs, true);
72 void BundleResource::setAttributes(const RCSResourceAttributes &attrs, bool notify)
74 std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
76 for (auto &it : m_resourceAttributes){
77 OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'",
78 std::string(it.key() + "\', with " + it.value().toString()).c_str());
80 m_resourceAttributes[it.key()] = it.value();
84 // asynchronous notification
85 auto notifyFunc = [](NotificationReceiver *notificationReceiver,
88 if (notificationReceiver){
89 notificationReceiver->onNotificationReceived(uri);
92 auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
93 boost::thread notifyThread(f);
98 void BundleResource::setAttribute(const std::string &key,
99 RCSResourceAttributes::Value &&value, bool notify)
101 OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'", std::string(key + "\', with " +
102 value.toString()).c_str());
103 std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
104 m_resourceAttributes[key] = std::move(value);
107 // asynchronous notification
108 auto notifyFunc = [](NotificationReceiver *notificationReceiver,
111 if (notificationReceiver){
112 notificationReceiver->onNotificationReceived(uri);
115 auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
116 boost::thread notifyThread(f);
121 void BundleResource::setAttribute(const std::string &key,
122 RCSResourceAttributes::Value &value, bool notify)
124 setAttribute(key, RCSResourceAttributes::Value(value), notify);
127 void BundleResource::setAttribute(const std::string &key,
128 RCSResourceAttributes::Value &&value)
130 setAttribute(key, std::move(value), true);
133 void BundleResource::setAttribute(const std::string &key,
134 RCSResourceAttributes::Value &value)
136 setAttribute(key, value, true);
139 RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
141 OIC_LOG_V(INFO, CONTAINER_TAG, "get attribute \'(%s)" , std::string(key + "\'").c_str());
142 std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
143 return m_resourceAttributes.at(key);