Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / examples / client / simulator_client.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 #include "simulator_manager.h"
22 #include <map>
23 #include <mutex>
24
25 std::string getOperationStateString(OperationState state)
26 {
27     switch (state)
28     {
29         case OP_START: return "OP_START";
30         case OP_COMPLETE: return "OP_COMPLETE";
31         case OP_ABORT: return "OP_ABORT";
32     }
33
34     return "OP_UNKNOWN";
35 }
36
37 class AppLogger : public ILogger
38 {
39     public:
40         void write(std::string time, ILogger::Level level, std::string message)
41         {
42             std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
43         }
44 };
45 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
46
47 class ClientController
48 {
49     public:
50         void startTest()
51         {
52             printMenu();
53             bool cont = true;
54             while (cont)
55             {
56                 int choice = -1;
57                 std::cout << "Enter your choice: ";
58                 std::cin >> choice;
59                 if (choice < 0 || choice > 14)
60                 {
61                     std::cout << "Invaild choice !" << std::endl; continue;
62                 }
63
64                 switch (choice)
65                 {
66                     case 1: findResource(); break;
67                     case 2: displayResource(); break;
68                     case 3: observeResource(); break;
69                     case 4: cancelObserving(); break;
70                     case 5: sendGet(); break;
71                     case 6: sendPut(); break;
72                     case 7: sendPost(); break;
73                     case 8: sendAllGETRequests(); break;
74                     case 9: sendAllPUTRequests(); break;
75                     case 10: sendAllPOSTRequests(); break;
76                     case 11: configure(); break;
77                     case 12: getDeviceInfo(); break;
78                     case 13: getPlatformInfo(); break;
79                     case 14: printMenu(); break;
80                     case 0: cont = false;
81                 }
82             }
83         }
84
85     private:
86         void printMenu()
87         {
88             std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
89             std::cout << "1. Find resource" << std::endl;
90             std::cout << "2. Display resource information" << std::endl;
91             std::cout << "3. Observe for resource change" << std::endl;
92             std::cout << "4. Cancel observation" << std::endl;
93             std::cout << "5. Send GET message" << std::endl;
94             std::cout << "6. Send PUT message" << std::endl;
95             std::cout << "7. Send POST message" << std::endl;
96             std::cout << "8. Send All GET requests" << std::endl;
97             std::cout << "9. Send All PUT requests" << std::endl;
98             std::cout << "10. Send All POST requests" << std::endl;
99             std::cout << "11. Configure (using RAML file)" << std::endl;
100             std::cout << "12. Get Device Information" << std::endl;
101             std::cout << "13. Get Platform Information" << std::endl;
102             std::cout << "14: Help" << std::endl;
103             std::cout << "0. Exit" << std::endl;
104             std::cout << "###################################################" << std::endl;
105         }
106
107         SimulatorRemoteResourceSP selectResource()
108         {
109             std::lock_guard<std::recursive_mutex> lock(m_mutex);
110             if (0 == m_resList.size())
111             {
112                 std::cout << "No resources!" << std::endl;
113                 return nullptr;
114             }
115
116             int index = 1;
117             std::vector<std::string> ids;
118             for (auto &resourceEntry : m_resList)
119             {
120                 std::cout << index++ << ": " << (resourceEntry.second)->getURI() << "[" <<
121                           (resourceEntry.second)->getHost()  << "]" << std::endl;
122                 ids.push_back((resourceEntry.second)->getID());
123             }
124
125             int choice = -1;
126             std::cout << "Choose the resource: ";
127             std::cin >> choice;
128
129             if (choice < 1 || choice > index - 1)
130             {
131                 std::cout << "Invalid choice !" << std::endl;
132                 return nullptr;
133             }
134
135             return m_resList[ids[choice - 1]];
136         }
137
138         void findResource()
139         {
140             std::string resourceType;
141             std::cout << "Enter resource type : ";
142             std::cin >> resourceType;
143
144             ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
145             {
146                 std::cout << "Resource found ######" << std::endl;
147                 displayResource(resource);
148
149                 // Add to local list
150                 std::lock_guard<std::recursive_mutex> lock(m_mutex);
151                 if (m_resList.end() == m_resList.find(resource->getID()))
152                     m_resList[resource->getID()] = resource;
153                 else
154                     std::cout << "Resource with UID: " << resource->getID() << "already exist in the list!" <<
155                               std::endl;
156             };
157
158             try
159             {
160                 SimulatorManager::getInstance()->findResource(resourceType, callback);
161                 std::cout << "SimulatorManager::findResource is successful" << std::endl;
162                 m_resList.clear();
163             }
164             catch (InvalidArgsException &e)
165             {
166                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
167                           << std::endl;
168             }
169             catch (SimulatorException &e)
170             {
171                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
172                           std::endl;
173             }
174         }
175
176         void displayResource()
177         {
178             displayResource(selectResource());
179         }
180
181         void displayResource(SimulatorRemoteResourceSP resource)
182         {
183             if (!resource) return;
184
185             std::cout << "#############################" << std::endl;
186             std::cout << "URI: " << resource->getURI().c_str() << std::endl;
187             std::cout << "Host: " << resource->getHost().c_str() << std::endl;
188             std::cout << "ID: " << resource->getID().c_str() << std::endl;
189             std::cout << "Resource Types: ";
190             for (auto &type : resource->getResourceTypes())
191                 std::cout << type << " ";
192             std::cout << "\nInterface Types: ";
193             for (auto &type : resource->getInterface())
194                 std::cout << type << " ";
195             std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable()
196                       << std::noboolalpha << std::endl;
197             std::cout << "#############################" << std::endl;
198         }
199
200         void observeResource()
201         {
202             SimulatorRemoteResourceSP resource = selectResource();
203             if (!resource) return;
204
205             // callback implementaion
206             SimulatorRemoteResource::ObserveNotificationCallback callback =
207                 [](const std::string & uid, SimulatorResult result,
208                    const SimulatorResourceModel & rep, int seq)
209             {
210                 std::cout << "\nObserve notification received ###[errorcode:  " << result <<
211                           " seq:  " << seq << "UID: " << uid << "]" << std::endl;
212
213                 std::cout << "Representation is: " << std::endl;
214                 std::cout << rep.asString() << std::endl;
215             };
216
217             try
218             {
219                 resource->observe(ObserveType::OBSERVE, callback);
220                 std::cout << "Observe is successful!" << std::endl;
221             }
222             catch (InvalidArgsException &e)
223             {
224                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
225                           << e.what() << "]" << std::endl;
226             }
227             catch (SimulatorException &e)
228             {
229                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
230                           e.what() << "]" << std::endl;
231             }
232         }
233
234         void cancelObserving()
235         {
236             SimulatorRemoteResourceSP resource = selectResource();
237             if (!resource) return;
238
239             try
240             {
241                 resource->cancelObserve();
242                 std::cout << "Cancelling observe is successful!" << std::endl;
243             }
244             catch (SimulatorException &e)
245             {
246                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
247                           e.what() << "]" << std::endl;
248             }
249         }
250
251         void sendGet()
252         {
253             SimulatorRemoteResourceSP resource = selectResource();
254             if (!resource) return;
255
256             // callback implementaion
257             SimulatorRemoteResource::ResponseCallback callback =
258                 [](const std::string & uid, SimulatorResult result, const SimulatorResourceModel & rep)
259             {
260                 std::cout << "\nGET Response received ### [errorcode:  " << result << "]"
261                           << std::endl;
262                 std::cout << "UID is: " << uid << std::endl;
263                 std::cout << "Representation is: " << std::endl;
264                 std::cout << rep.asString() << std::endl;
265             };
266
267             try
268             {
269                 resource->get(std::map <std::string, std::string>(), callback);
270                 std::cout << "GET is successful!" << std::endl;
271             }
272             catch (InvalidArgsException &e)
273             {
274                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
275                           << e.what() << "]" << std::endl;
276             }
277             catch (NoSupportException &e)
278             {
279                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
280                           e.what() << "]" << std::endl;
281             }
282             catch (SimulatorException &e)
283             {
284                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
285                           e.what() << "]" << std::endl;
286             }
287         }
288
289         void sendPut()
290         {
291             SimulatorRemoteResourceSP resource = selectResource();
292             if (!resource) return;
293
294             // callback implementaion
295             SimulatorRemoteResource::ResponseCallback callback =
296                 [](const std::string & uid, SimulatorResult result, const SimulatorResourceModel & rep)
297             {
298                 std::cout << "\nPUT Response received ![errorcode:  " << result << "]"
299                           << std::endl;
300                 std::cout << "UID is: " << uid << std::endl;
301                 std::cout << "Representation is: " << std::endl;
302                 std::cout << rep.asString() << std::endl;
303             };
304
305             try
306             {
307                 SimulatorResourceModel rep;
308                 rep.add("power", false);
309                 rep.add("intensity", 15);
310
311                 resource->put(rep, callback);
312                 std::cout << "PUT is successful!" << std::endl;
313             }
314             catch (InvalidArgsException &e)
315             {
316                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
317                           << e.what() << "]" << std::endl;
318             }
319             catch (NoSupportException &e)
320             {
321                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
322                           e.what() << "]" << std::endl;
323             }
324             catch (SimulatorException &e)
325             {
326                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
327                           e.what() << "]" << std::endl;
328             }
329         }
330
331         void sendPost()
332         {
333             SimulatorRemoteResourceSP resource = selectResource();
334             if (!resource) return;
335
336             // callback implementaion
337             SimulatorRemoteResource::ResponseCallback callback =
338                 [](const std::string & uid, SimulatorResult result, const SimulatorResourceModel & rep)
339             {
340                 std::cout << "\nPOST Response received ![errorcode:  " << result << "]"
341                           << std::endl;
342                 std::cout << "UID is: " << uid << std::endl;
343                 std::cout << "Representation is: " << std::endl;
344                 std::cout << rep.asString() << std::endl;
345             };
346
347             try
348             {
349                 SimulatorResourceModel rep;
350                 rep.add("power", true);
351                 rep.add("intensity", 17);
352
353                 resource->post(rep, callback);
354                 std::cout << "POST is successful!" << std::endl;
355             }
356             catch (InvalidArgsException &e)
357             {
358                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
359                           << e.what() << "]" << std::endl;
360             }
361             catch (NoSupportException &e)
362             {
363                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
364                           e.what() << "]" << std::endl;
365             }
366             catch (SimulatorException &e)
367             {
368                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
369                           e.what() << "]" << std::endl;
370             }
371         }
372
373         void sendAllGETRequests()
374         {
375             SimulatorRemoteResourceSP resource = selectResource();
376             if (!resource) return;
377
378             SimulatorRemoteResource::AutoRequestGenerationCallback callback =
379                 [] (const std::string & uid, int sessionId, OperationState state)
380             {
381                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
382                           "  State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
383                           std::endl;
384             };
385
386             try
387             {
388                 int id = resource->startAutoRequesting(RequestType::RQ_TYPE_GET, callback);
389                 std::cout << "startVerification for GET is successful!id: " << id << std::endl;
390             }
391             catch (InvalidArgsException &e)
392             {
393                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
394                           << e.what() << "]" << std::endl;
395             }
396             catch (NoSupportException &e)
397             {
398                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
399                           e.what() << "]" << std::endl;
400             }
401             catch (SimulatorException &e)
402             {
403                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
404                           e.what() << "]" << std::endl;
405             }
406         }
407
408         void sendAllPUTRequests()
409         {
410             SimulatorRemoteResourceSP resource = selectResource();
411             if (!resource) return;
412
413             SimulatorRemoteResource::AutoRequestGenerationCallback callback =
414                 [] (const std::string & uid, int sessionId, OperationState state)
415             {
416                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
417                           "  State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
418                           std::endl;
419             };
420
421             try
422             {
423                 int id = resource->startAutoRequesting(RequestType::RQ_TYPE_PUT, callback);
424                 std::cout << "startVerification for PUT is successful!id: " << id << std::endl;
425             }
426             catch (InvalidArgsException &e)
427             {
428                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
429                           << e.what() << "]" << std::endl;
430             }
431             catch (NoSupportException &e)
432             {
433                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
434                           e.what() << "]" << std::endl;
435             }
436             catch (SimulatorException &e)
437             {
438                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
439                           e.what() << "]" << std::endl;
440             }
441         }
442
443         void sendAllPOSTRequests()
444         {
445             SimulatorRemoteResourceSP resource = selectResource();
446             if (!resource) return;
447
448             SimulatorRemoteResource::AutoRequestGenerationCallback callback =
449                 [] (const std::string & uid, int sessionId, OperationState state)
450             {
451                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
452                           "  State: " << getOperationStateString(state) << " UID: " << uid << "]"
453                           << std::endl;
454             };
455
456             try
457             {
458                 int id = resource->startAutoRequesting(RequestType::RQ_TYPE_POST, callback);
459                 std::cout << "startVerification for POST is successful!id: " << id << std::endl;
460             }
461             catch (InvalidArgsException &e)
462             {
463                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
464                           << e.what() << "]" << std::endl;
465             }
466             catch (NoSupportException &e)
467             {
468                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
469                           e.what() << "]" << std::endl;
470             }
471             catch (SimulatorException &e)
472             {
473                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
474                           e.what() << "]" << std::endl;
475             }
476         }
477
478         void configure()
479         {
480             SimulatorRemoteResourceSP resource = selectResource();
481             if (!resource)
482                 return;
483
484             try
485             {
486                 std::string configPath;
487                 std::cout << "Enter the config path: ";
488                 std::cin >> configPath;
489
490                 resource->configure(configPath);
491             }
492             catch (InvalidArgsException &e)
493             {
494                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
495                           << e.what() << "]" << std::endl;
496             }
497             catch (SimulatorException &e)
498             {
499                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
500                           e.what() << "]" << std::endl;
501             }
502         }
503
504         void getDeviceInfo()
505         {
506             SimulatorRemoteResourceSP resource = selectResource();
507             if (!resource)
508                 return;
509
510             try
511             {
512                 SimulatorManager::getInstance()->getDeviceInfo(resource->getHost(),
513                         std::bind([](const std::string & host, DeviceInfo & deviceInfo)
514                 {
515                     std::cout << "###Device Information received...." << std::endl;
516                     std::ostringstream out;
517                     out << "Host URI: " << host << std::endl;
518                     out << "Device name: " << deviceInfo.getName() << std::endl;
519                     out << "Device ID: " << deviceInfo.getID() << std::endl;
520                     out << "Device Spec version: " << deviceInfo.getSpecVersion() << std::endl;
521                     out << "Device dat model version: " << deviceInfo.getDataModelVersion() << std::endl;
522
523                     std::cout << out.str() << std::endl;
524                 }, std::placeholders::_1, std::placeholders::_2));
525             }
526             catch (InvalidArgsException &e)
527             {
528                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
529                           << std::endl;
530             }
531             catch (SimulatorException &e)
532             {
533                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
534                           std::endl;
535             }
536         }
537
538         void getPlatformInfo()
539         {
540             SimulatorRemoteResourceSP resource = selectResource();
541             if (!resource)
542                 return;
543
544             try
545             {
546                 SimulatorManager::getInstance()->getPlatformInfo(resource->getHost(),
547                         std::bind([](const std::string & host, PlatformInfo & platformInfo)
548                 {
549                     std::cout << "###Platform Information received...." << std::endl;
550                     std::ostringstream out;
551                     out << "Host URI: " << host << std::endl;
552                     out << "Platform ID: " << platformInfo.getPlatformID() << std::endl;
553                     out << "Platform version: " << platformInfo.getPlatformVersion() << std::endl;
554                     out << "Manufacturer name: " << platformInfo.getManufacturerName() << std::endl;
555                     out << "Manufacturer url: " << platformInfo.getManufacturerUrl() << std::endl;
556                     out << "Modle number: " << platformInfo.getModelNumber() << std::endl;
557                     out << "Date of manufacture: " << platformInfo.getDateOfManfacture() << std::endl;
558                     out << "Operatio system version: " << platformInfo.getOSVersion() << std::endl;
559                     out << "Hardware version: " << platformInfo.getHardwareVersion() << std::endl;
560                     out << "Firmware version: " << platformInfo.getFirmwareVersion() << std::endl;
561                     out << "Support url: " << platformInfo.getSupportUrl() << std::endl;
562                     out << "System time: " << platformInfo.getSystemTime() << std::endl;
563
564                     std::cout << out.str() << std::endl;
565                 }, std::placeholders::_1, std::placeholders::_2));
566             }
567             catch (InvalidArgsException &e)
568             {
569                 std::cout << "InvalidArgsException occured [code : " << e.code()
570                           << " Detail: " << e.what() << "]" << std::endl;
571             }
572             catch (SimulatorException &e)
573             {
574                 std::cout << "SimulatorException occured [code : " << e.code()
575                           << " Detail: " << e.what() << "]" << std::endl;
576             }
577         }
578
579     private:
580         std::recursive_mutex m_mutex;
581         std::map<std::string, SimulatorRemoteResourceSP> m_resList;
582 };
583
584 void printMainMenu()
585 {
586     std::cout << "############### MAIN MENU###############" << std::endl;
587     std::cout << "1. Client Controller Test" << std::endl;
588     std::cout << "2. Set Logger" << std::endl;
589     std::cout << "3. Help" << std::endl;
590     std::cout << "0. Exit" << std::endl;
591     std::cout << "######################################" << std::endl;
592 }
593
594 void setLogger()
595 {
596     std::cout << "1. Default console logger" << std::endl;
597     std::cout << "2. Default file logger" << std::endl;
598     std::cout << "3. custom logger" << std::endl;
599
600     int choice = -1;
601     std::cin >> choice;
602     if (choice <= 0 || choice > 3)
603     {
604         std::cout << "Invalid selection !" << std::endl;
605         return;
606     }
607
608     switch (choice)
609     {
610         case 1:
611             {
612                 if (false == SimulatorManager::getInstance()->setConsoleLogger())
613                     std::cout << "Failed to set the default console logger" << std::endl;
614             }
615             break;
616
617         case 2:
618             {
619                 std::string filePath;
620                 std::cout << "Enter the file path (without file name) : ";
621                 std::cin >> filePath;
622                 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
623                     std::cout << "Failed to set default file logger" << std::endl;
624             }
625             break;
626
627         case 3:
628             SimulatorManager::getInstance()->setLogger(gAppLogger);
629     }
630 }
631
632 int main(void)
633 {
634     ClientController clientController;
635     printMainMenu();
636     bool cont = true;
637     while (cont == true)
638     {
639         int choice = -1;
640         std::cout << "Enter your choice: ";
641         std::cin >> choice;
642         if (choice < 0 || choice > 5)
643         {
644             std::cout << "Invaild choice !" << std::endl; continue;
645         }
646
647         switch (choice)
648         {
649             case 1: clientController.startTest();
650                 std::cout << "Welcome back to main menu !" << std::endl;
651                 break;
652
653             case 2: setLogger(); break;
654
655             case 3: printMainMenu(); break;
656
657             case 0: cont = false;
658         }
659     }
660
661     std::cout << "Terminating test !!!" << std::endl;
662 }