replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / provider / src / NSAcceptedConsumers.cpp
1 //******************************************************************
2 //
3 // Copyright 2017 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
22 #include "NSProviderService.h"
23 #include <cstring>
24 #include "NSCommon.h"
25 #include "NSConstants.h"
26 #include "NSAcceptedConsumers.h"
27
28 namespace OIC
29 {
30     namespace Service
31     {
32         std::shared_ptr<NSConsumer> NSAcceptedConsumers::getConsumer(const std::string &id)
33         {
34             std::lock_guard<std::mutex> lock(m_mutex);
35             NS_LOG_V(DEBUG, "AcceptedConsumers::getConsumer size  : %d", (int) m_consumers.size());
36             auto it = m_consumers.find(id);
37             if (it != m_consumers.end() )
38             {
39                 NS_LOG(DEBUG, "getConsumer : Found Consumer with given ID");
40                 return it->second;
41             }
42             NS_LOG(DEBUG, "getConsumer : Not Found Consumer with given ID");
43             return NULL;
44         }
45         void NSAcceptedConsumers::addConsumer(std::shared_ptr<NSConsumer> consumer)
46         {
47             std::lock_guard<std::mutex> lock(m_mutex);
48             NS_LOG_V(INFO_PRIVATE, "AcceptedConsumers::addConsumer  Id : %s", consumer->getConsumerId().c_str());
49             m_consumers[consumer->getConsumerId()] = consumer;
50             return;
51         }
52
53         void NSAcceptedConsumers::removeConsumer(const std::string &id)
54         {
55             std::lock_guard<std::mutex> lock(m_mutex);
56             NS_LOG_V(INFO_PRIVATE, "AcceptedConsumers::removeConsumer  Id : %s", id.c_str());
57             m_consumers.erase(id);
58             return;
59         }
60
61         bool NSAcceptedConsumers::isAccepted(const std::string &id)
62         {
63             std::lock_guard<std::mutex> lock(m_mutex);
64             NS_LOG_V(INFO_PRIVATE, "AcceptedConsumers::isAccepted  Id : %s", id.c_str());
65             if ( m_consumers.find(id) == m_consumers.end() )
66             {
67                 NS_LOG(DEBUG, "isAccepted : Not Found Consumer with given ID");
68                 return false;
69             }
70             else
71             {
72                 NS_LOG(DEBUG, "isAccepted : Found Consumer with given ID");
73                 return true;
74             }
75         }
76
77         int NSAcceptedConsumers::size()
78         {
79             std::lock_guard<std::mutex> lock(m_mutex);
80             NS_LOG_V(DEBUG, "AcceptedConsumers::size  : %d", (int) m_consumers.size());
81             return m_consumers.size();
82         }
83
84         void NSAcceptedConsumers::removeConsumers()
85         {
86             std::lock_guard<std::mutex> lock(m_mutex);
87             NS_LOG(DEBUG, "AcceptedConsumers::removeConsumers ");
88             m_consumers.clear();
89             return;
90         }
91
92         std::map<std::string, std::shared_ptr<NSConsumer> > NSAcceptedConsumers::getConsumers()
93         {
94             std::lock_guard<std::mutex> lock(m_mutex);
95             return m_consumers;
96         }
97     }
98 }