[SSM] Apply OIC presence feature
authorminjii.park <minjii.park@samsung.com>
Thu, 18 Dec 2014 05:18:46 +0000 (14:18 +0900)
committerminjii.park <minjii.park@samsung.com>
Thu, 18 Dec 2014 05:18:46 +0000 (14:18 +0900)
1. Apply OIC presence check feature to Soft Sensor Manager
2. Add OIC presence feature to Thin Device Server Example

Change-Id: Iaa65942fb7bea5d0954cb65757ccb35bb2c12708
Signed-off-by: minjii.park <minjii.park@samsung.com>
service/soft-sensor-manager/SSMCore/src/SensorProcessor/ResourceFinder.cpp
service/soft-sensor-manager/SSMCore/src/SensorProcessor/ResourceFinder.h
service/soft-sensor-manager/SampleApp/linux/THSensorApp/src/ThingResourceServer.cpp
service/soft-sensor-manager/SampleApp/linux/THSensorApp1/src/ThingResourceServer1.cpp

index 1eaa069..57dbe2c 100644 (file)
@@ -1,30 +1,30 @@
 /******************************************************************
-*
-* Copyright 2014 Samsung Electronics All Rights Reserved.
-*
-*
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*      http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-******************************************************************/
+ *
+ * Copyright 2014 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
 #include "ResourceFinder.h"
 
 SSMRESULT CResourceFinder::finalConstruct()
 {
     SSMRESULT res = SSM_E_FAIL;
 
-    OC::PlatformConfig cfg(OC::ServiceType::InProc, OC::ModeType::Both,
-                           "0.0.0.0", 0, OC::QualityOfService::LowQos);
+    OC::PlatformConfig cfg(OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0,
+                           OC::QualityOfService::LowQos);
 
     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
 
@@ -46,29 +46,97 @@ SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *
     return SSM_S_OK;
 }
 
-void CResourceFinder::onResourceFound(std::shared_ptr<OC::OCResource> resource)
+void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource)
 {
     if (resource)
     {
-        int     *pMessage = new int[2];
+        std::string path = resource->host() + resource->uri();
+
+        if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end())
+            return;
+
+        intptr_t      *pMessage = new intptr_t [2];
         pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
-        pMessage[1] = (int)new std::shared_ptr<OC::OCResource>(resource);
+        pMessage[1] = reinterpret_cast<intptr_t> (new  std::shared_ptr<OC::OCResource>(resource));
+
+        std::cout << "Resource Found !! >> " << path << std::endl;
 
         m_pTasker->addTask(this, pMessage);
     }
 }
 
+void CResourceFinder::presenceHandler(OCStackResult result, const unsigned int nonce,
+                                      const std::string &hostAddress)
+{
+    SSMRESULT res = SSM_E_FAIL;
+    OCStackResult ret = OC_STACK_ERROR;
+    intptr_t *pMessage = NULL;
+
+    switch (result)
+    {
+        case OC_STACK_OK:
+            ret = OC::OCPlatform::findResource("",
+                                               "coap://" + hostAddress + "/oc/core?rt=SoftSensorManager.Sensor",
+                                               std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
+
+            if (ret != OC_STACK_OK)
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
+
+            break;
+
+        case OC_STACK_PRESENCE_STOPPED:
+        case OC_STACK_PRESENCE_TIMEOUT:
+            if (m_mapResources.find(hostAddress) != m_mapResources.end())
+            {
+                while (!m_mapResources[hostAddress].empty())
+                {
+                    pMessage = new intptr_t[2];
+                    pMessage[0] = RESOURCE_DISCOVER_UNINSTALL_RESOURCE;
+                    pMessage[1] = reinterpret_cast<intptr_t> (m_mapResourceHandler[m_mapResources[hostAddress].back()]);
+                    m_mapResources[hostAddress].pop_back();
+                    m_pTasker->addTask(this, pMessage);
+                }
+
+                m_mapResources.erase(hostAddress);
+            }
+            break;
+
+        case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
+            break;
+
+        default:
+            break;
+    }
+
+CLEANUP:
+    ;
+}
+
 SSMRESULT CResourceFinder::startResourceFinder()
 {
-    OCStackResult res = OC_STACK_ERROR;
+    SSMRESULT res = SSM_E_FAIL;
+    OCStackResult ret = OC_STACK_ERROR;
+
+    OC::OCPlatform::OCPresenceHandle presenceHandle = nullptr;
 
-    res = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
+    ret = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
 
-    if (res != OC_STACK_OK)
-        return SSM_E_FAIL;
+    if (ret != OC_STACK_OK)
+        SSM_CLEANUP_ASSERT(SSM_E_FAIL);
 
-    return SSM_S_OK;
+    ret = OC::OCPlatform::subscribePresence(presenceHandle, "coap://224.0.1.187",
+                                            "SoftSensorManager.Sensor",
+                                            std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
+                                                    std::placeholders::_2, std::placeholders::_3));
+
+    if (ret != OC_STACK_OK)
+        SSM_CLEANUP_ASSERT(SSM_E_FAIL);
+
+    res = SSM_S_OK;
+
+CLEANUP:
+    return res;
 }
 
 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
