Imported Upstream version 0.9.2
[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 OC;
27 using namespace OC::OCPlatform;
28 using namespace OIC::Service;
29
30 constexpr int DEFALUT_VALUE = 0;
31
32 constexpr int DEFALUT_SERVER = 1;
33 constexpr int CUSTOM_SERVER = 2;
34 constexpr int STOP = 3;
35
36 constexpr int INCREASE_TEMPERATURE = 1;
37 constexpr int DECREASE_TEMPERATURE = 2;
38 constexpr int STOP_SENSOR = 3;
39
40 constexpr int CORRECT_INPUT = 1;
41 constexpr int INCORRECT_INPUT = 2;
42 constexpr int QUIT = 3;
43
44 std::string resourceUri = "/a/TempSensor";
45 std::string resourceType = "core.TemperatureSensor";
46 std::string resourceInterface = "oic.if.";
47 std::string attributeKey = "Temperature";
48
49 RCSResourceObject::Ptr server;
50
51 enum class Control{
52     INCREASE,
53     DECREASE
54 };
55
56 void displayMenu()
57 {
58     std::cout << "====================================================================="
59               << std::endl;
60     std::cout << "   1 - Creation of Resource [Auto control for requests]" << std::endl;
61     std::cout << "   2 - Creation of Resource [Developer control for Get and Set requests]"
62               << std::endl;
63     std::cout << "   3 - Quit" << std::endl;
64     std::cout << "====================================================================="
65               << std::endl;
66 }
67
68 void displayControlTemperatureMenu()
69 {
70     std::cout << "========================================================" << std::endl;
71     std::cout << "1. Increase Temperature by 10 degree" << std::endl;
72     std::cout << "2. Decrease Temperature by 10 degree" << std::endl;
73     std::cout << "3. Stop the Sensor" << std::endl;
74     std::cout << "========================================================" << std::endl;
75 }
76
77 void printAttribute(const RCSResourceAttributes& attrs)
78 {
79     for(const auto& attr : attrs)
80     {
81         std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
82                   << attr.value().toString() << std::endl;
83     }
84 }
85
86 //hander for get request (if developer choose second option for resource Creation)
87 RCSGetResponse requestHandlerForGet(const RCSRequest& request,
88         RCSResourceAttributes& attrs)
89 {
90     std::cout << "Recieved a Get request from Client" << std::endl;
91
92     RCSResourceObject::LockGuard lock(*server);
93     RCSResourceAttributes attributes = server->getAttributes();
94
95     std::cout << "\nSending response to Client : " << std::endl;
96     printAttribute(attributes);
97
98     return RCSGetResponse::defaultAction();
99 }
100
101 //hander for set request (if developer choose second option for resource Creation)
102 RCSSetResponse requestHandlerForSet(const RCSRequest& request,
103         RCSResourceAttributes& attrs)
104 {
105     std::cout << "Recieved a Set request from Client" << std::endl;
106
107     std::cout << "\n\nSending response to Client : " << std::endl;
108     RCSResourceObject::LockGuard lock(*server);
109     printAttribute(attrs);
110     return RCSSetResponse::defaultAction();
111 }
112
113 void createResource()
114 {
115     server = RCSResourceObject::Builder(resourceUri, resourceType,
116                              resourceInterface).setDiscoverable(true).setObservable(true).build();
117 }
118
119 void initServer()
120 {
121     try
122     {
123         createResource();
124     }
125     catch (const PlatformException& e)
126     {
127         std::cout << "Exception in initServer : " << e.what() << std::endl;
128     }
129
130     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
131     server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
132     server->setAttribute(attributeKey, DEFALUT_VALUE);
133 }
134
135 void changeTemperature(Control control)
136 {
137     RCSResourceObject::LockGuard lock(server);
138     if(Control::INCREASE == control)
139     {
140         server->getAttributes()[attributeKey] =
141                 server->getAttribute<int>(attributeKey) + 10;
142         std::cout << "\nTemperature increased by 10 degree" << std::endl;
143     }
144     else if(Control::DECREASE == control)
145     {
146         server->getAttributes()[attributeKey] =
147                         server->getAttribute<int>(attributeKey) - 10;
148         std::cout << "\nTemperature Decreased by 10 degree" << std::endl;
149     }
150     std::cout << "\nCurrent Temperature : "
151             << server->getAttributeValue(attributeKey).get<int>() << std::endl;
152 }
153
154 int processUserInput()
155 {
156     int userInput;
157     std::cin >> userInput;
158     if (std::cin.fail())
159     {
160         std::cin.clear();
161         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
162         return -1;
163     }
164     return userInput;
165 }
166
167 int selectServerMenu()
168 {
169     switch (processUserInput())
170     {
171         case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client.
172             initServer();
173             return CORRECT_INPUT;
174
175         case CUSTOM_SERVER:
176             // Creation of Resource & setting get and set handler for handling get and
177             // set request from client in application.
178             initServer();
179
180             server->setGetRequestHandler(requestHandlerForGet);
181             server->setSetRequestHandler(requestHandlerForSet);
182             return CORRECT_INPUT;
183         case STOP :
184             return QUIT;
185
186         default :
187             std::cout << "Invalid input, please try again" << std::endl;
188             return INCORRECT_INPUT;
189     }
190 }
191
192 int selectControlTemperatureMenu()
193 {
194    switch (processUserInput())
195    {
196        case INCREASE_TEMPERATURE:
197            changeTemperature(Control::INCREASE);
198            return CORRECT_INPUT;
199
200        case DECREASE_TEMPERATURE:
201            changeTemperature(Control::DECREASE);
202            return CORRECT_INPUT;
203
204        case STOP_SENSOR:
205            return QUIT;
206
207        default:
208            std::cout << "Invalid input. Please try again." << std::endl;
209            return INCORRECT_INPUT;
210    }
211 }
212
213 void process()
214 {
215     while(true)
216     {
217         displayMenu();
218
219         int ret = selectServerMenu();
220
221         if(ret == QUIT) return;
222         if(ret == CORRECT_INPUT) break;
223     }
224
225     while(true)
226     {
227         displayControlTemperatureMenu();
228
229         if (selectControlTemperatureMenu() == QUIT) return;
230     }
231 }
232
233 int main(void)
234 {
235     startPresence(3);
236
237     try
238     {
239         process();
240         server = NULL;
241     }
242     catch (const std::exception& e)
243     {
244         std::cout << "main exception  : " << e.what() << std::endl;
245     }
246
247     std::cout << "Stopping the Server" << std::endl;
248 }
249
250