Implementation of JNI for subscribe device presence to RD
[platform/upstream/iotivity.git] / resource / include / OCSerialization.h
index 63668d5..693f546 100644 (file)
@@ -21,6 +21,7 @@
 #include <StringConstants.h>
 #include "ocpayload.h"
 #include "ocrandom.h"
+#include "oic_string.h"
 
 namespace OC
 {
@@ -43,299 +44,115 @@ namespace OC
                     OCDevAddr& devAddr, OCDiscoveryPayload* payload)
                     : m_clientWrapper(cw), m_devAddr(devAddr)
             {
-                OCResourcePayload* res = payload->resources;
-
-                while(res)
-                {
-                    char uuidString[UUID_STRING_SIZE];
-                    if(OCConvertUuidToString(res->sid, uuidString) != RAND_UUID_OK)
-                    {
-                        uuidString[0]= '\0';
-                    }
-
-                    if (res->secure)
-                    {
-                        m_devAddr.flags =
-                              (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
-                    }
-                    if (res->port != 0)
-                    {
-                         m_devAddr.port = res->port;
-                    }
-
-                    m_resources.push_back(std::shared_ptr<OC::OCResource>(
-                                new OC::OCResource(m_clientWrapper, m_devAddr,
-                                    std::string(res->uri),
-                                    std::string(uuidString),
-                                    (res->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE,
-                                    StringLLToVector(res->types),
-                                    StringLLToVector(res->interfaces)
-                                    )));
-                    res = res->next;
-                }
-
-            }
-
-            const std::vector<std::shared_ptr<OCResource>>& Resources() const
-            {
-                return m_resources;
-            }
-        private:
-            std::vector<std::shared_ptr<OC::OCResource>> m_resources;
-            std::weak_ptr<IClientWrapper> m_clientWrapper;
-            OCDevAddr& m_devAddr;
-    };
-    /*
-    class ListenOCContainer
-    {
-        private:
-        enum class OCSecureType
-        {
-            NotSecure,
-            Secure
-        };
-
-        class DiscoveredResources
-        {
-            class Resource
-            {
-                friend class cereal::access;
-
-                class ResourcePolicy
+                while (payload)
                 {
-                    friend class cereal::access;
-                    friend class Resource;
-
-                    template<class Archive>
-                    void serialize(Archive& ar)
+                    OCResourcePayload* res = payload->resources;
+                    while (res)
                     {
-                        try
+                        if (res->secure)
                         {
-                            m_observable = false;
-                            ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
-
-                            if(m_bm & OC_OBSERVABLE)
-                            {
-                                m_observable = true;
-                            }
+                            m_devAddr.flags =
+                                  (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
                         }
-                        catch(cereal::Exception&)
+
+                        if (res->port != 0)
                         {
-                            ar.setNextName(nullptr);
+                            m_devAddr.port = res->port;
                         }
 
-                        try
+                        if (payload->baseURI)
                         {
-                            m_secure = false;
-                            int secureTemp;
-                            ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
-                            m_secure = secureTemp != 0;
-
-                            m_port = -1;
-                            ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
+                            OCDevAddr rdPubAddr = m_devAddr;
+                            OICStrcpy(rdPubAddr.addr, sizeof(rdPubAddr.addr), payload->baseURI);
+                            rdPubAddr.port = res->port;
+                            m_resources.push_back(std::shared_ptr<OC::OCResource>(
+                                        new OC::OCResource(m_clientWrapper, rdPubAddr,
+                                            std::string(res->uri),
+                                            std::string(payload->sid),
+                                            res->bitmap,
+                                            StringLLToVector(res->types),
+                                            StringLLToVector(res->interfaces)
+                                            )));
                         }
-                        catch(cereal::Exception&)
+                        else
                         {
-                            ar.setNextName(nullptr);
+                            m_resources.push_back(std::shared_ptr<OC::OCResource>(
+                                    new OC::OCResource(m_clientWrapper, m_devAddr,
+                                        std::string(res->uri),
+                                        std::string(payload->sid),
+                                        res->bitmap,
+                                        StringLLToVector(res->types),
+                                        StringLLToVector(res->interfaces)
+                                        )));
+
+#ifdef TCP_ADAPTER
+                            if (res->tcpPort != 0)
+                            {
+                                OCDevAddr tcpDevAddr = m_devAddr;
+                                tcpDevAddr.port = res->tcpPort;
+                                tcpDevAddr.adapter = OC_ADAPTER_TCP;
+                                m_resources.push_back(std::shared_ptr<OC::OCResource>(
+                                            new OC::OCResource(m_clientWrapper, tcpDevAddr,
+                                                std::string(res->uri),
+                                                std::string(payload->sid),
+                                                res->bitmap,
+                                                StringLLToVector(res->types),
+                                                StringLLToVector(res->interfaces)
+                                                )));
+                            }
+#endif
                         }
-                     }
-
-                     bool m_observable;
-                     uint8_t m_bm;
-                     bool m_secure;
-                     int m_port;
-                };
-
-                template <class Archive>
-                void serialize(Archive& ar)
-                {
-                    try
-                    {
-                        ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
-                        m_loaded = true;
-                    }
-                    catch(cereal::Exception&)
-                    {
-                        ar.setNextName(nullptr);
-                    }
-
-                    try
-                    {
-                        ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
-                        m_loaded = true;
-                    }
-                    catch(cereal::Exception&)
-                    {
-                        ar.setNextName(nullptr);
-                    }
-
-                    try
-                    {
-                        ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
-                        m_loaded = true;
-                    }
-                    catch(cereal::Exception&)
-                    {
-                        ar.setNextName(nullptr);
-                    }
-
-                    try
-                    {
-                        ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
-                        m_loaded = true;
-                    }
-                    catch(cereal::Exception&)
-                    {
-                        ar.setNextName(nullptr);
+                        res = res->next;
                     }
-
-                    // Although not expected, a server id as part of a resource's own
-                    // representation is legal. It may be used if needed.
-                    try
-                    {
-                        ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverId));
-                        m_loaded = true;
-                    }
-                    catch(cereal::Exception&)
-                    {
-                        ar.setNextName(nullptr);
-                    }
-                }
-            public:
-                Resource(): m_loaded(false)
-                {}
-
-                bool observable() const
-                {
-                    return m_policy.m_observable;
-                }
-
-                OCSecureType secureType() const
-                {
-                    return m_policy.m_secure ? OCSecureType::Secure : OCSecureType::NotSecure;
-                }
-
-                int port() const
-                {
-                    return m_policy.m_port;
-                }
-
-                std::vector<std::string> resourceTypes() const
-                {
-                    return m_resourceTypes;
-                }
-
-                std::vector<std::string> interfaces() const
-                {
-                    return m_interfaces;
-                }
-
-                bool loaded() const{
-                    return m_loaded;
+                    payload = payload->next;
                 }
+            }
 
-                std::string m_uri;
-                std::string m_serverId;
-                std::vector<std::string> m_resourceTypes;
-                std::vector<std::string> m_interfaces;
-                ResourcePolicy m_policy;
-                bool m_loaded;
-            };
-
-            public:
-            DiscoveredResources()
-            {}
-
-            private:
-            friend class cereal::access;
-            friend class ListenOCContainer;
-
-            template <class Archive>
-            void serialize(Archive& ar)
+#ifdef WITH_MQ
+            ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
+                                OCDevAddr& devAddr, OCRepPayload* payload)
+                                : m_clientWrapper(cw), m_devAddr(devAddr)
             {
-                try
+                if (payload)
                 {
-                    ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverIdOfThisDevice));
-                }
-                catch(cereal::Exception&) { ar.setNextName(nullptr); }
+                    char**topicList = nullptr;
+                    size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0};
+                    OCRepPayloadGetStringArray(payload, "topiclist", &topicList, dimensions);
 
-                try
-                {
-                    ar(cereal::make_nvp(OC::Key::LINKS, resources));
+                    for(size_t idx = 0; idx < dimensions[0]; idx++)
+                    {
+                        m_resources.push_back(std::shared_ptr<OC::OCResource>(
+                                new OC::OCResource(m_clientWrapper, m_devAddr,
+                                                   std::string(topicList[idx]),
+                                                   "",
+                                                   OC_OBSERVABLE,
+                                                   {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
+                                                   {DEFAULT_INTERFACE})));
+                    }
                 }
-                catch(cereal::Exception&) { ar.setNextName(nullptr); }
             }
 
-            std::string m_serverIdOfThisDevice;
-            std::vector<Resource> resources;
-        };
-
-        private:
-            friend class cereal::access;
-            template <class Archive>
-            void serialize(Archive& ar)
-            {
-                std::vector<DiscoveredResources> resources;
-                ar(resources);
-            }
-        public:
             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
-                    const OCDevAddr& devAddr, std::stringstream& json)
-                    : m_clientWrapper(cw), m_devAddr(devAddr)
-
+                              OCDevAddr& devAddr, const std::string& topicUri)
+                              : m_clientWrapper(cw), m_devAddr(devAddr)
             {
-                LoadFromJson(json);
+                    m_resources.push_back(std::shared_ptr<OC::OCResource>(
+                            new OC::OCResource(m_clientWrapper, m_devAddr,
+                                               topicUri,
+                                               "",
+                                               OC_OBSERVABLE,
+                                               {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
+                                               {DEFAULT_INTERFACE})));
             }
+#endif
 
             const std::vector<std::shared_ptr<OCResource>>& Resources() const
             {
                 return m_resources;
             }
-
         private:
-            void LoadFromJson(std::stringstream& json)
-            {
-                cereal::JSONInputArchive archive(json);
-
-                std::vector<DiscoveredResources> resources;
-                archive(cereal::make_nvp(OC::Key::OCKEY, resources));
-
-                m_resources.clear();
-
-                for(const auto& resourcesAtDevice : resources)
-                {
-                    std::string serverIDForThisResourceRep = resourcesAtDevice.m_serverIdOfThisDevice;
-
-                    for (const auto& resource : resourcesAtDevice.resources)
-                    {
-                        try
-                        {
-                            if(resource.loaded())
-                            {
-                                m_resources.push_back(std::shared_ptr<OCResource>
-                                (
-                                    new OCResource
-                                    (
-                                        m_clientWrapper,
-                                        m_devAddr,
-                                        resource.m_uri,
-                                        serverIDForThisResourceRep,
-                                        resource.observable(),
-                                        resource.resourceTypes(),
-                                        resource.interfaces()
-                                    )
-                                ));
-                            }
-                        }
-                        catch(ResourceInitException& e)
-                        {
-                            oclog() << "listenCallback(): failed to create resource: " << e.what()
-                                    << std::flush;
-                        }
-                    }
-                }
-            }
+            std::vector<std::shared_ptr<OC::OCResource>> m_resources;
+            std::weak_ptr<IClientWrapper> m_clientWrapper;
+            OCDevAddr& m_devAddr;
     };
-*/
 }