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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "condition_variable"
27 #include "RCSDiscoveryManager.h"
28 #include "RCSRemoteResourceObject.h"
29 #include "RCSResourceAttributes.h"
30 #include "RCSAddress.h"
31 #include "OCPlatform.h"
34 using namespace OIC::Service;
36 constexpr int CORRECT_INPUT = 1;
37 constexpr int INCORRECT_INPUT = 2;
38 constexpr int QUIT_INPUT = 3;
40 constexpr int REQUEST_TEMP = 1;
41 constexpr int REQUEST_LIGHT = 2;
43 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask = nullptr;
44 std::shared_ptr<RCSRemoteResourceObject> resource;
46 std::string defaultKey = "Temperature";
47 const std::string relativeUri = OC_RSRVD_WELL_KNOWN_URI;
48 const std::string multicastAdd = "multi";
51 std::condition_variable cond;
53 void startMonitoring();
54 void startMonitoring();
55 void stopMonitoring();
56 void getAttributeFromRemoteServer();
57 void setAttributeToRemoteServer();
58 void startCachingWithoutCallback();
59 void startCachingWithCallback();
60 void getResourceCacheState();
61 void getCachedAttributes();
62 void getCachedAttribute();
64 void discoverResource();
65 void cancelDiscovery();
66 int processUserInput();
74 START_CACHING_NO_UPDATE,
76 GET_RESOURCE_CACHE_STATE,
77 GET_CACHED_ATTRIBUTES,
86 typedef void(*ClientMenuHandler)();
87 typedef int ReturnValue;
92 ClientMenuHandler m_handler;
96 ClientMenu clientMenu[] = {
97 {Menu::START_MONITORING, startMonitoring, CORRECT_INPUT},
98 {Menu::STOP_MONITORING, stopMonitoring, CORRECT_INPUT},
99 {Menu::GET_ATTRIBUTE, getAttributeFromRemoteServer, CORRECT_INPUT},
100 {Menu::SET_ATTRIBUTE, setAttributeToRemoteServer, CORRECT_INPUT},
101 {Menu::START_CACHING_NO_UPDATE, startCachingWithoutCallback, CORRECT_INPUT},
102 {Menu::START_CACHING_UPDATE, startCachingWithCallback, CORRECT_INPUT},
103 {Menu::GET_RESOURCE_CACHE_STATE, getResourceCacheState, CORRECT_INPUT},
104 {Menu::GET_CACHED_ATTRIBUTES, getCachedAttributes, CORRECT_INPUT},
105 {Menu::GET_CACHED_ATTRIBUTE, getCachedAttribute, CORRECT_INPUT},
106 {Menu::STOP_CACHING, stopCaching, CORRECT_INPUT},
107 {Menu::DISCOVERY_RESOURCE, discoverResource, CORRECT_INPUT},
108 {Menu::CANCEL_DISCOVERY, cancelDiscovery, CORRECT_INPUT},
109 {Menu::QUIT, [](){}, QUIT_INPUT},
110 {Menu::END_OF_MENU, nullptr, INCORRECT_INPUT}
113 void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> discoveredResource)
115 std::cout << "onResourceDiscovered callback :: " << std::endl;
116 std::string resourceURI = discoveredResource->getUri();
117 std::string hostAddress = discoveredResource->getAddress();
119 std::cout << resourceURI << std::endl;
120 std::cout << hostAddress << std::endl;
122 resource = discoveredResource;
125 void onResourceStateChanged(const ResourceState& resourceState)
127 std::cout << "onResourceStateChanged callback" << std::endl;
129 switch(resourceState)
131 case ResourceState::NONE:
132 std::cout << "\tState changed to : NOT_MONITORING" << std::endl;
135 case ResourceState::ALIVE:
136 std::cout << "\tState changed to : ALIVE" << std::endl;
139 case ResourceState::REQUESTED:
140 std::cout << "\tState changed to : REQUESTED" << std::endl;
143 case ResourceState::LOST_SIGNAL:
144 std::cout << "\tState changed to : LOST_SIGNAL" << std::endl;
148 case ResourceState::DESTROYED:
149 std::cout << "\tState changed to : DESTROYED" << std::endl;
154 void onCacheUpdated(const RCSResourceAttributes& attributes)
156 std::cout << "onCacheUpdated callback" << std::endl;
158 if (attributes.empty())
160 std::cout << "\tAttribute is Empty" << std::endl;
164 for(const auto& attr : attributes)
166 std::cout << "\tkey : " << attr.key() << std::endl
167 << "\tvalue : " << attr.value().toString() << std::endl;
171 void onRemoteAttributesReceivedCallback(const RCSResourceAttributes& attributes)
173 std::cout << "onRemoteAttributesReceivedCallback callback" << std::endl;
175 if (attributes.empty())
177 std::cout << "\tAttribute is Empty" << std::endl;
181 for(const auto& attr : attributes)
183 std::cout << "\tkey : " << attr.key() << std::endl
184 << "\tvalue : " << attr.value().toString() << std::endl;
190 std::cout << std::endl;
191 std::cout << "1 :: Start Monitoring" << std::endl;
192 std::cout << "2 :: Stop Monitoring" << std::endl;
193 std::cout << "3 :: Get Attribute" << std::endl;
194 std::cout << "4 :: Set Attribute" << std::endl;
195 std::cout << "5 :: Start Caching (No update to Application)" << std::endl;
196 std::cout << "6 :: Start Caching (Update the application when data change)"<< std::endl;
197 std::cout << "7 :: Get Resource cache State" << std::endl;
198 std::cout << "8 :: Get Cached Attributes" << std::endl;
199 std::cout << "9 :: Get Cached Attribute" << std::endl;
200 std::cout << "10 :: Stop Caching" << std::endl;
201 std::cout << "11 :: Discover Resource" << std::endl;
202 std::cout << "12 :: Cancel Discovery" << std::endl;
203 std::cout << "13 :: Stop Server" << std::endl;
206 int processUserInput()
209 std::cin >> userInput;
213 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
219 void startMonitoring()
221 if (!resource->isMonitoring())
223 resource->startMonitoring(&onResourceStateChanged);
224 std::cout << "\tHosting Started..." << std::endl;
228 std::cout << "\tAlready Started..." << std::endl;
232 void stopMonitoring()
234 if (resource->isMonitoring())
236 resource->stopMonitoring();
237 std::cout << "\tHosting stopped..." << std::endl;
241 std::cout << "\tHosting not started..." << std::endl;
245 void getAttributeFromRemoteServer()
247 resource->getRemoteAttributes(&onRemoteAttributesReceivedCallback);
250 void setAttributeToRemoteServer()
255 RCSResourceAttributes setAttribute;
257 std::cout << "\tEnter the Key you want to set : ";
259 std::cout << "\tEnter the value you want to set :";
262 setAttribute[key] = value;
263 resource->setRemoteAttributes(setAttribute,
264 &onRemoteAttributesReceivedCallback);
267 void startCaching(std::function <void (const RCSResourceAttributes&)>cb)
269 if (!resource->isCaching())
271 if(cb) resource->startCaching(&onCacheUpdated);
273 else resource->startCaching();
275 std::cout << "\tCaching Started..." << std::endl;
279 std::cout << "\tAlready Started Caching..." << std::endl;
283 void startCachingWithoutCallback()
285 startCaching(nullptr);
288 void startCachingWithCallback()
290 startCaching(onCacheUpdated);
293 void getResourceCacheState()
295 switch(resource->getCacheState())
297 case CacheState::READY:
298 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::READY" << std::endl;
301 case CacheState::UNREADY:
302 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::UNREADY" << std::endl;
305 case CacheState::LOST_SIGNAL:
306 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::LOST_SIGNAL" << std::endl;
309 case CacheState::NONE:
310 std::cout << "\tCurrent Cache State : " << "CACHE_STATE ::NONE" << std::endl;
318 void getCachedAttributes()
322 if (resource->getCachedAttributes().empty())
324 std::cout << "\tReceived cached attribute is empty" << std::endl;
328 for(const auto& attr : resource->getCachedAttributes())
330 std::cout << "\tkey : " << attr.key() << std::endl
331 << "\tvalue : " << attr.value().toString() << std::endl;
335 catch (const BadRequestException& e)
337 std::cout << "Exception in getCachedAttributes : " << e.what() << std::endl;
341 void getCachedAttribute()
345 std::cout << "\tkey : " << defaultKey << std::endl
346 << "\tvalue : " << resource->getCachedAttribute(defaultKey).get< int >()
349 catch (const BadRequestException& e)
351 std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl;
353 catch (const BadGetException& e)
355 std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl;
361 if(resource->isCaching())
363 resource->stopCaching();
364 std::cout << "\tCaching stopped..." << std::endl;
368 std::cout << "\tCaching not started..." << std::endl;
372 int selectClientMenu(int selectedMenu)
374 for(int i = 0; clientMenu[i].m_menu != Menu::END_OF_MENU; i++)
376 if(clientMenu[i].m_menu == selectedMenu)
378 clientMenu[i].m_handler();
379 return clientMenu[i].m_result;
383 std::cout << "Invalid input, please try again" << std::endl;
385 return INCORRECT_INPUT;
394 if(selectClientMenu(processUserInput()) == QUIT_INPUT) break;
398 void platFormConfigure()
400 PlatformConfig config
402 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
404 OCPlatform::Configure(config);
407 void discoverResource()
409 std::string resourceType;
411 std::cout << "========================================================" << std::endl;
412 std::cout << "1. Temperature Resource Discovery" << std::endl;
413 std::cout << "2. Light Resource Discovery" << std::endl;
414 std::cout << "========================================================" << std::endl;
416 switch (processUserInput())
419 resourceType = "core.TemperatureSensor";
422 resourceType = "core.light";
423 defaultKey = "Light";
426 std::cout << "Invalid input, please try again" << std::endl;
430 std::string addressInput;
431 std::cout << "========================================================" << std::endl;
432 std::cout << "Please input address" << std::endl;
433 std::cout << "(want to use multicast -> please input 'multi')" << std::endl;
434 std::cout << "========================================================" << std::endl;
436 std::cin >> addressInput;
438 if(addressInput == multicastAdd)
440 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(),
441 relativeUri, resourceType, &onResourceDiscovered);
445 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::unicast
446 (addressInput), relativeUri, resourceType, &onResourceDiscovered);
450 void cancelDiscovery()
454 std::cout << "There isn't discovery request..." << std::endl;
457 discoveryTask->cancel();
468 catch (const std::exception& e)
470 std::cout << "main exception : " << e.what() << std::endl;
473 std::cout << "Stopping the Client" << std::endl;