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 && sequenceNumber != OC_OBSERVE_NO_OPTION)
69 if(sequenceNumber == OC_OBSERVE_REGISTER)
71 std::cout << "Observe registration action is successful" << std::endl;
73 else if(sequenceNumber == OC_OBSERVE_DEREGISTER)
75 std::cout << "Observe De-registration action is successful" << std::endl;
78 std::cout << "OBSERVE RESULT:"<<std::endl;
79 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
80 rep.getValue("state", mylight.m_state);
81 rep.getValue("power", mylight.m_power);
82 rep.getValue("name", mylight.m_name);
84 std::cout << "\tstate: " << mylight.m_state << std::endl;
85 std::cout << "\tpower: " << mylight.m_power << std::endl;
86 std::cout << "\tname: " << mylight.m_name << std::endl;
88 if(observe_count() == 11)
90 std::cout<<"Cancelling Observe..."<<std::endl;
91 OCStackResult result = curResource->cancelObserve();
93 std::cout << "Cancel result: "<< result <<std::endl;
95 std::cout << "DONE"<<std::endl;
101 if(sequenceNumber == OC_OBSERVE_NO_OPTION)
103 std::cout << "Observe registration or de-registration action is failed" << std::endl;
107 std::cout << "onObserve Response error: " << eCode << std::endl;
112 catch(std::exception& e)
114 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
119 void onPost2(const HeaderOptions& /*headerOptions*/,
120 const OCRepresentation& rep, const int eCode)
124 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
126 std::cout << "POST request was successful" << std::endl;
128 if(rep.hasAttribute("createduri"))
130 std::cout << "\tUri of the created resource: "
131 << rep.getValue<std::string>("createduri") << std::endl;
135 rep.getValue("state", mylight.m_state);
136 rep.getValue("power", mylight.m_power);
137 rep.getValue("name", mylight.m_name);
139 std::cout << "\tstate: " << mylight.m_state << std::endl;
140 std::cout << "\tpower: " << mylight.m_power << std::endl;
141 std::cout << "\tname: " << mylight.m_name << std::endl;
144 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
145 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
146 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
147 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
149 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
154 std::cout << "onPost2 Response error: " << eCode << std::endl;
158 catch(std::exception& e)
160 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
165 void onPost(const HeaderOptions& /*headerOptions*/,
166 const OCRepresentation& rep, const int eCode)
170 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
172 std::cout << "POST request was successful" << std::endl;
174 if(rep.hasAttribute("createduri"))
176 std::cout << "\tUri of the created resource: "
177 << rep.getValue<std::string>("createduri") << std::endl;
181 rep.getValue("state", mylight.m_state);
182 rep.getValue("power", mylight.m_power);
183 rep.getValue("name", mylight.m_name);
185 std::cout << "\tstate: " << mylight.m_state << std::endl;
186 std::cout << "\tpower: " << mylight.m_power << std::endl;
187 std::cout << "\tname: " << mylight.m_name << std::endl;
190 OCRepresentation rep2;
192 std::cout << "Posting light representation..."<<std::endl;
194 mylight.m_state = true;
195 mylight.m_power = 55;
197 rep2.setValue("state", mylight.m_state);
198 rep2.setValue("power", mylight.m_power);
200 curResource->post(rep2, QueryParamsMap(), &onPost2);
204 std::cout << "onPost Response error: " << eCode << std::endl;
208 catch(std::exception& e)
210 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
214 // Local function to put a different state for this resource
215 void postLightRepresentation(std::shared_ptr<OCResource> resource)
219 OCRepresentation rep;
221 std::cout << "Posting light representation..."<<std::endl;
223 mylight.m_state = false;
224 mylight.m_power = 105;
226 rep.setValue("state", mylight.m_state);
227 rep.setValue("power", mylight.m_power);
229 // Invoke resource's post API with rep, query map and the callback parameter
230 resource->post(rep, QueryParamsMap(), &onPost);
234 // callback handler on PUT request
235 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
239 if(eCode == OC_STACK_OK)
241 std::cout << "PUT request was successful" << std::endl;
243 rep.getValue("state", mylight.m_state);
244 rep.getValue("power", mylight.m_power);
245 rep.getValue("name", mylight.m_name);
247 std::cout << "\tstate: " << mylight.m_state << std::endl;
248 std::cout << "\tpower: " << mylight.m_power << std::endl;
249 std::cout << "\tname: " << mylight.m_name << std::endl;
251 postLightRepresentation(curResource);
255 std::cout << "onPut Response error: " << eCode << std::endl;
259 catch(std::exception& e)
261 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
265 // Local function to put a different state for this resource
266 void putLightRepresentation(std::shared_ptr<OCResource> resource)
270 OCRepresentation rep;
272 std::cout << "Putting light representation..."<<std::endl;
274 mylight.m_state = true;
275 mylight.m_power = 15;
277 rep.setValue("state", mylight.m_state);
278 rep.setValue("power", mylight.m_power);
280 // Invoke resource's put API with rep, query map and the callback parameter
281 resource->put(rep, QueryParamsMap(), &onPut);
285 // Callback handler on GET request
286 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
290 if(eCode == OC_STACK_OK)
292 std::cout << "GET request was successful" << std::endl;
293 std::cout << "Resource URI: " << rep.getUri() << std::endl;
295 rep.getValue("state", mylight.m_state);
296 rep.getValue("power", mylight.m_power);
297 rep.getValue("name", mylight.m_name);
299 std::cout << "\tstate: " << mylight.m_state << std::endl;
300 std::cout << "\tpower: " << mylight.m_power << std::endl;
301 std::cout << "\tname: " << mylight.m_name << std::endl;
303 putLightRepresentation(curResource);
307 std::cout << "onGET Response error: " << eCode << std::endl;
311 catch(std::exception& e)
313 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
317 // Local function to get representation of light resource
318 void getLightRepresentation(std::shared_ptr<OCResource> resource)
322 std::cout << "Getting Light Representation..."<<std::endl;
323 // Invoke resource's get API with the callback parameter
326 resource->get(test, &onGet);
330 // Callback to found resources
331 void foundResource(std::shared_ptr<OCResource> resource)
333 std::cout << "In foundResource\n";
334 std::string resourceURI;
335 std::string hostAddress;
339 std::lock_guard<std::mutex> lock(curResourceLock);
340 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
342 std::cout << "Found resource " << resource->uniqueIdentifier() <<
343 " for the first time on server with ID: "<< resource->sid()<<std::endl;
344 discoveredResources[resource->uniqueIdentifier()] = resource;
348 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
353 std::cout << "Found another resource, ignoring"<<std::endl;
358 // Do some operations with resource object.
361 std::cout<<"DISCOVERED Resource:"<<std::endl;
362 // Get the resource URI
363 resourceURI = resource->uri();
364 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
366 // Get the resource host address
367 hostAddress = resource->host();
368 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
370 // Get the resource types
371 std::cout << "\tList of resource types: " << std::endl;
372 for(auto &resourceTypes : resource->getResourceTypes())
374 std::cout << "\t\t" << resourceTypes << std::endl;
377 // Get the resource interfaces
378 std::cout << "\tList of resource interfaces: " << std::endl;
379 for(auto &resourceInterfaces : resource->getResourceInterfaces())
381 std::cout << "\t\t" << resourceInterfaces << std::endl;
384 if(resourceURI == "/a/light")
386 curResource = resource;
387 // Call a local function which will internally invoke get API on the resource pointer
388 getLightRepresentation(resource);
393 // Resource is invalid
394 std::cout << "Resource is invalid" << std::endl;
398 catch(std::exception& e)
400 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
406 std::cout << std::endl;
407 std::cout << "---------------------------------------------------------------------\n";
408 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
409 std::cout << " ObserveType : 1 - Observe" << std::endl;
410 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
411 std::cout << "---------------------------------------------------------------------\n\n";
414 void checkObserverValue(int value)
418 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
419 std::cout << "<===Setting ObserveType to Observe===>\n\n";
423 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
424 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
428 std::cout << "<===Invalid ObserveType selected."
429 <<" Setting ObserveType to Observe===>\n\n";
433 static FILE* client_open(const char* /*path*/, const char *mode)
435 return fopen("./oic_svr_db_client.json", mode);
438 int main(int argc, char* argv[]) {
440 std::ostringstream requestURI;
441 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
447 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
451 checkObserverValue(std::stoi(argv[1]));
455 std::cout << "<===Invalid number of command line arguments===>\n\n";
459 catch(std::exception& )
461 std::cout << "<===Invalid input arguments===>\n\n";
465 // Create PlatformConfig object
467 OC::ServiceType::InProc,
471 OC::QualityOfService::LowQos,
475 OCPlatform::Configure(cfg);
478 // makes it so that all boolean values are printed as 'true/false' in this stream
479 std::cout.setf(std::ios::boolalpha);
480 // Find all resources
481 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
483 OCPlatform::findResource("", requestURI.str(),
484 CT_DEFAULT, &foundResource);
485 std::cout<< "Finding Resource... " <<std::endl;
487 // Find resource is done twice so that we discover the original resources a second time.
488 // These resources will have the same uniqueidentifier (yet be different objects), so that
489 // we can verify/show the duplicate-checking code in foundResource(above);
490 OCPlatform::findResource("", requestURI.str(),
491 CT_DEFAULT, &foundResource);
492 std::cout<< "Finding Resource for second time..." << std::endl;
494 // A condition variable will free the mutex it is given, then do a non-
495 // intensive block until 'notify' is called on it. In this case, since we
496 // don't ever call cv.notify, this should be a non-processor intensive version
499 std::condition_variable cv;
500 std::unique_lock<std::mutex> lock(blocker);
503 }catch(OCException& e)
505 oclog() << "Exception in main: "<<e.what();