1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // OCClient.cpp : Defines the entry point for the console application.
28 #include <condition_variable>
29 #include "OCPlatform.h"
34 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
36 DiscoveredResourceMap discoveredResources;
37 std::shared_ptr<OCResource> curResource;
38 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
39 std::mutex curResourceLock;
49 Light() : m_state(false), m_power(0), m_name("")
62 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
63 const int& eCode, const int& sequenceNumber)
67 if(eCode == OC_STACK_OK)
69 std::cout << "OBSERVE RESULT:"<<std::endl;
70 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
71 rep.getValue("state", mylight.m_state);
72 rep.getValue("power", mylight.m_power);
73 rep.getValue("name", mylight.m_name);
75 std::cout << "\tstate: " << mylight.m_state << std::endl;
76 std::cout << "\tpower: " << mylight.m_power << std::endl;
77 std::cout << "\tname: " << mylight.m_name << std::endl;
79 if(observe_count() > 10)
81 std::cout<<"Cancelling Observe..."<<std::endl;
82 OCStackResult result = curResource->cancelObserve();
84 std::cout << "Cancel result: "<< result <<std::endl;
86 std::cout << "DONE"<<std::endl;
92 std::cout << "onObserve Response error: " << eCode << std::endl;
96 catch(std::exception& e)
98 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
103 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
107 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
109 std::cout << "POST request was successful" << std::endl;
111 if(rep.hasAttribute("createduri"))
113 std::cout << "\tUri of the created resource: "
114 << rep.getValue<std::string>("createduri") << std::endl;
118 rep.getValue("state", mylight.m_state);
119 rep.getValue("power", mylight.m_power);
120 rep.getValue("name", mylight.m_name);
122 std::cout << "\tstate: " << mylight.m_state << std::endl;
123 std::cout << "\tpower: " << mylight.m_power << std::endl;
124 std::cout << "\tname: " << mylight.m_name << std::endl;
127 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
128 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
129 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
130 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
132 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
137 std::cout << "onPost2 Response error: " << eCode << std::endl;
141 catch(std::exception& e)
143 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
148 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
152 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
154 std::cout << "POST request was successful" << std::endl;
156 if(rep.hasAttribute("createduri"))
158 std::cout << "\tUri of the created resource: "
159 << rep.getValue<std::string>("createduri") << std::endl;
163 rep.getValue("state", mylight.m_state);
164 rep.getValue("power", mylight.m_power);
165 rep.getValue("name", mylight.m_name);
167 std::cout << "\tstate: " << mylight.m_state << std::endl;
168 std::cout << "\tpower: " << mylight.m_power << std::endl;
169 std::cout << "\tname: " << mylight.m_name << std::endl;
172 OCRepresentation rep2;
174 std::cout << "Posting light representation..."<<std::endl;
176 mylight.m_state = true;
177 mylight.m_power = 55;
179 rep2.setValue("state", mylight.m_state);
180 rep2.setValue("power", mylight.m_power);
182 curResource->post(rep2, QueryParamsMap(), &onPost2);
186 std::cout << "onPost Response error: " << eCode << std::endl;
190 catch(std::exception& e)
192 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
196 // Local function to put a different state for this resource
197 void postLightRepresentation(std::shared_ptr<OCResource> resource)
201 OCRepresentation rep;
203 std::cout << "Posting light representation..."<<std::endl;
205 mylight.m_state = false;
206 mylight.m_power = 105;
208 rep.setValue("state", mylight.m_state);
209 rep.setValue("power", mylight.m_power);
211 // Invoke resource's post API with rep, query map and the callback parameter
212 resource->post(rep, QueryParamsMap(), &onPost);
216 // callback handler on PUT request
217 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
221 if(eCode == OC_STACK_OK)
223 std::cout << "PUT request was successful" << std::endl;
225 rep.getValue("state", mylight.m_state);
226 rep.getValue("power", mylight.m_power);
227 rep.getValue("name", mylight.m_name);
229 std::cout << "\tstate: " << mylight.m_state << std::endl;
230 std::cout << "\tpower: " << mylight.m_power << std::endl;
231 std::cout << "\tname: " << mylight.m_name << std::endl;
233 postLightRepresentation(curResource);
237 std::cout << "onPut Response error: " << eCode << std::endl;
241 catch(std::exception& e)
243 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
247 // Local function to put a different state for this resource
248 void putLightRepresentation(std::shared_ptr<OCResource> resource)
252 OCRepresentation rep;
254 std::cout << "Putting light representation..."<<std::endl;
256 mylight.m_state = true;
257 mylight.m_power = 15;
259 rep.setValue("state", mylight.m_state);
260 rep.setValue("power", mylight.m_power);
262 // Invoke resource's put API with rep, query map and the callback parameter
263 resource->put(rep, QueryParamsMap(), &onPut);
267 // Callback handler on GET request
268 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
272 if(eCode == OC_STACK_OK)
274 std::cout << "GET request was successful" << std::endl;
275 std::cout << "Resource URI: " << rep.getUri() << std::endl;
277 rep.getValue("state", mylight.m_state);
278 rep.getValue("power", mylight.m_power);
279 rep.getValue("name", mylight.m_name);
281 std::cout << "\tstate: " << mylight.m_state << std::endl;
282 std::cout << "\tpower: " << mylight.m_power << std::endl;
283 std::cout << "\tname: " << mylight.m_name << std::endl;
285 putLightRepresentation(curResource);
289 std::cout << "onGET Response error: " << eCode << std::endl;
293 catch(std::exception& e)
295 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
299 // Local function to get representation of light resource
300 void getLightRepresentation(std::shared_ptr<OCResource> resource)
304 std::cout << "Getting Light Representation..."<<std::endl;
305 // Invoke resource's get API with the callback parameter
308 resource->get(test, &onGet);
312 // Callback to found resources
313 void foundResource(std::shared_ptr<OCResource> resource)
315 std::cout << "In foundResource\n";
316 std::string resourceURI;
317 std::string hostAddress;
321 std::lock_guard<std::mutex> lock(curResourceLock);
322 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
324 std::cout << "Found resource " << resource->uniqueIdentifier() <<
325 " for the first time on server with ID: "<< resource->sid()<<std::endl;
326 discoveredResources[resource->uniqueIdentifier()] = resource;
330 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
335 std::cout << "Found another resource, ignoring"<<std::endl;
340 // Do some operations with resource object.
343 std::cout<<"DISCOVERED Resource:"<<std::endl;
344 // Get the resource URI
345 resourceURI = resource->uri();
346 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
348 // Get the resource host address
349 hostAddress = resource->host();
350 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
352 // Get the resource types
353 std::cout << "\tList of resource types: " << std::endl;
354 for(auto &resourceTypes : resource->getResourceTypes())
356 std::cout << "\t\t" << resourceTypes << std::endl;
359 // Get the resource interfaces
360 std::cout << "\tList of resource interfaces: " << std::endl;
361 for(auto &resourceInterfaces : resource->getResourceInterfaces())
363 std::cout << "\t\t" << resourceInterfaces << std::endl;
366 if(resourceURI == "/a/light")
368 curResource = resource;
369 // Call a local function which will internally invoke get API on the resource pointer
370 getLightRepresentation(resource);
375 // Resource is invalid
376 std::cout << "Resource is invalid" << std::endl;
380 catch(std::exception& e)
382 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
388 std::cout << std::endl;
389 std::cout << "---------------------------------------------------------------------\n";
390 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
391 std::cout << " ObserveType : 1 - Observe" << std::endl;
392 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
393 std::cout << "---------------------------------------------------------------------\n\n";
396 void checkObserverValue(int value)
400 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
401 std::cout << "<===Setting ObserveType to Observe===>\n\n";
405 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
406 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
410 std::cout << "<===Invalid ObserveType selected."
411 <<" Setting ObserveType to Observe===>\n\n";
415 int main(int argc, char* argv[]) {
417 std::ostringstream requestURI;
424 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
428 checkObserverValue(std::stoi(argv[1]));
432 std::cout << "<===Invalid number of command line arguments===>\n\n";
436 catch(std::exception& )
438 std::cout << "<===Invalid input arguments===>\n\n";
442 // Create PlatformConfig object
444 OC::ServiceType::InProc,
445 OC::ModeType::Client,
448 OC::QualityOfService::LowQos
451 OCPlatform::Configure(cfg);
454 // makes it so that all boolean values are printed as 'true/false' in this stream
455 std::cout.setf(std::ios::boolalpha);
456 // Find all resources
457 requestURI << OC_MULTICAST_DISCOVERY_URI;// << "?rt=core.light";
459 OCPlatform::findResource("", requestURI.str(),
460 CT_DEFAULT, &foundResource);
461 std::cout<< "Finding Resource... " <<std::endl;
463 // Find resource is done twice so that we discover the original resources a second time.
464 // These resources will have the same uniqueidentifier (yet be different objects), so that
465 // we can verify/show the duplicate-checking code in foundResource(above);
466 OCPlatform::findResource("", requestURI.str(),
467 CT_DEFAULT, &foundResource);
468 std::cout<< "Finding Resource for second time..." << std::endl;
470 // A condition variable will free the mutex it is given, then do a non-
471 // intensive block until 'notify' is called on it. In this case, since we
472 // don't ever call cv.notify, this should be a non-processor intensive version
475 std::condition_variable cv;
476 std::unique_lock<std::mutex> lock(blocker);
479 }catch(OCException& e)
481 oclog() << "Exception in main: "<<e.what();