#if defined(__linux__)
#include <unistd.h>
+#include <string.h>
#endif
#include "RCSResourceContainer.h"
#include "RCSBundleInfo.h"
-#include "oc_logger.hpp"
+
#include <iostream>
+#include <functional>
+#include <limits>
+#include <stdexcept>
+#include <string>
-using namespace std;
using namespace OIC::Service;
-using OC::oc_log_stream;
-
-#define MAX_PATH 2048
+namespace
+{
+ 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);
pPath->append(buffer);
}
-int main()
+int processUserInput(int min, int max)
{
+ int input;
- cout << "Starting container test." << endl;
+ 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<std::streamsize>::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<string, string> bundleParams;
- container->addBundle("oic.bundle.hueSample", "", "libHueBundle.so", "huesample", bundleParams);
+std::string processUserStringInput()
+{
+ std::string input;
+
+ std::cin >> input;
+
+ return input;
+}
- std::list<unique_ptr<RCSBundleInfo>> bundles = container->listBundles();
- std::list<unique_ptr<RCSBundleInfo>>::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<string, string> 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<string> resources = container->listBundleResources("oic.bundle.hueSample");
- std::list<string>::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;
+ }
+ 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;
+ }
+}
+
+void AddSampleBundle()
+{
+ std::map< std::string, std::string > bundleParams;
+
+ if (g_pResourceContainer && g_bContainerStarted)
+ {
+ 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;
+ }
+}
+
+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;
+ }
+}
- cout << "Press enter to test remove SO Bundle " << endl;
- getchar();
- container->removeBundle("oic.bundle.hueSample");
+void StopSampleBundle()
+{
+ if (g_pResourceContainer && g_bContainerStarted)
+ {
+ 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;
+ }
+}
- bundles = container->listBundles();
- cout << "\t>>> bundle list size : " << bundles.size() << endl;
- for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++)
+void RemoveSampleBundle()
+{
+ if (g_pResourceContainer && g_bContainerStarted)
{
- cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl;
+ if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
+ g_pResourceContainer->removeBundle(EXAMPLE_BUNDLE_ID);
}
+ 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 AddSampleBundleResource()
+{
+ std::map< std::string, std::string > resourceParams;
- 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;
+ 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;
+ }
+}
- cout << "\nPress enter to start java bundle " << endl;
- getchar();
- container->startBundle("oic.bundle.hueJavaSample2");
+void RemoveSampleBundleResource()
+{
+ if (g_pResourceContainer && g_bContainerStarted)
+ {
+ 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;
+ }
+ else
+ {
+ std::cout << "Container is not started." << std::endl;
+ }
+}
- cout << "Press enter to stop java Bundle " << endl;
- getchar();
- container->stopBundle("oic.bundle.hueJavaSample2");
+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;
+}
- cout << "Press enter to test remove java Bundle " << endl;
- getchar();
- container->removeBundle("oic.bundle.hueJavaSample2");
+void ListBundles()
+{
+ std::list< std::unique_ptr< RCSBundleInfo > > bundles;
- 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;
+ bundles = g_pResourceContainer->listBundles();
+ printBundleList(bundles);
}
-#endif
+ else
+ {
+ std::cout << "Container is not started." << std::endl;
+ }
+}
- cout << "\nPress enter to stop container " << endl;
- getchar();
- container->stopContainer();
+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;
+ }
+}
- cout << "Container stopped. Bye" << 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();
+
+ g_pResourceContainer = nullptr;
+
+ return 0;
+}
\ No newline at end of file