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. */
76 * The states of monitoring.
78 * @see startMonitoring
81 enum class ResourceState
83 NONE, /**< Monitoring is not started.*/
84 REQUESTED, /**< Monitoring is started and checking state is in progress.
85 This is the default state after startMonitoring. */
86 ALIVE, /**< The resource is alive. */
87 LOST_SIGNAL, /**< Failed to reach the resource. */
88 DESTROYED /**< The resource is deleted. */
92 * The options available to report cache.
96 enum class CacheReport
98 REPORT_CHANGES, /**< Report only when there is any change in the representation.*/
99 REPORT_ALL /**< Report all irrespective of the representation.*/
102 class PrimitiveResource;
105 * This is to specify query parameters for requests to the server.
107 * @see RCSRemoteResourceObject
112 typedef std::unordered_map< std::string, std::string > Map;
117 * Sets an interface of the resource to operate on
119 * @param interface interface
121 RCSQueryParams& setResourceInterface(std::string interface);
124 * Sets a resource type of the resource to operate on
126 * @param type resource type
128 RCSQueryParams& setResourceType(std::string type);
131 * Sets a resource type of the resource to operate on
133 * @param key key to be inserted
134 * @param value value to be inserted
136 * @note "rt" and "if" are reserved, so you should avoid them as a key.
139 RCSQueryParams& put(std::string key, std::string value);
142 * Returns the resource interface.
144 std::string getResourceInterface() const;
147 * Returns the resource type.
149 std::string getResourceType() const;
154 * @param key key of the element whose mapped value is accessed.
156 * @throws InvalidKeyException If @a key doesn't match the key of any value.
158 std::string get(const std::string& key) const;
161 * Returns all params.
163 const Map& getAll() const;
166 std::string m_resourceInterface;
167 std::string m_resourceType;
169 std::unordered_map< std::string, std::string > m_map;
174 * This represents a remote resource and provides simple ways to interact with it.
175 * Basically this is a client of a remote resource that runs on other device.
177 * The class supports features to help get information of a remote resource
178 * such as monitoring and caching.
180 * @see RCSDiscoveryManager
183 class RCSRemoteResourceObject : public std::enable_shared_from_this<RCSRemoteResourceObject>
186 typedef std::shared_ptr< RCSRemoteResourceObject > Ptr;
189 * Callback definition to be invoked when monitoring state is changed.
191 * @see startMonitioring
194 typedef std::function< void(ResourceState) > StateChangedCallback;
197 * Callback definition to be invoked when cache is updated.
199 * @param attrs the updated attributes
201 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
202 CacheUpdatedCallback;
205 * Callback definition to be invoked when the response of getRemoteAttributes is
208 * @param attrs the result attributes
209 * @param eCode the error code received from the resource
211 * @see getRemoteAttributes
213 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
214 RemoteAttributesGetCallback;
217 * Callback definition to be invoked when the response of get is received.
220 * @param rep the result representation
221 * @param eCode the error code received from the resource
225 typedef std::function< void(const HeaderOpts& headerOpts,
226 const RCSRepresentation& rep, int eCode) > GetCallback;
229 * Callback definition to be invoked when the response of setRemoteAttributes is
232 * @param attrs the result attributes
233 * @param eCode the error code received from the resource
235 * @see setRemoteAttributes
237 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
238 RemoteAttributesSetCallback;
241 * Callback definition to be invoked when the response of set is received.
244 * @param rep the result representation
245 * @param eCode the error code received from the resource
249 typedef std::function< void(const HeaderOpts& headerOpts,
250 const RCSRepresentation& rep, int eCode) > SetCallback;
254 typedef unsigned int BrokerID;
258 RCSRemoteResourceObject(std::shared_ptr< PrimitiveResource >);
261 ~RCSRemoteResourceObject();
264 * Creates an instance from an OCResource instance.
266 * @throw RCSInvalidParameterException If ocResource is nullptr.
268 static RCSRemoteResourceObject::Ptr fromOCResource(
269 std::shared_ptr< OC::OCResource > ocResource);
272 * Returns an equivalent OCResource using RCSRemoteResourceObject instance.
274 * @throw RCSInvalidParameterException If rcsResource is nullptr.
276 static std::shared_ptr< OC::OCResource > toOCResource(
277 RCSRemoteResourceObject::Ptr rcsResource);
280 * Returns whether monitoring is enabled.
282 * @see startMonitoring()
284 bool isMonitoring() const;
287 * Returns whether caching is enabled.
289 * @see startCaching()
292 bool isCaching() const;
295 * Returns whether the resource is observable.
298 bool isObservable() const;
301 * Starts monitoring the resource.
303 * Monitoring provides a feature to check the presence of a resource,
304 * even when the server is not announcing Presence using startPresnece.
306 * @param cb A Callback to get changed resource state.
308 * @throws InvalidParameterException If cb is an empty function or null.
309 * @throws BadRequestException If monitoring is already started.
311 * @note The callback will be invoked in an internal thread.
313 * @see StateChangedCallback
315 * @see isMonitoring()
316 * @see stopMonitoring()
319 void startMonitoring(StateChangedCallback cb);
322 * Stops monitoring the resource.
324 * It does nothing if monitoring is not started.
326 * @see startMonitoring()
329 void stopMonitoring();
332 * Returns the current state of the resource.
334 * @see startMonitoring
336 ResourceState getState() const;
339 * Starts caching attributes of the resource.
341 * This will start caching for the resource.
342 * Once caching started it will look for the data updation on the resource
343 * and updates the cache data accordingly.
345 * It is equivalent to calling startCaching(CacheUpdatedCallback) with an empty function.
347 * @see getCacheState()
348 * @see getCachedAttributes()
349 * @see getCachedAttribute(const std::string&) const
351 * @throws BadRequestException
357 * Starts caching attributes for the resource.
359 * This will start data caching for the resource.
360 * Once caching started it will look for the data updation on the resource and
361 * updates the cached data accordingly.
363 * @param cb If non-empty function, it will be invoked whenever the cache updated.
364 * @param mode if CacheMode is OBSERVE_ONLY, it will be invoked when receive observe response only.
365 * @param reportType if CacheReport is REPORT_CHANGES, it will be invoked when observe response has state changes.
367 * @throws BadRequestException If caching is already started.
369 * @note The callback will be invoked in an internal thread.
371 * @see CacheUpdatedCallback
372 * @see getCacheState()
373 * @see isCachedAvailable()
374 * @see getCachedAttributes()
375 * @see getCachedAttribute(const std::string&) const
378 void startCaching(CacheUpdatedCallback cb, CacheMode mode = CacheMode::OBSERVE_WITH_POLLING,
379 CacheReport reportType = CacheReport::REPORT_CHANGES);
384 * It does nothing if caching is not started.
386 * @see startCaching()
387 * @see startCaching(CacheUpdatedCallback)
392 * Returns the current cache state.
395 CacheState getCacheState() const;
398 * Returns whether cached data is available.
400 * Cache will be available always once cache state had been CacheState::READY
401 * even if current state is CacheState::LOST_SIGNAL.
403 * @see getCacheState()
405 bool isCachedAvailable() const;
408 * Gets the cached RCSResourceAttributes data.
410 * @pre Cache should be available.
412 * @return The cached attributes.
414 * @throws BadRequestException If the precondition is not fulfilled.
416 * @see RCSResourceAttributes
417 * @see isCachedAvailable()
418 * @see startCaching()
419 * @see startCaching(CacheUpdatedCallback)
422 RCSResourceAttributes getCachedAttributes() const;
425 * Gets a particular cached a ResourceAttribute Value.
427 * @pre Cache should be available.
429 * @return A requested attribute value.
431 * @throws BadRequestException If the precondition is not fulfilled.
432 * @throws InvalidKeyException If @a key doesn't match the key of any value.
434 * @see RCSResourceAttributes::Value
435 * @see isCachedAvailable()
436 * @see startCaching()
437 * @see startCaching(CacheUpdatedCallback)
440 RCSResourceAttributes::Value getCachedAttribute(const std::string& key) const;
443 * Gets resource attributes directly from the server.
445 * This API send a get request to the resource of interest and provides
446 * the attributes to the caller in the RemoteAttributesGetCallback.
448 * @throws PlatformException If the operation failed
449 * @throws InvalidParameterException If cb is an empty function or null.
451 * @note The callback will be invoked in an internal thread.
453 void getRemoteAttributes(RemoteAttributesGetCallback cb);
456 * Gets resource representation with empty query parameters directly from the server.
458 * @param cb A callback to receive the response.
460 * @throws PlatformException If the operation failed
461 * @throws InvalidParameterException If cb is an empty function or null.
463 * @note The callback will be invoked in an internal thread.
465 void get(GetCallback cb);
468 * Gets resource representation directly from the server.
470 * The response could be different by the query parameters, it depends on server.
472 * @param queryParams Query parameters
473 * @param cb A callback to receive the response.
475 * @throws PlatformException If the operation failed
476 * @throws InvalidParameterException If cb is an empty function or null.
478 * @note The callback will be invoked in an internal thread.
480 void get(const RCSQueryParams& queryParams, GetCallback cb);
483 * Sends a set request with resource attributes to the server.
485 * The SetRequest behavior depends on the server, whether updating its attributes or not.
487 * @param attributes Attributes to set
488 * @param cb A callback to receive the response.
490 * @throws PlatformException If the operation failed
491 * @throws InvalidParameterException If cb is an empty function or null.
493 * @see RCSResourceObject
494 * @see RCSResourceObject::SetRequestHandlerPolicy
496 * @note The callback will be invoked in an internal thread.
498 void setRemoteAttributes(const RCSResourceAttributes& attributes,
499 RemoteAttributesSetCallback cb);
502 * Sends a set request with resource attributes to the server.
504 * The SetRequest behavior depends on query parameters and the server.
506 * @param attributes Attributes to set
507 * @param cb A callback to receive the response.
509 * @throws PlatformException If the operation failed
510 * @throws InvalidParameterException If cb is an empty function or null.
512 * @see RCSResourceObject
513 * @see RCSResourceObject::SetRequestHandlerPolicy
515 * @note The callback will be invoked in an internal thread.
517 void set(const RCSResourceAttributes& attributes, SetCallback cb);
520 * Sends a set request with resource attributes to the server.
522 * The SetRequest behavior depends on query parameters and the server.
524 * @param queryParams Query parameters
525 * @param attributes Attributes to set
526 * @param cb A callback to receive the response.
528 * @throws PlatformException If the operation failed
529 * @throws InvalidParameterException If cb is an empty function or null.
531 * @see RCSResourceObject
532 * @see RCSResourceObject::SetRequestHandlerPolicy
534 * @note The callback will be invoked in an internal thread.
536 void set(const RCSQueryParams& queryParams, const RCSResourceAttributes& attributes,
540 * Sends a set request with resource representation to the server.
542 * The SetRequest behavior depends on query parameters and the server.
544 * @param queryParams Query parameters
545 * @param rep Representation to set
546 * @param cb A callback to receive the response.
548 * @throws PlatformException If the operation failed
549 * @throws InvalidParameterException If cb is an empty function or null.
551 * @see RCSResourceObject
552 * @see RCSResourceObject::SetRequestHandlerPolicy
554 * @note The callback will be invoked in an internal thread.
556 void set(const RCSQueryParams& queryParams, const RCSRepresentation &rep,
560 * Returns the uri of the resource.
563 std::string getUri() const;
566 * Returns the address of the resource .
569 std::string getAddress() const;
572 * Returns the resource types of the resource.
575 std::vector< std::string > getTypes() const;
578 * Returns the resource interfaces of the resource.
581 std::vector< std::string > getInterfaces() const;
584 std::weak_ptr< RCSRemoteResourceObject > weakFromThis();
585 std::shared_ptr< PrimitiveResource > m_primitiveResource;
591 #endif // RCSREMOTERESOURCEOBJECT_H