Added the simpleclient/simpleserver changes from Sashi's review. Should be able to use them to test post.
Change-Id: I3e96e0a1915e4da0e4ce5880db0b00c6dd73f247
{
std::cout << "POST request was successful" << std::endl;
- rep.getValue("state", mylight.m_state);
- rep.getValue("power", mylight.m_power);
- rep.getValue("name", mylight.m_name);
+ if(rep.hasAttribute("createduri"))
+ {
+ std::cout << "\tUri of the created resource: "
+ << rep.getValue<std::string>("createduri") << std::endl;
+ }
+ else
+ {
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- std::cout << "\tstate: " << mylight.m_state << std::endl;
- std::cout << "\tpower: " << mylight.m_power << std::endl;
- std::cout << "\tname: " << mylight.m_name << std::endl;
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
+ }
if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
std::cout << endl << "Observe is used." << endl << endl;
{
std::cout << "POST request was successful" << std::endl;
- rep.getValue("state", mylight.m_state);
- rep.getValue("power", mylight.m_power);
- rep.getValue("name", mylight.m_name);
+ if(rep.hasAttribute("createduri"))
+ {
+ std::cout << "\tUri of the created resource: "
+ << rep.getValue<std::string>("createduri") << std::endl;
+ }
+ else
+ {
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- std::cout << "\tstate: " << mylight.m_state << std::endl;
- std::cout << "\tpower: " << mylight.m_power << std::endl;
- std::cout << "\tname: " << mylight.m_name << std::endl;
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
+ }
OCRepresentation rep2;
public:
/// Access this property from a TB client
+ OCPlatform m_platform;
std::string m_name;
bool m_state;
int m_power;
public:
/// Constructor
- LightResource(): m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
+ LightResource(PlatformConfig& cfg): m_platform(cfg),
+ m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
// Initialize representation
m_lightRep.setUri(m_lightUri);
access to, you can accomplish this with a free function: */
/// This function internally calls registerResource API.
- void createResource(OC::OCPlatform& platform)
+ void createResource()
{
std::string resourceURI = m_lightUri; // URI of the resource
std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2);
// This will internally create and register the resource.
- OCStackResult result = platform.registerResource(
+ OCStackResult result = m_platform.registerResource(
m_resourceHandle, resourceURI, resourceTypeName,
resourceInterface, cb, resourceProperty);
}
}
+ OCStackResult createResource1()
+ {
+ std::string resourceURI = "/a/light1"; // URI of the resource
+ std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
+ std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
+
+ // OCResourceProperty is defined ocstack.h
+ uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+
+ EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2);
+
+ OCResourceHandle resHandle;
+
+ // This will internally create and register the resource.
+ OCStackResult result = m_platform.registerResource(
+ resHandle, resourceURI, resourceTypeName,
+ resourceInterface, cb, resourceProperty);
+
+ if (OC_STACK_OK != result)
+ {
+ cout << "Resource creation was unsuccessful\n";
+ }
+
+ return result;
+ }
+
OCResourceHandle getHandle()
{
return m_resourceHandle;
// updates the internal state
OCRepresentation post(OCRepresentation& rep)
{
- put(rep);
- return m_lightRep;
+ static int first = 1;
+
+ std::cout << "In POST\n";
+
+ // for the first time it tries to create a resource
+ if(first)
+ {
+ std::cout << "In POST/First\n";
+
+ first = 0;
+
+ if(OC_STACK_OK == createResource1())
+ {
+ std::cout << "Created a new resource\n";
+
+ OCRepresentation rep1;
+ rep1.setValue("createduri", std::string("/a/light1"));
+
+ return rep1;
+ }
+ }
+
+ // from second time onwards it just puts
+ put(rep);
+ return get();
}
return m_lightRep;
}
- void addType(const OC::OCPlatform& platform, const std::string& type) const
+ void addType(const std::string& type) const
{
- OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type);
+ OCStackResult result = m_platform.bindTypeToResource(m_resourceHandle, type);
if (OC_STACK_OK != result)
{
cout << "Binding TypeName to Resource was unsuccessful\n";
}
}
- void addInterface(const OC::OCPlatform& platform, const std::string& interface) const
+ void addInterface(const std::string& interface) const
{
- OCStackResult result = platform.bindInterfaceToResource(m_resourceHandle, interface);
+ OCStackResult result = m_platform.bindInterfaceToResource(m_resourceHandle, interface);
if (OC_STACK_OK != result)
{
cout << "Binding TypeName to Resource was unsuccessful\n";
// Do related operations related to POST request
- // Update the lightResource
- post(rep);
+ OCRepresentation rep_post = post(rep);
if(response)
{
// TODO Error Code
response->setErrorCode(200);
- response->setResourceRepresentation(get());
+ response->setResourceRepresentation(rep_post);
}
// POST request operations
}
else
{
- result = OCPlatform::notifyAllObservers(lightPtr->getHandle());
+ result = lightPtr->m_platform.notifyAllObservers(lightPtr->getHandle());
}
if(OC_STACK_NO_OBSERVERS == result)
OC::QualityOfService::NonConfirmable
};
- // Create a OCPlatform instance.
- // Note: Platform creation is synchronous call.
try
{
- OCPlatform platform(cfg);
-
// Create the instance of the resource class (in this case instance of class 'LightResource').
- LightResource myLight;
+ LightResource myLight(cfg);
// Invoke createResource function of class light.
- myLight.createResource(platform);
+ myLight.createResource();
- myLight.addType(platform, std::string("core.brightlight"));
- myLight.addInterface(platform, std::string("oc.mi.ll"));
+ myLight.addType(std::string("core.brightlight"));
+ myLight.addInterface(std::string("oc.mi.ll"));
// Perform app tasks
while(true)
{
class InProcClientWrapper : public IClientWrapper
{
public:
- InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock,
+ InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::recursive_mutex> csdkLock,
PlatformConfig cfg);
virtual ~InProcClientWrapper();
std::string assembleSetResourcePayload(const OCRepresentation& attributes);
std::thread m_listeningThread;
bool m_threadRun;
- std::weak_ptr<std::mutex> m_csdkLock;
+ std::weak_ptr<std::recursive_mutex> m_csdkLock;
private:
OC::OCPlatform& m_owner;
class InProcServerWrapper : public IServerWrapper
{
public:
- InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg);
+ InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::recursive_mutex> csdkLock,
+ PlatformConfig cfg);
virtual ~InProcServerWrapper();
virtual OCStackResult registerResource(
void processFunc();
std::thread m_processThread;
bool m_threadRun;
- std::weak_ptr<std::mutex> m_csdkLock;
+ std::weak_ptr<std::recursive_mutex> m_csdkLock;
};
}
std::unique_ptr<WrapperFactory> m_WrapperInstance;
IServerWrapper::Ptr m_server;
IClientWrapper::Ptr m_client;
- std::shared_ptr<std::mutex> m_csdkLock;
+ std::shared_ptr<std::recursive_mutex> m_csdkLock;
private:
/**
class OutOfProcClientWrapper : public IClientWrapper
{
public:
- OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock,
+ OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::recursive_mutex> csdkLock,
PlatformConfig cfg)
: IClientWrapper(owner)
{}
public:
typedef std::shared_ptr<IWrapperFactory> Ptr;
- virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
- virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
+ virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner,
+ std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg) =0;
+ virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner,
+ std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg) =0;
virtual ~IWrapperFactory(){}
};
public:
WrapperFactory(){}
- virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner,
+ std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
{
switch(cfg.serviceType)
{
return nullptr;
}
- virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner,
+ std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
{
switch(cfg.serviceType)
{
namespace OC
{
- InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner,
+ std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
: IClientWrapper(owner),
m_threadRun(false), m_csdkLock(csdkLock),
m_owner(owner),
auto cLock = m_csdkLock.lock();
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCProcess();
}
else
auto cLock = m_csdkLock.lock();
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
OCDoHandle handle;
result = OCDoResource(&handle, OC_REST_GET,
resourceType.c_str(),
std::ostringstream os;
os << host << assembleSetResourceUri(uri, queryParams).c_str();
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
OCDoHandle handle;
result = OCDoResource(&handle, OC_REST_GET, os.str().c_str(),
nullptr, nullptr,
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
OCDoHandle handle;
result = OCDoResource(&handle, OC_REST_POST,
os.str().c_str(), nullptr,
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
OCDoHandle handle;
result = OCDoResource(&handle, OC_REST_PUT,
os.str().c_str(), nullptr,
std::ostringstream os;
os << host << assembleSetResourceUri(uri, queryParams).c_str();
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCDoResource(handle, method,
os.str().c_str(), nullptr,
nullptr,
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCCancel(handle, OC_NON_CONFIRMABLE, NULL, 0);
}
else
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCCancel(handle, OC_NON_CONFIRMABLE, NULL, 0);
}
else
namespace OC
{
InProcServerWrapper::InProcServerWrapper(OC::OCPlatform& owner,
- std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+ std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
: IServerWrapper(owner),
m_csdkLock(csdkLock)
{
OCStackResult result;
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCProcess();
}
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
if(NULL != eHandler)
{
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCDeleteResource(resourceHandle);
if(result == OC_STACK_OK)
OCStackResult result;
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCBindResourceTypeToResource(resourceHandle, resourceTypeName.c_str());
}
else
OCStackResult result;
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCBindResourceInterfaceToResource(resourceHandle,
resourceInterfaceName.c_str());
}
OCStackResult result = OC_STACK_ERROR;
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCStartPresence(seconds);
}
OCStackResult result = OC_STACK_ERROR;
if(cLock)
{
- std::lock_guard<std::mutex> lock(*cLock);
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
result = OCStopPresence();
}
: m_log_stream { std::move(oc_log_stream {oc_make_ostream_logger}) },
m_cfg { config },
m_WrapperInstance { make_unique<WrapperFactory>() },
- m_csdkLock { make_shared<std::mutex>() }
+ m_csdkLock { make_shared<std::recursive_mutex>() }
{
init(m_cfg);
}
: m_log_stream { log_target },
m_cfg { config },
m_WrapperInstance { make_unique<WrapperFactory>() },
- m_csdkLock { make_shared<std::mutex>() }
+ m_csdkLock { make_shared<std::recursive_mutex>() }
{
init(m_cfg);
}