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 != OC_OBSERVE_NO_OPTION)
70 if(sequenceNumber == OC_OBSERVE_REGISTER)
72 std::cout << "Observe registration action is successful" << std::endl;
74 else if(sequenceNumber == OC_OBSERVE_DEREGISTER)
76 std::cout << "Observe De-registration action is successful" << std::endl;
79 std::cout << "OBSERVE RESULT:"<<std::endl;
80 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
81 rep.getValue("state", mylight.m_state);
82 rep.getValue("power", mylight.m_power);
83 rep.getValue("name", mylight.m_name);
85 std::cout << "\tstate: " << mylight.m_state << std::endl;
86 std::cout << "\tpower: " << mylight.m_power << std::endl;
87 std::cout << "\tname: " << mylight.m_name << std::endl;
89 if(observe_count() == 11)
91 std::cout<<"Cancelling Observe..."<<std::endl;
92 OCStackResult result = curResource->cancelObserve();
94 std::cout << "Cancel result: "<< result <<std::endl;
96 std::cout << "DONE"<<std::endl;
102 if(sequenceNumber == OC_OBSERVE_NO_OPTION)
104 std::cout << "Observe registration or de-registration action is failed" << std::endl;
108 std::cout << "onObserve Response error: " << eCode << std::endl;
113 catch(std::exception& e)
115 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
120 void onPost2(const HeaderOptions& /*headerOptions*/,
121 const OCRepresentation& rep, const int eCode)
125 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
127 std::cout << "POST request was successful" << std::endl;
129 if(rep.hasAttribute("createduri"))
131 std::cout << "\tUri of the created resource: "
132 << rep.getValue<std::string>("createduri") << std::endl;
136 rep.getValue("state", mylight.m_state);
137 rep.getValue("power", mylight.m_power);
138 rep.getValue("name", mylight.m_name);
140 std::cout << "\tstate: " << mylight.m_state << std::endl;
141 std::cout << "\tpower: " << mylight.m_power << std::endl;
142 std::cout << "\tname: " << mylight.m_name << std::endl;
145 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
146 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
147 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
148 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
150 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
155 std::cout << "onPost2 Response error: " << eCode << std::endl;
159 catch(std::exception& e)
161 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
166 void onPost(const HeaderOptions& /*headerOptions*/,
167 const OCRepresentation& rep, const int eCode)
171 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
173 std::cout << "POST request was successful" << std::endl;
175 if(rep.hasAttribute("createduri"))
177 std::cout << "\tUri of the created resource: "
178 << rep.getValue<std::string>("createduri") << std::endl;
182 rep.getValue("state", mylight.m_state);
183 rep.getValue("power", mylight.m_power);
184 rep.getValue("name", mylight.m_name);
186 std::cout << "\tstate: " << mylight.m_state << std::endl;
187 std::cout << "\tpower: " << mylight.m_power << std::endl;
188 std::cout << "\tname: " << mylight.m_name << std::endl;
191 OCRepresentation rep2;
193 std::cout << "Posting light representation..."<<std::endl;
195 mylight.m_state = true;
196 mylight.m_power = 55;
198 rep2.setValue("state", mylight.m_state);
199 rep2.setValue("power", mylight.m_power);
201 curResource->post(rep2, QueryParamsMap(), &onPost2);
205 std::cout << "onPost Response error: " << eCode << std::endl;
209 catch(std::exception& e)
211 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
215 // Local function to put a different state for this resource
216 void postLightRepresentation(std::shared_ptr<OCResource> resource)
220 OCRepresentation rep;
222 std::cout << "Posting light representation..."<<std::endl;
224 mylight.m_state = false;
225 mylight.m_power = 105;
227 rep.setValue("state", mylight.m_state);
228 rep.setValue("power", mylight.m_power);
230 // Invoke resource's post API with rep, query map and the callback parameter
231 resource->post(rep, QueryParamsMap(), &onPost);
235 // callback handler on PUT request
236 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
240 if(eCode == OC_STACK_OK)
242 std::cout << "PUT request was successful" << std::endl;
244 rep.getValue("state", mylight.m_state);
245 rep.getValue("power", mylight.m_power);
246 rep.getValue("name", mylight.m_name);
248 std::cout << "\tstate: " << mylight.m_state << std::endl;
249 std::cout << "\tpower: " << mylight.m_power << std::endl;
250 std::cout << "\tname: " << mylight.m_name << std::endl;
252 postLightRepresentation(curResource);
256 std::cout << "onPut Response error: " << eCode << std::endl;
260 catch(std::exception& e)
262 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
266 // Local function to put a different state for this resource
267 void putLightRepresentation(std::shared_ptr<OCResource> resource)
271 OCRepresentation rep;
273 std::cout << "Putting light representation..."<<std::endl;
275 mylight.m_state = true;
276 mylight.m_power = 15;
278 rep.setValue("state", mylight.m_state);
279 rep.setValue("power", mylight.m_power);
281 // Invoke resource's put API with rep, query map and the callback parameter
282 resource->put(rep, QueryParamsMap(), &onPut);
286 // Callback handler on GET request
287 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
291 if(eCode == OC_STACK_OK)
293 std::cout << "GET request was successful" << std::endl;
294 std::cout << "Resource URI: " << rep.getUri() << std::endl;
296 rep.getValue("state", mylight.m_state);
297 rep.getValue("power", mylight.m_power);
298 rep.getValue("name", mylight.m_name);
300 std::cout << "\tstate: " << mylight.m_state << std::endl;
301 std::cout << "\tpower: " << mylight.m_power << std::endl;
302 std::cout << "\tname: " << mylight.m_name << std::endl;
304 putLightRepresentation(curResource);
308 std::cout << "onGET Response error: " << eCode << std::endl;
312 catch(std::exception& e)
314 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
318 // Local function to get representation of light resource
319 void getLightRepresentation(std::shared_ptr<OCResource> resource)
323 std::cout << "Getting Light Representation..."<<std::endl;
324 // Invoke resource's get API with the callback parameter
327 resource->get(test, &onGet);
331 // Callback to found resources
332 void foundResource(std::shared_ptr<OCResource> resource)
334 std::cout << "In foundResource\n";
335 std::string resourceURI;
336 std::string hostAddress;
340 std::lock_guard<std::mutex> lock(curResourceLock);
341 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
343 std::cout << "Found resource " << resource->uniqueIdentifier() <<
344 " for the first time on server with ID: "<< resource->sid()<<std::endl;
345 discoveredResources[resource->uniqueIdentifier()] = resource;
349 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
354 std::cout << "Found another resource, ignoring"<<std::endl;
359 // Do some operations with resource object.
362 std::cout<<"DISCOVERED Resource:"<<std::endl;
363 // Get the resource URI
364 resourceURI = resource->uri();
365 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
367 // Get the resource host address
368 hostAddress = resource->host();
369 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
371 // Get the resource types
372 std::cout << "\tList of resource types: " << std::endl;
373 for(auto &resourceTypes : resource->getResourceTypes())
375 std::cout << "\t\t" << resourceTypes << std::endl;
378 // Get the resource interfaces
379 std::cout << "\tList of resource interfaces: " << std::endl;
380 for(auto &resourceInterfaces : resource->getResourceInterfaces())
382 std::cout << "\t\t" << resourceInterfaces << std::endl;
385 if(resourceURI == "/a/light")
387 curResource = resource;
388 // Call a local function which will internally invoke get API on the resource pointer
389 getLightRepresentation(resource);
394 // Resource is invalid
395 std::cout << "Resource is invalid" << std::endl;
399 catch(std::exception& e)
401 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
407 std::cout << std::endl;
408 std::cout << "---------------------------------------------------------------------\n";
409 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
410 std::cout << " ObserveType : 1 - Observe" << std::endl;
411 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
412 std::cout << "---------------------------------------------------------------------\n\n";
415 void checkObserverValue(int value)
419 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
420 std::cout << "<===Setting ObserveType to Observe===>\n\n";
424 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
425 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
429 std::cout << "<===Invalid ObserveType selected."
430 <<" Setting ObserveType to Observe===>\n\n";
434 static FILE* client_open(const char* /*path*/, const char *mode)
436 return fopen(SVR_DB_FILE_NAME, mode);
439 int main(int argc, char* argv[]) {
441 std::ostringstream requestURI;
442 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
448 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
452 checkObserverValue(std::stoi(argv[1]));
456 std::cout << "<===Invalid number of command line arguments===>\n\n";
460 catch(std::exception& )
462 std::cout << "<===Invalid input arguments===>\n\n";
466 // Create PlatformConfig object
468 OC::ServiceType::InProc,
472 OC::QualityOfService::LowQos,
476 OCPlatform::Configure(cfg);
479 // makes it so that all boolean values are printed as 'true/false' in this stream
480 std::cout.setf(std::ios::boolalpha);
481 // Find all resources
482 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
484 OCPlatform::findResource("", requestURI.str(),
485 CT_DEFAULT, &foundResource);
486 std::cout<< "Finding Resource... " <<std::endl;
488 // Find resource is done twice so that we discover the original resources a second time.
489 // These resources will have the same uniqueidentifier (yet be different objects), so that
490 // we can verify/show the duplicate-checking code in foundResource(above);
491 OCPlatform::findResource("", requestURI.str(),
492 CT_DEFAULT, &foundResource);
493 std::cout<< "Finding Resource for second time..." << std::endl;
495 // A condition variable will free the mutex it is given, then do a non-
496 // intensive block until 'notify' is called on it. In this case, since we
497 // don't ever call cv.notify, this should be a non-processor intensive version
500 std::condition_variable cv;
501 std::unique_lock<std::mutex> lock(blocker);
504 }catch(OCException& e)
506 oclog() << "Exception in main: "<<e.what();