1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 #include "OCResource.h"
8 #include "InProcClientWrapper.h"
17 InProcClientWrapper::InProcClientWrapper(PlatformConfig cfg)
19 OCStackResult result = OCInit(cfg.ipAddress.c_str(), cfg.port, OC_CLIENT);
21 if(OC_STACK_OK != result)
23 throw InitializeException("Error Initializing Stack", result);
27 m_listeningThread = std::thread(&InProcClientWrapper::listeningFunc, this);
30 InProcClientWrapper::~InProcClientWrapper()
32 if(m_listeningThread.joinable())
35 m_listeningThread.join();
41 void InProcClientWrapper::listeningFunc()
47 std::lock_guard<std::mutex> lock(m_csdkLock);
51 if(result != OC_STACK_OK)
53 // TODO: @Erich do something with result if failed?
56 std::this_thread::yield();
57 // To minimize CPU utilization we may wish to do this with sleep
58 //std::this_thread::sleep_for(std::chrono::milliseconds(1));
64 std::string convertOCAddrToString(OCDevAddr* addr)
66 if(addr->size != 4) { return "NOT SUPPORTED ADDR;";}
68 if(addr->size == 4) // IPV4
70 std::ostringstream address;
71 address<<"coap://"<<addr->addr[0]<<"."<<addr->addr[1]<<"."<<addr->addr[2]<<"."<<addr->addr[3]<<"/";
75 // TODO: @Erich Convert the device address to a valid string!
80 OCStackApplicationResult listenCallback(void* ctx, OCClientResponse* clientResponse)
82 auto &callback =*(std::function <void(OCResource::Ptr)>*)ctx;
83 std::stringstream requestStream;
84 requestStream << clientResponse->resJSONPayload;
87 boost::property_tree::ptree root;
88 boost::property_tree::read_json(requestStream, root);
90 boost::property_tree::ptree payload = root.get_child("oc.payload", boost::property_tree::ptree());
93 for(auto payloadItr : payload)
97 std::string host = convertOCAddrToString(clientResponse->addr);
98 OCResource::Ptr resource = std::make_shared<OCResource>(host, payloadItr.second);
100 // Note: the call to detach allows the underlying thread to continue until completion
101 // and allows us to destroy the exec object.
102 // This is apparently NOT a memory leak, as the thread will apparently take care of itself.
103 // Additionally, the only parameter here is
104 // a shared ptr, so OCResource will be disposed of properly upon completion of the callback handler.
105 std::thread exec(callback,resource);
108 catch(ResourceInitException)
110 // TODO: Do we want to handle this somehow? Perhaps we need to log this?
114 delete clientResponse;
116 return OC_STACK_KEEP_TRANSACTION;
119 int InProcClientWrapper::ListenForResource(const std::string& serviceUrl, const std::string& resourceType, std::function<void (OCResource::Ptr)>& callback)
121 OCStackResult result;
123 OCCallbackData* cbdata = new OCCallbackData();
124 cbdata->context = (void*)(&callback);
125 cbdata->cb = &listenCallback;
127 std::lock_guard<std::mutex> lock(m_csdkLock);
129 result = OCDoResource(OC_REST_GET, resourceType.c_str(), nullptr, nullptr, OC_NON_CONFIRMABLE, cbdata);