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. */
91 class PrimitiveResource;
94 * This is to specify query parameters for requests to the server.
96 * @see RCSRemoteResourceObject
101 typedef std::unordered_map< std::string, std::string > Map;
106 * Sets an interface of the resource to operate on
108 * @param interface interface
110 RCSQueryParams& setResourceInterface(std::string interface);
113 * Sets a resource type of the resource to operate on
115 * @param type resource type
117 RCSQueryParams& setResourceType(std::string type);
120 * Sets a resource type of the resource to operate on
122 * @param key key to be inserted
123 * @param value value to be inserted
125 * @note "rt" and "if" are reserved, so you should avoid them as a key.
128 RCSQueryParams& put(std::string key, std::string value);
131 * Returns the resource interface.
133 std::string getResourceInterface() const;
136 * Returns the resource type.
138 std::string getResourceType() const;
143 * @param key key of the element whose mapped value is accessed.
145 * @throws InvalidKeyException If @a key doesn't match the key of any value.
147 std::string get(const std::string& key) const;
150 * Returns all params.
152 const Map& getAll() const;
155 std::string m_resourceInterface;
156 std::string m_resourceType;
158 std::unordered_map< std::string, std::string > m_map;
163 * This represents a remote resource and provides simple ways to interact with it.
164 * Basically this is a client of a remote resource that runs on other device.
166 * The class supports features to help get information of a remote resource
167 * such as monitoring and caching.
169 * @see RCSDiscoveryManager
172 class RCSRemoteResourceObject
175 typedef std::shared_ptr< RCSRemoteResourceObject > Ptr;
178 * Callback definition to be invoked when monitoring state is changed.
180 * @see startMonitioring
183 typedef std::function< void(ResourceState) > StateChangedCallback;
186 * Callback definition to be invoked when cache is updated.
188 * @param attrs the updated attributes
190 typedef std::function< void(const RCSResourceAttributes& attrs) > CacheUpdatedCallback;
193 * Callback definition to be invoked when the response of getRemoteAttributes is
196 * @param attrs the result attributes
197 * @param eCode the error code received from the resource
199 * @see getRemoteAttributes
201 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
202 RemoteAttributesGetCallback;
205 * Callback definition to be invoked when the response of get is received.
208 * @param rep the result representation
209 * @param eCode the error code received from the resource
213 typedef std::function< void(const HeaderOpts& headerOpts,
214 const RCSRepresentation& rep, int eCode) > GetCallback;
217 * Callback definition to be invoked when the response of setRemoteAttributes is
220 * @param attrs the result attributes
221 * @param eCode the error code received from the resource
223 * @see setRemoteAttributes
225 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
226 RemoteAttributesSetCallback;
229 * Callback definition to be invoked when the response of set is received.
232 * @param rep the result representation
233 * @param eCode the error code received from the resource
237 typedef std::function< void(const HeaderOpts& headerOpts,
238 const RCSRepresentation& rep, int eCode) > SetCallback;
242 typedef unsigned int BrokerID;
246 RCSRemoteResourceObject(std::shared_ptr< PrimitiveResource >);
249 ~RCSRemoteResourceObject();
252 * Creates an instance from an OCResource instance.
254 * @throw RCSInvalidParameterException If ocResource is nullptr.
256 static RCSRemoteResourceObject::Ptr fromOCResource(
257 std::shared_ptr< OC::OCResource > ocResource);
260 * Returns an equivalent OCResource using RCSRemoteResourceObject instance.
262 * @throw RCSInvalidParameterException If rcsResource is nullptr.
264 static std::shared_ptr< OC::OCResource > toOCResource(
265 RCSRemoteResourceObject::Ptr rcsResource);
268 * Returns whether monitoring is enabled.
270 * @see startMonitoring()
272 bool isMonitoring() const;
275 * Returns whether caching is enabled.
277 * @see startCaching()
280 bool isCaching() const;
283 * Returns whether the resource is observable.
286 bool isObservable() const;
289 * Starts monitoring the resource.
291 * Monitoring provides a feature to check the presence of a resource,
292 * even when the server is not announcing Presence using startPresnece.
294 * @param cb A Callback to get changed resource state.
296 * @throws InvalidParameterException If cb is an empty function or null.
297 * @throws BadRequestException If monitoring is already started.
299 * @note The callback will be invoked in an internal thread.
301 * @see StateChangedCallback
303 * @see isMonitoring()
304 * @see stopMonitoring()
307 void startMonitoring(StateChangedCallback cb);
310 * Stops monitoring the resource.
312 * It does nothing if monitoring is not started.
314 * @see startMonitoring()
317 void stopMonitoring();
320 * Returns the current state of the resource.
322 * @see startMonitoring
324 ResourceState getState() const;
327 * Starts caching attributes of the resource.
329 * This will start caching for the resource.
330 * Once caching started it will look for the data updation on the resource
331 * and updates the cache data accordingly.
333 * It is equivalent to calling startCaching(CacheUpdatedCallback) with an empty function.
335 * @see getCacheState()
336 * @see getCachedAttributes()
337 * @see getCachedAttribute(const std::string&) const
339 * @throws BadRequestException
345 * Starts caching attributes for the resource.
347 * This will start data caching for the resource.
348 * Once caching started it will look for the data updation on the resource and
349 * updates the cached data accordingly.
351 * @param cb If non-empty function, it will be invoked whenever the cache updated.
352 * @param mode if CacheMode is OBSERVE_ONLY, it will be invoked when receive observe response only.
354 * @throws BadRequestException If caching is already started.
356 * @note The callback will be invoked in an internal thread.
358 * @see CacheUpdatedCallback
359 * @see getCacheState()
360 * @see isCachedAvailable()
361 * @see getCachedAttributes()
362 * @see getCachedAttribute(const std::string&) const
365 void startCaching(CacheUpdatedCallback cb, CacheMode mode = CacheMode::OBSERVE_WITH_POLLING);
370 * It does nothing if caching is not started.
372 * @see startCaching()
373 * @see startCaching(CacheUpdatedCallback)
378 * Returns the current cache state.
381 CacheState getCacheState() const;
384 * Returns whether cached data is available.
386 * Cache will be available always once cache state had been CacheState::READY
387 * even if current state is CacheState::LOST_SIGNAL.
389 * @see getCacheState()
391 bool isCachedAvailable() const;
394 * Gets the cached RCSResourceAttributes data.
396 * @pre Cache should be available.
398 * @return The cached attributes.
400 * @throws BadRequestException If the precondition is not fulfilled.
402 * @see RCSResourceAttributes
403 * @see isCachedAvailable()
404 * @see startCaching()
405 * @see startCaching(CacheUpdatedCallback)
408 RCSResourceAttributes getCachedAttributes() const;
411 * Gets a particular cached a ResourceAttribute Value.
413 * @pre Cache should be available.
415 * @return A requested attribute value.
417 * @throws BadRequestException If the precondition is not fulfilled.
418 * @throws InvalidKeyException If @a key doesn't match the key of any value.
420 * @see RCSResourceAttributes::Value
421 * @see isCachedAvailable()
422 * @see startCaching()
423 * @see startCaching(CacheUpdatedCallback)
426 RCSResourceAttributes::Value getCachedAttribute(const std::string& key) const;
429 * Gets resource attributes directly from the server.
431 * This API send a get request to the resource of interest and provides
432 * the attributes to the caller in the RemoteAttributesGetCallback.
434 * @throws PlatformException If the operation failed
435 * @throws InvalidParameterException If cb is an empty function or null.
437 * @note The callback will be invoked in an internal thread.
439 void getRemoteAttributes(RemoteAttributesGetCallback cb);
442 * Gets resource representation with empty query parameters directly from the server.
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(GetCallback cb);
454 * Gets resource representation directly from the server.
456 * The response could be different by the query parameters, it depends on server.
458 * @param queryParams Query parameters
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 * @note The callback will be invoked in an internal thread.
466 void get(const RCSQueryParams& queryParams, GetCallback cb);
469 * Sends a set request with resource attributes to the server.
471 * The SetRequest behavior depends on the server, whether updating its attributes or not.
473 * @param attributes Attributes to set
474 * @param cb A callback to receive the response.
476 * @throws PlatformException If the operation failed
477 * @throws InvalidParameterException If cb is an empty function or null.
479 * @see RCSResourceObject
480 * @see RCSResourceObject::SetRequestHandlerPolicy
482 * @note The callback will be invoked in an internal thread.
484 void setRemoteAttributes(const RCSResourceAttributes& attributes,
485 RemoteAttributesSetCallback cb);
488 * Sends a set request with resource attributes to the server.
490 * The SetRequest behavior depends on query parameters and the server.
492 * @param attributes Attributes to set
493 * @param cb A callback to receive the response.
495 * @throws PlatformException If the operation failed
496 * @throws InvalidParameterException If cb is an empty function or null.
498 * @see RCSResourceObject
499 * @see RCSResourceObject::SetRequestHandlerPolicy
501 * @note The callback will be invoked in an internal thread.
503 void set(const RCSResourceAttributes& attributes, SetCallback cb);
506 * Sends a set request with resource attributes to the server.
508 * The SetRequest behavior depends on query parameters and the server.
510 * @param queryParams Query parameters
511 * @param attributes Attributes to set
512 * @param cb A callback to receive the response.
514 * @throws PlatformException If the operation failed
515 * @throws InvalidParameterException If cb is an empty function or null.
517 * @see RCSResourceObject
518 * @see RCSResourceObject::SetRequestHandlerPolicy
520 * @note The callback will be invoked in an internal thread.
522 void set(const RCSQueryParams& queryParams, const RCSResourceAttributes& attributes,
526 * Sends a set request with resource representation to the server.
528 * The SetRequest behavior depends on query parameters and the server.
530 * @param queryParams Query parameters
531 * @param rep Representation to set
532 * @param cb A callback to receive the response.
534 * @throws PlatformException If the operation failed
535 * @throws InvalidParameterException If cb is an empty function or null.
537 * @see RCSResourceObject
538 * @see RCSResourceObject::SetRequestHandlerPolicy
540 * @note The callback will be invoked in an internal thread.
542 void set(const RCSQueryParams& queryParams, const RCSRepresentation &rep,
546 * Returns the uri of the resource.
549 std::string getUri() const;
552 * Returns the address of the resource .
555 std::string getAddress() const;
558 * Returns the resource types of the resource.
561 std::vector< std::string > getTypes() const;
564 * Returns the resource interfaces of the resource.
567 std::vector< std::string > getInterfaces() const;
570 std::shared_ptr< PrimitiveResource > m_primitiveResource;
576 #endif // RCSREMOTERESOURCEOBJECT_H