Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / service / resource-container / examples / ContainerSample.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #if defined(__linux__)
22 #include <unistd.h>
23 #include <string.h>
24 #endif
25
26 #include "RCSResourceContainer.h"
27 #include "RCSBundleInfo.h"
28
29 #include <iostream>
30 #include <functional>
31 #include <limits>
32 #include <stdexcept>
33 #include <string>
34
35 using namespace OIC::Service;
36
37 namespace
38 {
39     typedef enum
40     {
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,
46         EXIT = 11
47     } APPMenu;
48
49     struct CloseApp
50     {
51     };
52
53     const int MAX_PATH = 2048;
54
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";
63 };
64
65 bool g_bContainerStarted = false;
66 bool g_bSampleBundleStarted = false;
67 RCSResourceContainer *g_pResourceContainer = nullptr;
68
69 void getCurrentPath(std::string *pPath)
70 {
71     char buffer[MAX_PATH];
72
73     if (!pPath->empty())
74     {
75         pPath->clear();
76     }
77
78 #if defined(__linux__)
79     char *strPath = NULL;
80     int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1);
81
82     if (length != -1)
83     {
84         buffer[length] = '\0';
85         strPath = strrchr(buffer, '/');
86
87         if (strPath != NULL)
88         {
89             *strPath = '\0';
90         }
91     }
92 #endif
93     pPath->append(buffer);
94 }
95
96 int processUserInput(int min, int max)
97 {
98     int input;
99
100     std::cin >> input;
101
102     if (!std::cin.fail() && min <= input && input <= max)
103     {
104         return input;
105     }
106
107     std::cin.clear();
108     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
109     throw std::runtime_error("Invalid Input, please try again");
110 }
111
112 std::string processUserStringInput()
113 {
114     std::string input;
115
116     std::cin >> input;
117
118     return input;
119 }
120
121 void displayMenu()
122 {
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: ";
139 }
140
141 bool checkBundleRegistered(std::string bundleId)
142 {
143     std::list< std::unique_ptr< RCSBundleInfo > > bundleList;
144
145     if (g_bContainerStarted)
146     {
147         bundleList = g_pResourceContainer->listBundles();
148
149         for (auto &bundle : bundleList)
150         {
151             if (bundle->getID().compare(bundleId) == 0)
152             {
153                 return true;
154             }
155         }
156     }
157
158     std::cout << "Bundle \'" << bundleId << "\' is not registered." << std::endl;
159     return false;
160 }
161
162 bool checkResourceRegistered(std::string bundleId, std::string resourceUri)
163 {
164     std::list< std::string > resourceList;
165
166     if (g_bContainerStarted && checkBundleRegistered(bundleId))
167     {
168         resourceList = g_pResourceContainer->listBundleResources(bundleId);
169
170         for (auto &resource : resourceList)
171         {
172             if (resource.compare(resourceUri) == 0)
173             {
174                 return true;
175             }
176         }
177     }
178
179     std::cout << "Resource \'" << resourceUri << "\' is not registered." << std::endl;
180     return false;
181 }
182
183 void StartContainer(std::string configPath)
184 {
185     if (!g_bContainerStarted)
186     {
187         g_pResourceContainer->startContainer(configPath);
188         g_bContainerStarted = true;
189         std::cout << "Container started." << std::endl;
190     }
191     else
192     {
193         std::cout << "Container is already started." << std::endl;
194     }
195 }
196
197 void StopContainer()
198 {
199     if (g_pResourceContainer && g_bContainerStarted)
200     {
201         g_pResourceContainer->stopContainer();
202         g_bContainerStarted = false;
203     }
204     else
205     {
206         std::cout << "Container is not started." << std::endl;
207     }
208 }
209
210 void AddSampleBundle()
211 {
212     std::map< std::string, std::string > bundleParams;
213
214     if (g_pResourceContainer && g_bContainerStarted)
215     {
216         g_pResourceContainer->addBundle(EXAMPLE_BUNDLE_ID, EXAMPLE_BUNDLE_URI,
217                                         EXAMPLE_BUNDLE_PATH, EXAMPLE_BUNDLE_ACTIVATOR,
218                                         bundleParams);
219     }
220     else
221     {
222         std::cout << "Container is not started." << std::endl;
223     }
224 }
225
226 void StartSampleBundle()
227 {
228     if (g_pResourceContainer && g_bContainerStarted)
229     {
230         if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
231         {
232             g_pResourceContainer->startBundle(EXAMPLE_BUNDLE_ID);
233             g_bSampleBundleStarted = true;
234         }
235     }
236     else
237     {
238         std::cout << "Container is not started." << std::endl;
239     }
240 }
241
242 void StopSampleBundle()
243 {
244     if (g_pResourceContainer && g_bContainerStarted)
245     {
246         if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
247         {
248             g_pResourceContainer->stopBundle(EXAMPLE_BUNDLE_ID);
249             g_bSampleBundleStarted = false;
250         }
251         else
252         {
253             std::cout << "Sample Bundle is not started." << std::endl;
254         }
255     }
256     else
257     {
258         std::cout << "Container is not started." << std::endl;
259     }
260 }
261
262 void RemoveSampleBundle()
263 {
264     if (g_pResourceContainer && g_bContainerStarted)
265     {
266         if (checkBundleRegistered(EXAMPLE_BUNDLE_ID))
267         {
268             g_pResourceContainer->removeBundle(EXAMPLE_BUNDLE_ID);
269         }
270     }
271     else
272     {
273         std::cout << "Container is not started." << std::endl;
274     }
275 }
276
277 void AddSampleBundleResource()
278 {
279     std::map< std::string, std::string > resourceParams;
280
281     if (g_pResourceContainer && g_bContainerStarted)
282     {
283         if (checkBundleRegistered(EXAMPLE_BUNDLE_ID) && g_bSampleBundleStarted)
284         {
285             resourceParams.insert(std::make_pair("resourceType", EXAMPLE_RESOURCE_TYPE));
286             resourceParams.insert(std::make_pair("address", EXAMPLE_ADDRESS));
287
288             g_pResourceContainer->addResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI,
289                                                     resourceParams);
290         }
291         else
292         {
293             std::cout << "Sample Bundle is not started." << std::endl;
294         }
295     }
296     else
297     {
298         std::cout << "Container is not started." << std::endl;
299     }
300 }
301
302 void RemoveSampleBundleResource()
303 {
304     if (g_pResourceContainer && g_bContainerStarted)
305     {
306         if (checkResourceRegistered(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI)
307             && g_bSampleBundleStarted)
308         {
309             g_pResourceContainer->removeResourceConfig(EXAMPLE_BUNDLE_ID, EXAMPLE_RESOURCE_URI);
310         }
311         else
312         {
313             std::cout << "Sample Bundle is not started." << std::endl;
314         }
315     }
316     else
317     {
318         std::cout << "Container is not started." << std::endl;
319     }
320 }
321
322 void printBundleList(std::list< std::unique_ptr< RCSBundleInfo > > &list)
323 {
324     std::cout << std::endl;
325     for (auto &bundleinfo : list)
326     {
327         std::cout << "-- " << bundleinfo->getID() << std::endl;
328     }
329     std::cout << std::endl;
330 }
331
332 void ListBundles()
333 {
334     std::list< std::unique_ptr< RCSBundleInfo > > bundles;
335
336     if (g_pResourceContainer && g_bContainerStarted)
337     {
338         bundles = g_pResourceContainer->listBundles();
339         printBundleList(bundles);
340     }
341     else
342     {
343         std::cout << "Container is not started." << std::endl;
344     }
345 }
346
347 void printResourceList(std::list< std::string > &list)
348 {
349     std::cout << std::endl;
350     for (auto &bundleResource : list)
351     {
352         std::cout << "-- " << bundleResource << std::endl;
353     }
354     std::cout << std::endl;
355 }
356
357 void ListResources(std::string bundleId)
358 {
359     std::list< std::string > resources;
360
361     if (g_pResourceContainer && g_bContainerStarted)
362     {
363         if (checkBundleRegistered(bundleId))
364         {
365             resources = g_pResourceContainer->listBundleResources(bundleId);
366             printResourceList(resources);
367         }
368     }
369     else
370     {
371         std::cout << "Container is not started." << std::endl;
372     }
373 }
374
375 void ExecuteCommand(APPMenu menu)
376 {
377     switch (menu)
378     {
379         case APPMenu::START_RC:
380             {
381                 std::string filePath;
382                 std::cout << "Type Configuration File Path (Press \'0\' to start with example): ";
383
384                 if ((filePath = processUserStringInput()).compare("0") == 0)
385                 {
386                     getCurrentPath(&filePath);
387                     filePath.append(EXAMPLE_CONFIG_PATH);
388                 }
389
390                 StartContainer(filePath);
391             }
392             break;
393         case APPMenu::STOP_RC:
394             StopContainer();
395             break;
396         case APPMenu::ADD_SAMPLE_BUNDLE:
397             AddSampleBundle();
398             break;
399         case APPMenu::START_SAMPLE_BUNDLE:
400             StartSampleBundle();
401             break;
402         case APPMenu::STOP_SAMPLE_BUNDLE:
403             StopSampleBundle();
404             break;
405         case APPMenu::REMOVE_SAMPLE_BUNDLE:
406             RemoveSampleBundle();
407             break;
408         case APPMenu::ADD_SAMPLE_RESOURCE:
409             AddSampleBundleResource();
410             break;
411         case APPMenu::REMOVE_SAMPLE_RESOURCE:
412             RemoveSampleBundleResource();
413             break;
414         case APPMenu::LIST_BUNDLES:
415             ListBundles();
416             break;
417         case APPMenu::LIST_RESOURCES:
418             {
419                 std::string bundleId;
420                 std::cout <<
421                           "Type Bundle Id to get Resource List (Press \'0\' to get example resource list): ";
422
423                 if ((bundleId = processUserStringInput()).compare("0") == 0)
424                 {
425                     bundleId = EXAMPLE_BUNDLE_ID;
426                 }
427
428                 ListResources(bundleId);
429             }
430             break;
431         case APPMenu::EXIT:
432             throw CloseApp();
433             break;
434     }
435 }
436
437 int main()
438 {
439     g_pResourceContainer = RCSResourceContainer::getInstance();
440
441     while (true)
442     {
443         try
444         {
445             displayMenu();
446             ExecuteCommand((APPMenu)processUserInput(APPMenu::START_RC, APPMenu::EXIT));
447         }
448         catch (const std::exception &e)
449         {
450             std::cout << e.what() << std::endl;
451         }
452         catch (const CloseApp &)
453         {
454             break;
455         }
456     }
457
458     if (g_bContainerStarted)
459     {
460         g_pResourceContainer->stopContainer();
461     }
462
463     g_pResourceContainer = nullptr;
464
465     return 0;
466 }