Merge branch 'master' into simulator
[platform/upstream/iotivity.git] / service / simulator / examples / client-controller / client_controller.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
23 class AppLogger : public ILogger
24 {
25     public:
26         void write(std::string time, ILogger::Level level, std::string message)
27         {
28             std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
29         }
30 };
31 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
32
33 class ClientController
34 {
35     public:
36         void startTest()
37         {
38             printMenu();
39             bool cont = true;
40             while (cont)
41             {
42                 int choice = -1;
43                 std::cout << "Enter your choice: ";
44                 std::cin >> choice;
45                 if (choice < 0 || choice > 8)
46                 {
47                     std::cout << "Invaild choice !" << std::endl; continue;
48                 }
49
50                 switch (choice)
51                 {
52                     case 1: findResource(); break;
53                     case 2: displayResource(); break;
54                     case 3: observeResource(); break;
55                     case 4: cancelObserving(); break;
56                     case 5: sendGet(); break;
57                     case 6: sendPut(); break;
58                     case 7: sendPost(); break;
59                     case 8: printMenu(); break;
60                     case 0: cont = false;
61                 }
62             }
63         }
64
65     private:
66         void printMenu()
67         {
68             std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
69             std::cout << "1. Find resource" << std::endl;
70             std::cout << "2. Display resource information" << std::endl;
71             std::cout << "3. Observe for resource change" << std::endl;
72             std::cout << "4. Cancel observation" << std::endl;
73             std::cout << "5. Send GET message" << std::endl;
74             std::cout << "6. Send PUT message" << std::endl;
75             std::cout << "7. Send POST message" << std::endl;
76             std::cout << "8: Help" << std::endl;
77             std::cout << "0. Exit" << std::endl;
78             std::cout << "###################################################" << std::endl;
79         }
80
81         int selectResource(std::vector<SimulatorRemoteResourcePtr> resourceList)
82         {
83             if (0 == resourceList.size())
84             {
85                 std::cout << "No resouces!" << std::endl;
86                 return -1;
87             }
88
89             int index = 1;
90             for (auto & resource : resourceList)
91             {
92                 std::cout << index++ << ": " << resource->getURI() << "[" << resource->getHost()  << "]" << std::endl;
93             }
94
95             int choice = -1;
96             std::cout << "Choose the resource: ";
97             std::cin >> choice;
98
99             if (choice < 1 || choice > index - 1)
100             {
101                 std::cout << "Invalid choice !" << std::endl;
102                 choice = -1;
103             }
104
105             return choice;
106         }
107
108         void findResource()
109         {
110             std::string resourceType;
111             std::cout << "Enter resource type : ";
112             std::cin >> resourceType;
113
114             ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
115             {
116                 std::cout << "Resource found ######" << std::endl;
117                 displayResource(resource);
118             };
119
120             SimulatorResult result = SimulatorManager::getInstance()->findResource
121                                           (resourceType, callback);
122             if (SIMULATOR_SUCCESS != result)
123                 std::cout << "SimulatorManager::findResource returned error : " << result << std::endl;
124         }
125
126         void displayResource()
127         {
128             std::vector<SimulatorRemoteResourcePtr> resourceList =
129                 SimulatorManager::getInstance()->getFoundResources();
130
131             int index = selectResource(resourceList);
132             if (-1 == index)
133                 return;
134
135             displayResource(resourceList[index - 1]);
136         }
137
138         void displayResource(SimulatorRemoteResourcePtr resource)
139         {
140             std::cout << "#############################" << std::endl;
141             std::cout << "URI: " << resource->getURI().c_str() << std::endl;
142             std::cout << "Host: " << resource->getHost().c_str() << std::endl;
143             std::cout << "Resource Types: ";
144             for (auto &type : resource->getResourceTypes())
145                 std::cout << type << " ";
146             std::cout << "\nInterface Types: ";
147             for (auto &type : resource->getResourceInterfaces())
148                 std::cout << type << " ";
149             std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable() << std::endl;
150             std::cout << "#############################" << std::endl;
151         }
152
153         void observeResource()
154         {
155             std::vector<SimulatorRemoteResourcePtr> resourceList =
156                 SimulatorManager::getInstance()->getFoundResources();
157
158             int index = selectResource(resourceList);
159             if (-1 == index)
160                 return;
161
162             SimulatorRemoteResourcePtr resource = resourceList[index - 1];
163
164             // callback implementaion
165             SimulatorRemoteResource::RepresentationChangeCallback callback =
166             [](int errorCode, const SimulatorResourceModel &rep, int seq)
167             {
168                 std::cout << "\nObserve notificatoin received ###[errorcode:  " << errorCode << " seq:  " << seq << "]" << std::endl;
169                 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
170                 for (auto & attribute : attributes)
171                 {
172                     std::cout << (attribute.second).getName() << " :  {" << std::endl;
173                     std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
174                     std::cout << "}" << std::endl;
175                 }
176                 std::cout << std::endl;
177             };
178
179             std::map <std::string, std::string> queryParams;
180             SimulatorResult result = resource->observe(SimulatorRemoteResource::OBSERVE, queryParams, callback);
181             if ( SIMULATOR_SUCCESS == result)
182                 std::cout << "Observe is successfull!" << std::endl;
183             else
184                 std::cout << "Observe is failed!error: " << result << std::endl;
185         }
186
187         void cancelObserving()
188         {
189             std::vector<SimulatorRemoteResourcePtr> resourceList =
190                 SimulatorManager::getInstance()->getFoundResources();
191
192             int index = selectResource(resourceList);
193             if (-1 == index)
194                 return;
195
196             SimulatorRemoteResourcePtr resource = resourceList[index - 1];
197             SimulatorResult result = resource->cancelObserve();
198             if ( SIMULATOR_SUCCESS == result)
199                 std::cout << "Cancelling observe is successfull!" << std::endl;
200             else
201                 std::cout << "Cancelling observe is failed!error: " << result << std::endl;
202         }
203
204         void sendGet()
205         {
206             std::vector<SimulatorRemoteResourcePtr> resourceList =
207                 SimulatorManager::getInstance()->getFoundResources();
208
209             int index = selectResource(resourceList);
210             if (-1 == index)
211                 return;
212
213             SimulatorRemoteResourcePtr resource = resourceList[index - 1];
214
215             // callback implementaion
216             SimulatorRemoteResource::ResponseCallback callback =
217             [](int errorCode, const SimulatorResourceModel & rep)
218             {
219                 std::cout << "\nGET Response received ### [errorcode:  " << errorCode << "]" << std::endl;
220                 std::cout << "Representation is: " << std::endl;
221                 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
222                 for (auto & attribute : attributes)
223                 {
224                     std::cout << (attribute.second).getName() << " :  {" << std::endl;
225                     std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
226                     std::cout << "}" << std::endl;
227                 }
228                 std::cout << std::endl;
229             };
230
231             std::map <std::string, std::string> queryParams;
232             SimulatorResult result = resource->get(queryParams, callback);
233             if ( SIMULATOR_SUCCESS == result)
234                 std::cout << "GET is successfull!" << std::endl;
235             else
236                 std::cout << "GET is failed!error: " << result << std::endl;
237         }
238
239         void sendPut()
240         {
241             std::vector<SimulatorRemoteResourcePtr> resourceList =
242                 SimulatorManager::getInstance()->getFoundResources();
243
244             int index = selectResource(resourceList);
245             if (-1 == index)
246                 return;
247
248             SimulatorRemoteResourcePtr resource = resourceList[index - 1];
249
250             // callback implementaion
251             SimulatorRemoteResource::ResponseCallback callback =
252             [](int errorCode, const SimulatorResourceModel & rep)
253             {
254                 std::cout << "\nPUT Response received ![errorcode:  " << errorCode << "]" << std::endl;
255                 std::cout << "Representation is: " << std::endl;
256                 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
257                 for (auto & attribute : attributes)
258                 {
259                     std::cout << (attribute.second).getName() << " :  {" << std::endl;
260                     std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
261                     std::cout << "}" << std::endl;
262                 }
263                 std::cout << std::endl;
264             };
265
266             std::map <std::string, std::string> queryParams;
267             SimulatorResourceModel rep;
268             rep.addAttribute("power", "off");
269             rep.addAttribute("intensity", 5);
270
271             SimulatorResult result = resource->put(rep, queryParams, callback);
272             if ( SIMULATOR_SUCCESS == result)
273                 std::cout << "PUT is successfull!" << std::endl;
274             else
275                 std::cout << "PUT is failed!error: " << result << std::endl;
276         }
277
278         void sendPost()
279         {
280             std::vector<SimulatorRemoteResourcePtr> resourceList =
281                 SimulatorManager::getInstance()->getFoundResources();
282
283             int index = selectResource(resourceList);
284             if (-1 == index)
285                 return;
286
287             SimulatorRemoteResourcePtr resource = resourceList[index - 1];
288
289             // callback implementaion
290             SimulatorRemoteResource::ResponseCallback callback =
291             [](int errorCode, const SimulatorResourceModel & rep)
292             {
293                 std::cout << "\nPOST Response received ![errorcode:  " << errorCode << "]" << std::endl;
294                 std::cout << "Representation is: " << std::endl;
295                 std::map<std::string, SimulatorResourceModel::Attribute> attributes = rep.getAttributes();
296                 for (auto & attribute : attributes)
297                 {
298                     std::cout << (attribute.second).getName() << " :  {" << std::endl;
299                     std::cout << "value: " << (attribute.second).valueToString().c_str() << std::endl;
300                     std::cout << "}" << std::endl;
301                 }
302                 std::cout << std::endl;
303             };
304
305             std::map <std::string, std::string> queryParams;
306             SimulatorResourceModel rep;
307             rep.addAttribute("power", "on");
308             rep.addAttribute("intensity", 7);
309
310             SimulatorResult result = resource->post(rep, queryParams, callback);
311             if ( SIMULATOR_SUCCESS == result)
312                 std::cout << "POST is successfull!" << std::endl;
313             else
314                 std::cout << "POST is failed!error: " << result << std::endl;
315         }
316 };
317
318 void printMainMenu()
319 {
320     std::cout << "############### MAIN MENU###############" << std::endl;
321     std::cout << "1. Client Controller Test" << std::endl;
322     std::cout << "2. Set Logger" << std::endl;
323     std::cout << "3. Help" << std::endl;
324     std::cout << "0. Exit" << std::endl;
325     std::cout << "######################################" << std::endl;
326 }
327
328 void setLogger()
329 {
330     std::cout << "1. Default console logger" << std::endl;
331     std::cout << "2. Default file logger" << std::endl;
332     std::cout << "3. custom logger" << std::endl;
333
334     int choice = -1;
335     std::cin >> choice;
336     if (choice <= 0 || choice > 3)
337     {
338         std::cout << "Invalid selection !" << std::endl;
339         return;
340     }
341
342     switch (choice)
343     {
344         case 1:
345             {
346                 if (false == SimulatorManager::getInstance()->setDefaultConsoleLogger())
347                     std::cout << "Failed to set the default console logger" << std::endl;
348             } break;
349         case 2:
350             {
351                 std::string filePath;
352                 std::cout << "Enter the file path (without file name) : ";
353                 std::cin >> filePath;
354                 if (false == SimulatorManager::getInstance()->setDefaultFileLogger(filePath))
355                     std::cout << "Failed to set default file logger" << std::endl;
356             } break;
357         case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
358     }
359 }
360
361 int main(void)
362 {
363     ClientController clientController;
364     printMainMenu();
365     bool cont = true;
366     while (cont == true)
367     {
368         int choice = -1;
369         std::cout << "Enter your choice: ";
370         std::cin >> choice;
371         if (choice < 0 || choice > 3)
372         {
373             std::cout << "Invaild choice !" << std::endl; continue;
374         }
375
376         switch (choice)
377         {
378             case 1: clientController.startTest();
379                 std::cout << "Welcome back to main menu !" << std::endl;
380                 break;
381             case 2: setLogger(); break;
382             case 3: printMainMenu(); break;
383             case 0: cont = false;
384         }
385     }
386
387     std::cout << "Terminating test !!!" << std::endl;
388 }