X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fresource-container%2Fexamples%2FContainerSample.cpp;h=420119acf8eb5e05aeac3f542f4b97be5f98c7d8;hb=390866079e285d2c74918432c0d597d5da52f8a0;hp=e2236299abe6be2edfd354376a1360d29d2ae226;hpb=3e9402ad71cb3e93266a77796f44d17bab9853fd;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/resource-container/examples/ContainerSample.cpp b/service/resource-container/examples/ContainerSample.cpp index e223629..420119a 100644 --- a/service/resource-container/examples/ContainerSample.cpp +++ b/service/resource-container/examples/ContainerSample.cpp @@ -20,31 +20,57 @@ #if defined(__linux__) #include +#include #endif #include "RCSResourceContainer.h" #include "RCSBundleInfo.h" -#include "oc_logger.hpp" + #include +#include +#include +#include +#include -using namespace std; using namespace OIC::Service; -using OC::oc_log_stream; - -#define MAX_PATH 2048 -/* Another way to create a context: */ -auto info_logger = []() -> boost::iostreams::stream & +namespace { - static OC::oc_log_stream ols(oc_make_ostream_logger); - static boost::iostreams::stream os(ols); - return os; + typedef enum + { + START_RC = 1, STOP_RC, + ADD_SAMPLE_BUNDLE, START_SAMPLE_BUNDLE, + STOP_SAMPLE_BUNDLE, REMOVE_SAMPLE_BUNDLE, + ADD_SAMPLE_RESOURCE, REMOVE_SAMPLE_RESOURCE, + LIST_BUNDLES, LIST_RESOURCES, + EXIT = 11 + } APPMenu; + + struct CloseApp {}; + + const int MAX_PATH = 2048; + + const std::string EXAMPLE_CONFIG_PATH = "/examples/ResourceContainerConfig.xml"; + const std::string EXAMPLE_BUNDLE_ID = "oic.bundle.hueSample"; + const std::string EXAMPLE_BUNDLE_URI = "/hueSample"; + const std::string EXAMPLE_BUNDLE_PATH = "libHueBundle.so"; + const std::string EXAMPLE_BUNDLE_ACTIVATOR = "huesample"; + const std::string EXAMPLE_RESOURCE_URI = "/hue/light/sample"; + const std::string EXAMPLE_RESOURCE_TYPE = "oic.r.light"; + const std::string EXAMPLE_ADDRESS = "http://192.168.0.2/api/newdeveloper/lights/1"; }; +bool g_bContainerStarted = false; +bool g_bSampleBundleStarted = false; +RCSResourceContainer *g_pResourceContainer = nullptr; + void getCurrentPath(std::string *pPath) { char buffer[MAX_PATH]; + if (!pPath->empty()) + pPath->clear(); + #if defined(__linux__) char *strPath = NULL; int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1); @@ -61,129 +87,356 @@ void getCurrentPath(std::string *pPath) pPath->append(buffer); } -int main() +int processUserInput(int min, int max) { - info_logger()->set_module("ContainerTest"); - info_logger()->set_level(OC_LOG_INFO); + int input; - info_logger() << "Starting container test." << std::flush; + std::cin >> input; - std::string strConfigPath; - getCurrentPath(&strConfigPath); - strConfigPath.append("/examples/ResourceContainerConfig.xml"); + if (!std::cin.fail() && min <= input && input <= max) + return input; - RCSResourceContainer *container = RCSResourceContainer::getInstance(); - container->startContainer(strConfigPath); + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + throw std::runtime_error("Invalid Input, please try again"); +} - /*so bundle add test*/ - cout << "++++++++++++++++++++++++++" << endl; - cout << "+++ Test for SO Bundle +++" << endl; - cout << "++++++++++++++++++++++++++" << endl; - cout << "Press enter to add SO bundle " << endl; - getchar(); - std::map bundleParams; - container->addBundle("oic.bundle.hueSample", "", "libHueBundle.so", "huesample", bundleParams); +std::string processUserStringInput() +{ + std::string input; + + std::cin >> input; + + return input; +} - std::list> bundles = container->listBundles(); - std::list>::iterator bundleIt; +void displayMenu() +{ + std::cout << "================================================================" << std::endl; + std::cout << " IoTivity Resource Container Test Application " << std::endl; + std::cout << "================================================================" << std::endl; + std::cout << " 1. Start Resource Container " << std::endl; + std::cout << " 2. Stop Resource Container " << std::endl; + std::cout << " 3. Add Sample Bundle " << std::endl; + std::cout << " 4. Start Sample Bundle " << std::endl; + std::cout << " 5. Stop Sample Bundle " << std::endl; + std::cout << " 6. Remove Sample Bundle " << std::endl; + std::cout << " 7. Add Sample Resource " << std::endl; + std::cout << " 8. Remove Sample Resource " << std::endl; + std::cout << " 9. List Bundles " << std::endl; + std::cout << " 10. List Resources " << std::endl; + std::cout << " 11. Exit " << std::endl; + std::cout << "================================================================" << std::endl; + std::cout << " Please Enter the NO: "; +} - cout << "\t>>> bundle list size : " << bundles.size() << endl; - for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) +bool checkBundleRegistered(std::string bundleId) +{ + std::list< std::unique_ptr< RCSBundleInfo > > bundleList; + + if (g_bContainerStarted) { - cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; + bundleList = g_pResourceContainer->listBundles(); + + for (auto &bundle : bundleList) + { + if (bundle->getID().compare(bundleId) == 0) + return true; + } } - cout << "\nPress enter to start SO bundle " << endl; - getchar(); - container->startBundle("oic.bundle.hueSample"); + std::cout << "Bundle \'" << bundleId << "\' is not registered." << std::endl; + return false; +} - std::map resourceParams; - cout << "Press enter to add SO bundle resource " << endl; - getchar(); - resourceParams["resourceType"] = "oic.r.light"; - resourceParams["address"] = "http://192.168.0.2/api/newdeveloper/lights/1"; - container->addResourceConfig("oic.bundle.hueSample", "", resourceParams); - container->addResourceConfig("oic.bundle.hueSample", "", resourceParams); +bool checkResourceRegistered(std::string bundleId, std::string resourceUri) +{ + std::list< std::string > resourceList; - std::list resources = container->listBundleResources("oic.bundle.hueSample"); - std::list::iterator resourceIt; - cout << "\t>>> resource list size : " << resources.size() << endl; - for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++) + if (g_bContainerStarted && checkBundleRegistered(bundleId)) { - cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl; + resourceList = g_pResourceContainer->listBundleResources(bundleId); + + for (auto &resource : resourceList) + { + if (resource.compare(resourceUri) == 0) + return true; + } } - cout << "\nPress enter to remove SO bundle resource " << endl; - getchar(); - container->removeResourceConfig("oic.bundle.hueSample", "/hue/light/1"); + std::cout << "Resource \'" << resourceUri << "\' is not registered." << std::endl; + return false; +} - resources = container->listBundleResources("oic.bundle.hueSample"); - cout << "\t>>> resource list size : " << resources.size() << endl; - for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++) +void StartContainer(std::string configPath) +{ + if (!g_bContainerStarted) + { + g_pResourceContainer->startContainer(configPath); + g_bContainerStarted = true; + std::cout << "Container started." << std::endl; + } + else { - cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl; + std::cout << "Container is already started." << std::endl; } +} - cout << "\nPress enter to stop SO Bundle " << endl; - getchar(); - container->stopBundle("oic.bundle.hueSample"); +void StopContainer() +{ + if (g_pResourceContainer && g_bContainerStarted) + { + g_pResourceContainer->stopContainer(); + g_bContainerStarted = false; + } + else + { + std::cout << "Container is not started." << std::endl; + } +} - cout << "Press enter to test remove SO Bundle " << endl; - getchar(); - container->removeBundle("oic.bundle.hueSample"); +void AddSampleBundle() +{ + std::map< std::string, std::string > bundleParams; - bundles = container->listBundles(); - cout << "\t>>> bundle list size : " << bundles.size() << endl; - for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) + if (g_pResourceContainer && g_bContainerStarted) { - cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; + g_pResourceContainer->addBundle(EXAMPLE_BUNDLE_ID, EXAMPLE_BUNDLE_URI, + EXAMPLE_BUNDLE_PATH, EXAMPLE_BUNDLE_ACTIVATOR, + bundleParams); } + else + { + std::cout << "Container is not started." << std::endl; + } +} -#if(JAVA_SUPPORT) - /*java bundle add test*/ - cout << "\n++++++++++++++++++++++++++++" << endl; - cout << "+++ Test for JAVA Bundle +++" << endl; - cout << "++++++++++++++++++++++++++++" << endl; - cout << "Press enter to add java bundle " << endl; - getchar(); - bundleParams["libraryPath"] = "."; - std::string activator = "org.iotivity.bundle.hue.HueBundleActivator"; - container->addBundle("oic.bundle.hueJavaSample2", "/hueJava", - "../../../../../../service/resource-container/" \ - "examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar", - activator, - bundleParams); +void StartSampleBundle() +{ + if (g_pResourceContainer && g_bContainerStarted) + { + if (checkBundleRegistered(EXAMPLE_BUNDLE_ID)) + { + g_pResourceContainer->startBundle(EXAMPLE_BUNDLE_ID); + g_bSampleBundleStarted = true; + } + } + else + { + std::cout << "Container is not started." << std::endl; + } +} - bundles = container->listBundles(); - cout << "\t>>> bundle list size : " << bundles.size() << endl; - for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) +void StopSampleBundle() +{ + if (g_pResourceContainer && g_bContainerStarted) { - cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; + if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted) + { + g_pResourceContainer->stopBundle(EXAMPLE_BUNDLE_ID); + g_bSampleBundleStarted = false; + } + else + std::cout << "Sample Bundle is not started." << std::endl; } + else + { + std::cout << "Container is not started." << std::endl; + } +} - cout << "\nPress enter to start java bundle " << endl; - getchar(); - container->startBundle("oic.bundle.hueJavaSample2"); +void RemoveSampleBundle() +{ + if (g_pResourceContainer && g_bContainerStarted) + { + if (checkBundleRegistered(EXAMPLE_BUNDLE_ID)) + g_pResourceContainer->removeBundle(EXAMPLE_BUNDLE_ID); + } + else + { + std::cout << "Container is not started." << std::endl; + } +} - cout << "Press enter to stop java Bundle " << endl; - getchar(); - container->stopBundle("oic.bundle.hueJavaSample2"); +void AddSampleBundleResource() +{ + std::map< std::string, std::string > resourceParams; - cout << "Press enter to test remove java Bundle " << endl; - getchar(); - container->removeBundle("oic.bundle.hueJavaSample2"); + if (g_pResourceContainer && g_bContainerStarted) + { + if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted) + { + resourceParams.insert(std::make_pair("resourceType", EXAMPLE_RESOURCE_TYPE)); + resourceParams.insert(std::make_pair("address", EXAMPLE_ADDRESS)); + + g_pResourceContainer->addResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI, + resourceParams); + } + else + std::cout << "Sample Bundle is not started." << std::endl; + } + else + { + std::cout << "Container is not started." << std::endl; + } +} - bundles = container->listBundles(); - cout << "\t>>> bundle list size : " << bundles.size() << endl; - for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) +void RemoveSampleBundleResource() +{ + if (g_pResourceContainer && g_bContainerStarted) { - cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; + if (checkResourceRegistered(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI) + && g_bSampleBundleStarted) + g_pResourceContainer->removeResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI); + else + std::cout << "Sample Bundle is not started." << std::endl; } -#endif + else + { + std::cout << "Container is not started." << std::endl; + } +} + +void printBundleList(std::list< std::unique_ptr< RCSBundleInfo > > &list) +{ + std::cout << std::endl; + for (auto &bundleinfo : list) + { + std::cout << "-- " << bundleinfo->getID() << std::endl; + } + std::cout << std::endl; +} + +void ListBundles() +{ + std::list< std::unique_ptr< RCSBundleInfo > > bundles; + + if (g_pResourceContainer && g_bContainerStarted) + { + bundles = g_pResourceContainer->listBundles(); + printBundleList(bundles); + } + else + { + std::cout << "Container is not started." << std::endl; + } +} + +void printResourceList(std::list< std::string > &list) +{ + std::cout << std::endl; + for (auto &bundleResource : list) + { + std::cout << "-- " << bundleResource << std::endl; + } + std::cout << std::endl; +} + +void ListResources(std::string bundleId) +{ + std::list< std::string > resources; + + if (g_pResourceContainer && g_bContainerStarted) + { + if (checkBundleRegistered(bundleId)) + { + resources = g_pResourceContainer->listBundleResources(bundleId); + printResourceList(resources); + } + } + else + { + std::cout << "Container is not started." << std::endl; + } +} + +void ExecuteCommand(APPMenu menu) +{ + switch (menu) + { + case APPMenu::START_RC: + { + std::string filePath; + std::cout << "Type Configuration File Path (Press \'0\' to start with example): "; + + if ((filePath = processUserStringInput()).compare("0") == 0) + { + getCurrentPath(&filePath); + filePath.append(EXAMPLE_CONFIG_PATH); + } + + StartContainer(filePath); + } + break; + case APPMenu::STOP_RC: + StopContainer(); + break; + case APPMenu::ADD_SAMPLE_BUNDLE: + AddSampleBundle(); + break; + case APPMenu::START_SAMPLE_BUNDLE: + StartSampleBundle(); + break; + case APPMenu::STOP_SAMPLE_BUNDLE: + StopSampleBundle(); + break; + case APPMenu::REMOVE_SAMPLE_BUNDLE: + RemoveSampleBundle(); + break; + case APPMenu::ADD_SAMPLE_RESOURCE: + AddSampleBundleResource(); + break; + case APPMenu::REMOVE_SAMPLE_RESOURCE: + RemoveSampleBundleResource(); + break; + case APPMenu::LIST_BUNDLES: + ListBundles(); + break; + case APPMenu::LIST_RESOURCES: + { + std::string bundleId; + std::cout << + "Type Bundle Id to get Resource List (Press \'0\' to get example resource list): "; + + if ((bundleId = processUserStringInput()).compare("0") == 0) + { + bundleId = EXAMPLE_BUNDLE_ID; + } + + ListResources(bundleId); + } + break; + case APPMenu::EXIT: + throw CloseApp(); + break; + } +} + +int main() +{ + g_pResourceContainer = RCSResourceContainer::getInstance(); + + while (true) + { + try + { + displayMenu(); + ExecuteCommand((APPMenu)processUserInput(APPMenu::START_RC, APPMenu::EXIT)); + } + catch (const std::exception &e) + { + std::cout << e.what() << std::endl; + } + catch (const CloseApp &) + { + break; + } + } + + if (g_bContainerStarted) + g_pResourceContainer->stopContainer(); - cout << "\nPress enter to stop container " << endl; - getchar(); - container->stopContainer(); + g_pResourceContainer = nullptr; - cout << "Container stopped. Bye" << endl; + return 0; }