Merge branch 'master' into simulator
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / linux / SampleResourceServer.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 "PrimitiveResource.h"
22 #include "RCSResourceObject.h"
23 #include "OCPlatform.h"
24 #include "OCApi.h"
25
26 using namespace std;
27 using namespace OC;
28 using namespace OC::OCPlatform;
29 using namespace OIC::Service;
30
31 std::string resourceUri = "/a/TempSensor";
32 std::string resourceType = "core.TemperatureSensor";
33 std::string resourceInterface = "oic.if.";
34 std::string attributeKey = "Temperature";
35 RCSResourceObject::Ptr server;
36
37 //display the menu on the screen
38 void displayMenu()
39 {
40     std::cout << "=====================================================================\n\n";
41     std::cout << "   1 - Creation of Resource [Auto control for requests]" << std::endl;
42     std::cout << "   2 - Creation of Resource [Developer control for Get and Set requests]" <<std::endl;
43     std::cout << "   3 - Quit" << std::endl;
44     std::cout << "=====================================================================\n\n";
45 }
46
47 //hander for get request (if developer choose second option for resource Creation)
48 RCSGetResponse RequestHandlerForGet(const RCSRequest &request,
49         RCSResourceAttributes &attrs)
50 {
51     cout << "Recieved a Get request from Client" << std::endl;
52     RCSResourceObject::LockGuard lock(*server);
53     RCSResourceAttributes attr = server->getAttributes();
54     RCSResourceAttributes::const_iterator iter = attr.begin();
55     cout << "\nSending response to Client : " << std::endl;
56     for (unsigned int i = 0; i < attr.size(); ++i)
57     {
58         std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
59         ++iter;
60     }
61     return RCSGetResponse::create(attr);
62 }
63
64 //hander for set request (if developer choose second option for resource Creation)
65 RCSSetResponse RequestHandlerForSet(const RCSRequest &request,
66         RCSResourceAttributes &attrs)
67 {
68     cout << "Recieved a Set request from Client" << std::endl;
69     RCSResourceAttributes::const_iterator iter = attrs.begin();
70     for (unsigned int i = 0; i < attrs.size(); ++i)
71     {
72         std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
73         ++iter;
74     }
75     iter = attrs.begin();
76     cout << "\n\nSending response to Client : " << std::endl;
77     for (unsigned int i = 0; i < attrs.size(); ++i)
78     {
79         std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
80         ++iter;
81     }
82     return RCSSetResponse::create(attrs);
83 }
84
85 int main(void)
86 {
87
88     int userInput;
89     int initialTemperature = 0;
90     int temperatureInput;
91     bool flag = true;
92     startPresence(3);
93
94     displayMenu();
95
96     //userInput for creation of Resource
97     std::cin >> userInput;
98     std::cin.clear();
99     std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
100     try
101     {
102         while (flag)
103         {
104
105             switch (userInput)
106             {
107
108                 case 1:
109                //Creation of Resource & Auto control for all requests from Client
110                     {
111                         //creation of Resource
112                         server = RCSResourceObject::Builder(resourceUri, resourceType,
113                                                          resourceInterface).setDiscoverable(true).setObservable(false).build();
114                         std::cout << "Resource created successfully " << std::endl;
115
116                         //setting attribute for the Created Resource
117                         server->setAttribute(attributeKey, initialTemperature);
118                         flag = false;
119                         break;
120                     }
121
122                 case 2:
123             //Creation of Resource & setting get and set handler for handling get and set request from client in application
124                     {
125                         server = RCSResourceObject::Builder(resourceUri, resourceType,
126                                                          resourceInterface).setDiscoverable(true).setObservable(false).build();
127                         std::cout << "Resource created successfully " << std::endl;
128
129                         //setting attribute for the Created Resource
130                         server->setAttribute(attributeKey, initialTemperature);
131
132                         //setting handler for handling get request from the client
133                         server->setGetRequestHandler(RequestHandlerForGet);
134
135                         //  setting handler for handling set request from the client
136                         server->setSetRequestHandler(RequestHandlerForSet);
137                         flag = false;
138                         break;
139                     }
140                 case 3:
141                     return 0;
142                 default :
143                     std::cout << "Invalid Input" << std::endl;
144                     break;
145             }
146         }
147
148         while (true)
149         {
150             bool end = false;
151             cout << endl;
152             cout << "========================================================" << endl;
153             cout << "1. Increase Temperature by 10 degree" << endl;
154             cout << "2. Decrease Temperature by 10 degree" << endl;
155             cout << "3. Stop the Sensor" << endl;
156             cout << "========================================================" << endl;
157
158             //user Input for increasing/decreasing the temperature
159             cin >> temperatureInput;
160             if (std::cin.fail())
161             {
162                 std::cin.clear();
163                 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
164                 std::cout << "Invalid input type, please try again" << std::endl;
165                 continue;
166             }
167
168             switch (temperatureInput)
169             {
170                 case 1:
171                     {
172                         RCSResourceObject::LockGuard lock(*server);
173                         RCSResourceAttributes attrs = server->getAttributes();
174
175                         attrs[attributeKey] =  (server->getAttribute<int>(attributeKey)  + 10);
176                         server->setAttribute(attributeKey, attrs[attributeKey]);
177                         cout << "\nTemperature increased by 10 degree" << endl;
178
179                         //getting the current attribute and priniting it on the console
180                         attrs = server->getAttributes();
181                         cout << "\nCurrent Temperature : ";
182                         RCSResourceAttributes::const_iterator iter = attrs.begin();
183                         for (unsigned int i = 0; i < attrs.size(); ++i)
184                         {
185                             std::cout << iter->value().toString() << std::endl;
186                             ++iter;
187                         }
188                         break;
189                     }
190                 case 2:
191                     {
192                         RCSResourceObject::LockGuard lock(*server);
193                         RCSResourceAttributes attrs = server->getAttributes();
194                         attrs[attributeKey] =  (server->getAttribute<int>(attributeKey)  - 10);
195                         server->setAttribute(attributeKey, attrs[attributeKey]);
196                         cout << "\nTemperature decreased by 10 degree" << endl;
197
198                         //getting the current attribute and priniting it on the console
199                         attrs = server->getAttributes();
200                         cout << "\nCurrent Temperature : ";
201                         RCSResourceAttributes::const_iterator iter = attrs.begin();
202                         for (unsigned int i = 0; i < attrs.size(); ++i)
203                         {
204                             std::cout << iter->value().toString() << std::endl;
205                             ++iter;
206                         }
207                         break;
208                     }
209                 case 3:
210                     {
211                         cout << "Stopping the Resource" << endl;
212                         end = true;
213                         break;
214                     }
215                 default:
216                     {
217                         cout << "Invalid input. Please try again." << endl;
218                         break;
219                     }
220             }
221             if (end == true)
222             {
223                 server=NULL;
224                 break;
225             }
226         }
227     }
228     catch (exception &e)
229     {
230         cout << "main exception  : " << e.what() << endl;
231     }
232 }
233
234