Updated Resource Client and Sample Applications
authorJay Sharma <jay.sharma@samsung.com>
Wed, 15 Jul 2015 07:48:05 +0000 (13:18 +0530)
committerUze Choi <uzchoi@samsung.com>
Wed, 15 Jul 2015 13:23:05 +0000 (13:23 +0000)
- Added exception for the missed api.
- Updated exception handling in Resource Client
- Added more logs in the Sample Applications

Change-Id: I7a212e05856f37628f2e744d83d4f1619f6a1e06
Signed-off-by: Jay Sharma <jay.sharma@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1663
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/examples/linux/SampleResourceClient.cpp
service/resource-manipulation/examples/linux/SampleResourceServer.cpp
service/resource-manipulation/include/ResourceClient.h
service/resource-manipulation/src/ResourceClient.cpp

index 219428d..9146e6f 100644 (file)
@@ -255,20 +255,27 @@ int main()
             }
             else if (userInput == 8)
             {
-                ResourceAttributes atttribute = resource->getCachedAttributes();
-                if (atttribute.empty())
-                {
-                    cout << "Received cached attribute is empty" << std::endl;
-                }
-                else
+                try
                 {
-                    ResourceAttributes::const_iterator iter = atttribute.begin();
-                    for (unsigned int i = 0; i < atttribute.size(); ++i)
+                    ResourceAttributes atttribute = resource->getCachedAttributes();
+                    if (atttribute.empty())
                     {
-                        std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
-                        ++iter;
+                        cout << "Received cached attribute is empty" << std::endl;
+                    }
+                    else
+                    {
+                        ResourceAttributes::const_iterator iter = atttribute.begin();
+                        for (unsigned int i = 0; i < atttribute.size(); ++i)
+                        {
+                            std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
+                            ++iter;
+                        }
                     }
                 }
+                catch (BadRequestException e)
+                {
+                    cout << "getCachedAttributes exception : " << e.what() << std::endl;
+                }
             }
             else if (userInput == 9)
             {
@@ -279,6 +286,10 @@ int main()
                     int value = valueObj.get< int >();
                     cout << "\nkey : " << key << "\nValue : " << value << std::endl;
                 }
+                catch (BadRequestException e)
+                {
+                    cout << "getCachedAttribute exception : " << e.what() << std::endl;
+                }
                 catch (BadGetException e)
                 {
                     cout << "Exeception in getCachedAttribute  BadGetException:: " << e.what() << std::endl;
index 7da312c..de47741 100644 (file)
@@ -28,7 +28,7 @@ using namespace OC;
 using namespace OC::OCPlatform;
 using namespace OIC::Service;
 
-std::string resourceUri = "/a/TempHumSensor";
+std::string resourceUri = "/a/TempSensor";
 std::string resourceType = "core.TemperatureSensor";
 std::string resourceInterface = "oic.if.";
 std::string attributeKey = "Temperature";
@@ -48,13 +48,14 @@ void displayMenu()
 PrimitiveGetResponse RequestHandlerForGet(const PrimitiveRequest &request,
         ResourceAttributes &attrs)
 {
-    cout << "\nRecieved a Get request from Client" << std::endl;
+    cout << "Recieved a Get request from Client" << std::endl;
     ResourceObject::LockGuard lock(*server);
     ResourceAttributes attr = server->getAttributes();
     ResourceAttributes::const_iterator iter = attr.begin();
+    cout << "\nSending response to Client : " << std::endl;
     for (unsigned int i = 0; i < attr.size(); ++i)
     {
-        std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
+        std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
         ++iter;
     }
     return PrimitiveGetResponse::create(attr);
@@ -64,11 +65,18 @@ PrimitiveGetResponse RequestHandlerForGet(const PrimitiveRequest &request,
 PrimitiveSetResponse RequestHandlerForSet(const PrimitiveRequest &request,
         ResourceAttributes &attrs)
 {
-    cout << "\nRecieved a Set request from Client" << std::endl;
+    cout << "Recieved a Set request from Client" << std::endl;
     ResourceAttributes::const_iterator iter = attrs.begin();
     for (unsigned int i = 0; i < attrs.size(); ++i)
     {
-        std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
+        std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
+        ++iter;
+    }
+    iter = attrs.begin();
+    cout << "\n\nSending response to Client : " << std::endl;
+    for (unsigned int i = 0; i < attrs.size(); ++i)
+    {
+        std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
         ++iter;
     }
     return PrimitiveSetResponse::create(attrs);
index 4916142..6476e4e 100644 (file)
@@ -207,6 +207,9 @@ namespace OIC
                  * API to get cached ResourceAttributes data.
                  *
                  * @return ResourceAttributes - cached ResourceAttributes data
+                 *
+                 * @throw BadRequestException
+                 *
                  */
                 ResourceAttributes getCachedAttributes() const;
 
@@ -214,6 +217,9 @@ namespace OIC
                  * API to get particular cached ResourceAttribute value
                  *
                  * @return Value - ResourceAttributes::Value class object
+                 *
+                 * @throw BadRequestException
+                 *
                  */
                 ResourceAttributes::Value getCachedAttribute( const std::string &) ;
 
index 545f079..f24776f 100644 (file)
@@ -196,7 +196,7 @@ namespace OIC
                     m_watchingFlag = true;
                     m_brokerId = brokerId;
                 }
-                catch (InvalidParameterException exception )
+                catch (std::exception &exception)
                 {
                     throw InvalidParameterException {exception.what()};
                 }
@@ -214,7 +214,7 @@ namespace OIC
                     ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
                     m_watchingFlag = false;
                 }
-                catch (InvalidParameterException exception )
+                catch (std::exception &exception)
                 {
                     OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopWatching InvalidParameterException");
                 }
@@ -236,7 +236,7 @@ namespace OIC
                 OC_LOG(DEBUG, CLIENT_W_TAG, " RemoteResourceObject::getState exit");
                 return getResourceStateFromBrokerState(brokerState);
             }
-            catch (InvalidParameterException exception)
+            catch (std::exception &exception)
             {
                 OC_LOG(DEBUG, CLIENT_W_TAG, " RemoteResourceObject::getState InvalidParameterException");
             }
@@ -260,7 +260,7 @@ namespace OIC
                     m_cachingFlag = true;
                     OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching CACHE ID %d", cacheId);
                 }
