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 Resource Client APIs provided to the developers.
27 #ifndef RCSREMOTERESOURCEOBJECT_H
28 #define RCSREMOTERESOURCEOBJECT_H
32 #include "RCSResourceAttributes.h"
39 * The states of caching.
46 NONE, /**< Caching is not started.*/
47 UNREADY, /**< Caching is started, but the data is not ready yet.
48 This is the default state after startCaching. */
49 READY, /**< The data is ready.*/
50 LOST_SIGNAL, /**< Failed to reach the resource. */
54 * The states of monitoring.
56 * @see startMonitoring
59 enum class ResourceState
61 NONE, /**< Monitoring is not started.*/
62 REQUESTED, /**< Monitoring is started and checking state is in progress.
63 This is the default state after startMonitoring. */
64 ALIVE, /**< The resource is alive. */
65 LOST_SIGNAL, /**< Failed to reach the resource. */
66 DESTROYED /**< The resource is deleted. */
69 class PrimitiveResource;
73 * This represents a remote resource and provides simple ways to interact with it.
74 * Basically this is a client of a remote resource that runs on other device.
76 * The class supports features to help get information of a remote resource
77 * such as monitoring and caching.
79 * @see RCSDiscoveryManager
82 class RCSRemoteResourceObject
85 typedef std::shared_ptr< RCSRemoteResourceObject > Ptr;
88 * Typedef for callback of startMonitoring API
92 typedef std::function< void(ResourceState) > StateChangedCallback;
95 * Typedef for callback of startCaching API
97 * @see RCSResourceAttributes
99 typedef std::function< void(const RCSResourceAttributes&) > CacheUpdatedCallback;
102 * Typedef for callback of getRemoteAttributes API
104 * @see RCSResourceAttributes
106 typedef std::function< void(const RCSResourceAttributes&, int) >
107 RemoteAttributesGetCallback;
110 * Typedef for callback of setRemoteAttributes API
112 * @see RCSResourceAttributes
114 typedef std::function< void(const RCSResourceAttributes&, int) >
115 RemoteAttributesSetCallback;
119 typedef unsigned int BrokerID;
123 RCSRemoteResourceObject(std::shared_ptr< PrimitiveResource >);
126 ~RCSRemoteResourceObject();
129 * Returns whether monitoring is enabled.
131 * @see startMonitoring()
133 bool isMonitoring() const;
136 * Returns whether caching is enabled.
138 * @see startCaching()
141 bool isCaching() const;
144 * Returns whether the resource is observable.
147 bool isObservable() const;
150 * Starts monitoring the resource.
152 * Monitoring provides a feature to check the presence of a resource,
153 * even when the server is not announcing Presence using startPresnece.
155 * @param cb A Callback to get changed resource state.
157 * @throws InvalidParameterException If cb is an empty function or null.
158 * @throws BadRequestException If monitoring is already started.
160 * @note The callback will be invoked in an internal thread.
162 * @see StateChangedCallback
164 * @see isMonitoring()
165 * @see stopMonitoring()
168 void startMonitoring(StateChangedCallback cb);
171 * Stops monitoring the resource.
173 * It does nothing if monitoring is not started.
175 * @see startMonitoring()
178 void stopMonitoring();
181 * Returns the current state of the resource.
183 * @see startMonitoring
185 ResourceState getState() const;
188 * Starts caching attributes of the resource.
190 * This will start caching for the resource.
191 * Once caching started it will look for the data updation on the resource
192 * and updates the cache data accordingly.
194 * It is equivalent to calling startCaching(CacheUpdatedCallback) with an empty function.
196 * @see getCacheState()
197 * @see getCachedAttributes()
198 * @see getCachedAttribute(const std::string&) const
200 * @throws BadRequestException
206 * Starts caching attributes for the resource.
208 * This will start data caching for the resource.
209 * Once caching started it will look for the data updation on the resource and
210 * updates the cached data accordingly.
212 * @param cb If non-empty function, it will be invoked whenever the cache updated.
214 * @throws BadRequestException If caching is already started.
216 * @note The callback will be invoked in an internal thread.
218 * @see CacheUpdatedCallback
219 * @see getCacheState()
220 * @see isCachedAvailable()
221 * @see getCachedAttributes()
222 * @see getCachedAttribute(const std::string&) const
225 void startCaching(CacheUpdatedCallback cb);
230 * It does nothing if caching is not started.
232 * @see startCaching()
233 * @see startCaching(CacheUpdatedCallback)
238 * Returns the current cache state.
241 CacheState getCacheState() const;
244 * Returns whether cached data is available.
246 * Cache will be available always once cache state had been CacheState::READY
247 * even if current state is CacheState::LOST_SIGNAL.
249 * @see getCacheState()
251 bool isCachedAvailable() const;
254 * Gets the cached RCSResourceAttributes data.
256 * @pre Cache should be available.
258 * @return The cached attributes.
260 * @throws BadRequestException If the precondition is not fulfilled.
262 * @see RCSResourceAttributes
263 * @see isCachedAvailable()
264 * @see startCaching()
265 * @see startCaching(CacheUpdatedCallback)
268 RCSResourceAttributes getCachedAttributes() const;
271 * Gets a particular cached a ResourceAttribute Value.
273 * @pre Cache should be available.
275 * @return A requested attribute value.
277 * @throws BadRequestException If the precondition is not fulfilled.
278 * @throws InvalidKeyException If @a key doesn't match the key of any value.
280 * @see RCSResourceAttributes::Value
281 * @see isCachedAvailable()
282 * @see startCaching()
283 * @see startCaching(CacheUpdatedCallback)
286 RCSResourceAttributes::Value getCachedAttribute(const std::string& key) const;
289 * Gets resource attributes directly from the server.
291 * This API send a get request to the resource of interest and provides
292 * the attributes to the caller in the RemoteAttributesReceivedCallback.
294 * @throws PlatformException If the operation failed
295 * @throws InvalidParameterException If cb is an empty function or null.
297 * @see RCSResourceAttributes::Value
299 * @note The callback will be invoked in an internal thread.
301 void getRemoteAttributes(RemoteAttributesGetCallback cb);
304 * Sends a set request with resource attributes to the server.
306 * The SetRequest behavior depends on the server, whether updating its attributes or not.
308 * @param attributes Attributes to set
309 * @param cb A callback to receive the response.
311 * @throws PlatformException If the operation failed
312 * @throws InvalidParameterException If cb is an empty function or null.
314 * @see RCSResourceObject
315 * @see RCSResourceObject::SetRequestHandlerPolicy
317 * @note The callback will be invoked in an internal thread.
319 void setRemoteAttributes(const RCSResourceAttributes& attributes,
320 RemoteAttributesSetCallback cb);
323 * Returns the uri of the resource.
326 std::string getUri() const;
329 * Returns the address of the resource .
332 std::string getAddress() const;
335 * Returns the resource types of the resource.
338 std::vector< std::string > getTypes() const;
341 * Returns the resource interfaces of the resource.
344 std::vector< std::string > getInterfaces() const;
347 std::shared_ptr< PrimitiveResource > m_primitiveResource;
353 #endif // RCSREMOTERESOURCEOBJECT_H