// ChangeLightRepresentaion is an observation function, // which notifies any changes to the resource to stack // via notifyObservers void * ChangeLightRepresentation (void *param) { LightResource* lightPtr = (LightResource*) param; // This function continuously monitors for the changes while (1) { sleep (3); if (gObservation) { // If under observation if there are any changes to the light resource // we call notifyObservors // // For demostration we are changing the power value and notifying. lightPtr->m_power += 10; cout << "\nPower updated to : " << lightPtr->m_power << endl; cout << "Notifying observers with resource handle: " << lightPtr->getHandle() << endl; OCStackResult result = OC_STACK_OK; if(isListOfObservers) { std::shared_ptr<OCResourceResponse> resourceResponse = {std::make_shared<OCResourceResponse>()}; resourceResponse->setErrorCode(200); resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE); result = OCPlatform::notifyListOfObservers( lightPtr->getHandle(), lightPtr->m_interestedObservers, resourceResponse); } else { result = OCPlatform::notifyAllObservers(lightPtr->getHandle()); } if(OC_STACK_NO_OBSERVERS == result) { cout << "No More observers, stopping notifications" << endl; gObservation = 0; } } } return NULL; }
int main(int argc, char* argv[]) { // Create PlatformConfig object PlatformConfig cfg { OC::ServiceType::InProc, 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 }; OCPlatform::Configure(cfg); try { // Create the instance of the resource class // (in this case instance of class 'LightResource'). LightResource myLight; // Invoke createResource function of class light. myLight.createResource(); myLight.addType(std::string("core.brightlight")); myLight.addInterface(std::string("oc.mi.ll")); // 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); cv.wait(lock); } catch(OCException& e) { oclog() << "Exception in main: "<< e.what(); } // No explicit call to stop the platform. // When OCPlatform::destructor is invoked, internally we do platform cleanup return 0; }
void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest) { // This function handles slow response case LightResource* lightPtr = (LightResource*) param; // Induce a case for slow response by using sleep std::cout << "SLOW response" << std::endl; sleep (10); auto pResponse = std::make_shared<OC::OCResourceResponse>(); pResponse->setRequestHandle(pRequest->getRequestHandle()); pResponse->setResourceHandle(pRequest->getResourceHandle()); pResponse->setResourceRepresentation(lightPtr->get()); pResponse->setErrorCode(200); pResponse->setResponseResult(OC_EH_OK); // Set the slow response flag back to false isSlowResponse = false; OCPlatform::sendResponse(pResponse); return NULL; }
int main() { // Create PlatformConfig object PlatformConfig cfg { OC::ServiceType::InProc, 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 }; OCPlatform::Configure(cfg); try { using namespace OC::OCPlatform; // Time to Live is 30 seconds startPresence(30); // Invoke createResource function of class light. myLightResource.createResource(); std :: cout << "Creating first resource of type \"core.light\"" << std :: endl; std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n"; sleep(10); std :: cout << "\nCreating the second resource of type \"core.light\"" << std :: endl; sleep(1); myLightResource.createResource2(); std :: cout << "Stopping presence\n" << std :: endl; sleep(1); stopPresence(); std :: cout << "Restarting presence\n" << std :: endl; sleep(1); startPresence(30); std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl; sleep(1); myLightResource.createResource3(); std :: cout << "Creating two non-operational resources.\"\n" << std :: endl; sleep(1); createPresenceResources(); // 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); cv.wait(lock); } catch(OCException& e) { oclog() << "Exception in main: "<< e.what(); } // No explicit call to stop the platform. // When OCPlatform destructor is invoked, internally we do platform cleanup return 0; }
int main(int argc, char* argv[]) { PrintUsage(); OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink }; if (argc == 1) { isListOfObservers = false; isSecure = false; } else if (argc == 2) { int value = atoi(argv[1]); switch (value) { case 1: isListOfObservers = true; isSecure = false; break; case 2: isListOfObservers = false; isSecure = true; break; case 3: isListOfObservers = true; isSecure = true; break; case 4: isSlowResponse = true; break; default: break; } } else { return -1; } // Create PlatformConfig object PlatformConfig cfg { OC::ServiceType::InProc, 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, &ps }; OCPlatform::Configure(cfg); try { // Create the instance of the resource class // (in this case instance of class 'LightResource'). LightResource myLight; // Invoke createResource function of class light. myLight.createResource(); std::cout << "Created resource." << std::endl; myLight.addType(std::string("core.brightlight")); 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) { 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; }