Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-hosting / src / HostingObject.cpp
index 3c0a1c3..ffbd056 100644 (file)
 
 #include "HostingObject.h"
 
-namespace OIC
-{
-namespace Service
-{
+#include "RCSSeparateResponse.h"
+#include "RequestObject.h"
 
-void OIC_HOSTING_LOG(LogLevel level, const char * format, ...)
+namespace OIC
 {
-    if (!format)
+    namespace Service
     {
-        return;
-    }
-    char buffer[MAX_LOG_V_BUFFER_SIZE] = {};
-    va_list args;
-    va_start(args, format);
-    vsnprintf(buffer, sizeof buffer - 1, format, args);
-    va_end(args);
-    OCLog(level, PCF("Hosting"), buffer);
-}
 
-namespace
-{
-    std::mutex mutexForCB;
-}
+        namespace
+        {
+            const auto sizeofHostingTag = strlen("/hosting");
+        }
 
-HostingObject::HostingObject()
-: remoteObject(nullptr), mirroredServer(nullptr),
-  remoteState(ResourceState::NONE),
-  pStateChangedCB(nullptr), pDataUpdateCB(nullptr),
-  pDestroyCB(nullptr), pSetRequestHandler(nullptr)
-{
-}
+        HostingObject::HostingObject()
+        : remoteObject(nullptr), mirroredServer(nullptr),
+          pDataUpdateCB(nullptr), pDestroyCB(nullptr)
+        {
+        }
 
-HostingObject::~HostingObject()
-{
-       // shared_ptr release
-    pStateChangedCB = {};
-    pDataUpdateCB = {};
+        HostingObject::~HostingObject()
+        {
+            pDataUpdateCB = {};
 
-    if (remoteObject)
-    {
-        remoteObject->stopMonitoring();
-        remoteObject->stopCaching();
-    }
-}
+            if (remoteObject)
+            {
+                remoteObject->stopMonitoring();
+                remoteObject->stopCaching();
+            }
+        }
 
-HostingObject::RemoteObjectPtr HostingObject::getRemoteResource() const
-{
-    return remoteObject;
-}
+        auto HostingObject::getRemoteResource() const -> RemoteObjectPtr
+        {
+            return remoteObject;
+        }
 
-void HostingObject::initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB)
-{
-    remoteObject = rResource;
+        auto HostingObject::createHostingObject(const RemoteObjectPtr & rResource,
+                DestroyedCallback destroyCB) -> Ptr
+        {
+            auto newObject = std::make_shared<HostingObject>();
 
-    pStateChangedCB = std::bind(&HostingObject::stateChangedCB, this,
-            std::placeholders::_1, remoteObject);
-    pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, this,
-            std::placeholders::_1, remoteObject);
-    pDestroyCB = destroyCB;
+            newObject->remoteObject = rResource;
+            newObject->pDestroyCB = destroyCB;
 
-    pSetRequestHandler = std::bind(&HostingObject::setRequestHandler, this,
-            std::placeholders::_1, std::placeholders::_2);
+            newObject->pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, newObject,
+                    std::placeholders::_1);
 
-    try
-    {
-        remoteObject->startMonitoring(pStateChangedCB);
-        remoteObject->startCaching(pDataUpdateCB);
-    }catch(...)
-    {
-        throw;
-    }
-}
+            newObject->remoteObject->startMonitoring(
+                    std::bind(&HostingObject::stateChangedCB, newObject,
+                            std::placeholders::_1));
+            newObject->remoteObject->startCaching(newObject->pDataUpdateCB);
 
-void HostingObject::destroyHostingObject()
-{
-    pDestroyCB();
-}
+            return newObject;
+        }
 
