1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
12 #include "InProcServerWrapper.h"
13 #include <InitializeException.h>
14 #include <OCReflect.h>
18 using namespace OC::OCReflect;
22 void entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest* eHandlerReq)
24 cout << "Resource Handler: " << eHandlerReq->resource << endl;
25 cout << "Method: " << eHandlerReq->method << endl;
26 cout << "reqJSONPayLoad: " << eHandlerReq->reqJSONPayload << endl;
31 InProcServerWrapper::InProcServerWrapper(PlatformConfig cfg)
33 OCStackResult result = OCInit(cfg.ipAddress.c_str(), cfg.port, OC_SERVER);
35 if(OC_STACK_OK != result)
37 throw InitializeException("Error Initializing Stack", result);
41 m_processThread = std::thread(&InProcServerWrapper::processFunc, this);
44 void InProcServerWrapper::processFunc()
50 std::lock_guard<std::mutex> lock(m_csdkLock);
54 if(result != OC_STACK_OK)
56 cout << "Something wrong in OCProcess" << endl;
60 std::this_thread::yield();
61 // To minimize CPU utilization we may wish to do this with sleep
62 //std::this_thread::sleep_for(std::chrono::milliseconds(1));
66 void InProcServerWrapper::registerResource(const std::string& resourceURI,
67 const std::string& resourceTypeName,
68 named_property_binding_vector properties)
70 using OC::OCReflect::property_type;
71 using OC::OCReflect::named_property_binding;
72 using namespace OC::OCReflect::to_OCStack;
74 std::vector<std::string> reps { convert(properties) };
76 for(const auto& r : reps)
77 std::cout << r << '\n';
79 char *resourTypeRepresentation = flatten(reps);
81 std::cout << resourTypeRepresentation << "\n";
83 OCResourceHandle resourceHandle;
86 std::lock_guard<std::mutex> lock(m_csdkLock);
88 cout << "Creating a resource" << endl;
92 result = OCCreateResource(&resourceHandle, // OCResourceHandle *handl
93 resourceTypeName.c_str(), // const char * resourceTypeName
94 resourTypeRepresentation, //const char * resourceTypeRepresentation
95 "core.rw", //const char * resourceInterfaceName
96 OC_REST_GET | OC_REST_PUT, // uint8_t allowedMethods
97 resourceURI.c_str(), // const char * uri
98 entityHandler, // OCEntityHandler entityHandler
99 OC_DISCOVERABLE | OC_OBSERVABLE // uint8_t resourceProperties
102 if(result != OC_STACK_OK)
104 cout << "Something wrong in OCCreateResource" << endl;
109 cout << "Resource creation is successful" << endl;
116 InProcServerWrapper::~InProcServerWrapper()
118 if(m_processThread.joinable())
121 m_processThread.join();