[IOT-1538] Add support for Protocol-Independent ID
[platform/upstream/iotivity.git] / service / simulator / examples / server / simulator_server.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 std::vector<SimulatorSingleResourceSP> g_singleResources;
24 std::vector<SimulatorCollectionResourceSP> g_collectionResources;
25
26 class AppLogger : public ILogger
27 {
28     public:
29         void write(std::string time, ILogger::Level level, std::string message)
30         {
31             std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " "
32                       << message;
33         }
34 };
35 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
36
37 int selectResource()
38 {
39     if (0 == g_singleResources.size())
40     {
41         std::cout << "No resouces!" << std::endl;
42         return -1;
43     }
44
45     int index = 1;
46     for (auto &resource : g_singleResources)
47     {
48         std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
49     }
50
51     int choice = -1;
52     std::cout << "Choose the resource: ";
53     std::cin >> choice;
54
55     if (choice < 1 || choice > index - 1)
56     {
57         std::cout << "Invalid choice !" << std::endl;
58         choice = -1;
59     }
60
61     return choice;
62 }
63
64 void simulateResource()
65 {
66     try
67     {
68         // Resource model change callback
69         SimulatorResource::ResourceModelUpdateCallback modelChangeCB =
70             [](const std::string & uri, const SimulatorResourceModel & resModel)
71         {
72             std::cout << "[callback] Resource model is changed URI: " << uri.c_str() << std::endl;
73             std::cout << "#### Modified attributes are ####" << std::endl;
74             std::cout << "#### Updated resource model ####" << std::endl;
75             std::cout << resModel.asString() << std::endl;
76             std::cout << "########################" << std::endl;
77         };
78
79         // Observer added/removed callback
80         SimulatorResource::ObserverCallback observerCB =
81             [] (const std::string & uri, ObservationStatus state, const ObserverInfo & observerInfo)
82         {
83             std::cout << "[callback] Observer notification received..." << uri << std::endl;
84
85             std::ostringstream out;
86             out << "ID:  " << (int) observerInfo.id << std::endl;
87             out << " [address: " << observerInfo.address << " port: " << observerInfo.port
88                 << "]" << std::endl;
89             out << "State: " << ((state == ObservationStatus::REGISTER) ? "REGISTER" : "UNREGISTER") <<
90                 std::endl;
91             std::cout << out.str();
92         };
93
94         // Get the RAML file path from user
95         std::string configPath;
96         std::cout << "Enter RAML path: ";
97         std::cin >> configPath;
98
99         SimulatorResourceSP resource =
100             SimulatorManager::getInstance()->createResource(configPath);
101
102         // Add resource to appropriate list
103         if (SimulatorResource::Type::SINGLE_RESOURCE == resource->getType())
104         {
105             std::cout << "Single type resource created [URI:  " << resource->getURI() << " ]" << std::endl;
106             SimulatorSingleResourceSP singleRes =
107                 std::dynamic_pointer_cast<SimulatorSingleResource>(resource);
108             if (!singleRes)
109             {
110                 std::cout << "Error occured while converting SimulatorResource to SimulatorSingleResource!" << std::endl;
111                 return;
112             }
113
114             singleRes->setModelChangeCallback(modelChangeCB);
115             singleRes->setObserverCallback(observerCB);
116             g_singleResources.push_back(singleRes);
117         }
118         else
119         {
120             std::cout << "Collection type resource created [URI:  " << resource->getURI() << " ]" << std::endl;
121             SimulatorCollectionResourceSP collectionRes =
122                 std::dynamic_pointer_cast<SimulatorCollectionResource>(resource);
123             if (!collectionRes)
124             {
125                 std::cout << "Error occured while converting SimulatorResource to SimulatorCollectionResource!" << std::endl;
126                 return;
127             }
128
129             collectionRes->setObserverCallback(observerCB);
130             g_collectionResources.push_back(collectionRes);
131         }
132     }
133     catch (InvalidArgsException &e)
134     {
135         std::cout << "InvalidArgsException occured [code : " << e.code() << " Details: "
136                   << e.what() << "]" << std::endl;
137     }
138     catch (SimulatorException &e)
139     {
140         std::cout << "SimulatorException occured [code : " << e.code() << " Details: "
141                   << e.what() << "]" << std::endl;
142     }
143 }
144
145 void displayResource()
146 {
147     int index = selectResource();
148     if (-1 == index)
149         return;
150
151     SimulatorSingleResourceSP resource = g_singleResources[index - 1];
152
153     std::cout << "#############################" << std::endl;
154     std::cout << "Name: " << resource->getName() << std::endl;
155     std::cout << "URI: " << resource->getURI() << std::endl;
156     std::cout << "Resource type: " << resource->getResourceType() << std::endl;
157     std::cout << "Interface type:";
158     for (auto &interfaceType : resource->getInterface())
159         std::cout << " " << interfaceType << std::endl;
160
161     // Attributes
162     std::cout << "##### Representation #####" << std::endl;
163     std::cout << resource->getResourceModel().asString() << std::endl;
164     std::cout << "#############################" << std::endl;
165 }
166
167 void startResource()
168 {
169     int index = selectResource();
170     if (-1 == index)
171         return;
172
173     SimulatorSingleResourceSP resource = g_singleResources[index - 1];
174     resource->start();
175     std::cout << "Resource started!" << std::endl;
176 }
177
178 void stopResource()
179 {
180     int index = selectResource();
181     if (-1 == index)
182         return;
183
184     SimulatorSingleResourceSP resource = g_singleResources[index - 1];
185     resource->stop();
186     std::cout << "Resource stopped!" << std::endl;
187 }
188
189 void automateResourceUpdate()
190 {
191     SimulatorSingleResource::AutoUpdateCompleteCallback callback =
192         [](const std::string & uri, const int id)
193     {
194         std::cout << "Update automation is completed [URI: " << uri
195                   << "  AutomationID: " << id << "] ###" << std::endl;
196     };
197
198     int index = selectResource();
199     if (-1 == index)
200         return;
201
202     AutoUpdateType type = AutoUpdateType::ONE_TIME;
203     int choice = 0;
204     std::cout << "Press 1 if you want recurrent automation: ";
205     std::cin >> choice;
206     if (1 == choice)
207         type = AutoUpdateType::REPEAT;
208
209     try
210     {
211         int id = g_singleResources[index - 1]->startResourceUpdation(type, -1, callback);
212
213         std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
214     }
215     catch (SimulatorException &e)
216     {
217         std::cout << "SimulatorException occured [code : " << e.code() << " Details: " <<
218                   e.what() << "]" << std::endl;
219     }
220 }
221
222 void automateAttributeUpdate()
223 {
224     SimulatorSingleResource::AutoUpdateCompleteCallback callback =
225         [](const std::string & uri, const int id)
226     {
227         std::cout << "Update automation is completed [URI: " << uri
228                   << "  AutomationID: " << id << "] ###" << std::endl;
229     };
230
231     int index = selectResource();
232     if (-1 == index)
233         return;
234
235     SimulatorSingleResourceSP resource = g_singleResources[index - 1];
236     std::map<std::string, SimulatorResourceAttribute> attributes =
237         resource->getAttributes();
238     int size = 0;
239     for (auto &attributeEntry : attributes)
240     {
241         std::cout << ++size << ": " << attributeEntry.first << std::endl;
242     }
243
244     if (0 == size)
245     {
246         std::cout << "This resource doest not contain any attributes!" << std::endl;
247         return;
248     }
249
250     int choice = -1;
251     std::cout << "Select the attribute which you want to automate for updation: " <<
252               std::endl;
253     std::cin >> choice;
254     if (choice < 0 || choice > size)
255     {
256         std::cout << "Invalid selection!" << std::endl;
257         return;
258     }
259
260     int count = 0;
261     std::string attributeName;
262     for (auto &attributeEntry : attributes)
263     {
264         if (count == choice - 1)
265         {
266             attributeName = attributeEntry.first;
267             break;
268         }
269
270         count++;
271     }
272
273     AutoUpdateType type = AutoUpdateType::ONE_TIME;
274     std::cout << "Press 1 if you want recurrent automation: ";
275     std::cin >> choice;
276     if (1 == choice)
277         type = AutoUpdateType::REPEAT;
278
279     std::cout << "Requesting attribute automation for " << attributeName <<
280               std::endl;
281
282     try
283     {
284         int id = resource->startAttributeUpdation(attributeName, type, -1, callback);
285         std::cout << "startUpdateAutomation() returned succces : " << id << std::endl;
286     }
287     catch (SimulatorException &e)
288     {
289         std::cout << "SimulatorException occured [Error: " << e.code() << " Details: " <<
290                   e.what() << "]" << std::endl;
291     }
292 }
293
294 void stopAutomation()
295 {
296     int index = selectResource();
297     if (-1 == index)
298         return;
299
300     SimulatorSingleResourceSP resource = g_singleResources[index - 1];
301
302     // Select the automation to stop
303     std::vector<int> ids;
304     {
305         std::vector<int> rids = resource->getResourceUpdations();
306         std::vector<int> aids = resource->getAttributeUpdations();
307         ids.insert(ids.end(), rids.begin(), rids.end());
308         ids.insert(ids.end(), aids.begin(), aids.end());
309     }
310
311     if (!ids.size())
312     {
313         std::cout << "No automation operation is going on this resource right now!" <<
314                   std::endl;
315         return;
316     }
317
318     for (auto &id : ids)
319     {
320         std::cout <<  id  << " ";
321         resource->stopUpdation(id);
322     }
323 }
324
325 void getObservers()
326 {
327     int index = selectResource();
328     if (-1 == index)
329         return;
330
331     SimulatorSingleResourceSP resource = g_singleResources[index - 1];
332
333     std::vector<ObserverInfo> observersList = resource->getObservers();
334
335     std::cout << "##### Number of Observers [" << observersList.size() << "]" << std::endl;
336     for (auto &observerInfo : observersList)
337     {
338         std::cout << " ID :  " << (int) observerInfo.id << " [address: " <<
339                   observerInfo.address << " port: " << observerInfo.port << "]" << std::endl;
340     }
341     std::cout << "########################" << std::endl;
342 }
343
344 void printMainMenu()
345 {
346     std::cout << "############### MAIN MENU###############" << std::endl;
347     std::cout << "1. Simulate resource" << std::endl;
348     std::cout << "2. Display resource information" << std::endl;
349     std::cout << "3. Start resource" << std::endl;
350     std::cout << "4. Stop resource" << std::endl;
351     std::cout << "5. Automate resource update" << std::endl;
352     std::cout << "6. Automate attributes update" << std::endl;
353     std::cout << "7. Stop Automation" << std::endl;
354     std::cout << "8. Get Observers of a resource" << std::endl;
355     std::cout << "9. Set Logger" << std::endl;
356     std::cout << "10. Set Device Info" << std::endl;
357     std::cout << "11. Set Platform Info" << std::endl;
358     std::cout << "12. Add Interface" << std::endl;
359     std::cout << "13. Help" << std::endl;
360     std::cout << "0. Exit" << std::endl;
361     std::cout << "######################################" << std::endl;
362 }
363
364 void setLogger()
365 {
366     std::cout << "1. Default console logger" << std::endl;
367     std::cout << "2. Default file logger" << std::endl;
368     std::cout << "3. custom logger" << std::endl;
369
370     int choice = -1;
371     std::cin >> choice;
372     if (choice <= 0 || choice > 3)
373     {
374         std::cout << "Invalid selection !" << std::endl;
375         return;
376     }
377
378     switch (choice)
379     {
380         case 1:
381             {
382                 if (false == SimulatorManager::getInstance()->setConsoleLogger())
383                     std::cout << "Failed to set the default console logger" << std::endl;
384             } break;
385         case 2:
386             {
387                 std::string filePath;
388                 std::cout << "Enter the file path (without file name) : ";
389                 std::cin >> filePath;
390                 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
391                     std::cout << "Failed to set default file logger" << std::endl;
392             } break;
393         case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
394     }
395 }
396
397 void setDeviceInfo()
398 {
399     try
400     {
401         SimulatorManager::getInstance()->setDeviceInfo("IoTivity Simulator Linux Sample",
402                                                        "c49f7cba-3b3d-490b-9d3f-d4f17a3dad26");
403         std::cout << "Setting Device Info is successful" << std::endl;
404     }
405     catch (InvalidArgsException &e)
406     {
407         std::cout << "InvalidArgsException occured [code : " << e.code() << " Details: "
408                   << e.what() << "]" << std::endl;
409     }
410     catch (SimulatorException &e)
411     {
412         std::cout << "SimulatorException occured [code : " << e.code() << " Details: "
413                   << e.what() << "]" << std::endl;
414     }
415 }
416
417 void setPlatformInfo()
418 {
419     PlatformInfo pInfo;
420     pInfo.setPlatformID("Samsung Platform Identifier");
421     pInfo.setFirmwareVersion("FirwareVersion01");
422     pInfo.setHardwareVersion("HardwareVersion01");
423     pInfo.setManufacturerName("Samsung");
424     pInfo.setManufacturerUrl("www.samsung.com");
425     pInfo.setModelNumber("Samsung Model Num01");
426     pInfo.setOSVersion("OSVersion01");
427     pInfo.setPlatformVersion("PlatformVersion01");
428     pInfo.setSupportUrl("http://www.samsung.com/support");
429     pInfo.setSystemTime("2015-09-10T11:10:30Z");
430     pInfo.setDateOfManfacture("2015-09-10T11:10:30Z");
431
432     try
433     {
434         SimulatorManager::getInstance()->setPlatformInfo(pInfo);
435         std::cout << "Setting Platform Info is successful" << std::endl;
436     }
437     catch (SimulatorException &e)
438     {
439         std::cout << "SimulatorException occured [code : " << e.code() << " Details: "
440                   << e.what() << "]" << std::endl;
441     }
442 }
443
444 void addInterface()
445 {
446
447     int index = selectResource();
448     if (-1 == index)
449         return;
450
451     SimulatorSingleResourceSP resource = g_singleResources[index - 1];
452     resource->addInterface("oic.if.s");
453     resource->addInterface("oic.if.a");
454 }
455
456 int main(int argc, char *argv[])
457 {
458     printMainMenu();
459     bool cont = true;
460     while (cont == true)
461     {
462         int choice = -1;
463         std::cout << "Enter your choice: ";
464         std::cin >> choice;
465         if (choice < 0 || choice > 14)
466         {
467             std::cout << "Invaild choice !" << std::endl; continue;
468         }
469
470         switch (choice)
471         {
472             case 1 : simulateResource(); break;
473             case 2 : displayResource(); break;
474             case 3 : startResource(); break;
475             case 4 : stopResource(); break;
476             case 5 : automateResourceUpdate(); break;
477             case 6 : automateAttributeUpdate(); break;
478             case 7 : stopAutomation(); break;
479             case 8 : getObservers(); break;
480             case 9 : setLogger(); break;
481             case 10: setDeviceInfo(); break;
482             case 11: setPlatformInfo(); break;
483             case 12: addInterface(); break;
484             case 13: printMainMenu(); break;
485             case 0: cont = false;
486         }
487     }
488
489     std::cout << "Terminating test !!!" << std::endl;
490 }