1567f4f8221437a12fbb1823c09fd5edccbe64d3
[platform/upstream/iotivity.git] / service / simulator / src / service-provider / resource_update_automation_mngr.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 "resource_update_automation_mngr.h"
22 #include "simulator_exceptions.h"
23 #include "simulator_logger.h"
24 #include "logger.h"
25
26 #define TAG "UPDATE_AUTOMATION_MNGR"
27
28 UpdateAutomationMngr::UpdateAutomationMngr()
29     :   m_id(0) {}
30
31 int UpdateAutomationMngr::startResourceAutomation(SimulatorResourceServer *resource,
32         AutomationType type, int interval, updateCompleteCallback callback)
33 {
34     if (!callback)
35     {
36         OC_LOG(ERROR, TAG, "Invalid callback!");
37         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
38     }
39
40     ResourceUpdateAutomationSP resourceAutomation(new ResourceUpdateAutomation(
41                 m_id, resource, type, interval, callback,
42                 std::bind(&UpdateAutomationMngr::automationCompleted, this, std::placeholders::_1)));
43
44     std::lock_guard<std::mutex> lock(m_lock);
45     resourceAutomation->start();
46
47     OC_LOG_V(DEBUG, TAG, "Resource automation successfully started [id: %d]", m_id);
48     SIM_LOG(ILogger::INFO, "Resource automation successfully started [ id: " << m_id << " ]");
49
50     m_resourceUpdationList[m_id] = resourceAutomation;
51     return m_id++;
52 }
53
54 int UpdateAutomationMngr::startAttributeAutomation(SimulatorResourceServer *resource,
55         const std::string &attrName, AutomationType type, int interval,
56         updateCompleteCallback callback)
57 {
58     if (!callback)
59     {
60         OC_LOG(ERROR, TAG, "Invalid callback!");
61         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
62     }
63
64     AttributeUpdateAutomationSP attributeAutomation(new AttributeUpdateAutomation(
65                 m_id, resource, attrName, type, interval, callback,
66                 std::bind(&UpdateAutomationMngr::automationCompleted, this, std::placeholders::_1)));
67
68     std::lock_guard<std::mutex> lock(m_lock);
69     attributeAutomation->start();
70
71     OC_LOG_V(DEBUG, TAG, "Attribute automation successfully started [name: %s, id: %d]",
72              attrName.c_str(), m_id);
73     SIM_LOG(ILogger::INFO, "Automation for " << attrName << " attribute has successfully started [ id: "
74             <<
75             m_id << " ]");
76
77     m_attrUpdationList[m_id] = attributeAutomation;
78     return m_id++;
79 }
80
81 std::vector<int> UpdateAutomationMngr::getResourceAutomationIds()
82 {
83     std::vector<int> ids;
84     std::lock_guard<std::mutex> lock(m_lock);
85     for (auto & automation : m_resourceUpdationList)
86         ids.push_back(automation.first);
87
88     return ids;
89 }
90
91 std::vector<int> UpdateAutomationMngr::getAttributeAutomationIds()
92 {
93     std::vector<int> ids;
94     std::lock_guard<std::mutex> lock(m_lock);
95     for (auto & automation : m_attrUpdationList)
96         ids.push_back(automation.first);
97
98     return ids;
99 }
100
101 void UpdateAutomationMngr::stop(int id)
102 {
103     std::lock_guard<std::mutex> lock(m_lock);
104     if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
105     {
106         m_resourceUpdationList[id]->stop();
107         m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
108     }
109     else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
110     {
111         m_attrUpdationList[id]->stop();
112         m_attrUpdationList.erase(m_attrUpdationList.find(id));
113     }
114 }
115
116 void UpdateAutomationMngr::stopAll()
117 {
118     std::lock_guard<std::mutex> lock(m_lock);
119     std::for_each(m_resourceUpdationList.begin(),
120                   m_resourceUpdationList.end(), [] (std::pair<int, ResourceUpdateAutomationSP> element)
121     {
122         element.second->stop();
123     });
124     m_resourceUpdationList.clear();
125
126     std::for_each(m_attrUpdationList.begin(),
127                   m_attrUpdationList.end(), [] (std::pair<int, AttributeUpdateAutomationSP> element)
128     {
129         element.second->stop();
130     });
131
132     m_attrUpdationList.clear();
133 }
134
135 void UpdateAutomationMngr::automationCompleted(int id)
136 {
137     std::lock_guard<std::mutex> lock(m_lock);
138     if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
139     {
140         m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
141     }
142     else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
143     {
144         m_attrUpdationList.erase(m_attrUpdationList.find(id));
145     }
146 }