@@ -84,24 +152,75 @@ SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
 void CResourceFinder::onExecute(IN void *pArg)
 {
     SSMRESULT res = SSM_E_FAIL;
-    OC::QueryParamsMap  queryParams;
-    OICResourceHandler  *pResourceHandler = NULL;
-    int                 *pMessage = (int *)pArg;
-    std::shared_ptr<OC::OCResource> *pResource = NULL;
+    OCStackResult ret = OC_STACK_ERROR;
+    OC::QueryParamsMap queryParams;
+    OICResourceHandler *pResourceHandler = NULL;
+    intptr_t                 *pMessage =  reinterpret_cast<intptr_t *>(pArg);
+    std::shared_ptr< OC::OCResource > *pResource = NULL;
+    OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
+
+    std::string resourceHostAddress = "";
+    std::string resourceFullPath = "";
 
     switch (pMessage[0])
     {
         case RESOURCE_DISCOVER_REQUESTPROFILE:
-            pResource = (std::shared_ptr<OC::OCResource> *)pMessage[1];
+            pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
             pResourceHandler = new OICResourceHandler();
             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
-            m_mapResourceHandler[pResource->get()->host() + pResource->get()->uri()] = pResourceHandler;
-            pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
-                                  pResourceHandler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
+
+            resourceFullPath = pResource->get()->host() + pResource->get()->uri();
+
+            resourceHostAddress = pResource->get()->host();
+            resourceHostAddress.erase(0, 7);        // erase 'coap://'
+
+            m_mapResourceHandler[resourceFullPath] = pResourceHandler;
+
+            m_mapResources[resourceHostAddress].push_back(resourceFullPath);
+
+            ret = pResource->get()->get(queryParams,
+                                        std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
+                                                  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
+
+            if (ret != OC_STACK_OK)
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
+
+            break;
+
+        case RESOURCE_DISCOVER_INSTALL_RESOURCE:
+            if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
+                m_mapResourcePresenceHandles.end())
+            {
+                ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
+                                                        "SoftSensorManager.Sensor",
+                                                        std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
+                                                                std::placeholders::_2, std::placeholders::_3));
+
+                if (ret != OC_STACK_OK)
+                    SSM_CLEANUP_ASSERT(SSM_E_FAIL);
+
+                m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
+            }
+
+            m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
             break;
 
-        case RESOURCE_DISCOVER_SETUP_RESOURCE:
-            m_pResourceFinderEvent->onResourceFound((ISSMResource *)pMessage[1]);
+        case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
+            m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
+
+            if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
+                m_mapResourcePresenceHandles.end())
+            {
+                ret = OC::OCPlatform::unsubscribePresence(
+                          m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
+
+                if (ret != OC_STACK_OK)
+                    SSM_CLEANUP_ASSERT(SSM_E_FAIL);
+
+                m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
+            }
+
+            m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
             break;
     }
 
