#include <functional>
#include <pthread.h>
+#include <mutex>
+#include <condition_variable>
#include "OCPlatform.h"
#include "OCApi.h"
public:
/// Constructor
LightResource()
- :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
+ :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light"),
+ m_resourceHandle(nullptr) {
// Initialize representation
m_lightRep.setUri(m_lightUri);
/// This function internally calls registerResource API.
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
- std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
+ //URI of the resource
+ std::string resourceURI = m_lightUri;
+ //resource type name. In this case, it is light
+ std::string resourceTypeName = "core.light";
+ // resource interface.
+ std::string resourceInterface = DEFAULT_INTERFACE;
// OCResourceProperty is defined ocstack.h
uint8_t 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.
+ // URI of the resource
+ std::string resourceURI = "/a/light1";
+ // resource type name. In this case, it is light
+ std::string resourceTypeName = "core.light";
+ // resource interface.
+ std::string resourceInterface = DEFAULT_INTERFACE;
// OCResourceProperty is defined ocstack.h
uint8_t resourceProperty;
std::string requestType = request->getRequestType();
int requestFlag = request->getRequestHandlerFlag();
- if(requestFlag & RequestHandlerFlag::InitFlag)
- {
- cout << "\t\trequestFlag : Init\n";
-
- // entity handler to perform resource initialization operations
- }
if(requestFlag & RequestHandlerFlag::RequestFlag)
{
cout << "\t\trequestFlag : Request\n";
pResponse->setRequestHandle(request->getRequestHandle());
pResponse->setResourceHandle(request->getResourceHandle());
+ // Check for query params (if any)
+ QueryParamsMap queries = request->getQueryParameters();
+
+ if (!queries.empty())
+ {
+ std::cout << "\nQuery processing upto entityHandler" << std::endl;
+ }
+ for (auto it : queries)
+ {
+ std::cout << "Query key: " << it.first << " value : " << it.second
+ << std:: endl;
+ }
+
// If the request type is GET
if(requestType == "GET")
{
pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
}
+ else
+ {
+ pResponse->setResponseResult(OC_EH_OK);
+ }
if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
{
}
else if(requestType == "DELETE")
{
- // DELETE request operations
+ cout << "Delete request received" << endl;
}
}
// This function continuously monitors for the changes
while (1)
{
- sleep (5);
+ sleep (3);
if (gObservation)
{
if(isListOfObservers)
{
- std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
+ std::shared_ptr<OCResourceResponse> resourceResponse =
+ {std::make_shared<OCResourceResponse>()};
resourceResponse->setErrorCode(200);
resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
std::cout << " 4 - Non-secure resource, GET slow response, notify all observers\n";
}
+static FILE* client_open(const char* /*path*/, const char *mode)
+{
+ return fopen("./oic_svr_db_server.json", mode);
+}
-int main(int argc, char* argv[1])
+int main(int argc, char* argv[])
{
PrintUsage();
+ OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
if (argc == 1)
{
break;
case 4:
isSlowResponse = true;
+ break;
default:
break;
}
OC::ModeType::Server,
"0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
0, // Uses randomly available port
- OC::QualityOfService::LowQos
+ OC::QualityOfService::LowQos,
+ &ps
};
OCPlatform::Configure(cfg);
// Invoke createResource function of class light.
myLight.createResource();
+ std::cout << "Created resource." << std::endl;
myLight.addType(std::string("core.brightlight"));
- myLight.addInterface(std::string("oc.mi.ll"));
- // Perform app tasks
- while(true)
- {
- // some tasks
- }
+ myLight.addInterface(std::string(LINK_INTERFACE));
+ std::cout << "Added Interface and Type" << std::endl;
+
+
+ // A condition variable will free the mutex it is given, then do a non-
+ // intensive block until 'notify' is called on it. In this case, since we
+ // don't ever call cv.notify, this should be a non-processor intensive version
+ // of while(true);
+ std::mutex blocker;
+ std::condition_variable cv;
+ std::unique_lock<std::mutex> lock(blocker);
+ std::cout <<"Waiting" << std::endl;
+ cv.wait(lock, []{return false;});
}
- catch(OCException e)
+ catch(OCException &e)
{
- //log(e.what());
+ std::cout << "OCException in main : " << e.what() << endl;
}
// No explicit call to stop the platform.
// When OCPlatform::destructor is invoked, internally we do platform cleanup
+
+ return 0;
}