Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / src / BundleResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "BundleResource.h"
22
23 #include <list>
24 #include <string.h>
25 #include <iostream>
26 #include <boost/thread.hpp>
27 #include "NotificationReceiver.h"
28
29 #include "InternalTypes.h"
30
31 namespace OIC
32 {
33     namespace Service
34     {
35         BundleResource::BundleResource() : m_pNotiReceiver(nullptr), m_resourceAttributes_mutex()
36         {
37
38         }
39
40         BundleResource::~BundleResource()
41         {
42             m_pNotiReceiver = nullptr;
43         }
44
45         void BundleResource::registerObserver(NotificationReceiver *pNotiReceiver)
46         {
47             m_pNotiReceiver = pNotiReceiver;
48         }
49
50         std::list< std::string > BundleResource::getAttributeNames()
51         {
52             std::list< std::string > ret;
53
54             for (auto &it : m_resourceAttributes){
55                 ret.push_back(it.key());
56             }
57
58             return ret;
59         }
60
61         const RCSResourceAttributes BundleResource::getAttributes()
62         {
63             std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
64             return RCSResourceAttributes(m_resourceAttributes);
65         }
66
67         void BundleResource::setAttributes(const RCSResourceAttributes &attrs)
68         {
69             setAttributes(attrs, true);
70         }
71
72         void BundleResource::setAttributes(const RCSResourceAttributes &attrs, bool notify)
73         {
74             std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
75
76             for (auto &it : attrs)
77             {
78                 OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'",
79                            std::string(it.key() + "\', with " + it.value().toString()).c_str());
80
81                 m_resourceAttributes[it.key()] = it.value();
82             }
83
84             if(notify){
85                 // asynchronous notification
86                 auto notifyFunc = [](NotificationReceiver *notificationReceiver,
87                                         std::string uri)
88                 {
89                     if (notificationReceiver){
90                         notificationReceiver->onNotificationReceived(uri);
91                     }
92                 };
93                 auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
94                 boost::thread notifyThread(f);
95             }
96
97         }
98
99         void BundleResource::setAttribute(const std::string &key,
100                                           RCSResourceAttributes::Value &&value, bool notify)
101         {
102             OIC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'", std::string(key + "\', with " +
103                      value.toString()).c_str());
104             std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
105             m_resourceAttributes[key] = std::move(value);
106
107             if(notify){
108                 // asynchronous notification
109                 auto notifyFunc = [](NotificationReceiver *notificationReceiver,
110                                         std::string uri)
111                 {
112                     if (notificationReceiver){
113                         notificationReceiver->onNotificationReceived(uri);
114                     }
115                 };
116                 auto f = std::bind(notifyFunc, m_pNotiReceiver, m_uri);
117                 boost::thread notifyThread(f);
118             }
119
120         }
121
122         void BundleResource::setAttribute(const std::string &key,
123                                                  RCSResourceAttributes::Value &value, bool notify)
124         {
125             setAttribute(key, RCSResourceAttributes::Value(value), notify);
126         }
127
128         void BundleResource::setAttribute(const std::string &key,
129                 RCSResourceAttributes::Value &&value)
130         {
131             setAttribute(key, std::move(value), true);
132         }
133
134         void BundleResource::setAttribute(const std::string &key,
135                         RCSResourceAttributes::Value &value)
136         {
137             setAttribute(key, value, true);
138         }
139
140         RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
141         {
142             OIC_LOG_V(INFO, CONTAINER_TAG, "get attribute \'(%s)" , std::string(key + "\'").c_str());
143             std::lock_guard<std::mutex> lock(m_resourceAttributes_mutex);
144             return m_resourceAttributes.at(key);
145         }
146     }
147 }