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 != -1)
78 if(sequenceNumber == OC_OBSERVE_REGISTER)
80 std::cout << "Observe registration action is successful" << std::endl;
83 std::cout << "OBSERVE RESULT:"<<std::endl;
84 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
85 rep.getValue("state", mylight.m_state);
86 rep.getValue("power", mylight.m_power);
87 rep.getValue("name", mylight.m_name);
89 std::cout << "\tstate: " << mylight.m_state << std::endl;
90 std::cout << "\tpower: " << mylight.m_power << std::endl;
91 std::cout << "\tname: " << mylight.m_name << std::endl;
93 if(observe_count() == 11)
95 std::cout<<"Cancelling Observe..."<<std::endl;
96 OCStackResult result = curResource->cancelObserve();
98 std::cout << "Cancel result: "<< result <<std::endl;
100 std::cout << "DONE"<<std::endl;
106 if(eCode == OC_STACK_OK)
108 std::cout << "Observe registration failed or de-registration action failed/succeeded" << std::endl;
112 std::cout << "onObserve Response error: " << eCode << std::endl;
117 catch(std::exception& e)
119 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
124 void onPost2(const HeaderOptions& /*headerOptions*/,
125 const OCRepresentation& rep, const int eCode)
129 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
131 std::cout << "POST request was successful" << std::endl;
133 if(rep.hasAttribute("createduri"))
135 std::cout << "\tUri of the created resource: "
136 << rep.getValue<std::string>("createduri") << std::endl;
140 rep.getValue("state", mylight.m_state);
141 rep.getValue("power", mylight.m_power);
142 rep.getValue("name", mylight.m_name);
144 std::cout << "\tstate: " << mylight.m_state << std::endl;
145 std::cout << "\tpower: " << mylight.m_power << std::endl;
146 std::cout << "\tname: " << mylight.m_name << std::endl;
149 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
150 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
151 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
152 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
154 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
159 std::cout << "onPost2 Response error: " << eCode << std::endl;
163 catch(std::exception& e)
165 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
170 void onPost(const HeaderOptions& /*headerOptions*/,
171 const OCRepresentation& rep, const int eCode)
175 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
177 std::cout << "POST request was successful" << std::endl;
179 if(rep.hasAttribute("createduri"))
181 std::cout << "\tUri of the created resource: "
182 << rep.getValue<std::string>("createduri") << std::endl;
186 rep.getValue("state", mylight.m_state);
187 rep.getValue("power", mylight.m_power);
188 rep.getValue("name", mylight.m_name);
190 std::cout << "\tstate: " << mylight.m_state << std::endl;
191 std::cout << "\tpower: " << mylight.m_power << std::endl;
192 std::cout << "\tname: " << mylight.m_name << std::endl;
195 OCRepresentation rep2;
197 std::cout << "Posting light representation..."<<std::endl;
199 mylight.m_state = true;
200 mylight.m_power = 55;
202 rep2.setValue("state", mylight.m_state);
203 rep2.setValue("power", mylight.m_power);
205 curResource->post(rep2, QueryParamsMap(), &onPost2);
209 std::cout << "onPost Response error: " << eCode << std::endl;
213 catch(std::exception& e)
215 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
219 // Local function to put a different state for this resource
220 void postLightRepresentation(std::shared_ptr<OCResource> resource)
224 OCRepresentation rep;
226 std::cout << "Posting light representation..."<<std::endl;
228 mylight.m_state = false;
229 mylight.m_power = 105;
231 rep.setValue("state", mylight.m_state);
232 rep.setValue("power", mylight.m_power);
234 // Invoke resource's post API with rep, query map and the callback parameter
235 resource->post(rep, QueryParamsMap(), &onPost);
239 // callback handler on PUT request
240 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
244 if(eCode == OC_STACK_OK)
246 std::cout << "PUT request was successful" << std::endl;
248 rep.getValue("state", mylight.m_state);
249 rep.getValue("power", mylight.m_power);
250 rep.getValue("name", mylight.m_name);
252 std::cout << "\tstate: " << mylight.m_state << std::endl;
253 std::cout << "\tpower: " << mylight.m_power << std::endl;
254 std::cout << "\tname: " << mylight.m_name << std::endl;
256 postLightRepresentation(curResource);
260 std::cout << "onPut Response error: " << eCode << std::endl;
264 catch(std::exception& e)
266 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
270 // Local function to put a different state for this resource
271 void putLightRepresentation(std::shared_ptr<OCResource> resource)
275 OCRepresentation rep;
277 std::cout << "Putting light representation..."<<std::endl;
279 mylight.m_state = true;
280 mylight.m_power = 15;
282 rep.setValue("state", mylight.m_state);
283 rep.setValue("power", mylight.m_power);
285 // Invoke resource's put API with rep, query map and the callback parameter
286 resource->put(rep, QueryParamsMap(), &onPut);
290 // Callback handler on GET request
291 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
295 if(eCode == OC_STACK_OK)
297 std::cout << "GET request was successful" << std::endl;
298 std::cout << "Resource URI: " << rep.getUri() << std::endl;
300 rep.getValue("state", mylight.m_state);
301 rep.getValue("power", mylight.m_power);
302 rep.getValue("name", mylight.m_name);
304 std::cout << "\tstate: " << mylight.m_state << std::endl;
305 std::cout << "\tpower: " << mylight.m_power << std::endl;
306 std::cout << "\tname: " << mylight.m_name << std::endl;
308 putLightRepresentation(curResource);
312 std::cout << "onGET Response error: " << eCode << std::endl;
316 catch(std::exception& e)
318 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
322 // Local function to get representation of light resource
323 void getLightRepresentation(std::shared_ptr<OCResource> resource)
327 std::cout << "Getting Light Representation..."<<std::endl;
328 // Invoke resource's get API with the callback parameter
331 resource->get(test, &onGet);
335 // Callback to found resources
336 void foundResource(std::shared_ptr<OCResource> resource)
338 std::cout << "In foundResource\n";
339 std::string resourceURI;
340 std::string hostAddress;
344 std::lock_guard<std::mutex> lock(curResourceLock);
345 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
347 std::cout << "Found resource " << resource->uniqueIdentifier() <<
348 " for the first time on server with ID: "<< resource->sid()<<std::endl;
349 discoveredResources[resource->uniqueIdentifier()] = resource;
353 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
358 std::cout << "Found another resource, ignoring"<<std::endl;
363 // Do some operations with resource object.
366 std::cout<<"DISCOVERED Resource:"<<std::endl;
367 // Get the resource URI
368 resourceURI = resource->uri();
369 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
371 // Get the resource host address
372 hostAddress = resource->host();
373 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
375 // Get the resource types
376 std::cout << "\tList of resource types: " << std::endl;
377 for(auto &resourceTypes : resource->getResourceTypes())
379 std::cout << "\t\t" << resourceTypes << std::endl;
382 // Get the resource interfaces
383 std::cout << "\tList of resource interfaces: " << std::endl;
384 for(auto &resourceInterfaces : resource->getResourceInterfaces())
386 std::cout << "\t\t" << resourceInterfaces << std::endl;
389 if(resourceURI == "/a/light")
391 curResource = resource;
392 // Call a local function which will internally invoke get API on the resource pointer
393 getLightRepresentation(resource);
398 // Resource is invalid
399 std::cout << "Resource is invalid" << std::endl;
403 catch(std::exception& e)
405 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
411 std::cout << std::endl;
412 std::cout << "---------------------------------------------------------------------\n";
413 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
414 std::cout << " ObserveType : 1 - Observe" << std::endl;
415 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
416 std::cout << "---------------------------------------------------------------------\n\n";
419 void checkObserverValue(int value)
423 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
424 std::cout << "<===Setting ObserveType to Observe===>\n\n";
428 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
429 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
433 std::cout << "<===Invalid ObserveType selected."
434 <<" Setting ObserveType to Observe===>\n\n";
438 static FILE* client_open(const char* /*path*/, const char *mode)
440 return fopen(SVR_DB_FILE_NAME, mode);
443 int main(int argc, char* argv[]) {
445 std::ostringstream requestURI;
446 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
452 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
456 checkObserverValue(std::stoi(argv[1]));
460 std::cout << "<===Invalid number of command line arguments===>\n\n";
464 catch(std::exception& )
466 std::cout << "<===Invalid input arguments===>\n\n";
470 // Create PlatformConfig object
472 OC::ServiceType::InProc,
476 OC::QualityOfService::HighQos,
480 OCPlatform::Configure(cfg);
483 // makes it so that all boolean values are printed as 'true/false' in this stream
484 std::cout.setf(std::ios::boolalpha);
485 // Find all resources
486 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
488 OCPlatform::findResource("", requestURI.str(),
489 CT_DEFAULT, &foundResource);
490 std::cout<< "Finding Resource... " <<std::endl;
492 // Find resource is done twice so that we discover the original resources a second time.
493 // These resources will have the same uniqueidentifier (yet be different objects), so that
494 // we can verify/show the duplicate-checking code in foundResource(above);
495 OCPlatform::findResource("", requestURI.str(),
496 CT_DEFAULT, &foundResource);
497 std::cout<< "Finding Resource for second time..." << std::endl;
499 // A condition variable will free the mutex it is given, then do a non-
500 // intensive block until 'notify' is called on it. In this case, since we
501 // don't ever call cv.notify, this should be a non-processor intensive version
504 std::condition_variable cv;
505 std::unique_lock<std::mutex> lock(blocker);
508 }catch(OCException& e)
510 oclog() << "Exception in main: "<<e.what();