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 * The resource can be discovered with discoverResource.
74 * This class is an interaction point between Resource
75 * and the developers. Developer will get the RCSRemoteResourceObject
76 * by calling RCSDiscoveryManager::discoverResource.
78 * @see RCSDiscoveryManager
81 class RCSRemoteResourceObject
84 typedef std::shared_ptr< RCSRemoteResourceObject > Ptr;
87 * Typedef for callback of startMonitoring API
91 typedef std::function< void(ResourceState) > StateChangedCallback;
94 * Typedef for callback of startCaching API
96 * @see RCSResourceAttributes
98 typedef std::function< void(const RCSResourceAttributes&) > CacheUpdatedCallback;
101 * Typedef for callback of getRemoteAttributes API
103 * @see RCSResourceAttributes
105 typedef std::function< void(const RCSResourceAttributes&) >
106 RemoteAttributesGetCallback;
109 * Typedef for callback of setRemoteAttributes API
111 * @see RCSResourceAttributes
113 typedef std::function< void(const RCSResourceAttributes&) >
114 RemoteAttributesSetCallback;
118 typedef unsigned int BrokerID;
122 RCSRemoteResourceObject(std::shared_ptr< PrimitiveResource >);
125 ~RCSRemoteResourceObject();
128 * Returns whether monitoring is enabled.
130 * @see startMonitoring()
132 bool isMonitoring() const;
135 * Returns whether caching is enabled.
137 * @see startCaching()
140 bool isCaching() const;
143 * Returns whether the resource is observable.
146 bool isObservable() const;
149 * Starts monitoring the resource.
151 * Monitoring provides a feature to check the presence of a resource,
152 * even when the server is not announcing Presence using startPresnece.
154 * @param cb A Callback to get changed resource state.
156 * @throws InvalidParameterException If cb is an empty function or null.
157 * @throws BadRequestException If monitoring is already started.
159 * @note The callback will be invoked in an internal thread.
161 * @see StateChangedCallback
163 * @see isMonitoring()
164 * @see stopMonitoring()
167 void startMonitoring(StateChangedCallback cb);
170 * Stops monitoring the resource.
172 * It does nothing if monitoring is not started.
174 * @see startMonitoring()
177 void stopMonitoring();
180 * Returns the current state of the resource.
182 * @see startMonitoring
184 ResourceState getState() const;
187 * Starts caching attributes of the resource.
189 * This will start data caching for the resource.
190 * Once caching started it will look for the data updation on the resource
191 * and updates the cache data accordingly.
193 * It is equivalent to calling startCaching(CacheUpdatedCallback) with an empty function.
195 * @see getCacheState()
196 * @see getCachedAttributes()
197 * @see getCachedAttribute(const std::string&) const
199 * @throws BadRequestException
205 * Starts caching attributes for the resource.
207 * This will start data caching for the resource.
208 * Once caching started it will look for the data updation on the resource and
209 * updates the cached data accordingly.
211 * @param cb If non-empty function, it will be invoked whenever the cache updated.
213 * @throws BadRequestException If caching is already started.
215 * @note The callback will be invoked in an internal thread.
217 * @see CacheUpdatedCallback
218 * @see getCacheState()
219 * @see isCachedAvailable()
220 * @see getCachedAttributes()
221 * @see getCachedAttribute(const std::string&) const
224 void startCaching(CacheUpdatedCallback cb);
229 * It does nothing if caching is not started.
231 * @see startCaching()
232 * @see startCaching(CacheUpdatedCallback)
237 * Returns the current cache state.
240 CacheState getCacheState() const;
243 * Returns whether cached data is available.
245 * Cache will be available always after CacheState::READY even if current state is
246 * CacheState::LOST_SIGNAL.
248 * @see getCacheState()
250 bool isCachedAvailable() const;
253 * Gets the cached RCSResourceAttributes data.
255 * @pre Cache should be available.
257 * @return The cached attributes.
259 * @throws BadRequestException If the precondition is not fulfilled.
261 * @see RCSResourceAttributes
262 * @see isCachedAvailable()
263 * @see startCaching()
264 * @see startCaching(CacheUpdatedCallback)
267 RCSResourceAttributes getCachedAttributes() const;
270 * Gets a particular cached a ResourceAttribute Value.
272 * @pre Cache should be available.
274 * @return A requested attribute value.
276 * @throws BadRequestException If the precondition is not fulfilled.
277 * @throws InvalidKeyException If @a key doesn't match the key of any value.
279 * @see RCSResourceAttributes::Value
280 * @see isCachedAvailable()
281 * @see startCaching()
282 * @see startCaching(CacheUpdatedCallback)
285 RCSResourceAttributes::Value getCachedAttribute(const std::string& key) const;
288 * Gets resource attributes directly from the server.
290 * This API send a get request to the resource of interest and provides
291 * the attributes to the caller in the RemoteAttributesReceivedCallback.
293 * @throw InvalidParameterException If cb is an empty function or null.
295 * @see RCSResourceAttributes::Value
297 * @note The callback will be invoked in an internal thread.
299 void getRemoteAttributes(RemoteAttributesGetCallback cb);
302 * Sends a set request with resource attributes to the server.
304 * The SetRequest behavior depends on the server, whether updating its attributes or not.
306 * @param attributes Attributes to set
307 * @param cb A callback to receive the response.
309 * @throw InvalidParameterException If cb is an empty function or null.
311 * @see RCSResourceObject
312 * @see RCSResourceObject::SetRequestHandlerPolicy
314 * @note The callback will be invoked in an internal thread.
316 void setRemoteAttributes(const RCSResourceAttributes& attributes,
317 RemoteAttributesSetCallback cb);
320 * Returns the uri of the resource.
323 std::string getUri() const;
326 * Returns the address of the resource .
329 std::string getAddress() const;
332 * Returns the resource types of the resource.
335 std::vector< std::string > getTypes() const;
338 * Returns the resource interfaces of the resource.
341 std::vector< std::string > getInterfaces() const;
344 std::shared_ptr< PrimitiveResource > m_primitiveResource;
350 #endif // RCSREMOTERESOURCEOBJECT_H