@@ -111,17 +230,20 @@ CLEANUP:
 
 void CResourceFinder::onTerminate(IN void *pArg)
 {
-    std::shared_ptr<OC::OCResource>     *pResource = NULL;
-    int                 *pMessage = (int *)pArg;
+    std::shared_ptr< OC::OCResource > *pResource = NULL;
+    intptr_t *pMessage = (intptr_t *)pArg;
 
     switch (pMessage[0])
     {
         case RESOURCE_DISCOVER_REQUESTPROFILE:
-            pResource = (std::shared_ptr<OC::OCResource> *)pMessage[1];
+            pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
             delete pResource;
             break;
 
-        case RESOURCE_DISCOVER_SETUP_RESOURCE:
+        case RESOURCE_DISCOVER_INSTALL_RESOURCE:
+            break;
+
+        case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
             break;
     }
 
index b070a77..1ad2ccf 100644 (file)
@@ -1,22 +1,22 @@
 /******************************************************************
-*
-* Copyright 2014 Samsung Electronics All Rights Reserved.
-*
-*
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*      http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-******************************************************************/
+ *
+ * Copyright 2014 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
 #ifndef _ResourceFinder_H_
 #define _ResourceFinder_H_
 
 #include "OCPlatform.h"
 #include "OCApi.h"
 
-class CResourceFinder :
-    public CObjectRoot<CObjectMultiThreadModel>
-    , public IResourceFinder
-    , public IThreadClient
+class CResourceFinder: public CObjectRoot< CObjectMultiThreadModel >,
+    public IResourceFinder,
+    public IThreadClient
 {
     public:
         SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
@@ -39,7 +38,7 @@ class CResourceFinder :
 
             if (IsEqualOID(objectID, OID_IResourceFinder))
             {
-                IBase *pBase = (IResourceFinder *)this;
+                IBase *pBase = (IResourceFinder *) this;
                 pBase->addRef();
                 *ppObject = pBase;
                 return SSM_S_OK;
@@ -52,7 +51,9 @@ class CResourceFinder :
         void finalRelease();
 
         SSMRESULT registerResourceFinderEvent(IN IResourceFinderEvent *pEvent);
-        void onResourceFound(std::shared_ptr<OC::OCResource> resource);
+        void onResourceFound(std::shared_ptr< OC::OCResource > resource);
+        void presenceHandler(OCStackResult result, const unsigned int nonce,
+                             const std::string &hostAddress);
         SSMRESULT startResourceFinder();
 
         SSMRESULT startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent);
@@ -65,22 +66,24 @@ class CResourceFinder :
         class OICResourceHandler
         {
             public:
+                ISSMResource m_SSMResource;
+
                 OICResourceHandler()
                 {
                     m_pEvent = NULL;
                     m_pResourceFinderClient = NULL;
                 }
 
-                SSMRESULT initHandler(std::shared_ptr<OC::OCResource> resource, IN IThreadClient    *pThreadClient)
+                SSMRESULT initHandler(std::shared_ptr< OC::OCResource > resource,
+                                      IN IThreadClient *pThreadClient)
                 {
-                    SSMRESULT   res = SSM_E_FAIL;
+                    SSMRESULT res = SSM_E_FAIL;
 
                     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
                     m_pResource = resource;
                     m_pResourceFinderClient = pThreadClient;
 
-CLEANUP:
-                    return res;
+CLEANUP: return res;
                 }
 
                 SSMRESULT startObserve(IEvent *pEvent)
@@ -90,8 +93,9 @@ CLEANUP:
                     m_pEvent = pEvent;
 
                     m_pResource.get()->observe(OC::ObserveType::Observe, queryParams,
-                                               std::bind(&OICResourceHandler::onResourceDataReceived,
-                                                         this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
+                                               std::bind(&OICResourceHandler::onResourceDataReceived, this,
+                                                         std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                                                         std::placeholders::_4));
 
                     return SSM_S_OK;
                 }
@@ -104,14 +108,14 @@ CLEANUP:
                 }
 
                 void onResourceDataReceived(const OC::HeaderOptions headerOptions,
-                                            const OC::OCRepresentation &representation,
-                                            const int &eCode, const int &sequenceNumber)
+                                            const OC::OCRepresentation &representation, const int &eCode,
+                                            const int &sequenceNumber)
                 {
                     if (eCode == 0)
                     {
-                        std::vector<ContextData>        lstCtxData;
-                        ContextData             ctxData;
-                        std::map<std::string, std::string>  outputProperty;
+                        std::vector< ContextData > lstCtxData;
+                        ContextData ctxData;
+                        std::map< std::string, std::string > outputProperty;
 
                         //Bind data
                         ctxData.rootName = m_pResource->uri().substr(1);
@@ -135,7 +139,7 @@ CLEANUP:
                         }
                         else
                         {
-                            ;//Payload is empty!!
+                            ; //Payload is empty!!
                         }
                     }
                 }
@@ -146,18 +150,17 @@ CLEANUP:
                     //unpack attributeMap
 
                     //Create SSMResource using OCResource attributeMap