-void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject)
-{
-    remoteState = state;
+        void HostingObject::destroyHostingObject()
+        {
+            if (pDestroyCB) pDestroyCB();
+        }
 
-    switch (state)
-    {
-    case ResourceState::ALIVE:
-    {
-        if(rObject->isCaching() == false)
+        void HostingObject::stateChangedCB(ResourceState state)
         {
-            try
+            switch (state)
             {
-                rObject->startCaching(pDataUpdateCB);
-            }catch(const RCSInvalidParameterException &e)
+            case ResourceState::ALIVE:
             {
-                OIC_HOSTING_LOG(DEBUG,
-                        "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
-                        e.what());
+                if (!this->remoteObject->isCaching())
+                {
+                    try
+                    {
+                        this->remoteObject->startCaching(pDataUpdateCB);
+                    } catch (const RCSException & e)
+                    {
+                        OIC_HOSTING_LOG(DEBUG,
+                                "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
+                                e.what());
+                    }
+                }
+                break;
             }
-        }
-        break;
-    }
-    case ResourceState::LOST_SIGNAL:
-    case ResourceState::DESTROYED:
-    {
-        if(rObject->isCaching() == true)
-        {
-            try
-            {
-                rObject->stopCaching();
-            }catch(const RCSInvalidParameterException &e)
+            case ResourceState::LOST_SIGNAL:
+            case ResourceState::DESTROYED:
             {
-                OIC_HOSTING_LOG(DEBUG,
-                        "[HostingObject::stateChangedCB]stopCaching InvalidParameterException:%s",
-                        e.what());
+                try
+                {
+                    this->remoteObject->stopCaching();
+                    this->remoteObject->stopMonitoring();
+                } catch (const RCSException & e)
+                {
+                    OIC_HOSTING_LOG(DEBUG,
+                            "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
+                            e.what());
+                }
+                mirroredServer.reset();
+                destroyHostingObject();
+                break;
+            }
+            default:
+                // not support of state
+                break;
             }
         }
-        if(rObject->isMonitoring() == true)
+
+        void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes)
         {
-            try
+            if (attributes.empty())
             {
-                rObject->stopMonitoring();
-            }catch(const RCSInvalidParameterException &e)
-            {
-                OIC_HOSTING_LOG(DEBUG,
-                        "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
-                        e.what());
+                return;
             }
-        }
-        mirroredServer = nullptr;
-        destroyHostingObject();
-        break;
-    }
-    default:
-        // not support of state
-        break;
-    }
-}
 
-void HostingObject::dataChangedCB(const RCSResourceAttributes & attributes, RemoteObjectPtr rObject)
-{
-    std::unique_lock<std::mutex> lock(mutexForCB);
-    if(attributes.empty())
-    {
-        return;
-    }
+            std::unique_lock<std::mutex> lock(mutexForCB);
+            if (mirroredServer == nullptr)
+            {
+                try
+                {
+                    mirroredServer = createMirroredServer(this->remoteObject);
+                } catch (const RCSException & e)
+                {
+                    OIC_HOSTING_LOG(DEBUG,
+                                "[HostingObject::dataChangedCB]createMirroredServer Exception:%s",
+                                e.what());
+                    return;
+                }
+            }
+            lock.unlock();
 
-    if(mirroredServer == nullptr)
-    {
-        try
-        {
-            mirroredServer = createMirroredServer(rObject);
-        }catch(const RCSPlatformException &e)
-        {
-            OIC_HOSTING_LOG(DEBUG,
-                        "[HostingObject::dataChangedCB]createMirroredServer PlatformException:%s",
-                        e.what());
-            mirroredServer = nullptr;
-            return;
+            RCSResourceObject::LockGuard guard(mirroredServer);
+            mirroredServer->getAttributes() = std::move(attributes);
         }
-    }
 
-    RCSResourceAttributes rData;
-    {
-        RCSResourceObject::LockGuard guard(mirroredServer);
-        rData = mirroredServer->getAttributes();
-    }
-    if(rData.empty() || rData != attributes)
-    {
+        auto HostingObject::createMirroredServer(RemoteObjectPtr rObject) -> ResourceObjectPtr
         {
-            RCSResourceObject::LockGuard guard(mirroredServer);
-            for(auto it = rData.begin(); ; ++it)
+            if (rObject == nullptr)
             {
-                if(it == rData.end())
-                {
-                    break;
-                }
-                mirroredServer->removeAttribute(it->key());
+                throw RCSPlatformException(OC_STACK_ERROR);
             }
 
-            for(auto it = attributes.begin();; ++it)
+            std::string fulluri = rObject->getUri();
+            std::string uri = fulluri.substr(0, fulluri.size() - sizeofHostingTag);
+            std::vector<std::string> types = rObject->getTypes();
+            std::vector<std::string> interfaces = rObject->getInterfaces();
+            try
             {
-                if(it == attributes.end())
+                auto resourceBuild = RCSResourceObject::Builder(uri, types[0], interfaces[0]);
+                for (unsigned int i = 1; i < types.size(); ++i)
+                {
+                    resourceBuild.addType(types[i]);
+                }
+                for (unsigned int i = 1; i < interfaces.size(); ++i)
                 {
-                    break;
+                    resourceBuild.addInterface(interfaces[i]);
                 }
-                mirroredServer->setAttribute(it->key(), it->value());
+                auto retResource = resourceBuild.build();
+
+                retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
+                retResource->setSetRequestHandler(
+                        std::bind(&HostingObject::setRequestHandler, this,
+                                std::placeholders::_1, std::placeholders::_2));
+                return retResource;
+            } catch (...)
+            {
+                OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "Exception");
+                throw;
             }
         }
-    }
-}
 
