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())) resAttrs[kv.key()] = kv.value();
52 RCSSeparateResponse(request).set();
54 std::cout << "Separate response is set!" << std::endl;
57 void doSeparateResponse(const RCSRequest& request, const RCSResourceAttributes& requestedAttrs)
59 int delay = std::rand() % 3000 + 500;
61 std::cout << "Response will be sent in " << delay << "ms" << std::endl;
63 std::thread(handleRequestWithDelay, delay, request, requestedAttrs).detach();
66 int processUserInput(int min, int max)
78 if(min <= input && input <= max) return input;
82 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
84 std::cout << "Invalid Input, please try again\n";
88 void printAttributes(const RCSResourceAttributes& attrs)
90 for(const auto& attr : attrs)
92 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
93 << attr.value().toString() << std::endl;
97 RCSGetResponse requestHandlerForGet(const RCSRequest& request, RCSResourceAttributes&)
99 std::cout << "Received a Get request from Client" << std::endl;
101 doSeparateResponse(request, RCSResourceAttributes{ });
103 return RCSGetResponse::separate();
106 RCSSetResponse requestHandlerForSet(const RCSRequest& request, RCSResourceAttributes& attrs)
108 std::cout << "Received a Set request from Client" << std::endl;
109 printAttributes(attrs);
111 doSeparateResponse(request, attrs);
113 return RCSSetResponse::separate();
116 void initServer(const std::string& resourceUri, const std::string& resourceType)
118 g_resource = RCSResourceObject::Builder(resourceUri, resourceType, "oic.if.").build();
121 void selectResourceType()
123 std::cout << "========================================================\n";
124 std::cout << "Select Resource Type \n";
125 std::cout << RESOURCE_TEMP << ". Temperature \n";
126 std::cout << RESOURCE_LIGHT << ". Light \n";
127 std::cout << "========================================================\n";
129 int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT);
131 switch (resourceType)
134 initServer("/a/TempSensor", "oic.r.temperaturesensor");
138 initServer("/a/light", "oic.r.light");
142 g_resource->setGetRequestHandler(requestHandlerForGet);
143 g_resource->setSetRequestHandler(requestHandlerForSet);
148 selectResourceType();
150 std::cout << "Resource successfully created!" << std::endl;
152 std::cout << "Press '1' to stop the process" << std::endl;
154 while (std::cin.get() != '1');
156 std::cout << "Stopping the server" << std::endl;