1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 "resource_manager.h"
22 #include "simulator_logger.h"
24 int ResourceManager::id;
25 ResourceManager *ResourceManager::getInstance()
27 static ResourceManager s_instance;
31 ResourceManager::ResourceManager()
32 : m_resourceCreator(new SimulatorResourceCreator()) {}
34 ResourceManager::~ResourceManager()
36 delete m_resourceCreator;
39 SimulatorResourceServerPtr ResourceManager::createResource(const std::string &configPath,
40 SimulatorResourceServer::ResourceModelChangedCB callback)
43 * TODO: Temporarily creating the light resource for testing the basic flow
44 * Once the config parser is included this method will simulate the resource based on the config file
46 SimulatorResourceServerPtr simulatorResource = m_resourceCreator->createLightResoure();
47 simulatorResource->setModelChangeCallback(callback);
48 std::string uri = getURI(simulatorResource->getURI());
51 SIM_LOG(ILogger::ERROR, "Cannot register resource. Resource URI is empty");
54 simulatorResource->setURI(uri);
55 SimulatorResult result = simulatorResource->start();
56 if (SIMULATOR_SUCCESS != result)
58 SIM_LOG(ILogger::ERROR, "Failed to register resource [" << simulatorResource->getURI() <<
63 // Add the resource to resource list table
64 std::lock_guard<std::recursive_mutex> lock(m_listMutex);
65 m_resourceList[simulatorResource->getResourceType()].insert(
66 std::pair<std::string, SimulatorResourceServerPtr>(simulatorResource->getURI(), simulatorResource));
67 return simulatorResource;
70 std::vector<SimulatorResourceServerPtr> ResourceManager::createResource(
71 const std::string &configPath,
72 const int count, SimulatorResourceServer::ResourceModelChangedCB callback)
74 std::vector<SimulatorResourceServerPtr> resourceList;
75 for (int i = 0; i < count; i++)
78 * TODO: Temporarily creating the light resource for testing the basic flow
79 * Once the config parser is included this method will simulate the resource based on the config file
81 SimulatorResourceServerPtr simulatorResource = m_resourceCreator->createLightResoure();
82 simulatorResource->setModelChangeCallback(callback);
83 std::string uri = getURI(simulatorResource->getURI());
86 SIM_LOG(ILogger::ERROR, "Cannot register resource. Resource URI is empty");
89 simulatorResource->setURI(uri);
90 SimulatorResult result = simulatorResource->start();
91 if (SIMULATOR_SUCCESS != result)
94 SIM_LOG(ILogger::ERROR, "Failed to register resources!");
99 resourceList.push_back(simulatorResource);
103 // Add the resource to resource list table
104 std::lock_guard<std::recursive_mutex> lock(m_listMutex);
105 for (auto & resource : resourceList)
107 m_resourceList[resource->getResourceType()].insert(
108 std::pair<std::string, SimulatorResourceServerPtr>(resource->getURI(), resource));
114 std::vector<SimulatorResourceServerPtr> ResourceManager::getResources(
115 const std::string &resourceType)
117 std::lock_guard<std::recursive_mutex> lock(m_listMutex);
119 std::vector<SimulatorResourceServerPtr> resourceList;
120 for (auto resourceTableEntry : m_resourceList)
122 if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
125 for (auto resourceEntry : resourceTableEntry.second)
127 resourceList.push_back(resourceEntry.second);
134 SimulatorResult ResourceManager::deleteResource(SimulatorResourceServerPtr &resource)
136 std::lock_guard<std::recursive_mutex> lock(m_listMutex);
138 if (nullptr == resource)
139 return SIMULATOR_BAD_INPUT;
141 SimulatorResult result = SIMULATOR_RESOURCE_NOT_FOUND;
145 auto resourceTableEntry = m_resourceList.find(resource->getResourceType());
146 if (m_resourceList.end() != resourceTableEntry)
148 auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
149 if (resourceTableEntry->second.end() != resourceEntry)
151 if (SIMULATOR_SUCCESS == resource->stop())
153 resourceTableEntry->second.erase(resourceEntry);
154 result = SIMULATOR_SUCCESS;
158 result = SIMULATOR_ERROR;
163 catch (OC::OCException &except)
165 SIM_LOG(ILogger::ERROR, except.reason() << except.code())
166 result = SIMULATOR_ERROR;
172 SimulatorResult ResourceManager::deleteResources(const std::string &resourceType)
174 std::lock_guard<std::recursive_mutex> lock(m_listMutex);
176 SimulatorResult result = SIMULATOR_RESOURCE_NOT_FOUND;
179 for (auto & resourceTableEntry : m_resourceList)
181 if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
184 for (auto & resourceEntry : resourceTableEntry.second)
186 SimulatorResourceServerPtr resource = resourceEntry.second;
187 if (SIMULATOR_SUCCESS == resource->stop())
189 resourceTableEntry.second.erase(resourceTableEntry.second.find(resource->getURI()));
190 result = SIMULATOR_SUCCESS;
194 return SIMULATOR_ERROR;
199 catch (OC::OCException &except)
201 SIM_LOG(ILogger::ERROR, except.reason() << except.code())
202 result = SIMULATOR_ERROR;
208 std::string ResourceManager::getURI(std::string uri)
214 std::ostringstream os;
216 if ('/' != uri[uri.length() - 1])
218 os << "simulator/" << id++;