1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the declaration of classes and its members related to RCSRemoteResourceObject
27 #ifndef RCSREMOTERESOURCEOBJECT_H
28 #define RCSREMOTERESOURCEOBJECT_H
32 #include "RCSResourceAttributes.h"
33 #include "RCSRepresentation.h"
39 namespace HeaderOption
50 class RCSRepresentation;
52 typedef std::vector< OC::HeaderOption::OCHeaderOption > HeaderOpts;
55 * The states of caching.
62 NONE, /**< Caching is not started.*/
63 UNREADY, /**< Caching is started, but the data is not ready yet.
64 This is the default state after startCaching. */
65 READY, /**< The data is ready.*/
66 LOST_SIGNAL, /**< Failed to reach the resource. */
70 * The states of monitoring.
72 * @see startMonitoring
75 enum class ResourceState
77 NONE, /**< Monitoring is not started.*/
78 REQUESTED, /**< Monitoring is started and checking state is in progress.
79 This is the default state after startMonitoring. */
80 ALIVE, /**< The resource is alive. */
81 LOST_SIGNAL, /**< Failed to reach the resource. */
82 DESTROYED /**< The resource is deleted. */
85 class PrimitiveResource;
88 * This is to specify query parameters for requests to the server.
90 * @see RCSRemoteResourceObject
95 typedef std::unordered_map< std::string, std::string > Map;
100 * Sets an interface of the resource to operate on
102 * @param interface interface
104 RCSQueryParams& setResourceInterface(std::string interface);
107 * Sets a resource type of the resource to operate on
109 * @param type resource type
111 RCSQueryParams& setResourceType(std::string type);
114 * Sets a resource type of the resource to operate on
116 * @param key key to be inserted
117 * @param value value to be inserted
119 * @note "rt" and "if" are reserved, so you should avoid them as a key.
122 RCSQueryParams& put(std::string key, std::string value);
125 * Returns the resource interface.
127 std::string getResourceInterface() const;
130 * Returns the resource type.
132 std::string getResourceType() const;
137 * @param key key of the element whose mapped value is accessed.
139 * @throws InvalidKeyException If @a key doesn't match the key of any value.
141 std::string get(const std::string& key) const;
144 * Returns all params.
146 const Map& getAll() const;
149 std::string m_resourceInterface;
150 std::string m_resourceType;
152 std::unordered_map< std::string, std::string > m_map;
157 * This represents a remote resource and provides simple ways to interact with it.
158 * Basically this is a client of a remote resource that runs on other device.
160 * The class supports features to help get information of a remote resource
161 * such as monitoring and caching.
163 * @see RCSDiscoveryManager
166 class RCSRemoteResourceObject
169 typedef std::shared_ptr< RCSRemoteResourceObject > Ptr;
172 * Callback definition to be invoked when monitoring state is changed.
174 * @see startMonitioring
177 typedef std::function< void(ResourceState) > StateChangedCallback;
180 * Callback definition to be invoked when cache is updated.
182 * @param attrs the updated attributes
184 typedef std::function< void(const RCSResourceAttributes& attrs) > CacheUpdatedCallback;
187 * Callback definition to be invoked when the response of getRemoteAttributes is
190 * @param attrs the result attributes
191 * @param eCode the error code received from the resource
193 * @see getRemoteAttributes
195 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
196 RemoteAttributesGetCallback;
199 * Callback definition to be invoked when the response of get is received.
202 * @param rep the result representation
203 * @param eCode the error code received from the resource
207 typedef std::function< void(const HeaderOpts& headerOpts,
208 const RCSRepresentation& rep, int eCode) > GetCallback;
211 * Callback definition to be invoked when the response of setRemoteAttributes is
214 * @param attrs the result attributes
215 * @param eCode the error code received from the resource
217 * @see setRemoteAttributes
219 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
220 RemoteAttributesSetCallback;
223 * Callback definition to be invoked when the response of set is received.
226 * @param rep the result representation
227 * @param eCode the error code received from the resource
231 typedef std::function< void(const HeaderOpts& headerOpts,
232 const RCSRepresentation& rep, int eCode) > SetCallback;
236 typedef unsigned int BrokerID;
240 RCSRemoteResourceObject(std::shared_ptr< PrimitiveResource >);
243 ~RCSRemoteResourceObject();
246 * Creates an instance from an OCResource instance.
248 * @throw RCSInvalidParameterException If ocResource is nullptr.
250 static RCSRemoteResourceObject::Ptr fromOCResource(
251 std::shared_ptr< OC::OCResource > ocResource);
254 * Returns whether monitoring is enabled.
256 * @see startMonitoring()
258 bool isMonitoring() const;
261 * Returns whether caching is enabled.
263 * @see startCaching()
266 bool isCaching() const;
269 * Returns whether the resource is observable.
272 bool isObservable() const;
275 * Starts monitoring the resource.
277 * Monitoring provides a feature to check the presence of a resource,
278 * even when the server is not announcing Presence using startPresnece.
280 * @param cb A Callback to get changed resource state.
282 * @throws InvalidParameterException If cb is an empty function or null.
283 * @throws BadRequestException If monitoring is already started.
285 * @note The callback will be invoked in an internal thread.
287 * @see StateChangedCallback
289 * @see isMonitoring()
290 * @see stopMonitoring()
293 void startMonitoring(StateChangedCallback cb);
296 * Stops monitoring the resource.
298 * It does nothing if monitoring is not started.
300 * @see startMonitoring()
303 void stopMonitoring();
306 * Returns the current state of the resource.
308 * @see startMonitoring
310 ResourceState getState() const;
313 * Starts caching attributes of the resource.
315 * This will start caching for the resource.
316 * Once caching started it will look for the data updation on the resource
317 * and updates the cache data accordingly.
319 * It is equivalent to calling startCaching(CacheUpdatedCallback) with an empty function.
321 * @see getCacheState()
322 * @see getCachedAttributes()
323 * @see getCachedAttribute(const std::string&) const
325 * @throws BadRequestException
331 * Starts caching attributes for the resource.
333 * This will start data caching for the resource.
334 * Once caching started it will look for the data updation on the resource and
335 * updates the cached data accordingly.
337 * @param cb If non-empty function, it will be invoked whenever the cache updated.
339 * @throws BadRequestException If caching is already started.
341 * @note The callback will be invoked in an internal thread.
343 * @see CacheUpdatedCallback
344 * @see getCacheState()
345 * @see isCachedAvailable()
346 * @see getCachedAttributes()
347 * @see getCachedAttribute(const std::string&) const
350 void startCaching(CacheUpdatedCallback cb);
355 * It does nothing if caching is not started.
357 * @see startCaching()
358 * @see startCaching(CacheUpdatedCallback)
363 * Returns the current cache state.
366 CacheState getCacheState() const;
369 * Returns whether cached data is available.
371 * Cache will be available always once cache state had been CacheState::READY
372 * even if current state is CacheState::LOST_SIGNAL.
374 * @see getCacheState()
376 bool isCachedAvailable() const;
379 * Gets the cached RCSResourceAttributes data.
381 * @pre Cache should be available.
383 * @return The cached attributes.
385 * @throws BadRequestException If the precondition is not fulfilled.
387 * @see RCSResourceAttributes
388 * @see isCachedAvailable()
389 * @see startCaching()
390 * @see startCaching(CacheUpdatedCallback)
393 RCSResourceAttributes getCachedAttributes() const;
396 * Gets a particular cached a ResourceAttribute Value.
398 * @pre Cache should be available.
400 * @return A requested attribute value.
402 * @throws BadRequestException If the precondition is not fulfilled.
403 * @throws InvalidKeyException If @a key doesn't match the key of any value.
405 * @see RCSResourceAttributes::Value
406 * @see isCachedAvailable()
407 * @see startCaching()
408 * @see startCaching(CacheUpdatedCallback)
411 RCSResourceAttributes::Value getCachedAttribute(const std::string& key) const;
414 * Gets resource attributes directly from the server.
416 * This API send a get request to the resource of interest and provides
417 * the attributes to the caller in the RemoteAttributesGetCallback.
419 * @throws PlatformException If the operation failed
420 * @throws InvalidParameterException If cb is an empty function or null.
422 * @note The callback will be invoked in an internal thread.
424 void getRemoteAttributes(RemoteAttributesGetCallback cb);
427 * Gets resource representation with empty query parameters directly from the server.
429 * @param cb A callback to receive the response.
431 * @throws PlatformException If the operation failed
432 * @throws InvalidParameterException If cb is an empty function or null.
434 * @note The callback will be invoked in an internal thread.
436 void get(GetCallback cb);
439 * Gets resource representation directly from the server.
441 * The response could be different by the query parameters, it depends on server.
443 * @param queryParams Query parameters
444 * @param cb A callback to receive the response.
446 * @throws PlatformException If the operation failed
447 * @throws InvalidParameterException If cb is an empty function or null.
449 * @note The callback will be invoked in an internal thread.
451 void get(const RCSQueryParams& queryParams, GetCallback cb);
454 * Sends a set request with resource attributes to the server.
456 * The SetRequest behavior depends on the server, whether updating its attributes or not.
458 * @param attributes Attributes to set
459 * @param cb A callback to receive the response.
461 * @throws PlatformException If the operation failed
462 * @throws InvalidParameterException If cb is an empty function or null.
464 * @see RCSResourceObject
465 * @see RCSResourceObject::SetRequestHandlerPolicy
467 * @note The callback will be invoked in an internal thread.
469 void setRemoteAttributes(const RCSResourceAttributes& attributes,
470 RemoteAttributesSetCallback cb);
473 * Sends a set request with resource attributes to the server.
475 * The SetRequest behavior depends on query parameters and the server.
477 * @param attributes Attributes to set
478 * @param cb A callback to receive the response.
480 * @throws PlatformException If the operation failed
481 * @throws InvalidParameterException If cb is an empty function or null.
483 * @see RCSResourceObject
484 * @see RCSResourceObject::SetRequestHandlerPolicy
486 * @note The callback will be invoked in an internal thread.
488 void set(const RCSResourceAttributes& attributes, SetCallback cb);
491 * Sends a set request with resource attributes to the server.
493 * The SetRequest behavior depends on query parameters and the server.
495 * @param queryParams Query parameters
496 * @param attributes Attributes to set
497 * @param cb A callback to receive the response.
499 * @throws PlatformException If the operation failed
500 * @throws InvalidParameterException If cb is an empty function or null.
502 * @see RCSResourceObject
503 * @see RCSResourceObject::SetRequestHandlerPolicy
505 * @note The callback will be invoked in an internal thread.
507 void set(const RCSQueryParams& queryParams, const RCSResourceAttributes& attributes,
511 * Sends a set request with resource representation to the server.
513 * The SetRequest behavior depends on query parameters and the server.
515 * @param queryParams Query parameters
516 * @param rep Representation to set
517 * @param cb A callback to receive the response.
519 * @throws PlatformException If the operation failed
520 * @throws InvalidParameterException If cb is an empty function or null.
522 * @see RCSResourceObject
523 * @see RCSResourceObject::SetRequestHandlerPolicy
525 * @note The callback will be invoked in an internal thread.
527 void set(const RCSQueryParams& queryParams, const RCSRepresentation &rep,
531 * Returns the uri of the resource.
534 std::string getUri() const;
537 * Returns the address of the resource .
540 std::string getAddress() const;
543 * Returns the resource types of the resource.
546 std::vector< std::string > getTypes() const;
549 * Returns the resource interfaces of the resource.
552 std::vector< std::string > getInterfaces() const;
555 std::shared_ptr< PrimitiveResource > m_primitiveResource;
561 #endif // RCSREMOTERESOURCEOBJECT_H