#include <OCResourceResponse.h>
#include <ocstack.h>
#include <OCApi.h>
+#include <ocmalloc.h>
#include <OCPlatform.h>
#include <OCUtilities.h>
using namespace std;
using namespace OC;
-std::map <OCResourceHandle, OC::EntityHandler> entityHandlerMap;
-std::map <OCResourceHandle, std::string> resourceUriMap;
-EntityHandler defaultDeviceEntityHandler = 0;
+namespace OC
+{
+ namespace details
+ {
+ std::mutex serverWrapperLock;
+ std::map <OCResourceHandle, OC::EntityHandler> entityHandlerMap;
+ std::map <OCResourceHandle, std::string> resourceUriMap;
+ EntityHandler defaultDeviceEntityHandler = 0;
+ }
+}
void formResourceRequest(OCEntityHandlerFlag flag,
OCEntityHandlerRequest * entityHandlerRequest,
std::shared_ptr<OCResourceRequest> pRequest)
{
- pRequest->setRequestHandle(entityHandlerRequest->requestHandle);
- pRequest->setResourceHandle(entityHandlerRequest->resource);
-
- if(flag & OC_INIT_FLAG)
+ if(pRequest && entityHandlerRequest)
{
- pRequest->setRequestHandlerFlag(OC::RequestHandlerFlag::InitFlag);
+ pRequest->setRequestHandle(entityHandlerRequest->requestHandle);
+ pRequest->setResourceHandle(entityHandlerRequest->resource);
}
if(flag & OC_REQUEST_FLAG)
{
if(entityHandlerRequest->query)
{
- std::string querystr(reinterpret_cast<char*>(entityHandlerRequest->query));
-
- OC::Utilities::QueryParamsKeyVal qp = OC::Utilities::getQueryParams(querystr);
+ OC::Utilities::QueryParamsKeyVal qp = OC::Utilities::getQueryParams(
+ entityHandlerRequest->query);
if(qp.size() > 0)
{
pRequest->setResourceUri(std::string(uri));
- if(defaultDeviceEntityHandler)
+ EntityHandler defHandler;
{
- result = defaultDeviceEntityHandler(pRequest);
+ std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+ defHandler = OC::details::defaultDeviceEntityHandler;
+ }
+
+ if(defHandler)
+ {
+ result = defHandler(pRequest);
}
else
{
formResourceRequest(flag, entityHandlerRequest, pRequest);
+ std::map <OCResourceHandle, std::string>::iterator resourceUriEntry;
+ std::map <OCResourceHandle, std::string>::iterator resourceUriEnd;
+ {
+ std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+ resourceUriEntry = OC::details::resourceUriMap.find(entityHandlerRequest->resource);
+ resourceUriEnd = OC::details::resourceUriMap.end();
+ }
// Finding the corresponding URI for a resource handle and set the URI in the request
- auto resourceUriEntry = resourceUriMap.find(entityHandlerRequest->resource);
- if(resourceUriEntry != resourceUriMap.end())
+ if(resourceUriEntry != resourceUriEnd)
{
pRequest->setResourceUri(resourceUriEntry->second);
}
return OC_EH_ERROR;
}
- // Finding the corresponding CPP Application entityHandler for a given resource
- auto entityHandlerEntry = entityHandlerMap.find(entityHandlerRequest->resource);
+ std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEntry;
+ std::map <OCResourceHandle, OC::EntityHandler>::iterator entityHandlerEnd;
+ {
+ // Finding the corresponding CPP Application entityHandler for a given resource
+ std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+ entityHandlerEntry = OC::details::entityHandlerMap.find(entityHandlerRequest->resource);
+ entityHandlerEnd = OC::details::entityHandlerMap.end();
+ }
- if(entityHandlerEntry != entityHandlerMap.end())
+ if(entityHandlerEntry != entityHandlerEnd)
{
// Call CPP Application Entity Handler
if(entityHandlerEntry->second)
result = OCProcess();
}
- // ...the value of variable result is simply ignored for now.
if(OC_STACK_ERROR == result)
- ;
+ {
+ oclog() << "OCProcess failed with result " << result <<std::flush;
+ // ...the value of variable result is simply ignored for now.
+ }
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return result;
}
+ OCStackResult InProcServerWrapper::registerPlatformInfo(const OCPlatformInfo platformInfo)
+ {
+ auto cLock = m_csdkLock.lock();
+ OCStackResult result = OC_STACK_ERROR;
+ if(cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCSetPlatformInfo(platformInfo);
+ }
+ return result;
+ }
+
OCStackResult InProcServerWrapper::registerResource(
OCResourceHandle& resourceHandle,
std::string& resourceURI,
}
else
{
- entityHandlerMap[resourceHandle] = eHandler;
- resourceUriMap[resourceHandle] = resourceURI;
+ std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+ OC::details::entityHandlerMap[resourceHandle] = eHandler;
+ OC::details::resourceUriMap[resourceHandle] = resourceURI;
}
}
else
}
else
{
- entityHandlerMap[resourceHandle] = eHandler;
- resourceUriMap[resourceHandle] = resourceURI;
+ std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+ OC::details::entityHandlerMap[resourceHandle] = eHandler;
+ OC::details::resourceUriMap[resourceHandle] = resourceURI;
}
}
else
{
OCStackResult result = OC_STACK_ERROR;
- defaultDeviceEntityHandler = entityHandler;
+ {
+ std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+ OC::details::defaultDeviceEntityHandler = entityHandler;
+ }
if(entityHandler)
{
if(result == OC_STACK_OK)
{
- resourceUriMap.erase(resourceHandle);
+ std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock);
+ OC::details::resourceUriMap.erase(resourceHandle);
}
else
{
response.requestHandle = pResponse->getRequestHandle();
response.resourceHandle = pResponse->getResourceHandle();
response.ehResult = pResponse->getResponseResult();
- response.payload = (unsigned char*) payLoad.c_str();
+
+ response.payload = static_cast<char*>(OCMalloc(payLoad.length() + 1));
+ if(!response.payload)
+ {
+ result = OC_STACK_NO_MEMORY;
+ throw OCException(OC::Exception::NO_MEMORY, OC_STACK_NO_MEMORY);
+ }
+
+ strncpy(response.payload, payLoad.c_str(), payLoad.length()+1);
response.payloadSize = payLoad.length() + 1;
response.persistentBufferFlag = 0;
}
else
{
+ OCFree(response.payload);
result = OC_STACK_ERROR;
}
OCStop();
}
}
+