1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #if defined(__linux__)
26 #include "RCSResourceContainer.h"
27 #include "RCSBundleInfo.h"
35 using namespace OIC::Service;
41 START_RC = 1, STOP_RC,
42 ADD_SAMPLE_BUNDLE, START_SAMPLE_BUNDLE,
43 STOP_SAMPLE_BUNDLE, REMOVE_SAMPLE_BUNDLE,
44 ADD_SAMPLE_RESOURCE, REMOVE_SAMPLE_RESOURCE,
45 LIST_BUNDLES, LIST_RESOURCES,
51 const int MAX_PATH = 2048;
53 const std::string EXAMPLE_CONFIG_PATH = "/examples/ResourceContainerConfig.xml";
54 const std::string EXAMPLE_BUNDLE_ID = "oic.bundle.hueSample";
55 const std::string EXAMPLE_BUNDLE_URI = "/hueSample";
56 const std::string EXAMPLE_BUNDLE_PATH = "libHueBundle.so";
57 const std::string EXAMPLE_BUNDLE_ACTIVATOR = "huesample";
58 const std::string EXAMPLE_RESOURCE_URI = "/hue/light/sample";
59 const std::string EXAMPLE_RESOURCE_TYPE = "oic.r.light";
60 const std::string EXAMPLE_ADDRESS = "http://192.168.0.2/api/newdeveloper/lights/1";
63 bool g_bContainerStarted = false;
64 bool g_bSampleBundleStarted = false;
65 RCSResourceContainer *g_pResourceContainer = nullptr;
67 void getCurrentPath(std::string *pPath)
69 char buffer[MAX_PATH];
74 #if defined(__linux__)
76 int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1);
80 buffer[length] = '\0';
81 strPath = strrchr(buffer, '/');
87 pPath->append(buffer);
90 int processUserInput(int min, int max)
96 if (!std::cin.fail() && min <= input && input <= max)
100 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
101 throw std::runtime_error("Invalid Input, please try again");
104 std::string processUserStringInput()
115 std::cout << "================================================================" << std::endl;
116 std::cout << " IoTivity Resource Container Test Application " << std::endl;
117 std::cout << "================================================================" << std::endl;
118 std::cout << " 1. Start Resource Container " << std::endl;
119 std::cout << " 2. Stop Resource Container " << std::endl;
120 std::cout << " 3. Add Sample Bundle " << std::endl;
121 std::cout << " 4. Start Sample Bundle " << std::endl;
122 std::cout << " 5. Stop Sample Bundle " << std::endl;
123 std::cout << " 6. Remove Sample Bundle " << std::endl;
124 std::cout << " 7. Add Sample Resource " << std::endl;
125 std::cout << " 8. Remove Sample Resource " << std::endl;
126 std::cout << " 9. List Bundles " << std::endl;
127 std::cout << " 10. List Resources " << std::endl;
128 std::cout << " 11. Exit " << std::endl;
129 std::cout << "================================================================" << std::endl;
130 std::cout << " Please Enter the NO: ";
133 bool checkBundleRegistered(std::string bundleId)
135 std::list< std::unique_ptr< RCSBundleInfo > > bundleList;
137 if (g_bContainerStarted)
139 bundleList = g_pResourceContainer->listBundles();
141 for (auto &bundle : bundleList)
143 if (bundle->getID().compare(bundleId) == 0)
148 std::cout << "Bundle \'" << bundleId << "\' is not registered." << std::endl;
152 bool checkResourceRegistered(std::string bundleId, std::string resourceUri)
154 std::list< std::string > resourceList;
156 if (g_bContainerStarted && checkBundleRegistered(bundleId))
158 resourceList = g_pResourceContainer->listBundleResources(bundleId);
160 for (auto &resource : resourceList)
162 if (resource.compare(resourceUri) == 0)
167 std::cout << "Resource \'" << resourceUri << "\' is not registered." << std::endl;
171 void StartContainer(std::string configPath)
173 if (!g_bContainerStarted)
175 g_pResourceContainer->startContainer(configPath);
176 g_bContainerStarted = true;
177 std::cout << "Container started." << std::endl;
181 std::cout << "Container is already started." << std::endl;
187 if (g_pResourceContainer && g_bContainerStarted)
189 g_pResourceContainer->stopContainer();
190 g_bContainerStarted = false;
194 std::cout << "Container is not started." << std::endl;
198 void AddSampleBundle()
200 std::map< std::string, std::string > bundleParams;
202 if (g_pResourceContainer && g_bContainerStarted)
204 g_pResourceContainer->addBundle(EXAMPLE_BUNDLE_ID, EXAMPLE_BUNDLE_URI,
205 EXAMPLE_BUNDLE_PATH, EXAMPLE_BUNDLE_ACTIVATOR,
210 std::cout << "Container is not started." << std::endl;
214 void StartSampleBundle()
216 if (g_pResourceContainer && g_bContainerStarted)
218 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
220 g_pResourceContainer->startBundle(EXAMPLE_BUNDLE_ID);
221 g_bSampleBundleStarted = true;
226 std::cout << "Container is not started." << std::endl;
230 void StopSampleBundle()
232 if (g_pResourceContainer && g_bContainerStarted)
234 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
236 g_pResourceContainer->stopBundle(EXAMPLE_BUNDLE_ID);
237 g_bSampleBundleStarted = false;
240 std::cout << "Sample Bundle is not started." << std::endl;
244 std::cout << "Container is not started." << std::endl;
248 void RemoveSampleBundle()
250 if (g_pResourceContainer && g_bContainerStarted)
252 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
253 g_pResourceContainer->removeBundle(EXAMPLE_BUNDLE_ID);
257 std::cout << "Container is not started." << std::endl;
261 void AddSampleBundleResource()
263 std::map< std::string, std::string > resourceParams;
265 if (g_pResourceContainer && g_bContainerStarted)
267 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
269 resourceParams.insert(std::make_pair("resourceType", EXAMPLE_RESOURCE_TYPE));
270 resourceParams.insert(std::make_pair("address", EXAMPLE_ADDRESS));
272 g_pResourceContainer->addResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI,
276 std::cout << "Sample Bundle is not started." << std::endl;
280 std::cout << "Container is not started." << std::endl;
284 void RemoveSampleBundleResource()
286 if (g_pResourceContainer && g_bContainerStarted)
288 if (checkResourceRegistered(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI)
289 && g_bSampleBundleStarted)
290 g_pResourceContainer->removeResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI);
292 std::cout << "Sample Bundle is not started." << std::endl;
296 std::cout << "Container is not started." << std::endl;
300 void printBundleList(std::list< std::unique_ptr< RCSBundleInfo > > &list)
302 std::cout << std::endl;
303 for (auto &bundleinfo : list)
305 std::cout << "-- " << bundleinfo->getID() << std::endl;
307 std::cout << std::endl;
312 std::list< std::unique_ptr< RCSBundleInfo > > bundles;
314 if (g_pResourceContainer && g_bContainerStarted)
316 bundles = g_pResourceContainer->listBundles();
317 printBundleList(bundles);
321 std::cout << "Container is not started." << std::endl;
325 void printResourceList(std::list< std::string > &list)
327 std::cout << std::endl;
328 for (auto &bundleResource : list)
330 std::cout << "-- " << bundleResource << std::endl;
332 std::cout << std::endl;
335 void ListResources(std::string bundleId)
337 std::list< std::string > resources;
339 if (g_pResourceContainer && g_bContainerStarted)
341 if (checkBundleRegistered(bundleId))
343 resources = g_pResourceContainer->listBundleResources(bundleId);
344 printResourceList(resources);
349 std::cout << "Container is not started." << std::endl;
353 void ExecuteCommand(APPMenu menu)
357 case APPMenu::START_RC:
359 std::string filePath;
360 std::cout << "Type Configuration File Path (Press \'0\' to start with example): ";
362 if ((filePath = processUserStringInput()).compare("0") == 0)
364 getCurrentPath(&filePath);
365 filePath.append(EXAMPLE_CONFIG_PATH);
368 StartContainer(filePath);
371 case APPMenu::STOP_RC:
374 case APPMenu::ADD_SAMPLE_BUNDLE:
377 case APPMenu::START_SAMPLE_BUNDLE:
380 case APPMenu::STOP_SAMPLE_BUNDLE:
383 case APPMenu::REMOVE_SAMPLE_BUNDLE:
384 RemoveSampleBundle();
386 case APPMenu::ADD_SAMPLE_RESOURCE:
387 AddSampleBundleResource();
389 case APPMenu::REMOVE_SAMPLE_RESOURCE:
390 RemoveSampleBundleResource();
392 case APPMenu::LIST_BUNDLES:
395 case APPMenu::LIST_RESOURCES:
397 std::string bundleId;
399 "Type Bundle Id to get Resource List (Press \'0\' to get example resource list): ";
401 if ((bundleId = processUserStringInput()).compare("0") == 0)
403 bundleId = EXAMPLE_BUNDLE_ID;
406 ListResources(bundleId);
417 g_pResourceContainer = RCSResourceContainer::getInstance();
424 ExecuteCommand((APPMenu)processUserInput(APPMenu::START_RC, APPMenu::EXIT));
426 catch (const std::exception &e)
428 std::cout << e.what() << std::endl;
430 catch (const CloseApp &)
436 if (g_bContainerStarted)
437 g_pResourceContainer->stopContainer();
439 g_pResourceContainer = nullptr;