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() > 10)
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, const OCRepresentation& rep, const int eCode)
123 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
125 std::cout << "POST request was successful" << std::endl;
127 if(rep.hasAttribute("createduri"))
129 std::cout << "\tUri of the created resource: "
130 << rep.getValue<std::string>("createduri") << std::endl;
134 rep.getValue("state", mylight.m_state);
135 rep.getValue("power", mylight.m_power);
136 rep.getValue("name", mylight.m_name);
138 std::cout << "\tstate: " << mylight.m_state << std::endl;
139 std::cout << "\tpower: " << mylight.m_power << std::endl;
140 std::cout << "\tname: " << mylight.m_name << std::endl;
143 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
144 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
145 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
146 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
148 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
153 std::cout << "onPost2 Response error: " << eCode << std::endl;
157 catch(std::exception& e)
159 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
164 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
168 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
170 std::cout << "POST request was successful" << std::endl;
172 if(rep.hasAttribute("createduri"))
174 std::cout << "\tUri of the created resource: "
175 << rep.getValue<std::string>("createduri") << std::endl;
179 rep.getValue("state", mylight.m_state);
180 rep.getValue("power", mylight.m_power);
181 rep.getValue("name", mylight.m_name);
183 std::cout << "\tstate: " << mylight.m_state << std::endl;
184 std::cout << "\tpower: " << mylight.m_power << std::endl;
185 std::cout << "\tname: " << mylight.m_name << std::endl;
188 OCRepresentation rep2;
190 std::cout << "Posting light representation..."<<std::endl;
192 mylight.m_state = true;
193 mylight.m_power = 55;
195 rep2.setValue("state", mylight.m_state);
196 rep2.setValue("power", mylight.m_power);
198 curResource->post(rep2, QueryParamsMap(), &onPost2);
202 std::cout << "onPost Response error: " << eCode << std::endl;
206 catch(std::exception& e)
208 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
212 // Local function to put a different state for this resource
213 void postLightRepresentation(std::shared_ptr<OCResource> resource)
217 OCRepresentation rep;
219 std::cout << "Posting light representation..."<<std::endl;
221 mylight.m_state = false;
222 mylight.m_power = 105;
224 rep.setValue("state", mylight.m_state);
225 rep.setValue("power", mylight.m_power);
227 // Invoke resource's post API with rep, query map and the callback parameter
228 resource->post(rep, QueryParamsMap(), &onPost);
232 // callback handler on PUT request
233 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
237 if(eCode == OC_STACK_OK)
239 std::cout << "PUT request was successful" << std::endl;
241 rep.getValue("state", mylight.m_state);
242 rep.getValue("power", mylight.m_power);
243 rep.getValue("name", mylight.m_name);
245 std::cout << "\tstate: " << mylight.m_state << std::endl;
246 std::cout << "\tpower: " << mylight.m_power << std::endl;
247 std::cout << "\tname: " << mylight.m_name << std::endl;
249 postLightRepresentation(curResource);
253 std::cout << "onPut Response error: " << eCode << std::endl;
257 catch(std::exception& e)
259 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
263 // Local function to put a different state for this resource
264 void putLightRepresentation(std::shared_ptr<OCResource> resource)
268 OCRepresentation rep;
270 std::cout << "Putting light representation..."<<std::endl;
272 mylight.m_state = true;
273 mylight.m_power = 15;
275 rep.setValue("state", mylight.m_state);
276 rep.setValue("power", mylight.m_power);
278 // Invoke resource's put API with rep, query map and the callback parameter
279 resource->put(rep, QueryParamsMap(), &onPut);
283 // Callback handler on GET request
284 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
288 if(eCode == OC_STACK_OK)
290 std::cout << "GET request was successful" << std::endl;
291 std::cout << "Resource URI: " << rep.getUri() << std::endl;
293 rep.getValue("state", mylight.m_state);
294 rep.getValue("power", mylight.m_power);
295 rep.getValue("name", mylight.m_name);
297 std::cout << "\tstate: " << mylight.m_state << std::endl;
298 std::cout << "\tpower: " << mylight.m_power << std::endl;
299 std::cout << "\tname: " << mylight.m_name << std::endl;
301 putLightRepresentation(curResource);
305 std::cout << "onGET Response error: " << eCode << std::endl;
309 catch(std::exception& e)
311 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
315 // Local function to get representation of light resource
316 void getLightRepresentation(std::shared_ptr<OCResource> resource)
320 std::cout << "Getting Light Representation..."<<std::endl;
321 // Invoke resource's get API with the callback parameter
324 resource->get(test, &onGet);
328 // Callback to found resources
329 void foundResource(std::shared_ptr<OCResource> resource)
331 std::cout << "In foundResource\n";
332 std::string resourceURI;
333 std::string hostAddress;
337 std::lock_guard<std::mutex> lock(curResourceLock);
338 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
340 std::cout << "Found resource " << resource->uniqueIdentifier() <<
341 " for the first time on server with ID: "<< resource->sid()<<std::endl;
342 discoveredResources[resource->uniqueIdentifier()] = resource;
346 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
351 std::cout << "Found another resource, ignoring"<<std::endl;
356 // Do some operations with resource object.
359 std::cout<<"DISCOVERED Resource:"<<std::endl;
360 // Get the resource URI
361 resourceURI = resource->uri();
362 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
364 // Get the resource host address
365 hostAddress = resource->host();
366 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
368 // Get the resource types
369 std::cout << "\tList of resource types: " << std::endl;
370 for(auto &resourceTypes : resource->getResourceTypes())
372 std::cout << "\t\t" << resourceTypes << std::endl;
375 // Get the resource interfaces
376 std::cout << "\tList of resource interfaces: " << std::endl;
377 for(auto &resourceInterfaces : resource->getResourceInterfaces())
379 std::cout << "\t\t" << resourceInterfaces << std::endl;
382 if(resourceURI == "/a/light")
384 curResource = resource;
385 // Call a local function which will internally invoke get API on the resource pointer
386 getLightRepresentation(resource);
391 // Resource is invalid
392 std::cout << "Resource is invalid" << std::endl;
396 catch(std::exception& e)
398 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
404 std::cout << std::endl;
405 std::cout << "---------------------------------------------------------------------\n";
406 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
407 std::cout << " ObserveType : 1 - Observe" << std::endl;
408 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
409 std::cout << "---------------------------------------------------------------------\n\n";
412 void checkObserverValue(int value)
416 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
417 std::cout << "<===Setting ObserveType to Observe===>\n\n";
421 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
422 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
426 std::cout << "<===Invalid ObserveType selected."
427 <<" Setting ObserveType to Observe===>\n\n";
431 static FILE* client_open(const char *path, const char *mode)
433 return fopen("./oic_svr_db_client.json", mode);
436 int main(int argc, char* argv[]) {
438 std::ostringstream requestURI;
439 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
445 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
449 checkObserverValue(std::stoi(argv[1]));
453 std::cout << "<===Invalid number of command line arguments===>\n\n";
457 catch(std::exception& )
459 std::cout << "<===Invalid input arguments===>\n\n";
463 // Create PlatformConfig object
465 OC::ServiceType::InProc,
469 OC::QualityOfService::LowQos,
473 OCPlatform::Configure(cfg);
476 // makes it so that all boolean values are printed as 'true/false' in this stream
477 std::cout.setf(std::ios::boolalpha);
478 // Find all resources
479 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
481 OCPlatform::findResource("", requestURI.str(),
482 CT_DEFAULT, &foundResource);
483 std::cout<< "Finding Resource... " <<std::endl;
485 // Find resource is done twice so that we discover the original resources a second time.
486 // These resources will have the same uniqueidentifier (yet be different objects), so that
487 // we can verify/show the duplicate-checking code in foundResource(above);
488 OCPlatform::findResource("", requestURI.str(),
489 CT_DEFAULT, &foundResource);
490 std::cout<< "Finding Resource for second time..." << std::endl;
492 // A condition variable will free the mutex it is given, then do a non-
493 // intensive block until 'notify' is called on it. In this case, since we
494 // don't ever call cv.notify, this should be a non-processor intensive version
497 std::condition_variable cv;
498 std::unique_lock<std::mutex> lock(blocker);
501 }catch(OCException& e)
503 oclog() << "Exception in main: "<<e.what();