X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fresource-encapsulation%2Fexamples%2Flinux%2FNestedAttributesServer.cpp;h=005e0a51b23f3c0c5f01fd86a37be7fa5c70214f;hb=8229635f6d207516ccbbdf23b13be164e0fc1787;hp=b972be139fb1acfa5f2d829c89591d6d55cc85d5;hpb=c18686e7ec541dba62f1d783afe3f4664771844b;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp b/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp index b972be1..005e0a5 100644 --- a/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp +++ b/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp @@ -18,57 +18,35 @@ * ******************************************************************/ -#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; constexpr int UP_SPEED = 50; constexpr int DOWN_SPEED = 10; -constexpr int DEFALUT_SERVER = 1; +constexpr int DEFAULT_SERVER = 1; 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,45 +197,33 @@ 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; - } + g_server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); + g_server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER); - server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); - server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER); - - nestedAtrribute attr = createNestedAttribute(DEFAULT_SPEED); - server->setAttribute(attributeKey, attr); + g_server->setAttribute(attributeKey, createNestedAttribute(DEFAULT_SPEED)); } int processUserInput() { - int userInput; + int userInput = 0; std::cin >> userInput; if (std::cin.fail()) { @@ -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 DEFAULT_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; @@ -336,31 +293,41 @@ void process() int ret = selectServerMenu(); - if (ret == QUIT) return; - if (ret == CORRECT_INPUT) break; + if (ret == QUIT) + { + return; + } + if (ret == CORRECT_INPUT) + { + break; + } } while (true) { displayControlMenu(); - if (selectControlMenu() == QUIT) return; + if (selectControlMenu() == QUIT) + { + return; + } } } int main(void) { - startPresence(3); - try { process(); - server = NULL; } catch (const std::exception &e) { std::cout << "main exception : " << e.what() << std::endl; } + catch (...) + { + std::cout << "main exception : unknown" << std::endl; + } - std::cout << "Stopping the Server" << std::endl; + std::cout << "Stopping the server" << std::endl; }