Arrange Code for Resource Hosting.
authorjyong2.kim <jyong2.kim@samsung.com>
Sun, 19 Jul 2015 10:54:16 +0000 (19:54 +0900)
committerUze Choi <uzchoi@samsung.com>
Sun, 19 Jul 2015 13:16:24 +0000 (13:16 +0000)
fix doxygen description
add initialization for member variable.
handle exception.
update sample consumer app.

Change-Id: If1b40b7359cf09d7322fc265e1eadb1e2ce70f76
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1738
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/SConscript
service/notification-manager/NotificationManager/include/hosting.h
service/notification-manager/NotificationManager/src/HostingObject.cpp
service/notification-manager/NotificationManager/src/HostingObject.h
service/notification-manager/NotificationManager/src/ResourceHosting.cpp
service/notification-manager/NotificationManager/src/ResourceHosting.h
service/notification-manager/NotificationManager/src/hosting.cpp
service/notification-manager/SampleApp/linux/sampleConsumer/SampleConsumer.cpp

index ca02687..2330bfa 100644 (file)
@@ -39,11 +39,11 @@ if target_os not in ['arduino','darwin','ios']:
                SConscript('protocol-plugin/SConscript')
 
        # Build notification manager project
-       #if target_os not in ['android']:
-       #       SConscript('notification-manager/SConscript')
+       if target_os not in ['android', 'tizen']:
+               SConscript('notification-manager/SConscript')
 
        # Build resource-manipulation project
-       if target_os not in ['android']:
+       if target_os not in ['android', 'tizen']:
                SConscript('resource-manipulation/SConscript')
 #else:
 #      SConscript('notification-manager/SampleApp/arduino/SConscript')
index ecae22d..e4b7ed8 100644 (file)
@@ -30,7 +30,6 @@
 #include "ocstack.h"
 #include "logger.h"
 
-//#define OC_TRANSPORT CT_DEFAULT
 #define OC_TRANSPORT CT_ADAPTER_IP
 
 #ifdef __cplusplus
@@ -40,16 +39,16 @@ extern "C" {
 #define HOSTING_TAG  PCF("Hosting")
 
 /**
- * Start resource coordinator.
- * This function will create mirrorResourceList and start to discover coordinatee candidate.
+ * Start resource hosting.
+ * This function will start the resource hosting and the discovery for remote resource which want to be hosted.
  *
  * @return ::OC_STACK_OK upon success, ::OC_STACK_ERROR is returned except the case that OC_STACK_SUCCESS is returned.
  */
 OCStackResult OICStartCoordinate();
 
 /**
- * Stop resource coordinator.
- * This function will stop the resource hosting and delete mirrorResourceList used.
+ * Stop resource hosting.
+ * This function will stop the resource hosting and delete all hosting resource.
  *
  * @return ::OC_STACK_OK upon success, ::OC_STACK_ERROR is returned except the case that OC_STACK_SUCCESS is returned.
  */
index c23fc54..8c9a84f 100644 (file)
@@ -24,6 +24,16 @@ namespace OIC
 {
 namespace Service
 {
+#define HOSTING_LOG_TAG  PCF("Hosting")
+#define OIC_HOSTING_LOG(level, tag, ...)  OCLogv((level), (HOSTING_LOG_TAG), __VA_ARGS__)
+
+HostingObject::HostingObject()
+: remoteObject(nullptr), mirroredServer(nullptr),
+  remoteState(ResourceState::NOT_WATCHING),
+  pStateChangedCB(nullptr), pDataUpdateCB(nullptr),
+  pDestroyCB(nullptr), pSetRequestHandler(nullptr)
+{
+}
 
 HostingObject::RemoteObjectPtr HostingObject::getRemoteResource() const
 {
@@ -32,8 +42,6 @@ HostingObject::RemoteObjectPtr HostingObject::getRemoteResource() const
 
 void HostingObject::initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB)
 {
-    mirroredServer = nullptr;
-    remoteState = ResourceState::NOT_WATCHING;
 
     remoteObject = rResource;
 
@@ -48,11 +56,11 @@ void HostingObject::initializeHostingObject(RemoteObjectPtr rResource, Destroyed
 
     try
     {
-        remoteObject->startWatching(pStateChangedCB);
+        remoteObject->startMonitoring(pStateChangedCB);
         remoteObject->startCaching(pDataUpdateCB);
-    }catch(InvalidParameterException &e)
+    }catch(...)
     {
-        std::cout << e.what() << std::endl;
+        throw;
     }
 }
 
@@ -76,7 +84,9 @@ void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject)
                 rObject->startCaching(pDataUpdateCB);
             }catch(InvalidParameterException &e)
             {
-                std::cout << e.what() << std::endl;
+                OIC_HOSTING_LOG(DEBUG,
+                        "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
+                        e.what());
             }
         }
         break;
