2 /* Example client program (this is not a library header, don't include it in random programs): */
4 namespace Intel { namespace OCDemo { namespace client {
6 // Although not "done" here, this could be expanded into an interface to handle any sort of
10 friend class resource_handler;
13 const std::string URI;
14 std::shared_ptr<OC::OCResource> resource;
17 resource_handle(const std::string& URI_, std::shared_ptr<OC::OCResource> resource_)
22 resource_handle(const std::string& URI_)
26 // Callbacks (note that the signature after binding will match exactly:
28 void onFoundResource(std::shared_ptr<OC::OCResource> in_resource);
29 void onResourceGet(const OC::HeaderOptions& headerOptions,
30 OC::OCRepresentation rep, const int error_code);
31 void onResourcePut(const OC::HeaderOptions& headerOptions, const OC::OCRepresentation rep,
32 const int error_code);
33 void onObserve(const OC::HeaderOptions& headerOptions, const OC::OCRepresentation rep,
34 const int error_code, const int& sequence_number);
37 class resource_handler
40 static std::vector<std::shared_ptr<resource_handle>> resources; // URI -> Maybe resource
43 resource_handler(const std::vector<std::string>& resource_URIs_);
47 bool has(const std::string& URI)
49 for(const auto& r : resources)
58 void add(const std::string& URI)
61 resources.emplace_back(std::make_shared<resource_handle>(URI));
66 for(const auto& resource : resources)
68 std::cout << "* Finding resources \"" << resource->URI << "\".\n";
70 call_timer.mark("find_resources");
72 OC::OCPlatform::findResource("", resource->URI,
73 std::bind(&resource_handle::onFoundResource, resource, std::placeholders::_1));
78 std::vector<std::shared_ptr<resource_handle>> resource_handler::resources;
80 resource_handler::resource_handler(const std::vector<std::string>& resource_URIs)
82 for(const auto& URI : resource_URIs)
86 void resource_handle::onFoundResource(std::shared_ptr<OC::OCResource> in_resource)
90 cout << "* onFoundResource():\n";
94 if(nullptr == in_resource)
95 throw OC::OCException("invalid resource passed to client callback");
97 call_timer.report_and_reset("find_resources");
99 // Now, fixup our own representation ptr:
100 resource = in_resource;
102 /* Note: You can combine the host and URI to get a unique identifier, for
103 example to filter out events you aren't interested in. Here, we just report the
105 cout << "resource URI: " << resource->uri() << "; "
106 << "host address: " << resource->host() << '\n';
108 call_timer.mark("get_resource");
110 OC::QueryParamsMap qpm;
112 resource->get(qpm, std::bind(&resource_handle::onResourceGet, this,
113 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
115 catch(OC::OCException& e)
117 std::cerr << "onFoundResource(): exception " << e.reason() << ": " << e.what() << '\n';
119 catch(std::exception& e)
121 std::cerr << "onFoundResource(): exception: " << e.what() << '\n';
125 void resource_handle::onResourceGet(const OC::HeaderOptions& headerOptions,
126 const OC::OCRepresentation rep, const int error_code)
130 cout << "onResourceGet():\n";
132 call_timer.report_and_reset("get_resource");
136 std::cerr << "onResourceGet(): error: " << error_code << '\n';
140 if(nullptr == resource)
142 std::cerr << "onResourceGet(): empty resource pointer.\n";
146 std::cout << "input attributes:\n" << rep.getAttributeMap() << '\n';
148 // Now, make a change to the light representation (replacing, rather than parsing):
149 OC::AttributeMap attrs {
150 { "state", { "true" } },
151 { "power", { "10" } }
154 std::cout << "output attributes:\n" << attrs << '\n';
156 call_timer.mark("put_resource");
158 OC::OCRepresentation out_rep;
159 out_rep.setAttributeMap(attrs);
161 resource->put(out_rep, OC::QueryParamsMap(),
162 std::bind(&resource_handle::onResourcePut, this, std::placeholders::_1,
163 std::placeholders::_2, std::placeholders::_3));
166 void resource_handle::onResourcePut(const OC::HeaderOptions& headerOptions,
167 const OC::OCRepresentation rep, const int error_code)
169 std::cout << "onResourcePut():\n";
171 call_timer.report_and_reset("put_resource");
175 std::ostringstream os;
177 os << "onResourcePut(): error code " << error_code << " from server response.";
179 throw OC::OCException(os.str());
182 std::cout << "input attributes:\n" << rep.getAttributeMap() << '\n';
184 call_timer.mark("observe_resource");
186 // Start an observer:
187 resource->observe(OC::ObserveType::Observe, OC::QueryParamsMap(),
188 std::bind(&resource_handle::onObserve, this,
189 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
190 std::placeholders::_4));
193 void resource_handle::onObserve(const OC::HeaderOptions& headerOptions,
194 const OC::OCRepresentation rep, const int error_code, const int& sequence_number)
198 std::ostringstream os;
199 os << "onObserve(): error " << error_code << " from callback.\n";
200 throw OC::OCException(os.str());
203 std::cout << "onObserve(): sequence number: " << sequence_number << ":\n";
205 call_timer.report_and_reset("observe_resource");
207 std::cout << rep.getAttributeMap() << '\n';
209 const auto oc = observe_count();
211 std::cout << "onObserve(): observation count is: " << oc << '\n';
213 // We don't want to be observed forever for purposes of this demo:
216 std::cout << "onObserve(): cancelling observation.\n";
218 const auto result = resource->cancelObserve();
220 std::cout << "onObserve(): result of cancellation: " << result << ".\n";
222 this_thread::sleep_for(chrono::seconds(10));
226 }}} // namespace Intel::OCDemo::client