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,
53 const int MAX_PATH = 2048;
55 const std::string EXAMPLE_CONFIG_PATH = "/examples/ResourceContainerConfig.xml";
56 const std::string EXAMPLE_BUNDLE_ID = "oic.bundle.hueSample";
57 const std::string EXAMPLE_BUNDLE_URI = "/hueSample";
58 const std::string EXAMPLE_BUNDLE_PATH = "libHueBundle.so";
59 const std::string EXAMPLE_BUNDLE_ACTIVATOR = "huesample";
60 const std::string EXAMPLE_RESOURCE_URI = "/hue/light/sample";
61 const std::string EXAMPLE_RESOURCE_TYPE = "oic.r.light";
62 const std::string EXAMPLE_ADDRESS = "http://192.168.0.2/api/newdeveloper/lights/1";
65 bool g_bContainerStarted = false;
66 bool g_bSampleBundleStarted = false;
67 RCSResourceContainer *g_pResourceContainer = nullptr;
69 void getCurrentPath(std::string *pPath)
71 char buffer[MAX_PATH] = {0,};
78 #if defined(__linux__)
80 int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1);
84 buffer[length] = '\0';
85 strPath = strrchr(buffer, '/');
93 pPath->append(buffer);
96 int processUserInput(int min, int max)
102 if (!std::cin.fail() && min <= input && input <= max)
108 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
109 throw std::runtime_error("Invalid Input, please try again");
112 std::string processUserStringInput()
123 std::cout << "================================================================" << std::endl;
124 std::cout << " IoTivity Resource Container Test Application " << std::endl;
125 std::cout << "================================================================" << std::endl;
126 std::cout << " 1. Start Resource Container " << std::endl;
127 std::cout << " 2. Stop Resource Container " << std::endl;
128 std::cout << " 3. Add Sample Bundle " << std::endl;
129 std::cout << " 4. Start Sample Bundle " << std::endl;
130 std::cout << " 5. Stop Sample Bundle " << std::endl;
131 std::cout << " 6. Remove Sample Bundle " << std::endl;
132 std::cout << " 7. Add Sample Resource " << std::endl;
133 std::cout << " 8. Remove Sample Resource " << std::endl;
134 std::cout << " 9. List Bundles " << std::endl;
135 std::cout << " 10. List Resources " << std::endl;
136 std::cout << " 11. Exit " << std::endl;
137 std::cout << "================================================================" << std::endl;
138 std::cout << " Please Enter the NO: ";
141 bool checkBundleRegistered(std::string bundleId)
143 std::list< std::unique_ptr< RCSBundleInfo > > bundleList;
145 if (g_bContainerStarted)
147 bundleList = g_pResourceContainer->listBundles();
149 for (auto &bundle : bundleList)
151 if (bundle->getID().compare(bundleId) == 0)
158 std::cout << "Bundle \'" << bundleId << "\' is not registered." << std::endl;
162 bool checkResourceRegistered(std::string bundleId, std::string resourceUri)
164 std::list< std::string > resourceList;
166 if (g_bContainerStarted && checkBundleRegistered(bundleId))
168 resourceList = g_pResourceContainer->listBundleResources(bundleId);
170 for (auto &resource : resourceList)
172 if (resource.compare(resourceUri) == 0)
179 std::cout << "Resource \'" << resourceUri << "\' is not registered." << std::endl;
183 void StartContainer(std::string configPath)
185 if (!g_bContainerStarted)
187 g_pResourceContainer->startContainer(configPath);
188 g_bContainerStarted = true;
189 std::cout << "Container started." << std::endl;
193 std::cout << "Container is already started." << std::endl;
199 if (g_pResourceContainer && g_bContainerStarted)
201 g_pResourceContainer->stopContainer();
202 g_bContainerStarted = false;
206 std::cout << "Container is not started." << std::endl;
210 void AddSampleBundle()
212 std::map< std::string, std::string > bundleParams;
214 if (g_pResourceContainer && g_bContainerStarted)
216 g_pResourceContainer->addBundle(EXAMPLE_BUNDLE_ID, EXAMPLE_BUNDLE_URI,
217 EXAMPLE_BUNDLE_PATH, EXAMPLE_BUNDLE_ACTIVATOR,
222 std::cout << "Container is not started." << std::endl;
226 void StartSampleBundle()
228 if (g_pResourceContainer && g_bContainerStarted)
230 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
232 g_pResourceContainer->startBundle(EXAMPLE_BUNDLE_ID);
233 g_bSampleBundleStarted = true;
238 std::cout << "Container is not started." << std::endl;
242 void StopSampleBundle()
244 if (g_pResourceContainer && g_bContainerStarted)
246 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
248 g_pResourceContainer->stopBundle(EXAMPLE_BUNDLE_ID);
249 g_bSampleBundleStarted = false;
253 std::cout << "Sample Bundle is not started." << std::endl;
258 std::cout << "Container is not started." << std::endl;
262 void RemoveSampleBundle()
264 if (g_pResourceContainer && g_bContainerStarted)
266 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
268 g_pResourceContainer->removeBundle(EXAMPLE_BUNDLE_ID);
273 std::cout << "Container is not started." << std::endl;
277 void AddSampleBundleResource()
279 std::map< std::string, std::string > resourceParams;
281 if (g_pResourceContainer && g_bContainerStarted)
283 if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
285 resourceParams.insert(std::make_pair("resourceType", EXAMPLE_RESOURCE_TYPE));
286 resourceParams.insert(std::make_pair("address", EXAMPLE_ADDRESS));
288 g_pResourceContainer->addResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI,
293 std::cout << "Sample Bundle is not started." << std::endl;
298 std::cout << "Container is not started." << std::endl;
302 void RemoveSampleBundleResource()
304 if (g_pResourceContainer && g_bContainerStarted)
306 if (checkResourceRegistered(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI)
307 && g_bSampleBundleStarted)
309 g_pResourceContainer->removeResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI);
313 std::cout << "Sample Bundle is not started." << std::endl;
318 std::cout << "Container is not started." << std::endl;
322 void printBundleList(std::list< std::unique_ptr< RCSBundleInfo > > &list)
324 std::cout << std::endl;
325 for (auto &bundleinfo : list)
327 std::cout << "-- " << bundleinfo->getID() << std::endl;
329 std::cout << std::endl;
334 std::list< std::unique_ptr< RCSBundleInfo > > bundles;
336 if (g_pResourceContainer && g_bContainerStarted)
338 bundles = g_pResourceContainer->listBundles();
339 printBundleList(bundles);
343 std::cout << "Container is not started." << std::endl;
347 void printResourceList(std::list< std::string > &list)
349 std::cout << std::endl;
350 for (auto &bundleResource : list)
352 std::cout << "-- " << bundleResource << std::endl;
354 std::cout << std::endl;
357 void ListResources(std::string bundleId)
359 std::list< std::string > resources;
361 if (g_pResourceContainer && g_bContainerStarted)
363 if (checkBundleRegistered(bundleId))
365 resources = g_pResourceContainer->listBundleResources(bundleId);
366 printResourceList(resources);
371 std::cout << "Container is not started." << std::endl;
375 void ExecuteCommand(APPMenu menu)
379 case APPMenu::START_RC:
381 std::string filePath;
382 std::cout << "Type Configuration File Path (Press \'0\' to start with example): ";
384 if ((filePath = processUserStringInput()).compare("0") == 0)
386 getCurrentPath(&filePath);
387 filePath.append(EXAMPLE_CONFIG_PATH);
390 StartContainer(filePath);
393 case APPMenu::STOP_RC:
396 case APPMenu::ADD_SAMPLE_BUNDLE:
399 case APPMenu::START_SAMPLE_BUNDLE:
402 case APPMenu::STOP_SAMPLE_BUNDLE:
405 case APPMenu::REMOVE_SAMPLE_BUNDLE:
406 RemoveSampleBundle();
408 case APPMenu::ADD_SAMPLE_RESOURCE:
409 AddSampleBundleResource();
411 case APPMenu::REMOVE_SAMPLE_RESOURCE:
412 RemoveSampleBundleResource();
414 case APPMenu::LIST_BUNDLES:
417 case APPMenu::LIST_RESOURCES:
419 std::string bundleId;
421 "Type Bundle Id to get Resource List (Press \'0\' to get example resource list): ";
423 if ((bundleId = processUserStringInput()).compare("0") == 0)
425 bundleId = EXAMPLE_BUNDLE_ID;
428 ListResources(bundleId);
439 g_pResourceContainer = RCSResourceContainer::getInstance();
446 ExecuteCommand((APPMenu)processUserInput(APPMenu::START_RC, APPMenu::EXIT));
448 catch (const std::exception &e)
450 std::cout << e.what() << std::endl;
452 catch (const CloseApp &)
458 if (g_bContainerStarted)
460 g_pResourceContainer->stopContainer();
463 g_pResourceContainer = nullptr;