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"
38 namespace HeaderOption
49 class RCSRepresentation;
51 typedef std::vector< OC::HeaderOption::OCHeaderOption > HeaderOpts;
54 * The states of caching.
61 NONE, /**< Caching is not started.*/
62 UNREADY, /**< Caching is started, but the data is not ready yet.
63 This is the default state after startCaching. */
64 READY, /**< The data is ready.*/
65 LOST_SIGNAL, /**< Failed to reach the resource. */
69 * The states of monitoring.
71 * @see startMonitoring
74 enum class ResourceState
76 NONE, /**< Monitoring is not started.*/
77 REQUESTED, /**< Monitoring is started and checking state is in progress.
78 This is the default state after startMonitoring. */
79 ALIVE, /**< The resource is alive. */
80 LOST_SIGNAL, /**< Failed to reach the resource. */
81 DESTROYED /**< The resource is deleted. */
84 class PrimitiveResource;
89 typedef std::unordered_map< std::string, std::string > Map;
92 RCSQueryParams& setResourceInterface(const std::string&);
93 RCSQueryParams& setResourceInterface(std::string&&);
95 RCSQueryParams& setResuorceType(const std::string&);
96 RCSQueryParams& setResuorceType(std::string&&);
98 RCSQueryParams& put(const std::string&, const std::string&);
99 RCSQueryParams& put(std::string&&, std::string&&);
100 RCSQueryParams& put(const std::string&, std::string&&);
101 RCSQueryParams& put(std::string&&, const std::string&);
103 std::string getResourceInterface() const;
104 std::string getResourceType() const;
105 std::string get(const std::string&) const;
107 const Map& getAll() const;
110 std::string m_resourceInterface;
111 std::string m_resourceType;
113 std::unordered_map< std::string, std::string > m_map;
118 * This represents a remote resource and provides simple ways to interact with it.
119 * Basically this is a client of a remote resource that runs on other device.
121 * The class supports features to help get information of a remote resource
122 * such as monitoring and caching.
124 * @see RCSDiscoveryManager
127 class RCSRemoteResourceObject
130 typedef std::shared_ptr< RCSRemoteResourceObject > Ptr;
133 * Callback definition to be invoked when monitoring state is changed.
135 * @see startMonitioring
138 typedef std::function< void(ResourceState) > StateChangedCallback;
141 * Callback definition to be invoked when cache is updated.
143 * @param attrs the updated attributes
145 typedef std::function< void(const RCSResourceAttributes& attrs) > CacheUpdatedCallback;
148 * Callback definition to be invoked when the response of getRemoteAttributes is
151 * @param attrs the result attributes
152 * @param eCode the error code received from the resource
154 * @see getRemoteAttributes
156 typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
157 RemoteAttributesGetCallback;
159 typedef std::function< void(const HeaderOpts&, const RCSRepresentation& rep, int eCode) >
163 * Callback definition to be invoked when the response of setRemoteAttributes is
166 * @param attrs the result attributes
167 * @param eCode the error code received from the resource
169 * @see setRemoteAttributes
171 typedef std::function< void(const RCSResourceAttributes&, int) >
172 RemoteAttributesSetCallback;
174 typedef std::function< void(const HeaderOpts&, const RCSRepresentation& rep, int eCode) >
179 typedef unsigned int BrokerID;
183 RCSRemoteResourceObject(std::shared_ptr< PrimitiveResource >);
186 ~RCSRemoteResourceObject();
188 static RCSRemoteResourceObject::Ptr fromOCResource(std::shared_ptr< OC::OCResource >);
191 * Returns whether monitoring is enabled.
193 * @see startMonitoring()
195 bool isMonitoring() const;
198 * Returns whether caching is enabled.
200 * @see startCaching()
203 bool isCaching() const;
206 * Returns whether the resource is observable.
209 bool isObservable() const;
212 * Starts monitoring the resource.
214 * Monitoring provides a feature to check the presence of a resource,
215 * even when the server is not announcing Presence using startPresnece.
217 * @param cb A Callback to get changed resource state.
219 * @throws InvalidParameterException If cb is an empty function or null.
220 * @throws BadRequestException If monitoring is already started.
222 * @note The callback will be invoked in an internal thread.
224 * @see StateChangedCallback
226 * @see isMonitoring()
227 * @see stopMonitoring()
230 void startMonitoring(StateChangedCallback cb);
233 * Stops monitoring the resource.
235 * It does nothing if monitoring is not started.
237 * @see startMonitoring()
240 void stopMonitoring();
243 * Returns the current state of the resource.
245 * @see startMonitoring
247 ResourceState getState() const;
250 * Starts caching attributes of the resource.
252 * This will start caching for the resource.
253 * Once caching started it will look for the data updation on the resource
254 * and updates the cache data accordingly.
256 * It is equivalent to calling startCaching(CacheUpdatedCallback) with an empty function.
258 * @see getCacheState()
259 * @see getCachedAttributes()
260 * @see getCachedAttribute(const std::string&) const
262 * @throws BadRequestException
268 * Starts caching attributes for the resource.
270 * This will start data caching for the resource.
271 * Once caching started it will look for the data updation on the resource and
272 * updates the cached data accordingly.
274 * @param cb If non-empty function, it will be invoked whenever the cache updated.
276 * @throws BadRequestException If caching is already started.
278 * @note The callback will be invoked in an internal thread.
280 * @see CacheUpdatedCallback
281 * @see getCacheState()
282 * @see isCachedAvailable()
283 * @see getCachedAttributes()
284 * @see getCachedAttribute(const std::string&) const
287 void startCaching(CacheUpdatedCallback cb);
292 * It does nothing if caching is not started.
294 * @see startCaching()
295 * @see startCaching(CacheUpdatedCallback)
300 * Returns the current cache state.
303 CacheState getCacheState() const;
306 * Returns whether cached data is available.
308 * Cache will be available always once cache state had been CacheState::READY
309 * even if current state is CacheState::LOST_SIGNAL.
311 * @see getCacheState()
313 bool isCachedAvailable() const;
316 * Gets the cached RCSResourceAttributes data.
318 * @pre Cache should be available.
320 * @return The cached attributes.
322 * @throws BadRequestException If the precondition is not fulfilled.
324 * @see RCSResourceAttributes
325 * @see isCachedAvailable()
326 * @see startCaching()
327 * @see startCaching(CacheUpdatedCallback)
330 RCSResourceAttributes getCachedAttributes() const;
333 * Gets a particular cached a ResourceAttribute Value.
335 * @pre Cache should be available.
337 * @return A requested attribute value.
339 * @throws BadRequestException If the precondition is not fulfilled.
340 * @throws InvalidKeyException If @a key doesn't match the key of any value.
342 * @see RCSResourceAttributes::Value
343 * @see isCachedAvailable()
344 * @see startCaching()
345 * @see startCaching(CacheUpdatedCallback)
348 RCSResourceAttributes::Value getCachedAttribute(const std::string& key) const;
351 * Gets resource attributes directly from the server.
353 * This API send a get request to the resource of interest and provides
354 * the attributes to the caller in the RemoteAttributesReceivedCallback.
356 * @throws PlatformException If the operation failed
357 * @throws InvalidParameterException If cb is an empty function or null.
359 * @see RCSResourceAttributes::Value
361 * @note The callback will be invoked in an internal thread.
363 void getRemoteAttributes(RemoteAttributesGetCallback cb);
365 void get(GetCallback cb);
366 void get(const RCSQueryParams&, GetCallback cb);
369 * Sends a set request with resource attributes to the server.
371 * The SetRequest behavior depends on the server, whether updating its attributes or not.
373 * @param attributes Attributes to set
374 * @param cb A callback to receive the response.
376 * @throws PlatformException If the operation failed
377 * @throws InvalidParameterException If cb is an empty function or null.
379 * @see RCSResourceObject
380 * @see RCSResourceObject::SetRequestHandlerPolicy
382 * @note The callback will be invoked in an internal thread.
384 void setRemoteAttributes(const RCSResourceAttributes& attributes,
385 RemoteAttributesSetCallback cb);
387 void set(const RCSResourceAttributes& attributes, SetCallback cb);
388 void set(const RCSQueryParams&, const RCSResourceAttributes& attributes, SetCallback cb);
391 * Returns the uri of the resource.
394 std::string getUri() const;
397 * Returns the address of the resource .
400 std::string getAddress() const;
403 * Returns the resource types of the resource.
406 std::vector< std::string > getTypes() const;
409 * Returns the resource interfaces of the resource.
412 std::vector< std::string > getInterfaces() const;
415 std::shared_ptr< PrimitiveResource > m_primitiveResource;
421 #endif // RCSREMOTERESOURCEOBJECT_H