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;
180 std::cout << "Container is already started." << std::endl;
186 if (g_pResourceContainer && g_bContainerStarted)
188 g_pResourceContainer->stopContainer();
189 g_bContainerStarted = false;
193 std::cout << "Container is not started." << std::endl;
197 void AddSampleBundle()
199 std::map< std::string, std::string > bundleParams;
201 if (g_pResourceContainer && g_bContainerStarted)
203 g_pResourceContainer->addBundle(EXAMPLE_BUNDLE_ID, EXAMPLE_BUNDLE_URI,
204 EXAMPLE_BUNDLE_PATH, EXAMPLE_BUNDLE_ACTIVATOR,
209 std::cout << "Container is not started." << std::endl;
213 void StartSampleBundle()
215 if (g_pResourceContainer && g_bContainerStarted)
217 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
219 g_pResourceContainer->startBundle(EXAMPLE_BUNDLE_ID);
220 g_bSampleBundleStarted = true;
225 std::cout << "Container is not started." << std::endl;
229 void StopSampleBundle()
231 if (g_pResourceContainer && g_bContainerStarted)
233 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
235 g_pResourceContainer->stopBundle(EXAMPLE_BUNDLE_ID);
236 g_bSampleBundleStarted = false;
239 std::cout << "Sample Bundle is not started." << std::endl;
243 std::cout << "Container is not started." << std::endl;
247 void RemoveSampleBundle()
249 if (g_pResourceContainer && g_bContainerStarted)
251 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
252 g_pResourceContainer->removeBundle(EXAMPLE_BUNDLE_ID);
256 std::cout << "Container is not started." << std::endl;
260 void AddSampleBundleResource()
262 std::map< std::string, std::string > resourceParams;
264 if (g_pResourceContainer && g_bContainerStarted)
266 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
268 resourceParams.insert(std::make_pair("resourceType", EXAMPLE_RESOURCE_TYPE));
269 resourceParams.insert(std::make_pair("address", EXAMPLE_ADDRESS));
271 g_pResourceContainer->addResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI,
275 std::cout << "Sample Bundle is not started." << std::endl;
279 std::cout << "Container is not started." << std::endl;
283 void RemoveSampleBundleResource()
285 if (g_pResourceContainer && g_bContainerStarted)
287 if (checkResourceRegistered(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI)
288 && g_bSampleBundleStarted)
289 g_pResourceContainer->removeResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI);
291 std::cout << "Sample Bundle is not started." << std::endl;
295 std::cout << "Container is not started." << std::endl;
299 void printBundleList(std::list< std::unique_ptr< RCSBundleInfo > > &list)
301 std::cout << std::endl;
302 for (auto &bundleinfo : list)
304 std::cout << "-- " << bundleinfo->getID() << std::endl;
306 std::cout << std::endl;
311 std::list< std::unique_ptr< RCSBundleInfo > > bundles;
313 if (g_pResourceContainer && g_bContainerStarted)
315 bundles = g_pResourceContainer->listBundles();
316 printBundleList(bundles);
320 std::cout << "Container is not started." << std::endl;
324 void printResourceList(std::list< std::string > &list)
326 std::cout << std::endl;
327 for (auto &bundleResource : list)
329 std::cout << "-- " << bundleResource << std::endl;
331 std::cout << std::endl;
334 void ListResources(std::string bundleId)
336 std::list< std::string > resources;
338 if (g_pResourceContainer && g_bContainerStarted)
340 if (checkBundleRegistered(bundleId))
342 resources = g_pResourceContainer->listBundleResources(bundleId);
343 printResourceList(resources);
348 std::cout << "Container is not started." << std::endl;
352 void ExecuteCommand(APPMenu menu)
356 case APPMenu::START_RC:
358 std::string filePath;
359 std::cout << "Type Configuration File Path (Press \'0\' to start with example): ";
361 if ((filePath = processUserStringInput()).compare("0") == 0)
363 getCurrentPath(&filePath);
364 filePath.append(EXAMPLE_CONFIG_PATH);
367 StartContainer(filePath);
370 case APPMenu::STOP_RC:
373 case APPMenu::ADD_SAMPLE_BUNDLE:
376 case APPMenu::START_SAMPLE_BUNDLE:
379 case APPMenu::STOP_SAMPLE_BUNDLE:
382 case APPMenu::REMOVE_SAMPLE_BUNDLE:
383 RemoveSampleBundle();
385 case APPMenu::ADD_SAMPLE_RESOURCE:
386 AddSampleBundleResource();
388 case APPMenu::REMOVE_SAMPLE_RESOURCE:
389 RemoveSampleBundleResource();
391 case APPMenu::LIST_BUNDLES:
394 case APPMenu::LIST_RESOURCES:
396 std::string bundleId;
398 "Type Bundle Id to get Resource List (Press \'0\' to get example resource list): ";
400 if ((bundleId = processUserStringInput()).compare("0") == 0)
402 bundleId = EXAMPLE_BUNDLE_ID;
405 ListResources(bundleId);
416 g_pResourceContainer = RCSResourceContainer::getInstance();
423 ExecuteCommand((APPMenu)processUserInput(APPMenu::START_RC, APPMenu::EXIT));
425 catch (const std::exception &e)
427 std::cout << e.what() << std::endl;
429 catch (const CloseApp &)
435 if (g_bContainerStarted)
436 g_pResourceContainer->stopContainer();
438 g_pResourceContainer = nullptr;