-HostingObject::ResourceObjectPtr HostingObject::createMirroredServer(RemoteObjectPtr rObject)
-{
-    ResourceObjectPtr retResource = nullptr;
-    if(rObject != nullptr)
-    {
-        std::string fulluri = rObject->getUri();
-        std::string uri = fulluri.substr(0, fulluri.size()-8);
-        std::vector<std::string> types = rObject->getTypes();
-        std::vector<std::string> interfaces = rObject->getInterfaces();
-        try
-        {
-            std::string type = types.begin()->c_str();
-            std::string interface = interfaces.begin()->c_str();
-            retResource = RCSResourceObject::Builder(uri, type, interface).
-                    setDiscoverable(true).setObservable(true).build();
-
-            // TODO need to bind types and interfaces
-            retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
-            retResource->setSetRequestHandler(pSetRequestHandler);
-        }catch(...)
+        RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
+                    RCSResourceAttributes & resourceAttibutes)
         {
-            OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "PlatformException");
-            throw;
-        }
-    }
-    else
-    {
-        throw RCSPlatformException(OC_STACK_ERROR);
-    }
-
-    return retResource;
-}
+            try
+            {
+                RequestObject::invokeRequest(getRemoteResource(),
+                        primitiveRequest, RequestObject::RequestMethod::Set, resourceAttibutes);
 
-RCSSetResponse HostingObject::setRequestHandler(const RCSRequest & primitiveRequest,
-            RCSResourceAttributes & resourceAttibutes)
-{
-    (void)primitiveRequest;
-    try
-    {
-        RequestObject newRequest = { };
-        newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Setter,
-                resourceAttibutes);
-    }catch(const RCSPlatformException &e)
-    {
-        OIC_HOSTING_LOG(DEBUG,
-                "[HostingObject::setRequestHandler] PlatformException:%s",
-                e.what());
-        throw;
-    }
+            } catch (const RCSPlatformException & e)
+            {
+                OIC_HOSTING_LOG(DEBUG,
+                        "[HostingObject::setRequestHandler] PlatformException:%s",
+                        e.what());
+                throw;
+            }
 
-    return RCSSetResponse::create(resourceAttibutes);
-}
+            return RCSSetResponse::separate();
+        }
 
-} /* namespace Service */
+    } /* namespace Service */
 } /* namespace OIC */