******************************************************************/
#include "resource_manager.h"
+#include "simulator_logger.h"
int ResourceManager::id;
ResourceManager *ResourceManager::getInstance()
delete m_resourceCreator;
}
-SimulatorResourcePtr ResourceManager::createResource(const std::string &configPath,
- SimulatorResource::ResourceModelChangedCB callback)
+SimulatorResourceServerPtr ResourceManager::createResource(const std::string &configPath,
+ SimulatorResourceServer::ResourceModelChangedCB callback)
{
/**
* TODO: Temporarily creating the light resource for testing the basic flow
* Once the config parser is included this method will simulate the resource based on the config file
*/
- SimulatorResourcePtr simulatorResource = m_resourceCreator->createLightResoure();
+ SimulatorResourceServerPtr simulatorResource = m_resourceCreator->createLightResoure();
simulatorResource->setModelChangeCallback(callback);
-
- simulatorResource->setURI(getURI(simulatorResource->getURI()));
- OC::EntityHandler entityHandler = std::bind(&SimulatorResource::entityHandler,
- simulatorResource.get(), std::placeholders::_1);
- std::string uri = simulatorResource->getURI();
- OCStackResult result = OC::OCPlatform::registerResource(simulatorResource->m_resourceHandle,
- uri,
- simulatorResource->getResourceType(),
- simulatorResource->getInterfaceType(),
- entityHandler,
- OC_DISCOVERABLE | OC_OBSERVABLE);
- if (OC_STACK_OK != result)
+ std::string uri = getURI(simulatorResource->getURI());
+ if(uri.empty())
{
+ SIM_LOG(ILogger::ERROR, "Cannot register resource. Resource URI is empty");
+ return NULL;
+ }
+ simulatorResource->setURI(uri);
+ SimulatorResult result = simulatorResource->start();
+ if (SIMULATOR_SUCCESS != result)
+ {
+ SIM_LOG(ILogger::ERROR, "Failed to register resource [" << simulatorResource->getURI() <<
+ "] with platform");
return NULL;
}
// Add the resource to resource list table
+ std::lock_guard<std::recursive_mutex> lock(m_listMutex);
m_resourceList[simulatorResource->getResourceType()].insert(
- std::pair<std::string, SimulatorResourcePtr>(simulatorResource->getURI(), simulatorResource));
+ std::pair<std::string, SimulatorResourceServerPtr>(simulatorResource->getURI(), simulatorResource));
return simulatorResource;
}
-std::vector<SimulatorResourcePtr> ResourceManager::createResource(const std::string &configPath,
- const int count, SimulatorResource::ResourceModelChangedCB callback)
+std::vector<SimulatorResourceServerPtr> ResourceManager::createResource(
+ const std::string &configPath,
+ const int count, SimulatorResourceServer::ResourceModelChangedCB callback)
{
- std::vector<SimulatorResourcePtr> list;
- return list;
+ std::vector<SimulatorResourceServerPtr> resourceList;
+ for (int i = 0; i < count; i++)
+ {
+ /**
+ * TODO: Temporarily creating the light resource for testing the basic flow
+ * Once the config parser is included this method will simulate the resource based on the config file
+ */
+ SimulatorResourceServerPtr simulatorResource = m_resourceCreator->createLightResoure();
+ simulatorResource->setModelChangeCallback(callback);
+ std::string uri = getURI(simulatorResource->getURI());
+ if(uri.empty())
+ {
+ SIM_LOG(ILogger::ERROR, "Cannot register resource. Resource URI is empty");
+ break;
+ }
+ simulatorResource->setURI(uri);
+ SimulatorResult result = simulatorResource->start();
+ if (SIMULATOR_SUCCESS != result)
+ {
+ resourceList.clear();
+ SIM_LOG(ILogger::ERROR, "Failed to register resources!");
+ break;
+ }
+ else
+ {
+ resourceList.push_back(simulatorResource);
+ }
+ }
+
+ // Add the resource to resource list table
+ std::lock_guard<std::recursive_mutex> lock(m_listMutex);
+ for (auto & resource : resourceList)
+ {
+ m_resourceList[resource->getResourceType()].insert(
+ std::pair<std::string, SimulatorResourceServerPtr>(resource->getURI(), resource));
+ }
+
+ return resourceList;
}
-std::vector<SimulatorResourcePtr> ResourceManager::getResources(void) const
+std::vector<SimulatorResourceServerPtr> ResourceManager::getResources(
+ const std::string &resourceType)
{
- std::vector<SimulatorResourcePtr> list;
- return list;
+ std::lock_guard<std::recursive_mutex> lock(m_listMutex);
+
+ std::vector<SimulatorResourceServerPtr> resourceList;
+ for (auto resourceTableEntry : m_resourceList)
+ {
+ if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
+ continue;
+
+ for (auto resourceEntry : resourceTableEntry.second)
+ {
+ resourceList.push_back(resourceEntry.second);
+ }
+ }
+
+ return resourceList;
}
-SimulatorResult ResourceManager::deleteResource(SimulatorResourcePtr &resource)
+SimulatorResult ResourceManager::deleteResource(SimulatorResourceServerPtr &resource)
{
- if (!resource.get())
- return SIMULATOR_RESOURCE_NOT_FOUND;
+ std::lock_guard<std::recursive_mutex> lock(m_listMutex);
+
+ if (nullptr == resource)
+ return SIMULATOR_BAD_INPUT;
+
+ SimulatorResult result = SIMULATOR_RESOURCE_NOT_FOUND;
- auto resourceTableEntry = m_resourceList.find(resource->getResourceType());
- if (m_resourceList.end() != resourceTableEntry)
+ try
{
- auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
- if (resourceTableEntry->second.end() != resourceEntry)
+ auto resourceTableEntry = m_resourceList.find(resource->getResourceType());
+ if (m_resourceList.end() != resourceTableEntry)
{
- if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
- resourceTableEntry->second.erase(resourceEntry);
+ auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
+ if (resourceTableEntry->second.end() != resourceEntry)
+ {
+ if (SIMULATOR_SUCCESS == resource->stop())
+ {
+ resourceTableEntry->second.erase(resourceEntry);
+ result = SIMULATOR_SUCCESS;
+ }
+ else
+ {
+ result = SIMULATOR_ERROR;
+ }
+ }
}
}
+ catch (OC::OCException &except)
+ {
+ SIM_LOG(ILogger::ERROR, except.reason() << except.code())
+ result = SIMULATOR_ERROR;
+ }
- return SIMULATOR_SUCCESS;
+ return result;
}
SimulatorResult ResourceManager::deleteResources(const std::string &resourceType)
{
- auto resourceTableEntry = m_resourceList.find(resourceType);
- if (m_resourceList.end() != resourceTableEntry)
+ std::lock_guard<std::recursive_mutex> lock(m_listMutex);
+
+ SimulatorResult result = SIMULATOR_RESOURCE_NOT_FOUND;
+ try
{
- return SIMULATOR_RESOURCE_NOT_FOUND;
+ for (auto & resourceTableEntry : m_resourceList)
+ {
+ if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
+ continue;
+
+ for (auto & resourceEntry : resourceTableEntry.second)
+ {
+ SimulatorResourceServerPtr resource = resourceEntry.second;
+ if (SIMULATOR_SUCCESS == resource->stop())
+ {
+ resourceTableEntry.second.erase(resourceTableEntry.second.find(resource->getURI()));
+ result = SIMULATOR_SUCCESS;
+ }
+ else
+ {
+ return SIMULATOR_ERROR;
+ }
+ }
+ }
}
-
- for (auto resourceEntry : resourceTableEntry->second)
+ catch (OC::OCException &except)
{
- SimulatorResourcePtr resource = resourceEntry.second;
- if (OC_STACK_OK == OC::OCPlatform::unregisterResource(resource->getHandle()))
- resourceTableEntry->second.erase(resourceTableEntry->second.find(resource->getURI()));
+ SIM_LOG(ILogger::ERROR, except.reason() << except.code())
+ result = SIMULATOR_ERROR;
}
- return SIMULATOR_SUCCESS;
+ return result;
}
std::string ResourceManager::getURI(std::string uri)
{
+ if(uri.empty())
+ {
+ return uri;
+ }
std::ostringstream os;
os << uri;
- if (!uri.empty() && '/' != uri[uri.length() - 1])
+ if ('/' != uri[uri.length() - 1])
os << '/';
os << "simulator/" << id++;
return os.str();