1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "RCSResourceObject.h"
22 #include "RCSRequest.h"
23 #include "RCSSeparateResponse.h"
25 #include "OCPlatform.h"
27 using namespace OC::OCPlatform;
28 using namespace OIC::Service;
30 constexpr int RESOURCE_TEMP = 1;
31 constexpr int RESOURCE_LIGHT = 2;
33 RCSResourceObject::Ptr g_resource;
35 void handleRequestWithDelay(int delayInMillis, const RCSRequest& request,
36 const RCSResourceAttributes& requestedAttrs)
38 std::this_thread::sleep_for(std::chrono::milliseconds{ delayInMillis });
40 if (!requestedAttrs.empty())
42 auto resObject = request.getResourceObject().lock();
44 RCSResourceObject::LockGuard lock{ resObject };
45 auto& resAttrs = resObject->getAttributes();
46 for (const auto& kv : requestedAttrs)
48 if (resAttrs.contains(kv.key()))
50 resAttrs[kv.key()] = kv.value();
55 RCSSeparateResponse(request).set();
57 std::cout << "Separate response is set!" << std::endl;
60 void doSeparateResponse(const RCSRequest& request, const RCSResourceAttributes& requestedAttrs)
62 int delay = std::rand() % 3000 + 500;
64 std::cout << "Response will be sent in " << delay << "ms" << std::endl;
66 std::thread(handleRequestWithDelay, delay, request, requestedAttrs).detach();
69 int processUserInput(int min, int max)
81 if(min <= input && input <= max)
88 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
90 std::cout << "Invalid Input, please try again\n";
94 void printAttributes(const RCSResourceAttributes& attrs)
96 for(const auto& attr : attrs)
98 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
99 << attr.value().toString() << std::endl;
103 RCSGetResponse requestHandlerForGet(const RCSRequest& request, RCSResourceAttributes&)
105 std::cout << "Received a Get request from Client" << std::endl;
107 doSeparateResponse(request, RCSResourceAttributes{ });
109 return RCSGetResponse::separate();
112 RCSSetResponse requestHandlerForSet(const RCSRequest& request, RCSResourceAttributes& attrs)
114 std::cout << "Received a Set request from Client" << std::endl;
115 printAttributes(attrs);
117 doSeparateResponse(request, attrs);
119 return RCSSetResponse::separate();
122 void initServer(const std::string& resourceUri, const std::string& resourceType)
124 g_resource = RCSResourceObject::Builder(resourceUri, resourceType, "oic.if.").build();
127 void selectResourceType()
129 std::cout << "========================================================\n";
130 std::cout << "Select Resource Type \n";
131 std::cout << RESOURCE_TEMP << ". Temperature \n";
132 std::cout << RESOURCE_LIGHT << ". Light \n";
133 std::cout << "========================================================\n";
135 int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT);
137 switch (resourceType)
140 initServer("/a/TempSensor", "oic.r.temperaturesensor");
144 initServer("/a/light", "oic.r.light");
148 g_resource->setGetRequestHandler(requestHandlerForGet);
149 g_resource->setSetRequestHandler(requestHandlerForSet);
156 selectResourceType();
160 std::cout << "Can't select Resource Type" << std::endl;
164 std::cout << "Resource successfully created!" << std::endl;
166 std::cout << "Press '1' to stop the process" << std::endl;
168 while (std::cin.get() != '1');
170 std::cout << "Stopping the server" << std::endl;