-                    std::map<std::string, std::string>      outputProperty;
-                    ISSMResource        *pSSMResource = new ISSMResource();
-                    pSSMResource->location = SENSOR_LOCATION_REMOTE;
-                    pSSMResource->name = m_pResource->host() + m_pResource->uri();
-                    pSSMResource->type = m_pResource->uri().substr(1);
-                    pSSMResource->ip = m_pResource->host();
+                    std::map< std::string, std::string > outputProperty;
+                    m_SSMResource.location = SENSOR_LOCATION_REMOTE;
+                    m_SSMResource.name = m_pResource->host() + m_pResource->uri();
+                    m_SSMResource.type = m_pResource->uri().substr(1);
+                    m_SSMResource.ip = m_pResource->host();
 
                     //bind default properties
                     outputProperty["name"] = "lifetime";
                     outputProperty["type"] = "int";
                     outputProperty["value"] = "0";
-                    pSSMResource->outputProperty.push_back(outputProperty);
+                    m_SSMResource.outputProperty.push_back(outputProperty);
 
                     //bind default primitive sensor property, value to resource class for schema creating
 
@@ -167,22 +170,22 @@ CLEANUP:
                         outputProperty["name"] = representation.getValue<std::string>(toString(i * 3));
                         outputProperty["type"] = representation.getValue<std::string>(toString(i * 3 + 1));
                         outputProperty["value"] = representation.getValue<std::string>(toString(i * 3 + 2));
-                        pSSMResource->outputProperty.push_back(outputProperty);
+                        m_SSMResource.outputProperty.push_back(outputProperty);
                     }
                     /////////////////////////////////////////////////////
                     //
-                    int     *pMessage = new int[2];
-                    pMessage[0] = RESOURCE_DISCOVER_SETUP_RESOURCE;
-                    pMessage[1] = (int)pSSMResource;
+                    intptr_t      *pMessage = new intptr_t [2];
+                    pMessage[0] = RESOURCE_DISCOVER_INSTALL_RESOURCE;
+                    pMessage[1] = reinterpret_cast<intptr_t>(&m_SSMResource);
 
-                    m_pTasker->addTask(m_pResourceFinderClient, (void *)pMessage);
+                    m_pTasker->addTask(m_pResourceFinderClient, (void *) pMessage);
                 }
 
             private:
-                CObjectPtr<ITasker>                 m_pTasker;
-                std::shared_ptr<OC::OCResource>     m_pResource;
-                IThreadClient                       *m_pResourceFinderClient;
-                IEvent                              *m_pEvent;
+                CObjectPtr< ITasker > m_pTasker;
+                std::shared_ptr< OC::OCResource > m_pResource;
+                IThreadClient *m_pResourceFinderClient;
+                IEvent *m_pEvent;
 
                 std::string toString(int t)
                 {
@@ -192,10 +195,19 @@ CLEANUP:
                 }
         };
 
-        enum RESOURCE_DISCOVER_STATE {RESOURCE_DISCOVER_REQUESTPROFILE, RESOURCE_DISCOVER_SETUP_RESOURCE};
-        IResourceFinderEvent                *m_pResourceFinderEvent;
-        CObjectPtr<ITasker>             m_pTasker;
-        std::map<std::string , OICResourceHandler *> m_mapResourceHandler;
+        enum RESOURCE_DISCOVER_STATE
+        {
+            RESOURCE_DISCOVER_REQUESTPROFILE,
+            RESOURCE_DISCOVER_INSTALL_RESOURCE,
+            RESOURCE_DISCOVER_UNINSTALL_RESOURCE
+        };
+        IResourceFinderEvent *m_pResourceFinderEvent;
+        CObjectPtr< ITasker > m_pTasker;
+        std::map< std::string, OICResourceHandler * > m_mapResourceHandler;
+        std::map< std::string, std::vector<std::string> >
+        m_mapResources;    // <hostaddress, std::vector<resources> >
+        std::map< std::string, OC::OCPlatform::OCPresenceHandle >
+        m_mapResourcePresenceHandles;     // <hostaddress, presence handler>
 };
 
 #endif
index 39e0fff..73f43e7 100644 (file)
@@ -203,11 +203,15 @@ int main()
     {
         OC::OCPlatform::Configure(cfg);
 
+        OC::OCPlatform::startPresence(60);
+
         g_myResource.registerResource();
 
         int input = 0;
         cout << "Type any key to terminate" << endl;
         cin >> input;
+
+        OC::OCPlatform::stopPresence();
     }
     catch (std::exception e)
     {
index c52416b..93c8e0d 100644 (file)
@@ -203,11 +203,15 @@ int main()
     {
         OC::OCPlatform::Configure(cfg);
 
+        OC::OCPlatform::startPresence(60);
+
         g_myResource.registerResource();
 
         int input = 0;
         cout << "Type any key to terminate" << endl;
         cin >> input;
+
+        OC::OCPlatform::stopPresence();
     }
     catch (std::exception e)
     {