From 791aeeb7273d1d2aca8e4e416c2e428d56d136a9 Mon Sep 17 00:00:00 2001 From: coderhyme Date: Thu, 25 Feb 2016 01:48:32 -0800 Subject: [PATCH] Fixed Svace issues in NestedAttributes samples. Some ugly codes are also fixed. Change-Id: I057e6e8967019277fb54c2eab588a47c0ab09aea Signed-off-by: coderhyme Reviewed-on: https://gerrit.iotivity.org/gerrit/5201 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../examples/linux/NestedAttributesClient.cpp | 165 +++++++----------- .../examples/linux/NestedAttributesServer.cpp | 190 ++++++++------------- 2 files changed, 135 insertions(+), 220 deletions(-) diff --git a/service/resource-encapsulation/examples/linux/NestedAttributesClient.cpp b/service/resource-encapsulation/examples/linux/NestedAttributesClient.cpp index 73a417a..5c764f7 100644 --- a/service/resource-encapsulation/examples/linux/NestedAttributesClient.cpp +++ b/service/resource-encapsulation/examples/linux/NestedAttributesClient.cpp @@ -18,9 +18,10 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include -#include "mutex" -#include "condition_variable" +#include + +#include +#include #include "RCSDiscoveryManager.h" #include "RCSRemoteResourceObject.h" @@ -29,8 +30,6 @@ #include "OCPlatform.h" -#define nestedAtrribute std::vector> - using namespace OC; using namespace OIC::Service; @@ -38,37 +37,6 @@ constexpr int CORRECT_INPUT = 1; constexpr int INCORRECT_INPUT = 2; constexpr int QUIT_INPUT = 3; -std::shared_ptr resource; - -const std::string defaultKey = "deviceInfo"; -const std::string resourceType = "core.ac"; -const std::string relativetUri = OC_RSRVD_WELL_KNOWN_URI; - -RCSResourceAttributes model; -RCSResourceAttributes speed; -RCSResourceAttributes airCirculation; -RCSResourceAttributes temperature; -RCSResourceAttributes humidity; -RCSResourceAttributes power; -RCSResourceAttributes capacity; -RCSResourceAttributes weight; -RCSResourceAttributes dimensions; -RCSResourceAttributes red; -RCSResourceAttributes green; - -std::vector generalInfo; -std::vector fan; -std::vector tempSensor; -std::vector efficiency; -std::vector light; - - -std::mutex mtx; -std::condition_variable cond; - -void getAttributeFromRemoteServer(); -void setAttributeToRemoteServer(); - enum Menu { GET_ATTRIBUTE = 1, @@ -87,6 +55,9 @@ struct ClientMenu ReturnValue m_result; }; +void getAttributeFromRemoteServer(); +void setAttributeToRemoteServer(); + ClientMenu clientMenu[] = { {Menu::GET_ATTRIBUTE, getAttributeFromRemoteServer, CORRECT_INPUT}, @@ -95,6 +66,14 @@ ClientMenu clientMenu[] = {Menu::END_OF_MENU, nullptr, INCORRECT_INPUT} }; +const std::string defaultKey = "deviceInfo"; +const std::string resourceType = "core.ac"; + +std::mutex mtx; +std::condition_variable cond; + +RCSRemoteResourceObject::Ptr g_resource; + void displayMenu() { std::cout << std::endl; @@ -102,17 +81,14 @@ void displayMenu() std::cout << "2 :: Set Attribute" << std::endl; } -void onResourceDiscovered(std::shared_ptr foundResource) +void onResourceDiscovered(RCSRemoteResourceObject::Ptr foundResource) { std::cout << "onResourceDiscovered callback" << std::endl; - std::string resourceURI = foundResource->getUri(); - std::string hostAddress = foundResource->getAddress(); - - std::cout << "\t\tResource URI : " << resourceURI << std::endl; - std::cout << "\t\tResource Host : " << hostAddress << std::endl; + std::cout << "\t\tResource URI : " << foundResource->getUri() << std::endl; + std::cout << "\t\tResource Host : " << foundResource->getAddress() << std::endl; - resource = foundResource; + g_resource = foundResource; cond.notify_all(); } @@ -133,83 +109,76 @@ void onRemoteAttributesReceivedCallback(const RCSResourceAttributes &attributes, << attr.value().toString() << std::endl; std::cout << "=============================================\n" << std::endl; - OIC::Service::RCSResourceAttributes::Value attrValue = attr.value(); - std::vector< std::vector> attrVector = - attrValue.get>>(); + const auto& doubleVector = attr.value(). + get< std::vector< std::vector< RCSResourceAttributes > > >(); - for (auto itr = attrVector.begin(); itr != attrVector.end(); ++itr) + for (const auto& vector : doubleVector) { - std::vector attrKeyVector = *itr; - for (auto itrKey = attrKeyVector.begin(); itrKey != attrKeyVector.end(); ++itrKey) + for (const auto& attrs : vector) { - for (const auto & attribute : *itrKey) + for (const auto & kvPair : attrs) { - std::cout << "\t" << attribute.key() << " : " << attribute.value().toString() << std::endl; + std::cout << "\t" << kvPair.key() << " : " << + kvPair.value().toString() << std::endl; } } std::cout << std::endl; } + std::cout << "=============================================\n" << std::endl; } - std::cout << "=============================================\n" << std::endl; displayMenu(); } -nestedAtrribute createNestedAttribute(int speedValue, int aircValue) +std::vector< std::vector< RCSResourceAttributes > > createNestedAttribute(int speedValue, + int aircValue) { - nestedAtrribute *acServer = new nestedAtrribute(); + RCSResourceAttributes model; + RCSResourceAttributes weight; + RCSResourceAttributes dimensions; model["model"] = "SamsungAC"; + weight["weight"] = 3; + dimensions["dimensions"] = "10x25x35"; + + RCSResourceAttributes speed; + RCSResourceAttributes airCirculation; speed["speed"] = speedValue; airCirculation["air"] = aircValue; + RCSResourceAttributes temperature; + RCSResourceAttributes humidity; + temperature["temp"] = 30; humidity["humidity"] = 30; + RCSResourceAttributes power; + RCSResourceAttributes capacity; + power["power"] = 1600; capacity["capacity"] = 1; - weight["weight"] = 3; - dimensions["dimensions"] = "10x25x35"; + RCSResourceAttributes red; + RCSResourceAttributes green; red["red"] = 50; green["green"] = 60; - generalInfo.clear(); - generalInfo.push_back(model); - generalInfo.push_back(weight); - generalInfo.push_back(dimensions); - - fan.clear(); - fan.push_back(speed); - fan.push_back(airCirculation); + std::vector< RCSResourceAttributes > generalInfo{ model, weight, dimensions }; + std::vector< RCSResourceAttributes > fan{ speed, airCirculation } ; + std::vector< RCSResourceAttributes > tempSensor{ temperature, humidity } ; + std::vector< RCSResourceAttributes > efficiency{ power, capacity }; + std::vector< RCSResourceAttributes > light{ red, green }; - tempSensor.clear(); - tempSensor.push_back(temperature); - tempSensor.push_back(humidity); + std::vector< std::vector< RCSResourceAttributes > > acServer; - efficiency.clear(); - efficiency.push_back(power); - efficiency.push_back(capacity); + acServer.push_back(generalInfo); + acServer.push_back(fan); + acServer.push_back(tempSensor); + acServer.push_back(efficiency); + acServer.push_back(light); - light.clear(); - light.push_back(red); - light.push_back(green); - - if (nullptr == acServer) - { - std::cout << "Null nestedAtrribute" << std::endl; - } - else - { - acServer->push_back(generalInfo); - acServer->push_back(fan); - acServer->push_back(tempSensor); - acServer->push_back(efficiency); - acServer->push_back(light); - } - - return *acServer; + return acServer; } int processUserInput() @@ -227,17 +196,11 @@ int processUserInput() void getAttributeFromRemoteServer() { - if(nullptr == resource) - return; - - resource->getRemoteAttributes(&onRemoteAttributesReceivedCallback); + g_resource->getRemoteAttributes(&onRemoteAttributesReceivedCallback); } void setAttributeToRemoteServer() { - if(nullptr == resource) - return; - int speed, airc; std::cout << "\tEnter the Fan Speed you want to set : "; @@ -245,12 +208,10 @@ void setAttributeToRemoteServer() std::cout << "\tEnter the Air circulation value you want to set :"; std::cin >> airc; - nestedAtrribute nestedAttr = createNestedAttribute(speed, airc); - RCSResourceAttributes setAttribute; - setAttribute[defaultKey] = nestedAttr; + setAttribute[defaultKey] = createNestedAttribute(speed, airc); - resource->setRemoteAttributes(setAttribute, + g_resource->setRemoteAttributes(setAttribute, &onRemoteAttributesReceivedCallback); } @@ -297,7 +258,7 @@ bool discoverResource() try { RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), - relativetUri, resourceType, &onResourceDiscovered); + resourceType, &onResourceDiscovered); } catch(const RCSPlatformException& e) { @@ -306,7 +267,7 @@ bool discoverResource() std::unique_lock lck(mtx); cond.wait_for(lck, std::chrono::seconds(2)); - return resource != nullptr; + return g_resource != nullptr; } int main() @@ -328,7 +289,7 @@ int main() std::cout << "main exception : " << e.what() << std::endl; } - std::cout << "Stopping the Client" << std::endl; + std::cout << "Stopping the client" << std::endl; return 0; } diff --git a/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp b/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp index b972be1..fc579e7 100644 --- a/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp +++ b/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp @@ -18,14 +18,10 @@ * ******************************************************************/ -#include "RCSResourceObject.h" -#include "OCPlatform.h" -#include "OCApi.h" +#include -#define nestedAtrribute std::vector> +#include "RCSResourceObject.h" -using namespace OC; -using namespace OC::OCPlatform; using namespace OIC::Service; constexpr int DEFAULT_SPEED = 30; @@ -37,38 +33,20 @@ constexpr int CUSTOM_SERVER = 2; constexpr int STOP = 3; constexpr int PRINT_ATTRIBUTES = 1; -constexpr int INCREASE_SPEEDATTRIBUTE = 2; -constexpr int DECREASE_SPEEDATTRIBUTE = 3; +constexpr int INCREASE_SPEED_ATTRIBUTE = 2; +constexpr int DECREASE_SPEED_ATTRIBUTE = 3; constexpr int STOP_SENSOR = 4; constexpr int CORRECT_INPUT = 1; constexpr int INCORRECT_INPUT = 2; constexpr int QUIT = 3; -std::string resourceUri = "/a/airConditioner"; -std::string resourceType = "core.ac"; -std::string resourceInterface = "oic.if."; -std::string attributeKey = "deviceInfo"; - -RCSResourceAttributes model; -RCSResourceAttributes speed; -RCSResourceAttributes airCirculation; -RCSResourceAttributes temperature; -RCSResourceAttributes humidity; -RCSResourceAttributes power; -RCSResourceAttributes capacity; -RCSResourceAttributes weight; -RCSResourceAttributes dimensions; -RCSResourceAttributes red; -RCSResourceAttributes green; - -std::vector generalInfo; -std::vector fan; -std::vector tempSensor; -std::vector efficiency; -std::vector light; - -RCSResourceObject::Ptr server; +const std::string resourceUri = "/a/airConditioner"; +const std::string resourceType = "core.ac"; +const std::string resourceInterface = "oic.if.baseline"; +const std::string attributeKey = "deviceInfo"; + +RCSResourceObject::Ptr g_server; void displayMenu() { @@ -92,62 +70,55 @@ void displayControlMenu() std::cout << "========================================================" << std::endl; } -nestedAtrribute createNestedAttribute(int speedValue) +std::vector< std::vector< RCSResourceAttributes > > createNestedAttribute(int speedValue) { - nestedAtrribute *acServer = new nestedAtrribute(); + RCSResourceAttributes model; + RCSResourceAttributes weight; + RCSResourceAttributes dimensions; model["model"] = "SamsungAC"; + weight["weight"] = 3; + dimensions["dimensions"] = "10x25x35"; + + RCSResourceAttributes speed; + RCSResourceAttributes airCirculation; speed["speed"] = speedValue; airCirculation["air"] = 425; + RCSResourceAttributes temperature; + RCSResourceAttributes humidity; + temperature["temp"] = 30; humidity["humidity"] = 30; + RCSResourceAttributes power; + RCSResourceAttributes capacity; + power["power"] = 1600; capacity["capacity"] = 1; - weight["weight"] = 3; - dimensions["dimensions"] = "10x25x35"; + RCSResourceAttributes red; + RCSResourceAttributes green; red["red"] = 50; green["green"] = 60; - generalInfo.clear(); - generalInfo.push_back(model); - generalInfo.push_back(weight); - generalInfo.push_back(dimensions); - - fan.clear(); - fan.push_back(speed); - fan.push_back(airCirculation); - - tempSensor.clear(); - tempSensor.push_back(temperature); - tempSensor.push_back(humidity); - - efficiency.clear(); - efficiency.push_back(power); - efficiency.push_back(capacity); + std::vector< RCSResourceAttributes > generalInfo{ model, weight, dimensions }; + std::vector< RCSResourceAttributes > fan{ speed, airCirculation }; + std::vector< RCSResourceAttributes > tempSensor{ temperature, humidity }; + std::vector< RCSResourceAttributes > efficiency{ power, capacity }; + std::vector< RCSResourceAttributes > light{ red, green }; - light.clear(); - light.push_back(red); - light.push_back(green); + std::vector< std::vector< RCSResourceAttributes > > acServer; - if (nullptr == acServer) - { - std::cout << "Null nestedAtrribute" << std::endl; - } - else - { - acServer->push_back(generalInfo); - acServer->push_back(fan); - acServer->push_back(tempSensor); - acServer->push_back(efficiency); - acServer->push_back(light); - } + acServer.push_back(generalInfo); + acServer.push_back(fan); + acServer.push_back(tempSensor); + acServer.push_back(efficiency); + acServer.push_back(light); - return *acServer; + return acServer; } void printAttribute(const RCSResourceAttributes &attrs) @@ -158,18 +129,17 @@ void printAttribute(const RCSResourceAttributes &attrs) << attr.value().toString() << std::endl; std::cout << "=============================================\n" << std::endl; - OIC::Service::RCSResourceAttributes::Value attrValue = attr.value(); - std::vector< std::vector> attrVector = - attrValue.get>>(); + const auto& doubleVector = attr.value(). + get< std::vector< std::vector< RCSResourceAttributes > > >(); - for (auto itr = attrVector.begin(); itr != attrVector.end(); ++itr) + for (const auto& vector : doubleVector) { - std::vector attrKeyVector = *itr; - for (auto itrKey = attrKeyVector.begin(); itrKey != attrKeyVector.end(); ++itrKey) + for (const auto& attrs : vector) { - for (const auto & attribute : *itrKey) + for (const auto & kvPair : attrs) { - std::cout << "\t" << attribute.key() << " : " << attribute.value().toString() << std::endl; + std::cout << "\t" << kvPair.key() << " : " << + kvPair.value().toString() << std::endl; } } std::cout << std::endl; @@ -180,8 +150,8 @@ void printAttribute(const RCSResourceAttributes &attrs) void printNestedAttribute() { - RCSResourceObject::LockGuard lock(*server); - RCSResourceAttributes attributes = server->getAttributes(); + RCSResourceObject::LockGuard lock(*g_server); + const auto& attributes = g_server->getAttributes(); std::cout << "\nPrinting nested attributes" << std::endl; printAttribute(attributes); @@ -190,21 +160,21 @@ void printNestedAttribute() void changeSpeedAttribute(int state) { - nestedAtrribute attr; + std::vector< std::vector< RCSResourceAttributes > > attr; - if (INCREASE_SPEEDATTRIBUTE == state) + if (INCREASE_SPEED_ATTRIBUTE == state) { std::cout << "Increasing speed attribute to : " << UP_SPEED << std::endl; attr = createNestedAttribute(UP_SPEED); } - else if (DECREASE_SPEEDATTRIBUTE == state) + else if (DECREASE_SPEED_ATTRIBUTE == state) { std::cout << "Decreasing speed attribute to : " << DOWN_SPEED << std::endl; attr = createNestedAttribute(DOWN_SPEED); } - RCSResourceObject::LockGuard lock(*server); - server->getAttributes()[attributeKey] = attr; + RCSResourceObject::LockGuard lock(*g_server); + g_server->getAttributes()[attributeKey] = attr; printNestedAttribute(); } @@ -214,11 +184,9 @@ RCSGetResponse requestHandlerForGet(const RCSRequest& /*request*/, { std::cout << "Recieved a Get request from Client" << std::endl; - RCSResourceObject::LockGuard lock(*server); - RCSResourceAttributes attributes = server->getAttributes(); - - std::cout << "\nSending response to Client : " << std::endl; - printAttribute(attributes); + RCSResourceObject::LockGuard lock(*g_server); + std::cout << "Sending response to Client : " << std::endl; + printAttribute(g_server->getAttributes()); return RCSGetResponse::defaultAction(); } @@ -229,40 +197,28 @@ RCSSetResponse requestHandlerForSet(const RCSRequest& /*request*/, { std::cout << "Recieved a Set request from Client" << std::endl; - std::cout << "\n\nSending response to Client : " << std::endl; - RCSResourceObject::LockGuard lock(*server); + std::cout << "Requested attributes : " << std::endl; printAttribute(attrs); return RCSSetResponse::defaultAction(); } -void createResource() -{ - server = RCSResourceObject::Builder(resourceUri, resourceType, - resourceInterface).setDiscoverable(true).setObservable(true).build(); -} - void initServer() { try { - createResource(); + g_server = RCSResourceObject::Builder(resourceUri, resourceType, + resourceInterface).build(); } catch (const RCSPlatformException &e) { std::cout << "Exception in initServer : " << e.what() << std::endl; + return; } - if (nullptr == server) - { - std::cout << "Null server resource" << std::endl; - return; - } - - server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); - server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER); + g_server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); + g_server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER); - nestedAtrribute attr = createNestedAttribute(DEFAULT_SPEED); - server->setAttribute(attributeKey, attr); + g_server->setAttribute(attributeKey, createNestedAttribute(DEFAULT_SPEED)); } int processUserInput() @@ -286,12 +242,12 @@ int selectControlMenu() printNestedAttribute(); return CORRECT_INPUT; - case INCREASE_SPEEDATTRIBUTE: - changeSpeedAttribute(INCREASE_SPEEDATTRIBUTE); + case INCREASE_SPEED_ATTRIBUTE: + changeSpeedAttribute(INCREASE_SPEED_ATTRIBUTE); return CORRECT_INPUT; - case DECREASE_SPEEDATTRIBUTE: - changeSpeedAttribute(DECREASE_SPEEDATTRIBUTE); + case DECREASE_SPEED_ATTRIBUTE: + changeSpeedAttribute(DECREASE_SPEED_ATTRIBUTE); return CORRECT_INPUT; case STOP_SENSOR: @@ -307,7 +263,8 @@ int selectServerMenu() { switch (processUserInput()) { - case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client. + case DEFALUT_SERVER: + // Creation of Resource & Auto control for all requests from Client. initServer(); return CORRECT_INPUT; @@ -316,8 +273,8 @@ int selectServerMenu() // set request from client in application. initServer(); - server->setGetRequestHandler(requestHandlerForGet); - server->setSetRequestHandler(requestHandlerForSet); + g_server->setGetRequestHandler(requestHandlerForGet); + g_server->setSetRequestHandler(requestHandlerForSet); return CORRECT_INPUT; case STOP : return QUIT; @@ -350,17 +307,14 @@ void process() int main(void) { - startPresence(3); - try { process(); - server = NULL; } catch (const std::exception &e) { std::cout << "main exception : " << e.what() << std::endl; } - std::cout << "Stopping the Server" << std::endl; + std::cout << "Stopping the server" << std::endl; } -- 2.7.4