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.
36 #include <condition_variable>
37 #include "OCPlatform.h"
42 static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";
43 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
45 DiscoveredResourceMap discoveredResources;
46 std::shared_ptr<OCResource> curResource;
47 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
48 std::mutex curResourceLock;
58 Light() : m_state(false), m_power(0), m_name("")
71 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
72 const int& eCode, const int& sequenceNumber)
76 if(eCode == OC_STACK_OK && sequenceNumber != OC_OBSERVE_NO_OPTION)
78 if(sequenceNumber == OC_OBSERVE_REGISTER)
80 std::cout << "Observe registration action is successful" << std::endl;
82 else if(sequenceNumber == OC_OBSERVE_DEREGISTER)
84 std::cout << "Observe De-registration action is successful" << std::endl;
87 std::cout << "OBSERVE RESULT:"<<std::endl;
88 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
89 rep.getValue("state", mylight.m_state);
90 rep.getValue("power", mylight.m_power);
91 rep.getValue("name", mylight.m_name);
93 std::cout << "\tstate: " << mylight.m_state << std::endl;
94 std::cout << "\tpower: " << mylight.m_power << std::endl;
95 std::cout << "\tname: " << mylight.m_name << std::endl;
97 if(observe_count() == 11)
99 std::cout<<"Cancelling Observe..."<<std::endl;
100 OCStackResult result = curResource->cancelObserve();
102 std::cout << "Cancel result: "<< result <<std::endl;
104 std::cout << "DONE"<<std::endl;
110 if(sequenceNumber == OC_OBSERVE_NO_OPTION)
112 std::cout << "Observe registration or de-registration action is failed" << std::endl;
116 std::cout << "onObserve Response error: " << eCode << std::endl;
121 catch(std::exception& e)
123 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
128 void onPost2(const HeaderOptions& /*headerOptions*/,
129 const OCRepresentation& rep, const int eCode)
133 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
135 std::cout << "POST request was successful" << std::endl;
137 if(rep.hasAttribute("createduri"))
139 std::cout << "\tUri of the created resource: "
140 << rep.getValue<std::string>("createduri") << std::endl;
144 rep.getValue("state", mylight.m_state);
145 rep.getValue("power", mylight.m_power);
146 rep.getValue("name", mylight.m_name);
148 std::cout << "\tstate: " << mylight.m_state << std::endl;
149 std::cout << "\tpower: " << mylight.m_power << std::endl;
150 std::cout << "\tname: " << mylight.m_name << std::endl;
153 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
154 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
155 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
156 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
158 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
163 std::cout << "onPost2 Response error: " << eCode << std::endl;
167 catch(std::exception& e)
169 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
174 void onPost(const HeaderOptions& /*headerOptions*/,
175 const OCRepresentation& rep, const int eCode)
179 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
181 std::cout << "POST request was successful" << std::endl;
183 if(rep.hasAttribute("createduri"))
185 std::cout << "\tUri of the created resource: "
186 << rep.getValue<std::string>("createduri") << std::endl;
190 rep.getValue("state", mylight.m_state);
191 rep.getValue("power", mylight.m_power);
192 rep.getValue("name", mylight.m_name);
194 std::cout << "\tstate: " << mylight.m_state << std::endl;
195 std::cout << "\tpower: " << mylight.m_power << std::endl;
196 std::cout << "\tname: " << mylight.m_name << std::endl;
199 OCRepresentation rep2;
201 std::cout << "Posting light representation..."<<std::endl;
203 mylight.m_state = true;
204 mylight.m_power = 55;
206 rep2.setValue("state", mylight.m_state);
207 rep2.setValue("power", mylight.m_power);
209 curResource->post(rep2, QueryParamsMap(), &onPost2);
213 std::cout << "onPost Response error: " << eCode << std::endl;
217 catch(std::exception& e)
219 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
223 // Local function to put a different state for this resource
224 void postLightRepresentation(std::shared_ptr<OCResource> resource)
228 OCRepresentation rep;
230 std::cout << "Posting light representation..."<<std::endl;
232 mylight.m_state = false;
233 mylight.m_power = 105;
235 rep.setValue("state", mylight.m_state);
236 rep.setValue("power", mylight.m_power);
238 // Invoke resource's post API with rep, query map and the callback parameter
239 resource->post(rep, QueryParamsMap(), &onPost);
243 // callback handler on PUT request
244 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
248 if(eCode == OC_STACK_OK)
250 std::cout << "PUT request was successful" << std::endl;
252 rep.getValue("state", mylight.m_state);
253 rep.getValue("power", mylight.m_power);
254 rep.getValue("name", mylight.m_name);
256 std::cout << "\tstate: " << mylight.m_state << std::endl;
257 std::cout << "\tpower: " << mylight.m_power << std::endl;
258 std::cout << "\tname: " << mylight.m_name << std::endl;
260 postLightRepresentation(curResource);
264 std::cout << "onPut Response error: " << eCode << std::endl;
268 catch(std::exception& e)
270 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
274 // Local function to put a different state for this resource
275 void putLightRepresentation(std::shared_ptr<OCResource> resource)
279 OCRepresentation rep;
281 std::cout << "Putting light representation..."<<std::endl;
283 mylight.m_state = true;
284 mylight.m_power = 15;
286 rep.setValue("state", mylight.m_state);
287 rep.setValue("power", mylight.m_power);
289 // Invoke resource's put API with rep, query map and the callback parameter
290 resource->put(rep, QueryParamsMap(), &onPut);
294 // Callback handler on GET request
295 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
299 if(eCode == OC_STACK_OK)
301 std::cout << "GET request was successful" << std::endl;
302 std::cout << "Resource URI: " << rep.getUri() << std::endl;
304 rep.getValue("state", mylight.m_state);
305 rep.getValue("power", mylight.m_power);
306 rep.getValue("name", mylight.m_name);
308 std::cout << "\tstate: " << mylight.m_state << std::endl;
309 std::cout << "\tpower: " << mylight.m_power << std::endl;
310 std::cout << "\tname: " << mylight.m_name << std::endl;
312 putLightRepresentation(curResource);
316 std::cout << "onGET Response error: " << eCode << std::endl;
320 catch(std::exception& e)
322 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
326 // Local function to get representation of light resource
327 void getLightRepresentation(std::shared_ptr<OCResource> resource)
331 std::cout << "Getting Light Representation..."<<std::endl;
332 // Invoke resource's get API with the callback parameter
335 resource->get(test, &onGet);
339 // Callback to found resources
340 void foundResource(std::shared_ptr<OCResource> resource)
342 std::cout << "In foundResource\n";
343 std::string resourceURI;
344 std::string hostAddress;
348 std::lock_guard<std::mutex> lock(curResourceLock);
349 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
351 std::cout << "Found resource " << resource->uniqueIdentifier() <<
352 " for the first time on server with ID: "<< resource->sid()<<std::endl;
353 discoveredResources[resource->uniqueIdentifier()] = resource;
357 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
362 std::cout << "Found another resource, ignoring"<<std::endl;
367 // Do some operations with resource object.
370 std::cout<<"DISCOVERED Resource:"<<std::endl;
371 // Get the resource URI
372 resourceURI = resource->uri();
373 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
375 // Get the resource host address
376 hostAddress = resource->host();
377 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
379 // Get the resource types
380 std::cout << "\tList of resource types: " << std::endl;
381 for(auto &resourceTypes : resource->getResourceTypes())
383 std::cout << "\t\t" << resourceTypes << std::endl;
386 // Get the resource interfaces
387 std::cout << "\tList of resource interfaces: " << std::endl;
388 for(auto &resourceInterfaces : resource->getResourceInterfaces())
390 std::cout << "\t\t" << resourceInterfaces << std::endl;
393 if(resourceURI == "/a/light")
395 curResource = resource;
396 // Call a local function which will internally invoke get API on the resource pointer
397 getLightRepresentation(resource);
402 // Resource is invalid
403 std::cout << "Resource is invalid" << std::endl;
407 catch(std::exception& e)
409 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
415 std::cout << std::endl;
416 std::cout << "---------------------------------------------------------------------\n";
417 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
418 std::cout << " ObserveType : 1 - Observe" << std::endl;
419 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
420 std::cout << "---------------------------------------------------------------------\n\n";
423 void checkObserverValue(int value)
427 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
428 std::cout << "<===Setting ObserveType to Observe===>\n\n";
432 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
433 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
437 std::cout << "<===Invalid ObserveType selected."
438 <<" Setting ObserveType to Observe===>\n\n";
442 static FILE* client_open(const char* /*path*/, const char *mode)
444 return fopen(SVR_DB_FILE_NAME, mode);
447 int main(int argc, char* argv[]) {
449 std::ostringstream requestURI;
450 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
456 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
460 checkObserverValue(std::stoi(argv[1]));
464 std::cout << "<===Invalid number of command line arguments===>\n\n";
468 catch(std::exception& )
470 std::cout << "<===Invalid input arguments===>\n\n";
474 // Create PlatformConfig object
476 OC::ServiceType::InProc,
480 OC::QualityOfService::LowQos,
484 OCPlatform::Configure(cfg);
487 // makes it so that all boolean values are printed as 'true/false' in this stream
488 std::cout.setf(std::ios::boolalpha);
489 // Find all resources
490 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
492 OCPlatform::findResource("", requestURI.str(),
493 CT_DEFAULT, &foundResource);
494 std::cout<< "Finding Resource... " <<std::endl;
496 // Find resource is done twice so that we discover the original resources a second time.
497 // These resources will have the same uniqueidentifier (yet be different objects), so that
498 // we can verify/show the duplicate-checking code in foundResource(above);
499 OCPlatform::findResource("", requestURI.str(),
500 CT_DEFAULT, &foundResource);
501 std::cout<< "Finding Resource for second time..." << std::endl;
503 // A condition variable will free the mutex it is given, then do a non-
504 // intensive block until 'notify' is called on it. In this case, since we
505 // don't ever call cv.notify, this should be a non-processor intensive version
508 std::condition_variable cv;
509 std::unique_lock<std::mutex> lock(blocker);
512 }catch(OCException& e)
514 oclog() << "Exception in main: "<<e.what();