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 "RCSDiscoveryManager.h"
24 #include "RCSRemoteResourceObject.h"
25 #include "RCSResourceAttributes.h"
26 #include "RCSAddress.h"
28 #include "OCPlatform.h"
31 using namespace OIC::Service;
33 #define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC, __VA_ARGS__ }
35 void startMonitoring();
36 void startMonitoring();
37 void stopMonitoring();
38 void getAttributeFromRemoteServer();
39 void setAttributeToRemoteServer();
40 void startCachingWithoutCallback();
41 void startCachingWithCallback();
42 void getResourceCacheState();
43 void getCachedAttributes();
44 void getCachedAttribute();
46 void discoverResource();
47 void cancelDiscovery();
48 int processUserInput();
49 void selectResource();
50 void printAttributes(const RCSResourceAttributes& attributes);
57 typedef void(*MenuHandler)();
60 MenuItem(const char* funcName, MenuHandler handler = nullptr,
61 MenuHandler optionHandler = nullptr, MenuList* state = nullptr)
62 : m_name(funcName), m_handler(handler), m_optionHandler(optionHandler),
67 MenuList* command() const
69 std::cout << m_name << " start.." << std::endl;
70 if(m_handler) m_handler();
72 if(m_optionHandler != nullptr) m_optionHandler();
77 const char* getTitle() const
79 return m_name.c_str();
83 const std::string m_name;
84 const MenuHandler m_handler;
85 const MenuHandler m_optionHandler;
86 MenuList* const m_nextState ;
92 MenuList(std::initializer_list<MenuItem> il)
93 :m_menuItemList(std::move(il))
99 std::cout << std::endl;
102 for(const auto& menuItem : m_menuItemList)
105 std::cout << std::right << menuNum++ << ". ";
106 std::cout << menuItem.getTitle() << std::endl;
112 int input = processUserInput();
114 if(input == static_cast<int>(m_menuItemList.size())) return nullptr;
116 if(input > static_cast<int>(m_menuItemList.size()) || input <= 0)
118 std::cout << "Invalid input, please try again" << std::endl;
121 auto nextMenuList = m_menuItemList[input-1].command();
123 return nextMenuList == nullptr ? this : nextMenuList;
127 const std::vector<MenuItem> m_menuItemList;
130 constexpr int REQUEST_TEMP = 1;
131 constexpr int REQUEST_LIGHT = 2;
133 const std::string TEMP = "oic.r.temperaturesensor";
134 const std::string LIGHT = "oic.r.light";
136 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
138 std::vector<RCSRemoteResourceObject::Ptr> resourceList;
139 RCSRemoteResourceObject::Ptr selectedResource;
141 std::string defaultKey;
143 MenuList* currentMenuList;
145 MenuList resourceMenu = {
146 DECLARE_MENU(startMonitoring),
147 DECLARE_MENU(stopMonitoring),
148 DECLARE_MENU(getAttributeFromRemoteServer),
149 DECLARE_MENU(setAttributeToRemoteServer),
150 DECLARE_MENU(startCachingWithoutCallback),
151 DECLARE_MENU(startCachingWithCallback),
152 DECLARE_MENU(getResourceCacheState),
153 DECLARE_MENU(getCachedAttributes),
154 DECLARE_MENU(getCachedAttribute),
155 DECLARE_MENU(stopCaching),
159 MenuList cancelDiscoveryMenu = {
160 DECLARE_MENU(cancelDiscovery, selectResource, &resourceMenu),
164 MenuList discoveryMenu = {
165 DECLARE_MENU(discoverResource, nullptr, &cancelDiscoveryMenu),
169 void onResourceDiscovered(std::shared_ptr<RCSRemoteResourceObject> discoveredResource)
171 std::cout << "onResourceDiscovered callback :: " << std::endl;
173 std::cout << "resourceURI : " << discoveredResource->getUri() << std::endl;
174 std::cout << "hostAddress : " << discoveredResource->getAddress() << std::endl;
176 resourceList.push_back(discoveredResource);
179 void onResourceStateChanged(const ResourceState& resourceState)
181 std::cout << "onResourceStateChanged callback" << std::endl;
183 switch(resourceState)
185 case ResourceState::NONE:
186 std::cout << "\tState changed to : NOT_MONITORING" << std::endl;
189 case ResourceState::ALIVE:
190 std::cout << "\tState changed to : ALIVE" << std::endl;
193 case ResourceState::REQUESTED:
194 std::cout << "\tState changed to : REQUESTED" << std::endl;
197 case ResourceState::LOST_SIGNAL:
198 std::cout << "\tState changed to : LOST_SIGNAL" << std::endl;
201 case ResourceState::DESTROYED:
202 std::cout << "\tState changed to : DESTROYED" << std::endl;
207 void onCacheUpdated(const RCSResourceAttributes& attributes)
209 std::cout << "onCacheUpdated callback" << std::endl;
211 printAttributes(attributes);
214 void onRemoteAttributesReceived(const RCSResourceAttributes& attributes, int)
216 std::cout << "onRemoteAttributesReceived callback" << std::endl;
218 printAttributes(attributes);
221 void startMonitoring()
223 if (!selectedResource->isMonitoring())
225 selectedResource->startMonitoring(&onResourceStateChanged);
226 std::cout << "\tHosting Started..." << std::endl;
230 std::cout << "\tAlready Started..." << std::endl;
234 void stopMonitoring()
236 if (selectedResource->isMonitoring())
238 selectedResource->stopMonitoring();
239 std::cout << "\tHosting stopped..." << std::endl;
243 std::cout << "\tHosting not started..." << std::endl;
247 void getAttributeFromRemoteServer()
249 selectedResource->getRemoteAttributes(&onRemoteAttributesReceived);
252 void setAttributeToRemoteServer()
255 RCSResourceAttributes setAttribute;
257 std::cout << "\tEnter the Key you want to set : ";
259 std::cout << "\tEnter the value(INT) you want to set :";
261 setAttribute[key] = processUserInput();
262 selectedResource->setRemoteAttributes(setAttribute,
263 &onRemoteAttributesReceived);
266 void startCaching(RCSRemoteResourceObject::CacheUpdatedCallback cb)
268 if (!selectedResource->isCaching())
270 selectedResource->startCaching(std::move(cb));
271 std::cout << "\tCaching Started..." << std::endl;
275 std::cout << "\tAlready Started Caching..." << std::endl;
279 void startCachingWithoutCallback()
281 startCaching(nullptr);
284 void startCachingWithCallback()
286 startCaching(onCacheUpdated);
289 void getResourceCacheState()
291 switch(selectedResource->getCacheState())
293 case CacheState::READY:
294 std::cout << "\tCurrent Cache State : CACHE_STATE::READY" << std::endl;
297 case CacheState::UNREADY:
298 std::cout << "\tCurrent Cache State : CACHE_STATE::UNREADY" << std::endl;
301 case CacheState::LOST_SIGNAL:
302 std::cout << "\tCurrent Cache State : CACHE_STATE::LOST_SIGNAL" << std::endl;
305 case CacheState::NONE:
306 std::cout << "\tCurrent Cache State : CACHE_STATE::NONE" << std::endl;
311 void getCachedAttributes()
315 printAttributes(selectedResource->getCachedAttributes());
317 catch (const RCSBadRequestException& e)
319 std::cout << "Exception in getCachedAttributes : " << e.what() << std::endl;
323 void getCachedAttribute()
327 std::cout << "\tkey : " << defaultKey << std::endl
328 << "\tvalue : " << selectedResource->getCachedAttribute(defaultKey).get< int >()
331 catch (const RCSBadRequestException& e)
333 std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl;
335 catch (const RCSBadGetException& e)
337 std::cout << "Exception in getCachedAttribute : " << e.what() << std::endl;
343 if(selectedResource->isCaching())
345 selectedResource->stopCaching();
346 std::cout << "\tCaching stopped..." << std::endl;
350 std::cout << "\tCaching not started..." << std::endl;
354 void platFormConfigure()
356 PlatformConfig config
358 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
360 OCPlatform::Configure(config);
363 int processUserInput()
369 std::cin >> userInput;
373 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
374 std::cout << "Invalid Input, please try again" << std::endl;
381 bool selectResourceType(std::string& resourceType)
383 std::cout << "========================================================" << std::endl;
384 std::cout << "1. Temperature Resource Discovery" << std::endl;
385 std::cout << "2. Light Resource Discovery" << std::endl;
386 std::cout << "========================================================" << std::endl;
388 switch (processUserInput())
392 defaultKey = "Temperature";
395 resourceType = LIGHT;
396 defaultKey = "Light";
399 std::cout << "Invalid input, please try again" << std::endl;
404 RCSAddress inputAddress()
406 std::cout << "========================================================" << std::endl;
407 std::cout << "Please input address" << std::endl;
408 std::cout << "(default is multicast. when you want to use unicast, input address" << std::endl;
409 std::cout << "========================================================" << std::endl;
413 if(std::cin.peek() != '\n') std::cin >> address;
415 return address.empty() ? RCSAddress::multicast() : RCSAddress::unicast(address);
418 void discoverResource()
420 std::string resourceType = "";
422 while(!selectResourceType(resourceType)) {}
424 while(!discoveryTask)
428 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
429 inputAddress(), resourceType, &onResourceDiscovered);
431 catch (const RCSPlatformException& e)
433 std::cout << e.what() << std::endl;
438 void displayResourceList()
441 for(const auto& resource : resourceList)
443 std::cout << cnt++ << ".\tresourceURI : " << resource->getUri() << std::endl <<
444 "\thostAddress : " << resource->getAddress() << std::endl;
448 void selectResource()
450 displayResourceList();
452 std::cout << "select Resource..." << std::endl;
454 selectedResource = resourceList[processUserInput()-1];
456 resourceList.clear();
459 void cancelDiscovery()
463 std::cout << "There is no discovery request..." << std::endl;
466 discoveryTask->cancel();
469 void printAttribute(const std::string key, const RCSResourceAttributes::Value& value)
471 std::cout << "\tkey : " << key << std::endl
472 << "\tvalue : " << value.toString() << std::endl;
475 void printAttributes(const RCSResourceAttributes& attributes)
477 if (attributes.empty())
479 std::cout << "\tattributes is empty" << std::endl;
481 for(const auto& attr : attributes)
483 printAttribute(attr.key(), attr.value());
491 currentMenuList = &discoveryMenu;
495 while(currentMenuList)
497 currentMenuList->display();
499 currentMenuList = currentMenuList->select();
502 catch (const std::exception& e)
504 std::cout << "main exception : " << e.what() << std::endl;
507 std::cout << "Stopping the Client" << std::endl;