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 static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";
35 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
37 DiscoveredResourceMap discoveredResources;
38 std::shared_ptr<OCResource> curResource;
39 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
40 std::mutex curResourceLock;
50 Light() : m_state(false), m_power(0), m_name("")
63 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
64 const int& eCode, const int& sequenceNumber)
68 if(eCode == OC_STACK_OK && sequenceNumber != -1)
70 if(sequenceNumber == OC_OBSERVE_REGISTER)
72 std::cout << "Observe registration action is successful" << std::endl;
75 std::cout << "OBSERVE RESULT:"<<std::endl;
76 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
77 rep.getValue("state", mylight.m_state);
78 rep.getValue("power", mylight.m_power);
79 rep.getValue("name", mylight.m_name);
81 std::cout << "\tstate: " << mylight.m_state << std::endl;
82 std::cout << "\tpower: " << mylight.m_power << std::endl;
83 std::cout << "\tname: " << mylight.m_name << std::endl;
85 if(observe_count() == 11)
87 std::cout<<"Cancelling Observe..."<<std::endl;
88 OCStackResult result = curResource->cancelObserve();
90 std::cout << "Cancel result: "<< result <<std::endl;
92 std::cout << "DONE"<<std::endl;
98 if(eCode == OC_STACK_OK)
100 std::cout << "Observe registration failed or de-registration action failed/succeeded" << std::endl;
104 std::cout << "onObserve Response error: " << eCode << std::endl;
109 catch(std::exception& e)
111 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
116 void onPost2(const HeaderOptions& /*headerOptions*/,
117 const OCRepresentation& rep, const int eCode)
121 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
123 std::cout << "POST request was successful" << std::endl;
125 if(rep.hasAttribute("createduri"))
127 std::cout << "\tUri of the created resource: "
128 << rep.getValue<std::string>("createduri") << std::endl;
132 rep.getValue("state", mylight.m_state);
133 rep.getValue("power", mylight.m_power);
134 rep.getValue("name", mylight.m_name);
136 std::cout << "\tstate: " << mylight.m_state << std::endl;
137 std::cout << "\tpower: " << mylight.m_power << std::endl;
138 std::cout << "\tname: " << mylight.m_name << std::endl;
141 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
142 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
143 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
144 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
146 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
151 std::cout << "onPost2 Response error: " << eCode << std::endl;
155 catch(std::exception& e)
157 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
162 void onPost(const HeaderOptions& /*headerOptions*/,
163 const OCRepresentation& rep, const int eCode)
167 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
169 std::cout << "POST request was successful" << std::endl;
171 if(rep.hasAttribute("createduri"))
173 std::cout << "\tUri of the created resource: "
174 << rep.getValue<std::string>("createduri") << std::endl;
178 rep.getValue("state", mylight.m_state);
179 rep.getValue("power", mylight.m_power);
180 rep.getValue("name", mylight.m_name);
182 std::cout << "\tstate: " << mylight.m_state << std::endl;
183 std::cout << "\tpower: " << mylight.m_power << std::endl;
184 std::cout << "\tname: " << mylight.m_name << std::endl;
187 OCRepresentation rep2;
189 std::cout << "Posting light representation..."<<std::endl;
191 mylight.m_state = true;
192 mylight.m_power = 55;
194 rep2.setValue("state", mylight.m_state);
195 rep2.setValue("power", mylight.m_power);
197 curResource->post(rep2, QueryParamsMap(), &onPost2);
201 std::cout << "onPost Response error: " << eCode << std::endl;
205 catch(std::exception& e)
207 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
211 // Local function to put a different state for this resource
212 void postLightRepresentation(std::shared_ptr<OCResource> resource)
216 OCRepresentation rep;
218 std::cout << "Posting light representation..."<<std::endl;
220 mylight.m_state = false;
221 mylight.m_power = 105;
223 rep.setValue("state", mylight.m_state);
224 rep.setValue("power", mylight.m_power);
226 // Invoke resource's post API with rep, query map and the callback parameter
227 resource->post(rep, QueryParamsMap(), &onPost);
231 // callback handler on PUT request
232 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
236 if(eCode == OC_STACK_OK)
238 std::cout << "PUT request was successful" << std::endl;
240 rep.getValue("state", mylight.m_state);
241 rep.getValue("power", mylight.m_power);
242 rep.getValue("name", mylight.m_name);
244 std::cout << "\tstate: " << mylight.m_state << std::endl;
245 std::cout << "\tpower: " << mylight.m_power << std::endl;
246 std::cout << "\tname: " << mylight.m_name << std::endl;
248 postLightRepresentation(curResource);
252 std::cout << "onPut Response error: " << eCode << std::endl;
256 catch(std::exception& e)
258 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
262 // Local function to put a different state for this resource
263 void putLightRepresentation(std::shared_ptr<OCResource> resource)
267 OCRepresentation rep;
269 std::cout << "Putting light representation..."<<std::endl;
271 mylight.m_state = true;
272 mylight.m_power = 15;
274 rep.setValue("state", mylight.m_state);
275 rep.setValue("power", mylight.m_power);
277 // Invoke resource's put API with rep, query map and the callback parameter
278 resource->put(rep, QueryParamsMap(), &onPut);
282 // Callback handler on GET request
283 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
287 if(eCode == OC_STACK_OK)
289 std::cout << "GET request was successful" << std::endl;
290 std::cout << "Resource URI: " << rep.getUri() << std::endl;
292 rep.getValue("state", mylight.m_state);
293 rep.getValue("power", mylight.m_power);
294 rep.getValue("name", mylight.m_name);
296 std::cout << "\tstate: " << mylight.m_state << std::endl;
297 std::cout << "\tpower: " << mylight.m_power << std::endl;
298 std::cout << "\tname: " << mylight.m_name << std::endl;
300 putLightRepresentation(curResource);
304 std::cout << "onGET Response error: " << eCode << std::endl;
308 catch(std::exception& e)
310 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
314 // Local function to get representation of light resource
315 void getLightRepresentation(std::shared_ptr<OCResource> resource)
319 std::cout << "Getting Light Representation..."<<std::endl;
320 // Invoke resource's get API with the callback parameter
323 resource->get(test, &onGet);
327 // Callback to found resources
328 void foundResource(std::shared_ptr<OCResource> resource)
330 std::cout << "In foundResource\n";
331 std::string resourceURI;
332 std::string hostAddress;
336 std::lock_guard<std::mutex> lock(curResourceLock);
337 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
339 std::cout << "Found resource " << resource->uniqueIdentifier() <<
340 " for the first time on server with ID: "<< resource->sid()<<std::endl;
341 discoveredResources[resource->uniqueIdentifier()] = resource;
345 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
350 std::cout << "Found another resource, ignoring"<<std::endl;
355 // Do some operations with resource object.
358 std::cout<<"DISCOVERED Resource:"<<std::endl;
359 // Get the resource URI
360 resourceURI = resource->uri();
361 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
363 // Get the resource host address
364 hostAddress = resource->host();
365 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
367 // Get the resource types
368 std::cout << "\tList of resource types: " << std::endl;
369 for(auto &resourceTypes : resource->getResourceTypes())
371 std::cout << "\t\t" << resourceTypes << std::endl;
374 // Get the resource interfaces
375 std::cout << "\tList of resource interfaces: " << std::endl;
376 for(auto &resourceInterfaces : resource->getResourceInterfaces())
378 std::cout << "\t\t" << resourceInterfaces << std::endl;
381 if(resourceURI == "/a/light")
383 curResource = resource;
384 // Call a local function which will internally invoke get API on the resource pointer
385 getLightRepresentation(resource);
390 // Resource is invalid
391 std::cout << "Resource is invalid" << std::endl;
395 catch(std::exception& e)
397 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
403 std::cout << std::endl;
404 std::cout << "---------------------------------------------------------------------\n";
405 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
406 std::cout << " ObserveType : 1 - Observe" << std::endl;
407 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
408 std::cout << "---------------------------------------------------------------------\n\n";
411 void checkObserverValue(int value)
415 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
416 std::cout << "<===Setting ObserveType to Observe===>\n\n";
420 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
421 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
425 std::cout << "<===Invalid ObserveType selected."
426 <<" Setting ObserveType to Observe===>\n\n";
430 static FILE* client_open(const char* /*path*/, const char *mode)
432 return fopen(SVR_DB_FILE_NAME, mode);
435 int main(int argc, char* argv[]) {
437 std::ostringstream requestURI;
438 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
444 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
448 checkObserverValue(std::stoi(argv[1]));
452 std::cout << "<===Invalid number of command line arguments===>\n\n";
456 catch(std::exception& )
458 std::cout << "<===Invalid input arguments===>\n\n";
462 // Create PlatformConfig object
464 OC::ServiceType::InProc,
468 OC::QualityOfService::HighQos,
472 OCPlatform::Configure(cfg);
475 // makes it so that all boolean values are printed as 'true/false' in this stream
476 std::cout.setf(std::ios::boolalpha);
477 // Find all resources
478 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
480 OCPlatform::findResource("", requestURI.str(),
481 CT_DEFAULT, &foundResource);
482 std::cout<< "Finding Resource... " <<std::endl;
484 // Find resource is done twice so that we discover the original resources a second time.
485 // These resources will have the same uniqueidentifier (yet be different objects), so that
486 // we can verify/show the duplicate-checking code in foundResource(above);
487 OCPlatform::findResource("", requestURI.str(),
488 CT_DEFAULT, &foundResource);
489 std::cout<< "Finding Resource for second time..." << std::endl;
491 // A condition variable will free the mutex it is given, then do a non-
492 // intensive block until 'notify' is called on it. In this case, since we
493 // don't ever call cv.notify, this should be a non-processor intensive version
496 std::condition_variable cv;
497 std::unique_lock<std::mutex> lock(blocker);
500 }catch(OCException& e)
502 oclog() << "Exception in main: "<<e.what();