@@ -91,23 +101,24 @@ void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject)
                 rObject->stopCaching();
             }catch(InvalidParameterException &e)
             {
-                std::cout << e.what() << std::endl;
+                OIC_HOSTING_LOG(DEBUG,
+                        "[HostingObject::stateChangedCB]stopCaching InvalidParameterException:%s",
+                        e.what());
             }
         }
         if(rObject->isWatching() == true)
         {
             try
             {
-                std::cout << "stopWatching\n";
-//                rObject->stopWatching();
+//                rObject->stopMonitoring();
             }catch(InvalidParameterException &e)
             {
-                std::cout << e.what() << std::endl;
+                OIC_HOSTING_LOG(DEBUG,
+                        "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
+                        e.what());
             }
         }
-        std::cout << "delete Mirrored Server\n";
         mirroredServer = nullptr;
-        std::cout << "delete Mirrored Server Done\n";
         destroyHostingObject();
         break;
     }
@@ -129,8 +140,11 @@ void HostingObject::dataChangedCB(const ResourceAttributes & attributes, RemoteO
         try
         {
             mirroredServer = createMirroredServer(rObject);
-        }catch(...)
+        }catch(PlatformException &e)
         {
+            OIC_HOSTING_LOG(DEBUG,
+                        "[HostingObject::dataChangedCB]createMirroredServer PlatformException:%s",
+                        e.what());
             mirroredServer = nullptr;
             return;
         }
@@ -181,18 +195,18 @@ HostingObject::ResourceObjectPtr HostingObject::createMirroredServer(RemoteObjec
             std::string interface = interfaces.begin()->c_str();
             retResource = ResourceObject::Builder(uri, type, interface).
                     setDiscoverable(true).setObservable(true).build();
+
             // TODO need to bind types and interfaces
             retResource->setAutoNotifyPolicy(ResourceObject::AutoNotifyPolicy::UPDATED);
             retResource->setSetRequestHandler(pSetRequestHandler);
-        }catch(PlatformException &e)
+        }catch(...)
         {
-            // TODO logging and throw
+            OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "PlatformException");
             throw;
         }
     }
     else
     {
-        //TODO logging and throw
         throw PlatformException(OC_STACK_ERROR);
     }
 
@@ -204,12 +218,14 @@ RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequ
 {
     try
     {
-        RequestObject newRequest;
+        RequestObject newRequest = { };
         newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Setter,
                 primitiveRequest, resourceAttibutes);
-    }catch(...)
+    }catch(PlatformException &e)
     {
-        //TODO It is failed that setRequest send to remoteObject from mirrored server.
+        OIC_HOSTING_LOG(DEBUG,
+                "[HostingObject::setRequestHandler] PlatformException:%s",
+                e.what());
         throw;
     }
 
index 76a5762..a93a30a 100644 (file)
@@ -49,7 +49,7 @@ private:
             RCSSetResponse(const RCSRequest&, ResourceAttributes&)> SetRequestHandler;
 
 public:
-    HostingObject() = default;
+    HostingObject();
     ~HostingObject() = default;
 
     void initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB);
index a6f2846..46289b4 100755 (executable)
@@ -30,10 +30,26 @@ namespace Service
 
 #define HOSTING_TAG "/hosting"
 #define HOSTING_TAG_SIZE ((size_t)8)
+#define HOSTING_LOG_TAG  PCF("Hosting")
+#define OIC_HOSTING_LOG(level, tag, ...)  OCLogv((level), (HOSTING_LOG_TAG), __VA_ARGS__)
+
+namespace
+{
+    std::string MULTICAST_PRESENCE_ADDRESS = std::string("coap://") + OC_MULTICAST_PREFIX;
+    std::string HOSTING_RESOURSE_TYPE = "Resource.Hosting";
+}
 
 ResourceHosting * ResourceHosting::s_instance(nullptr);
 std::mutex ResourceHosting::s_mutexForCreation;
 
