From c18686e7ec541dba62f1d783afe3f4664771844b Mon Sep 17 00:00:00 2001 From: coderhyme Date: Sun, 18 Oct 2015 18:45:01 -0700 Subject: [PATCH] Refactoring resource-encapsulation linux examples Duplicated settings in sconscript for each sample are removed. And NestedAttributes samples are renamed. Change-Id: Id0d675155cc85e737bd534c7922b80227a8ff6d6 Signed-off-by: coderhyme Reviewed-on: https://gerrit.iotivity.org/gerrit/3905 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- ...ributeClient.cpp => NestedAttributesClient.cpp} | 0 ...ributeServer.cpp => NestedAttributesServer.cpp} | 1 - .../examples/linux/SConscript | 93 ++-- .../examples/linux/SampleResourceClient.cpp | 479 +++++++++------------ .../examples/linux/SampleResourceServer.cpp | 383 ++++++---------- 5 files changed, 371 insertions(+), 585 deletions(-) rename service/resource-encapsulation/examples/linux/{NestedAttributeClient.cpp => NestedAttributesClient.cpp} (100%) rename service/resource-encapsulation/examples/linux/{NestedAttributeServer.cpp => NestedAttributesServer.cpp} (99%) diff --git a/service/resource-encapsulation/examples/linux/NestedAttributeClient.cpp b/service/resource-encapsulation/examples/linux/NestedAttributesClient.cpp similarity index 100% rename from service/resource-encapsulation/examples/linux/NestedAttributeClient.cpp rename to service/resource-encapsulation/examples/linux/NestedAttributesClient.cpp diff --git a/service/resource-encapsulation/examples/linux/NestedAttributeServer.cpp b/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp similarity index 99% rename from service/resource-encapsulation/examples/linux/NestedAttributeServer.cpp rename to service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp index fa970f6..b972be1 100644 --- a/service/resource-encapsulation/examples/linux/NestedAttributeServer.cpp +++ b/service/resource-encapsulation/examples/linux/NestedAttributesServer.cpp @@ -18,7 +18,6 @@ * ******************************************************************/ -#include "PrimitiveResource.h" #include "RCSResourceObject.h" #include "OCPlatform.h" #include "OCApi.h" diff --git a/service/resource-encapsulation/examples/linux/SConscript b/service/resource-encapsulation/examples/linux/SConscript index e80012d..12fa444 100644 --- a/service/resource-encapsulation/examples/linux/SConscript +++ b/service/resource-encapsulation/examples/linux/SConscript @@ -27,77 +27,46 @@ Import('env') lib_env = env.Clone() SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') -ResourceClient_env = lib_env.Clone() -ResourceServer_env = lib_env.Clone() -NestedAttributeServer_env = lib_env.Clone() -NestedAttributeClient_env = lib_env.Clone() +sample_env = lib_env.Clone() -###################################################################### -# ##### Resource Client ##### -###################################################################### - -ResourceClient_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) -ResourceClient_env.AppendUnique(LIBS = ['rcs_client', 'rcs_common','oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', 'pthread']) -ResourceClient_env.AppendUnique(CPPPATH = ['../../include']) -ResourceClient_env.AppendUnique(CPPPATH = ['../../src/resourceBroker/include']) -ResourceClient_env.AppendUnique(CPPPATH = ['../../src/resourceCache/include']) -ResourceClient_env.AppendUnique(CPPPATH = ['../../src/common/primitiveResource/include']) +sample_env.AppendUnique(CXXFLAGS = ['-Wall', '-std=c++0x']) +sample_env.AppendUnique(LIBS = [ + 'rcs_common', + 'oc', + 'octbstack', + 'oc_logger', + 'connectivity_abstraction', + 'coap', + 'pthread' + ]) +sample_env.AppendUnique(CPPPATH = ['../../include']) -###################################################################### -# ##### Nested Attribute Client ##### -###################################################################### +if env.get('SECURED') == '1': + sample_env.AppendUnique(LIBS = ['tinydtls']) -NestedAttributeClient_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) -NestedAttributeClient_env.AppendUnique(LIBS = ['rcs_client', 'rcs_common','oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', 'pthread']) -NestedAttributeClient_env.AppendUnique(CPPPATH = ['../../include']) -NestedAttributeClient_env.AppendUnique(CPPPATH = ['../../src/resourceBroker/include']) -NestedAttributeClient_env.AppendUnique(CPPPATH = ['../../src/resourceCache/include']) -NestedAttributeClient_env.AppendUnique(CPPPATH = ['../../src/common/primitiveResource/include']) +if 'rt' in sample_env.get('LIBS'): + sample_env.Append(LIBS = ['rt']) ###################################################################### -# ##### Resource Server ##### +# ##### Client ##### ###################################################################### +client_env = sample_env.Clone() +client_env.AppendUnique(LIBS = 'rcs_client') -ResourceServer_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) -ResourceServer_env.AppendUnique(LIBS = ['rcs_server', 'rcs_common','oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', 'pthread']) -ResourceServer_env.AppendUnique(CPPPATH = ['../../include']) -ResourceServer_env.AppendUnique(CPPPATH = ['../../src/common/primitiveResource/include']) -ResourceServer_env.AppendUnique(CPPPATH = ['../../src/serverBuilder/include']) +sampleResourceClient = client_env.Program('sampleResourceClient', 'SampleResourceClient.cpp') +nestedAttributesClient = client_env.Program('nestedAttributesClient', 'NestedAttributesClient.cpp') + +client_env.InstallTarget(sampleResourceClient, 'sampleResourceClient') +client_env.InstallTarget(nestedAttributesClient, 'nestedAttributesClient') ###################################################################### -# ##### Nested Attribute Server ##### +# ##### Server ##### ###################################################################### +server_env = sample_env.Clone() +server_env.AppendUnique(LIBS = 'rcs_server') -NestedAttributeServer_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) -NestedAttributeServer_env.AppendUnique(LIBS = ['rcs_server', 'rcs_common','oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', 'pthread']) -NestedAttributeServer_env.AppendUnique(CPPPATH = ['../../include']) -NestedAttributeServer_env.AppendUnique(CPPPATH = ['../../src/common/primitiveResource/include']) -NestedAttributeServer_env.AppendUnique(CPPPATH = ['../../src/serverBuilder/include']) - -if env.get('SECURED') == '1': - ResourceClient_env.AppendUnique(LIBS = ['tinydtls']) - NestedAttributeClient_env.AppendUnique(LIBS = ['tinydtls']) - ResourceServer_env.AppendUnique(LIBS = ['tinydtls']) - NestedAttributeServer_env.AppendUnique(LIBS = ['tinydtls']) - -if 'rt' in ResourceClient_env.get('LIBS'): - ResourceClient_env.Append(LIBS = ['rt']) -if 'rt' in NestedAttributeClient_env.get('LIBS'): - NestedAttributeClient_env.Append(LIBS = ['rt']) -if 'rt' in ResourceServer_env.get('LIBS'): - ResourceServer_env.Append(LIBS = ['rt']) -if 'rt' in NestedAttributeServer_env.get('LIBS'): - NestedAttributeServer_env.Append(LIBS = ['rt']) - -#################################################################### -# Source files and Targets -#################################################################### -sampleResourceClient = ResourceClient_env.Program('sampleResourceClient', 'SampleResourceClient.cpp') -nestedAttributeClient = NestedAttributeClient_env.Program('nestedAttributeClient', 'NestedAttributeClient.cpp') -sampleResourceServer = ResourceServer_env.Program('sampleResourceServer', 'SampleResourceServer.cpp') -nestedAttributeServer = NestedAttributeServer_env.Program('nestedAttributeServer', 'NestedAttributeServer.cpp') +sampleResourceServer = server_env.Program('sampleResourceServer', 'SampleResourceServer.cpp') +nestedAttributesServer = server_env.Program('nestedAttributesServer', 'NestedAttributesServer.cpp') -ResourceClient_env.InstallTarget(sampleResourceClient, 'sampleResourceClient') -NestedAttributeClient_env.InstallTarget(nestedAttributeClient, 'nestedAttributeClient') -ResourceServer_env.InstallTarget(sampleResourceServer, 'sampleResourceServer') -NestedAttributeServer_env.InstallTarget(nestedAttributeServer, 'nestedAttributeServer') +server_env.InstallTarget(sampleResourceServer, 'sampleResourceServer') +server_env.InstallTarget(nestedAttributesServer, 'nestedAttributesServer') diff --git a/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp b/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp index 98d7076..61287a0 100755 --- a/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp +++ b/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp @@ -27,156 +27,137 @@ #include "OCPlatform.h" +#define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC } + using namespace OC; using namespace OIC::Service; -#define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC, __VA_ARGS__ } - -void startMonitoring(); -void startMonitoring(); -void stopMonitoring(); -void getAttributeFromRemoteServer(); -void setAttributeToRemoteServer(); -void startCachingWithoutCallback(); -void startCachingWithCallback(); -void getResourceCacheState(); -void getCachedAttributes(); -void getCachedAttribute(); -void stopCaching(); -void discoverResource(); -void cancelDiscovery(); -int processUserInput(); -void selectResource(); -void printAttributes(const RCSResourceAttributes& attributes); - -class MenuList; - -class MenuItem +struct CloseApp {}; + +struct MenuItem { private: - typedef void(*MenuHandler)(); + typedef void(*Handler)(); public: - MenuItem(const char* funcName, MenuHandler handler = nullptr, - MenuHandler optionHandler = nullptr, MenuList* state = nullptr) - : m_name(funcName), m_handler(handler), m_optionHandler(optionHandler), - m_nextState(state) - { - } + const std::string title; + const Handler handler; +}; - MenuList* command() const - { - std::cout << m_name << " start.." << std::endl; - if(m_handler) m_handler(); +typedef void(*Runner)(); - if(m_optionHandler != nullptr) m_optionHandler(); +constexpr int RESOURCE_TEMP = 1; +constexpr int RESOURCE_LIGHT = 2; - return m_nextState; - } +const std::string RESOURCE_TYPE_TEMP = "oic.r.temperaturesensor"; +const std::string RESOURCE_TYPE_LIGHT = "oic.r.light"; - const char* getTitle() const - { - return m_name.c_str(); - } +RCSRemoteResourceObject::Ptr g_selectedResource; +std::vector g_discoveredResources; -private: - const std::string m_name; - const MenuHandler m_handler; - const MenuHandler m_optionHandler; - MenuList* const m_nextState ; -}; +std::string g_attrKey; -class MenuList +Runner g_currentRun; + +std::ostream& operator<<(std::ostream& os, const RCSRemoteResourceObject::Ptr& object) { -public: - MenuList(std::initializer_list il) - :m_menuItemList(std::move(il)) - { - } + return os << "\turi : " << object->getUri() << std::endl << + "\thost address : " << object->getAddress(); +} - void display() const - { - std::cout << std::endl; - int menuNum = 1; +std::ostream& operator<<(std::ostream& os, const MenuItem& item) +{ + return os << item.title; +} - for(const auto& menuItem : m_menuItemList) - { - std::cout.width(2); - std::cout << std::right << menuNum++ << ". "; - std::cout << menuItem.getTitle() << std::endl; - } - } +void onSelected(const RCSRemoteResourceObject::Ptr& object) +{ + g_selectedResource = object; +} - MenuList* select() - { - int input = processUserInput(); +void onSelected(const MenuItem& item) +{ + std::cout << item.title << " start.." << std::endl; + item.handler(); +} - if(input == static_cast(m_menuItemList.size())) return nullptr; +int processUserInput(int min = std::numeric_limits::min(), + int max = std::numeric_limits::max()) +{ + assert(min <= max); - if(input > static_cast(m_menuItemList.size()) || input <= 0) - { - std::cout << "Invalid input, please try again" << std::endl; - return this; - } - auto nextMenuList = m_menuItemList[input-1].command(); + int input; - return nextMenuList == nullptr ? this : nextMenuList; - } + std::cin >> input; + std::cin.ignore(std::numeric_limits::max(), '\n'); -private: - const std::vector m_menuItemList; -}; + if (!std::cin.fail() && min <= input && input <= max) return input; -constexpr int REQUEST_TEMP = 1; -constexpr int REQUEST_LIGHT = 2; + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); -const std::string TEMP = "oic.r.temperaturesensor"; -const std::string LIGHT = "oic.r.light"; + throw std::runtime_error("Invalid Input, please try again"); +} -std::unique_ptr discoveryTask; +template +void displayItem(int width, int index, const D& data) +{ + std::cout.width(width); + std::cout << std::right << index << ". "; + std::cout << data << std::endl; +} -std::vector resourceList; -RCSRemoteResourceObject::Ptr selectedResource; +template +void displayItems(const std::vector& items) +{ + std::cout << std::endl; -std::string defaultKey; + const auto width = (items.size() + 1) / 10 + 1; -MenuList* currentMenuList; + for(size_t i = 0; i < items.size(); ++i) + { + displayItem(width, i + 1, items[i]); + } + displayItem(width, items.size() + 1, "quit"); +} -MenuList resourceMenu = { - DECLARE_MENU(startMonitoring), - DECLARE_MENU(stopMonitoring), - DECLARE_MENU(getAttributeFromRemoteServer), - DECLARE_MENU(setAttributeToRemoteServer), - DECLARE_MENU(startCachingWithoutCallback), - DECLARE_MENU(startCachingWithCallback), - DECLARE_MENU(getResourceCacheState), - DECLARE_MENU(getCachedAttributes), - DECLARE_MENU(getCachedAttribute), - DECLARE_MENU(stopCaching), - { "quit" } -}; +template +void selectItem(const std::vector& items) +{ + int selected = processUserInput(1, items.size() + 1) - 1; -MenuList cancelDiscoveryMenu = { - DECLARE_MENU(cancelDiscovery, selectResource, &resourceMenu), - { "quit" } -}; + if(selected == static_cast(items.size())) throw CloseApp(); -MenuList discoveryMenu = { - DECLARE_MENU(discoverResource, nullptr, &cancelDiscoveryMenu), - { "quit" } -}; + onSelected(items[selected]); +} + +template +void handleItems(const std::vector& items) +{ + displayItems(items); + selectItem(items); +} -void onResourceDiscovered(std::shared_ptr discoveredResource) +void printAttribute(const std::string& key, const RCSResourceAttributes::Value& value) { - std::cout << "onResourceDiscovered callback :: " << std::endl; + std::cout << "\tkey : " << key << std::endl + << "\tvalue : " << value.toString() << std::endl; +} - std::cout << "resourceURI : " << discoveredResource->getUri() << std::endl; - std::cout << "hostAddress : " << discoveredResource->getAddress() << std::endl; +void printAttributes(const RCSResourceAttributes& attributes) +{ + if (attributes.empty()) + { + std::cout << "\tattributes is empty" << std::endl; + } - resourceList.push_back(discoveredResource); + for(const auto& attr : attributes) + { + printAttribute(attr.key(), attr.value()); + } } -void onResourceStateChanged(const ResourceState& resourceState) +void onResourceStateChanged(ResourceState resourceState) { std::cout << "onResourceStateChanged callback" << std::endl; @@ -220,60 +201,57 @@ void onRemoteAttributesReceived(const RCSResourceAttributes& attributes, int) void startMonitoring() { - if (!selectedResource->isMonitoring()) - { - selectedResource->startMonitoring(&onResourceStateChanged); - std::cout << "\tHosting Started..." << std::endl; - } - else + if (g_selectedResource->isMonitoring()) { std::cout << "\tAlready Started..." << std::endl; + return; } + + g_selectedResource->startMonitoring(&onResourceStateChanged); + std::cout << "\tMonitoring Started..." << std::endl; } void stopMonitoring() { - if (selectedResource->isMonitoring()) - { - selectedResource->stopMonitoring(); - std::cout << "\tHosting stopped..." << std::endl; - } - else + if (!g_selectedResource->isMonitoring()) { - std::cout << "\tHosting not started..." << std::endl; + std::cout << "\tMonitoring not started..." << std::endl; + return; } + + g_selectedResource->stopMonitoring(); + std::cout << "\tMonitoring stopped..." << std::endl; } -void getAttributeFromRemoteServer() +void getRemoteAttributes() { - selectedResource->getRemoteAttributes(&onRemoteAttributesReceived); + g_selectedResource->getRemoteAttributes(onRemoteAttributesReceived); } -void setAttributeToRemoteServer() +void setRemoteAttributes() { std::string key; - RCSResourceAttributes setAttribute; std::cout << "\tEnter the Key you want to set : "; std::cin >> key; + std::cout << "\tEnter the value(INT) you want to set :"; + RCSResourceAttributes attrs; + attrs[key] = processUserInput(); - setAttribute[key] = processUserInput(); - selectedResource->setRemoteAttributes(setAttribute, - &onRemoteAttributesReceived); + g_selectedResource->setRemoteAttributes(attrs, onRemoteAttributesReceived); } void startCaching(RCSRemoteResourceObject::CacheUpdatedCallback cb) { - if (!selectedResource->isCaching()) - { - selectedResource->startCaching(std::move(cb)); - std::cout << "\tCaching Started..." << std::endl; - } - else + if (g_selectedResource->isCaching()) { std::cout << "\tAlready Started Caching..." << std::endl; + return; } + + g_selectedResource->startCaching(std::move(cb)); + std::cout << "\tCaching Started..." << std::endl; } void startCachingWithoutCallback() @@ -288,7 +266,7 @@ void startCachingWithCallback() void getResourceCacheState() { - switch(selectedResource->getCacheState()) + switch(g_selectedResource->getCacheState()) { case CacheState::READY: std::cout << "\tCurrent Cache State : CACHE_STATE::READY" << std::endl; @@ -310,102 +288,50 @@ void getResourceCacheState() void getCachedAttributes() { - try - { - printAttributes(selectedResource->getCachedAttributes()); - } - catch (const RCSBadRequestException& e) - { - std::cout << "Exception in getCachedAttributes : " << e.what() << std::endl; - } + printAttributes(g_selectedResource->getCachedAttributes()); } void getCachedAttribute() { - try - { - std::cout << "\tkey : " << defaultKey << std::endl - << "\tvalue : " << selectedResource->getCachedAttribute(defaultKey).get< int >() - << std::endl; - } - catch (const RCSBadRequestException& e) - { - std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl; - } - catch (const RCSBadGetException& e) - { - std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl; - } + printAttribute(g_attrKey, g_selectedResource->getCachedAttribute(g_attrKey)); } void stopCaching() { - if(selectedResource->isCaching()) - { - selectedResource->stopCaching(); - std::cout << "\tCaching stopped..." << std::endl; - } - else + if(!g_selectedResource->isCaching()) { std::cout << "\tCaching not started..." << std::endl; + return; } -} -void platFormConfigure() -{ - PlatformConfig config - { - OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos - }; - OCPlatform::Configure(config); -} - -int processUserInput() -{ - int userInput; - - while(true) - { - std::cin >> userInput; - if (std::cin.fail()) - { - std::cin.clear(); - std::cin.ignore(std::numeric_limits::max(), '\n'); - std::cout << "Invalid Input, please try again" << std::endl; - } - else break; - } - return userInput; + g_selectedResource->stopCaching(); + std::cout << "\tCaching stopped..." << std::endl; } -bool selectResourceType(std::string& resourceType) +std::string selectResourceType() { std::cout << "========================================================" << std::endl; std::cout << "1. Temperature Resource Discovery" << std::endl; std::cout << "2. Light Resource Discovery" << std::endl; std::cout << "========================================================" << std::endl; - switch (processUserInput()) + switch (processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT)) { - case REQUEST_TEMP: - resourceType = TEMP; - defaultKey = "Temperature"; - return true; - case REQUEST_LIGHT: - resourceType = LIGHT; - defaultKey = "Brightness"; - return true; - default : - std::cout << "Invalid input, please try again" << std::endl; - return false; + case RESOURCE_TEMP: + g_attrKey = "Temperature"; + return RESOURCE_TYPE_TEMP; + case RESOURCE_LIGHT: + g_attrKey = "Brightness"; + return RESOURCE_TYPE_LIGHT; } + + throw std::logic_error("unreachable"); } RCSAddress inputAddress() { std::cout << "========================================================" << std::endl; - std::cout << "Please input address" << std::endl; - std::cout << "(default is multicast. when you want to use unicast, input address" << std::endl; + std::cout << "Please input address (empty for multicast)" << std::endl; std::cout << "========================================================" << std::endl; std::string address; @@ -415,96 +341,109 @@ RCSAddress inputAddress() return address.empty() ? RCSAddress::multicast() : RCSAddress::unicast(address); } -void discoverResource() +void printDiscoveryInProgress() { - std::string resourceType = ""; - - while(!selectResourceType(resourceType)) {} - - while(!discoveryTask) - { - try - { - discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( - inputAddress(), resourceType, &onResourceDiscovered); - } - catch (const RCSPlatformException& e) - { - std::cout << e.what() << std::endl; - } - } + std::cout << "Discovery in progress, press '1' to stop." << std::endl; } -void displayResourceList() +void discoverResource() { - int cnt = 1; - for(const auto& resource : resourceList) + auto onResourceDiscovered = []( + const RCSRemoteResourceObject::Ptr& discoveredResource) { - std::cout << cnt++ << ".\tresourceURI : " << resource->getUri() << std::endl << - "\thostAddress : " << resource->getAddress() << std::endl; - } -} + std::cout << "onResourceDiscovered callback :: " << std::endl; -void selectResource() -{ - displayResourceList(); + std::cout << "luri : " << discoveredResource->getUri() << std::endl; + std::cout << "host address : " << discoveredResource->getAddress() << std::endl; + + g_discoveredResources.push_back(discoveredResource); + + printDiscoveryInProgress(); + }; + + auto resourceType = selectResourceType(); + auto address = inputAddress(); - std::cout << "select Resource..." << std::endl; + printDiscoveryInProgress(); - selectedResource = resourceList[processUserInput()-1]; + auto discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(address, + resourceType, onResourceDiscovered); - resourceList.clear(); + while(processUserInput() != 1); + + discoveryTask->cancel(); } -void cancelDiscovery() +void runResourceControl() { - if(!discoveryTask) - { - std::cout << "There is no discovery request..." << std::endl; - return; - } - discoveryTask->cancel(); + static std::vector resourceMenuItems { + DECLARE_MENU(startMonitoring), + DECLARE_MENU(stopMonitoring), + DECLARE_MENU(getRemoteAttributes), + DECLARE_MENU(setRemoteAttributes), + DECLARE_MENU(startCachingWithoutCallback), + DECLARE_MENU(startCachingWithCallback), + DECLARE_MENU(getResourceCacheState), + DECLARE_MENU(getCachedAttributes), + DECLARE_MENU(getCachedAttribute), + DECLARE_MENU(stopCaching), + }; + + handleItems(resourceMenuItems); } -void printAttribute(const std::string key, const RCSResourceAttributes::Value& value) +void runResourceSelection() { - std::cout << "\tkey : " << key << std::endl - << "\tvalue : " << value.toString() << std::endl; + handleItems(g_discoveredResources); + + g_currentRun = runResourceControl; } -void printAttributes(const RCSResourceAttributes& attributes) +void runDiscovery() { - if (attributes.empty()) - { - std::cout << "\tattributes is empty" << std::endl; - } - for(const auto& attr : attributes) + static std::vector discoveryMenuItems { + DECLARE_MENU(discoverResource), + }; + + handleItems(discoveryMenuItems); + + if (g_discoveredResources.empty()) throw std::runtime_error("No resource found!"); + + g_currentRun = runResourceSelection; +} + +void configurePlatform() +{ + PlatformConfig config { - printAttribute(attr.key(), attr.value()); - } + OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos + }; + OCPlatform::Configure(config); } int main() { - platFormConfigure(); + configurePlatform(); - currentMenuList = &discoveryMenu; + g_currentRun = runDiscovery; - try + while (true) { - while(currentMenuList) + try { - currentMenuList->display(); - - currentMenuList = currentMenuList->select(); + g_currentRun(); + } + catch (const std::exception& e) + { + std::cout << e.what() << std::endl; + } + catch (const CloseApp&) + { + break; } - } - catch (const std::exception& e) - { - 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/SampleResourceServer.cpp b/service/resource-encapsulation/examples/linux/SampleResourceServer.cpp index 0a6ec51..7c4532a 100755 --- a/service/resource-encapsulation/examples/linux/SampleResourceServer.cpp +++ b/service/resource-encapsulation/examples/linux/SampleResourceServer.cpp @@ -18,95 +18,71 @@ * ******************************************************************/ -#include "PrimitiveResource.h" #include "RCSResourceObject.h" #include "OCPlatform.h" -#include "OCApi.h" -using namespace OC; using namespace OC::OCPlatform; using namespace OIC::Service; -constexpr int DEFALUT_VALUE = 0; +struct CloseApp{}; -constexpr int REQUEST_TEMP = 1; -constexpr int REQUEST_LIGHT = 2; - -constexpr int PRESENCE_ON = 1; -constexpr int PRESENCE_OFF = 2; +constexpr int RESOURCE_TEMP = 1; +constexpr int RESOURCE_LIGHT = 2; constexpr int DEFALUT_SERVER = 1; constexpr int CUSTOM_SERVER = 2; -constexpr int STOP = 3; -constexpr int INCREASE_TEMPERATURE = 1; -constexpr int DECREASE_TEMPERATURE = 2; -constexpr int STOP_TEMPERATURE_SENSOR = 3; +constexpr int INCREASE = 1; +constexpr int DECREASE = 2; -constexpr int INCREASE_BRIGHTNESS = 1; -constexpr int DECREASE_BRIGHTNESS = 2; -constexpr int STOP_LIGHT_SENSOR = 3; +typedef void (*DisplayControlMenuFunc)(); +typedef std::function Run; -constexpr int CORRECT_INPUT = 1; -constexpr int INCORRECT_INPUT = 2; -constexpr int QUIT = 3; +Run g_currentRun; +bool g_isPresenceStarted = false; -std::string resourceType = "oic.r.temperaturesensor"; -std::string resourceInterface = "oic.if."; -std::string resourceUri; -std::string attributeKey; -int isPresenceOn = PRESENCE_ON; +RCSResourceObject::Ptr g_resource; -RCSResourceObject::Ptr server; +int processUserInput(int min, int max) +{ + assert(min <= max); -int processUserInput(); + int input; -enum class Control{ - INCREASE, - DECREASE -}; + std::cin >> input; -void displayMenu() -{ - std::cout << "=====================================================================" - << std::endl; - std::cout << " 1 - Creation of Resource [Auto control for requests]" << std::endl; - std::cout << " 2 - Creation of Resource [Developer control for Get and Set requests]" - << std::endl; - std::cout << " 3 - Quit" << std::endl; - std::cout << "=====================================================================" - << std::endl; -} + if (!std::cin.fail()) + { + if(input == max + 1) throw CloseApp(); + if(min <= input && input <= max) return input; + } -void displayResourceTypeMenu() -{ - std::cout << "========================================================" << std::endl; - std::cout << "Select Resource Type" << std::endl; - std::cout << "1. Temperature" << std::endl; - std::cout << "2. Light" << std::endl; - std::cout << "========================================================" << std::endl; + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + + throw std::runtime_error("Invalid Input, please try again"); } void displayControlTemperatureMenu() { - std::cout << "========================================================" << std::endl; - std::cout << "1. Increase Temperature by 1 degree" << std::endl; - std::cout << "2. Decrease Temperature by 1 degree" << std::endl; - std::cout << "3. Stop the Sensor" << std::endl; - std::cout << "========================================================" << std::endl; + std::cout << "========================================================\n"; + std::cout << INCREASE << ". Increase Temperature by 1 degree \n"; + std::cout << DECREASE << ". Decrease Temperature by 1 degree \n"; + std::cout << DECREASE + 1 << ". Quit \n"; + std::cout << "========================================================\n"; } void displayControlLightMenu() { - std::cout << "========================================================" << std::endl; - std::cout << "1. Increase Brightness by 1 stage" << std::endl; - std::cout << "2. Decrease Brightness by 1 stage" << std::endl; - std::cout << "3. Stop the Sensor" << std::endl; - std::cout << "========================================================" << std::endl; + std::cout << "========================================================\n"; + std::cout << INCREASE << ". Increase Brightness by 1 stage \n"; + std::cout << DECREASE << ". Decrease Brightness by 1 stage \n"; + std::cout << DECREASE + 1 << ". Quit \n"; + std::cout << "========================================================\n"; } -void printAttribute(const RCSResourceAttributes& attrs) +void printAttributes(const RCSResourceAttributes& attrs) { for(const auto& attr : attrs) { @@ -115,261 +91,164 @@ void printAttribute(const RCSResourceAttributes& attrs) } } -//hander for get request (if developer choose second option for resource Creation) -RCSGetResponse requestHandlerForGet(const RCSRequest& /*request*/, - RCSResourceAttributes& /*attrs*/) +RCSGetResponse requestHandlerForGet(const RCSRequest&, RCSResourceAttributes& attrs) { std::cout << "Received a Get request from Client" << std::endl; - RCSResourceObject::LockGuard lock(*server); - RCSResourceAttributes attributes = server->getAttributes(); + printAttributes(attrs); - std::cout << "\nSending response to Client : " << std::endl; - printAttribute(attributes); + { + RCSResourceObject::LockGuard lock(g_resource); + std::cout << "\nSending response to Client : " << std::endl; + printAttributes(g_resource->getAttributes()); + } return RCSGetResponse::defaultAction(); } -//hander for set request (if developer choose second option for resource Creation) -RCSSetResponse requestHandlerForSet(const RCSRequest& /*request*/, - RCSResourceAttributes& attrs) +RCSSetResponse requestHandlerForSet(const RCSRequest&, RCSResourceAttributes& attrs) { std::cout << "Received a Set request from Client" << std::endl; + printAttributes(attrs); - std::cout << "\n\nSending response to Client : " << std::endl; - RCSResourceObject::LockGuard lock(*server); - printAttribute(attrs); return RCSSetResponse::defaultAction(); } -void createResource() +void initServer(const std::string& resourceUri, const std::string& resourceType, + const std::string& attrKey) { - server = RCSResourceObject::Builder(resourceUri, resourceType,resourceInterface). + g_resource = RCSResourceObject::Builder(resourceUri, resourceType, "oic.if."). setDiscoverable(true).setObservable(true).build(); + + g_resource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); + g_resource->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER); + g_resource->setAttribute(attrKey, 0); } -void initServer() +void updateAttribute(const std::string& attrKey, int control) { - try - { - createResource(); - } - catch (const RCSPlatformException& e) + const int diff = control == INCREASE ? 1 : - 1; + { - std::cout << "Exception in initServer : " << e.what() << std::endl; + RCSResourceObject::LockGuard lock(g_resource); + auto& attrs = g_resource->getAttributes(); + attrs[attrKey] = attrs[attrKey].get() + diff; } - server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED); - server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER); - server->setAttribute(attributeKey, DEFALUT_VALUE); -} - -void changeAttribute(Control control) -{ - RCSResourceObject::LockGuard lock(server); - if(Control::INCREASE == control) + if(control == INCREASE) { - server->getAttributes()[attributeKey] = - server->getAttribute(attributeKey) + 1; - std::cout << attributeKey << " increased." << std::endl; + std::cout << attrKey << " increased." << std::endl; } - else if(Control::DECREASE == control) + else { - server->getAttributes()[attributeKey] = - server->getAttribute(attributeKey) - 1; - std::cout << attributeKey << " Decreased." << std::endl; + std::cout << attrKey << " decreased." << std::endl; } - std::cout << "\nCurrent " << attributeKey << ": " - << server->getAttributeValue(attributeKey).get() << std::endl; + std::cout << "\nCurrent " << attrKey << ": " + << g_resource->getAttributeValue(attrKey).get() << std::endl; } -int processUserInput() +void runResourceControl(DisplayControlMenuFunc displayMenuFunc, const std::string& attrKey) { - int userInput; - std::cin >> userInput; - if (std::cin.fail()) - { - std::cin.clear(); - std::cin.ignore(std::numeric_limits::max(), '\n'); - return -1; - } - return userInput; + displayMenuFunc(); + updateAttribute(attrKey, processUserInput(INCREASE, DECREASE)); } -int selectPresenceMenu() +void runResourceTypeSelection(int resourceMode) { - std::cout << "========================================================" << std::endl; - std::cout << "1. Presence On" << std::endl; - std::cout << "2. Presence Off" << std::endl; - std::cout << "========================================================" << std::endl; - - switch(processUserInput()) - { - case PRESENCE_ON: - isPresenceOn = PRESENCE_ON; - startPresence(3); - return CORRECT_INPUT; - case PRESENCE_OFF: - isPresenceOn = PRESENCE_OFF; - return CORRECT_INPUT; - default : - std::cout << "Invalid input, please try again" << std::endl; - return INCORRECT_INPUT; - } -} + std::cout << "========================================================\n"; + std::cout << "Select Resource Type \n"; + std::cout << RESOURCE_TEMP << ". Temperature \n"; + std::cout << RESOURCE_LIGHT << ". Light \n"; + std::cout << RESOURCE_LIGHT + 1 << ". Quit \n"; + std::cout << "========================================================\n"; -int selectResourceTypeMenu() -{ - displayResourceTypeMenu(); + int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT); + DisplayControlMenuFunc displayMenuFunc; + std::string attrKey; - switch(processUserInput()) + switch (resourceType) { - case REQUEST_TEMP: - resourceUri = "/a/TempSensor"; - resourceType = "oic.r.temperaturesensor"; - attributeKey = "Temperature"; - return CORRECT_INPUT; - case REQUEST_LIGHT: - resourceUri = "/a/light"; - resourceType = "oic.r.light"; - attributeKey = "Brightness"; - return CORRECT_INPUT; - default : - std::cout << "Invalid input, please try again" << std::endl; - return INCORRECT_INPUT; + case RESOURCE_TEMP: + attrKey = "Temperature"; + initServer("/a/TempSensor", "oic.r.temperaturesensor", attrKey); + + displayMenuFunc = displayControlTemperatureMenu; + break; + + case RESOURCE_LIGHT: + attrKey = "Brightness"; + initServer("/a/light", "oic.r.light", attrKey); + + displayMenuFunc = displayControlLightMenu; + break; } -} -int selectServerMenu() -{ - switch (processUserInput()) + if (resourceMode == CUSTOM_SERVER) { - case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client. - while(true) - { - int ret = selectResourceTypeMenu(); - if(ret == CORRECT_INPUT) break; - } - initServer(); - return CORRECT_INPUT; - - case CUSTOM_SERVER: - // Creation of Resource & setting get and set handler for handling get and - // set request from client in application. - while(true) - { - int ret = selectResourceTypeMenu(); - if(ret == CORRECT_INPUT) break; - } - initServer(); - - server->setGetRequestHandler(requestHandlerForGet); - server->setSetRequestHandler(requestHandlerForSet); - return CORRECT_INPUT; - case STOP : - return QUIT; - - default : - std::cout << "Invalid input, please try again" << std::endl; - return INCORRECT_INPUT; + g_resource->setGetRequestHandler(requestHandlerForGet); + g_resource->setSetRequestHandler(requestHandlerForSet); } + + g_currentRun = std::bind(runResourceControl, displayMenuFunc, std::move(attrKey)); } -int selectControlTemperatureMenu() +void runResourceModeSelection() { - displayControlTemperatureMenu(); - - switch (processUserInput()) - { - case INCREASE_TEMPERATURE: - changeAttribute(Control::INCREASE); - return CORRECT_INPUT; - - case DECREASE_TEMPERATURE: - changeAttribute(Control::DECREASE); - return CORRECT_INPUT; + std::cout << "======================================================== \n"; + std::cout << DEFALUT_SERVER << ". Creation of Simple Resource Without Handlers \n"; + std::cout << CUSTOM_SERVER << ". Creation of Resource With Set and Get Handlers \n"; + std::cout << CUSTOM_SERVER + 1 << ". Quit \n"; + std::cout << "======================================================== \n"; - case STOP_TEMPERATURE_SENSOR: - return QUIT; - - default: - std::cout << "Invalid input. Please try again." << std::endl; - return INCORRECT_INPUT; - } + g_currentRun = std::bind(runResourceTypeSelection, + processUserInput(DEFALUT_SERVER, CUSTOM_SERVER)); } -int selectControlLightMenu() +void runPresenceSelection() { - displayControlLightMenu(); - - switch (processUserInput()) - { - case INCREASE_BRIGHTNESS: - changeAttribute(Control::INCREASE); - return CORRECT_INPUT; + constexpr int PRESENCE_ON = 1; + constexpr int PRESENCE_OFF = 2; - case DECREASE_BRIGHTNESS: - changeAttribute(Control::DECREASE); - return CORRECT_INPUT; + std::cout << "========================================================\n"; + std::cout << PRESENCE_ON << ". Presence On \n"; + std::cout << PRESENCE_OFF << ". Presence Off \n"; + std::cout << PRESENCE_OFF + 1 << ". Quit \n"; + std::cout << "========================================================\n"; - case STOP_LIGHT_SENSOR: - return QUIT; - - default: - std::cout << "Invalid input. Please try again." << std::endl; - return INCORRECT_INPUT; - } -} - -void process() -{ - while(true) + if (processUserInput(PRESENCE_ON, PRESENCE_OFF) == PRESENCE_ON) { - int ret = selectPresenceMenu(); - if(ret == CORRECT_INPUT) break; + g_isPresenceStarted = true; + startPresence(3); } - while(true) - { - displayMenu(); - int ret = selectServerMenu(); + g_currentRun = runResourceModeSelection; +} - if(ret == QUIT) return; - if(ret == CORRECT_INPUT) break; - } +int main(void) +{ + g_currentRun = runPresenceSelection; while(true) { - if(resourceType == "oic.r.temperaturesensor") + try { - int ret = selectControlTemperatureMenu(); - if (ret == QUIT) return; - if (ret == INCORRECT_INPUT) continue; + g_currentRun(); } - else if(resourceType == "oic.r.light") + catch(const std::exception& e) { - int ret = selectControlLightMenu(); - if (ret == QUIT) return; - if (ret == INCORRECT_INPUT) continue; + std::cout << e.what() << std::endl; } - } -} - -int main(void) -{ - try - { - process(); - server = NULL; - - if(isPresenceOn == PRESENCE_ON) + catch(const CloseApp&) { - stopPresence(); + break; } } - catch (const std::exception& e) + std::cout << "Stopping the server" << std::endl; + + g_resource.reset(); + + if(g_isPresenceStarted) { - std::cout << "main exception : " << e.what() << std::endl; + stopPresence(); } - std::cout << "Stopping the Server" << std::endl; } -- 2.7.4