1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "condition_variable"
25 #include "RCSDiscoveryManager.h"
26 #include "RCSRemoteResourceObject.h"
27 #include "RCSResourceAttributes.h"
28 #include "RCSAddress.h"
30 #include "OCPlatform.h"
33 using namespace OIC::Service;
35 constexpr int CORRECT_INPUT = 1;
36 constexpr int INCORRECT_INPUT = 2;
37 constexpr int QUIT_INPUT = 3;
39 std::shared_ptr<RCSRemoteResourceObject> resource;
41 const std::string defaultKey = "Temperature";
42 const std::string resourceType = "?rt=core.TemperatureSensor";
43 const std::string targetUri = OC_RSRVD_WELL_KNOWN_URI + resourceType;
46 std::condition_variable cond;
48 void startMonitoring();
49 void startMonitoring();
50 void stopMonitoring();
51 void getAttributeFromRemoteServer();
52 void setAttributeToRemoteServer();
53 void startCachingWithoutCallback();
54 void startCachingWithCallback();
55 void getResourceCacheState();
56 void getCachedAttributes();
57 void getCachedAttribute();
66 START_CACHING_NO_UPDATE,
68 GET_RESOURCE_CACHE_STATE,
69 GET_CACHED_ATTRIBUTES,
76 typedef void(*ClientMenuHandler)();
77 typedef int ReturnValue;
82 ClientMenuHandler m_handler;
86 ClientMenu clientMenu[] = {
87 {Menu::START_MONITORING, startMonitoring, CORRECT_INPUT},
88 {Menu::STOP_MONITORING, stopMonitoring, CORRECT_INPUT},
89 {Menu::GET_ATTRIBUTE, getAttributeFromRemoteServer, CORRECT_INPUT},
90 {Menu::SET_ATTRIBUTE, setAttributeToRemoteServer, CORRECT_INPUT},
91 {Menu::START_CACHING_NO_UPDATE, startCachingWithoutCallback, CORRECT_INPUT},
92 {Menu::START_CACHING_UPDATE, startCachingWithCallback, CORRECT_INPUT},
93 {Menu::GET_RESOURCE_CACHE_STATE, getResourceCacheState, CORRECT_INPUT},
94 {Menu::GET_CACHED_ATTRIBUTES, getCachedAttributes, CORRECT_INPUT},
95 {Menu::GET_CACHED_ATTRIBUTE, getCachedAttribute, CORRECT_INPUT},
96 {Menu::STOP_CACHING, stopCaching, CORRECT_INPUT},
97 {Menu::QUIT, [](){}, QUIT_INPUT},
98 {Menu::END_OF_MENU, nullptr, INCORRECT_INPUT}
101 void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
103 std::cout << "onResourceDiscovered callback" << std::endl;
105 std::string resourceURI = foundResource->getUri();
106 std::string hostAddress = foundResource->getAddress();
108 std::cout << "\t\tResource URI : " << resourceURI << std::endl;
109 std::cout << "\t\tResource Host : " << hostAddress << std::endl;
111 resource = foundResource;
116 void onResourceStateChanged(const ResourceState& resourceState)
118 std::cout << "onResourceStateChanged callback" << std::endl;
120 switch(resourceState)
122 case ResourceState::NONE:
123 std::cout << "\tState changed to : NOT_MONITORING" << std::endl;
126 case ResourceState::ALIVE:
127 std::cout << "\tState changed to : ALIVE" << std::endl;
130 case ResourceState::REQUESTED:
131 std::cout << "\tState changed to : REQUESTED" << std::endl;
134 case ResourceState::LOST_SIGNAL:
135 std::cout << "\tState changed to : LOST_SIGNAL" << std::endl;
139 case ResourceState::DESTROYED:
140 std::cout << "\tState changed to : DESTROYED" << std::endl;
145 void onCacheUpdated(const RCSResourceAttributes& attributes)
147 std::cout << "onCacheUpdated callback" << std::endl;
149 if (attributes.empty())
151 std::cout << "\tAttribute is Empty" << std::endl;
155 for(const auto& attr : attributes)
157 std::cout << "\tkey : " << attr.key() << std::endl
158 << "\tvalue : " << attr.value().toString() << std::endl;
162 void onRemoteAttributesReceivedCallback(const RCSResourceAttributes& attributes)
164 std::cout << "onRemoteAttributesReceivedCallback callback" << std::endl;
166 if (attributes.empty())
168 std::cout << "\tAttribute is Empty" << std::endl;
172 for(const auto& attr : attributes)
174 std::cout << "\tkey : " << attr.key() << std::endl
175 << "\tvalue : " << attr.value().toString() << std::endl;
181 std::cout << std::endl;
182 std::cout << "1 :: Start Monitoring" << std::endl;
183 std::cout << "2 :: Stop Monitoring" << std::endl;
184 std::cout << "3 :: Get Attribute" << std::endl;
185 std::cout << "4 :: Set Attribute" << std::endl;
186 std::cout << "5 :: Start Caching (No update to Application)" << std::endl;
187 std::cout << "6 :: Start Caching (Update the application when data change)"<< std::endl;
188 std::cout << "7 :: Get Resource cache State" << std::endl;
189 std::cout << "8 :: Get Cached Attributes" << std::endl;
190 std::cout << "9 :: Get Cached Attribute" << std::endl;
191 std::cout << "10 :: Stop Caching" << std::endl;
192 std::cout << "11 :: Stop Server" << std::endl;
195 int processUserInput()
198 std::cin >> userInput;
202 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
208 void startMonitoring()
210 if (!resource->isMonitoring())
212 resource->startMonitoring(&onResourceStateChanged);
213 std::cout << "\tHosting Started..." << std::endl;
217 std::cout << "\tAlready Started..." << std::endl;
221 void stopMonitoring()
223 if (resource->isMonitoring())
225 resource->stopMonitoring();
226 std::cout << "\tHosting stopped..." << std::endl;
230 std::cout << "\tHosting not started..." << std::endl;
234 void getAttributeFromRemoteServer()
236 resource->getRemoteAttributes(&onRemoteAttributesReceivedCallback);
239 void setAttributeToRemoteServer()
244 RCSResourceAttributes setAttribute;
246 std::cout << "\tEnter the Key you want to set : ";
248 std::cout << "\tEnter the value you want to set :";
251 setAttribute[key] = value;
252 resource->setRemoteAttributes(setAttribute,
253 &onRemoteAttributesReceivedCallback);
256 void startCaching(std::function <void (const RCSResourceAttributes&)>cb)
258 if (!resource->isCaching())
260 if(cb) resource->startCaching(&onCacheUpdated);
262 else resource->startCaching();
264 std::cout << "\tCaching Started..." << std::endl;
268 std::cout << "\tAlready Started Caching..." << std::endl;
272 void startCachingWithoutCallback()
274 startCaching(nullptr);
277 void startCachingWithCallback()
279 startCaching(onCacheUpdated);
282 void getResourceCacheState()
284 switch(resource->getCacheState())
286 case CacheState::READY:
287 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::READY" << std::endl;
290 case CacheState::UNREADY:
291 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::UNREADY" << std::endl;
294 case CacheState::LOST_SIGNAL:
295 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::LOST_SIGNAL" << std::endl;
298 case CacheState::NONE:
299 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::NONE" << std::endl;
307 void getCachedAttributes()
311 if (resource->getCachedAttributes().empty())
313 std::cout << "\tReceived cached attribute is empty" << std::endl;
317 for(const auto& attr : resource->getCachedAttributes())
319 std::cout << "\tkey : " << attr.key() << std::endl
320 << "\tvalue : " << attr.value().toString() << std::endl;
324 catch (const BadRequestException& e)
326 std::cout << "Exception in getCachedAttributes : " << e.what() << std::endl;
330 void getCachedAttribute()
334 std::cout << "\tkey : " << defaultKey << std::endl
335 << "\tvalue : " << resource->getCachedAttribute(defaultKey).get< int >()
338 catch (const BadRequestException& e)
340 std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl;
342 catch (const BadGetException& e)
344 std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl;
350 if(resource->isCaching())
352 resource->stopCaching();
353 std::cout << "\tCaching stopped..." << std::endl;
357 std::cout << "\tCaching not started..." << std::endl;
361 int selectClientMenu(int selectedMenu)
363 for(int i = 0; clientMenu[i].m_menu != Menu::END_OF_MENU; i++)
365 if(clientMenu[i].m_menu == selectedMenu)
367 clientMenu[i].m_handler();
368 return clientMenu[i].m_result;
372 std::cout << "Invalid input, please try again" << std::endl;
374 return INCORRECT_INPUT;
383 if(selectClientMenu(processUserInput()) == QUIT_INPUT) break;
387 void platFormConfigure()
389 PlatformConfig config
391 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
393 OCPlatform::Configure(config);
396 bool discoverResource()
398 std::cout << "Wait 2 seconds until discovered." << std::endl;
400 RCSDiscoveryManager::getInstance()->discoverResource(RCSAddress::multicast(), targetUri,
401 &onResourceDiscovered);
403 std::unique_lock<std::mutex> lck(mtx);
404 cond.wait_for(lck,std::chrono::seconds(2));
406 return resource != nullptr;
413 if (!discoverResource())
415 std::cout << "Can't discovered Server... Exiting the Client." << std::endl;
423 catch (const std::exception& e)
425 std::cout << "main exception : " << e.what() << std::endl;
428 std::cout << "Stopping the Client" << std::endl;