+ResourceHosting::ResourceHosting()
+: hostingObjectList(),
+  discoveryManager(nullptr),
+  presenceHandle(),
+  pPresenceCB(nullptr), pDiscoveryCB(nullptr)
+{
+}
+
 ResourceHosting * ResourceHosting::getInstance()
 {
     if (!s_instance)
@@ -57,16 +73,25 @@ void ResourceHosting::startHosting()
         requestMulticastDiscovery();
     }catch(PlatformException &e)
     {
+        OIC_HOSTING_LOG(DEBUG,
+                "[ResourceHosting::startHosting]PlatformException:%s", e.what());
         throw;
     }catch(InvalidParameterException &e)
     {
+        OIC_HOSTING_LOG(DEBUG,
+                "[ResourceHosting::startHosting]InvalidParameterException:%s", e.what());
+        throw;
+    }catch(std::exception &e)
+    {
+        OIC_HOSTING_LOG(DEBUG,
+                "[ResourceHosting::startHosting]std::exception:%s", e.what());
         throw;
     }
 }
 
 void ResourceHosting::stopHosting()
 {
-    // TODO clear list hostingObjectList
+    // clear list hostingObjectList
     if(presenceHandle.isSubscribing())
     {
         presenceHandle.unsubscribe();
@@ -91,7 +116,7 @@ void ResourceHosting::requestMulticastPresence()
 {
     try
     {
-        presenceHandle = PresenceSubscriber(std::string("coap://") + OC_MULTICAST_PREFIX,
+        presenceHandle = PresenceSubscriber(MULTICAST_PRESENCE_ADDRESS,
                 OCConnectivityType::CT_DEFAULT, pPresenceCB);
     }catch(...)
     {
@@ -161,7 +186,7 @@ void ResourceHosting::requestMulticastDiscovery()
 void ResourceHosting::requestDiscovery(std::string address)
 {
     std::string host = address;
-    std::string uri = OC_RSRVD_WELL_KNOWN_URI + std::string("?rt=Resource.Hosting");
+    std::string uri = OC_RSRVD_WELL_KNOWN_URI + std::string("?rt=") + HOSTING_RESOURSE_TYPE;
     OCConnectivityType type = OCConnectivityType::CT_DEFAULT;
     discoveryManager->discoverResource(host, uri, type, pDiscoveryCB);
 }
@@ -178,14 +203,17 @@ void ResourceHosting::discoverHandler(RemoteObjectPtr remoteResource)
     HostingObjectPtr foundHostingObject = findRemoteResource(remoteResource);
     if(foundHostingObject == nullptr)
     {
-        foundHostingObject.reset(new HostingObject());
-        foundHostingObject->initializeHostingObject(remoteResource,
-                std::bind(&ResourceHosting::destroyedHostingObject, this, foundHostingObject));
-        hostingObjectList.push_back(foundHostingObject);
-    }
-    else
-    {
-        // this resource registered
+        try
+        {
+            foundHostingObject.reset(new HostingObject());
+            foundHostingObject->initializeHostingObject(remoteResource,
+                    std::bind(&ResourceHosting::destroyedHostingObject, this, foundHostingObject));
+            hostingObjectList.push_back(foundHostingObject);
+        }catch(InvalidParameterException &e)
+        {
+            OIC_HOSTING_LOG(DEBUG,
+                    "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
+        }
     }
 }
 
@@ -221,14 +249,7 @@ bool ResourceHosting::isSameRemoteResource(
 
 void ResourceHosting::destroyedHostingObject(HostingObjectPtr destroyedPtr)
 {
-    std::list<HostingObjectPtr>::iterator foundObjectIter
-    = std::find(hostingObjectList.begin(), hostingObjectList.end(), destroyedPtr);
-
-    if(foundObjectIter != hostingObjectList.end())
-    {
-        std::cout << "destroy hosting object.\n";
-        hostingObjectList.erase(foundObjectIter);
-    }
+    hostingObjectList.remove(destroyedPtr);
 }
 
 } /* namespace Service */
index b747474..c0fd62c 100644 (file)
@@ -60,7 +60,7 @@ public:
     static ResourceHosting * getInstance();
 
 private:
-    ResourceHosting() = default;
+    ResourceHosting();
     ~ResourceHosting() = default;
 
     ResourceHosting(const ResourceHosting&) = delete;
index b3a8323..79ba751 100644 (file)
 #include "logger.h"
 #include "ResourceHosting.h"
 
-#define OC_TRANSPORT CT_ADAPTER_IP
-#define HOSTING_TAG  PCF("Hosting")
-
 namespace
 {
+    #define OC_TRANSPORT CT_ADAPTER_IP
+    #define HOSTING_TAG  PCF("Hosting")
+    #define OIC_HOSTING_LOG(level, tag, ...)  OCLogv((level), (HOSTING_TAG), __VA_ARGS__)
     OIC::Service::ResourceHosting * rhInstance = OIC::Service::ResourceHosting::getInstance();
 }
 
@@ -44,8 +44,20 @@ OCStackResult OICStartCoordinate()
     try
     {
         rhInstance->startHosting();
+    }catch(OIC::Service::PlatformException &e)
+    {
+        OIC_HOSTING_LOG(DEBUG, "[OICStartCoordinate] platformException, reason:%s", e.what());
+        retResult = OC_STACK_ERROR;
+        throw;
+    }catch(OIC::Service::InvalidParameterException &e)
+    {
+        OIC_HOSTING_LOG(DEBUG,
+                "[OICStartCoordinate] InvalidParameterException, reason:%s", e.what());
+        retResult = OC_STACK_ERROR;
+        throw;
     }catch(...)
     {
+        OIC_HOSTING_LOG(DEBUG, "[OICStartCoordinate] Unknown Exception", "");
         retResult = OC_STACK_ERROR;
     }
 
index 7e37a1f..37488fd 100644 (file)
@@ -34,7 +34,7 @@ using namespace OC;
 
 const int SUCCESS_RESPONSE = OC_STACK_OK;
 
-#define OC_WELL_KNOWN_COORDINATING_QUERY "/oc/core?rt=Resource.Hosting"
+#define OC_WELL_KNOWN_COORDINATING_QUERY "/oic/res?rt=Resource.Hosting"
 
 #define OBSERVE 1
 #define GET     2