-                catch (InvalidParameterException e)
+                catch (std::exception &exception)
                 {
                     OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching InvalidParameterException");
                 }
@@ -279,7 +279,7 @@ namespace OIC
             {
                 try
                 {
-                   CacheID cacheId = ResourceCacheManager::getInstance()->requestResourceCache(m_primitiveResource,
+                  CacheID cacheId = ResourceCacheManager::getInstance()->requestResourceCache(m_primitiveResource,
                                       std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, cb),
                                       REPORT_FREQUENCY::UPTODATE,  0);
 
@@ -287,9 +287,9 @@ namespace OIC
                     m_cachingFlag = true;
                     OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching CACHE ID %d", cacheId);
                 }
-                catch (InvalidParameterException e)
+                catch (std::exception &exception)
                 {
-                    throw InvalidParameterException { e.what() };
+                    throw InvalidParameterException {"startCaching : Callback is NULL" };
                 }
             }
         }
@@ -305,7 +305,7 @@ namespace OIC
                     ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
                     m_cachingFlag = false;
                 }
-                catch (InvalidParameterException exception)
+                catch (std::exception &exception)
                 {
                     OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopCaching InvalidParameterException");
                 }
@@ -328,7 +328,7 @@ namespace OIC
                 OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getResourceCacheState exit");
                 return getCacheState(cacheState);
             }
-            catch (InvalidParameterException exception)
+            catch (std::exception &exception)
             {
                 OC_LOG(DEBUG, CLIENT_W_TAG,
                        "RemoteResourceObject::getResourceCacheState InvalidParameterException");
@@ -344,7 +344,7 @@ namespace OIC
                 ResourceCacheManager::getInstance()->updateResourceCache(
                     m_primitiveResource);
             }
-            catch (InvalidParameterException exception)
+            catch (std::exception &exception)
             {
                 OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache InvalidParameterException");
             }
@@ -356,11 +356,11 @@ namespace OIC
             OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttributes ");
             try
             {
-                return  ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
+                return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
             }
-            catch (InvalidParameterException e)
+            catch (std::exception &exception)
             {
-              OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getCachedAttributes InvalidParameterException");
+                throw BadRequestException { "[getCachedAttributes]  Caching not started" };
             }
         }
 
@@ -373,9 +373,9 @@ namespace OIC
                         m_primitiveResource);
                 return Cachedattributes[key];
             }
-            catch  (InvalidParameterException e)
+            catch (std::exception &exception)
             {
-               OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getCachedAttribute InvalidParameterException");
+                throw BadRequestException { "[getCachedAttribute]  Caching not started" };
             }
             OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttribute exit");
         }