X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Finclude%2FOCSerialization.h;h=e440ab0c91a98e06ac6ffc4de17c3e63bf2e1e38;hb=c315c87e07c4080ecd0ef488e7a1047bc3c509b2;hp=c9e0ce36919b3c89acc7d15f9d8764375b094b0e;hpb=2c79c70f6cf44c590dbe04ebbd9a1f656856c8a3;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/include/OCSerialization.h b/resource/include/OCSerialization.h index c9e0ce3..e440ab0 100644 --- a/resource/include/OCSerialization.h +++ b/resource/include/OCSerialization.h @@ -18,302 +18,153 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include -#include -#include -#include - #include +#include "ocpayload.h" +#include "ocrandom.h" +#include "oic_string.h" namespace OC { class ListenOCContainer { private: - enum class OCSecureType - { - IPv4Secure, - IPv4 - }; - - class ListenResourceContainer - { - class ListenResourcePolicyContainer + static std::vector StringLLToVector(OCStringLL* ll) { - friend class cereal::access; - friend class ListenResourceContainer; - friend class ListenResourcePropertiesContainer; - - template - void serialize(Archive& ar) + std::vector strs; + while(ll) { - try - { - m_observable = false; - ar(cereal::make_nvp(OC::Key::BMKEY, m_bm)); - // In case of observable - if(m_bm & OC_OBSERVABLE) - { - m_observable = true; - } - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - try - { - 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)); - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - - } - - bool m_observable; - uint8_t m_bm; - bool m_secure; - int m_port; - }; + strs.push_back(ll->value); + ll = ll->next; + } + return strs; + } - class ListenResourcePropertiesContainer + public: + ListenOCContainer(std::weak_ptr cw, + OCDevAddr& devAddr, OCDiscoveryPayload* payload) { - friend class cereal::access; - friend class ListenResourceContainer; + OCDevAddr currentDevAddr = devAddr; - template - void serialize(Archive& ar) + while (payload) { - try - { - ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy)); - - } - catch(cereal::Exception&) + std::string deviceName; + if (payload->name) { - // we swallow this exception, since it means the key - // doesn't exist, allowing these to be optional - oclog() << "Invalid POLICYKEY"<name; } - try - { - ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes)); - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - try - { - ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces)); - } - catch(cereal::Exception&) + OCResourcePayload* res = payload->resources; + while (res) { - ar.setNextName(nullptr); - } - } - std::vector m_resourceTypes; - std::vector m_interfaces; - ListenResourcePolicyContainer m_policy; - }; + currentDevAddr.flags = res->secure ? + (OCTransportFlags)(OC_FLAG_SECURE | devAddr.flags) : + devAddr.flags; - public: - ListenResourceContainer() : m_loaded(false) - {} + currentDevAddr.port = (res->port != 0) ? res->port : devAddr.port; - private: - friend class cereal::access; - friend class ListenOCContainer; - - template - 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::SERVERIDKEY, m_serverId)); - m_loaded=true; - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); - } - try - { - ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props)); - m_loaded=true; - } - catch(cereal::Exception&) - { - ar.setNextName(nullptr); + if (payload->baseURI) + { + OCDevAddr rdPubAddr = currentDevAddr; + + std::string baseURI = std::string(payload->baseURI); + size_t len = baseURI.length(); + int addressLen = baseURI.find_first_of(":"); + std::string ipaddress = baseURI.substr(0, addressLen); + int port = atoi(baseURI.substr(addressLen + 1, len).c_str()); + OICStrcpy(rdPubAddr.addr, addressLen + 1, ipaddress.c_str()); + rdPubAddr.port = port; + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, rdPubAddr, + std::string(res->uri), + std::string(payload->sid), + res->bitmap, + StringLLToVector(res->types), + StringLLToVector(res->interfaces), + deviceName + ))); + } + else + { + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, currentDevAddr, + std::string(res->uri), + std::string(payload->sid), + res->bitmap, + StringLLToVector(res->types), + StringLLToVector(res->interfaces), + deviceName + ))); + +#ifdef TCP_ADAPTER + if (res->tcpPort != 0) + { + OCDevAddr tcpDevAddr = currentDevAddr; + tcpDevAddr.port = res->tcpPort; + tcpDevAddr.adapter = OC_ADAPTER_TCP; + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, tcpDevAddr, + std::string(res->uri), + std::string(payload->sid), + res->bitmap, + StringLLToVector(res->types), + StringLLToVector(res->interfaces), + deviceName + ))); + } +#endif + } + res = res->next; + } + payload = payload->next; } - - } - - std::string m_uri; - std::string m_serverId; - bool m_loaded; - ListenResourcePropertiesContainer m_props; - - bool loaded() const - { - return m_loaded; - } - - bool observable() const - { - return m_props.m_policy.m_observable; } - OCSecureType secureType() const +#ifdef WITH_MQ + ListenOCContainer(std::weak_ptr cw, + OCDevAddr& devAddr, OCRepPayload* payload) { - return m_props.m_policy.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4; - } - - int port() const - { - return m_props.m_policy.m_port; - } - - std::vector resourceTypes() const - { - return m_props.m_resourceTypes; - } + if (payload) + { + char**topicList = nullptr; + size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0}; + OCRepPayloadGetStringArray(payload, "topiclist", &topicList, dimensions); - std::vector interfaces() const - { - return m_props.m_interfaces; + for(size_t idx = 0; idx < dimensions[0]; idx++) + { + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, devAddr, + std::string(topicList[idx]), + "", + OC_OBSERVABLE, + {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC}, + {DEFAULT_INTERFACE}, + deviceName + ))); + } + } } - }; - private: - friend class cereal::access; - template - void serialize(Archive& ar) - { - std::vector resources; - ar(resources); - } - public: - ListenOCContainer(std::weak_ptr cw, const OCDevAddr& address, - OCConnectivityType connectivityType, std::stringstream& json): - m_clientWrapper(cw), m_address(address), m_connectivityType(connectivityType) + ListenOCContainer(std::weak_ptr cw, + OCDevAddr& devAddr, const std::string& topicUri) { - LoadFromJson(json); + m_resources.push_back(std::shared_ptr( + new OC::OCResource(cw, devAddr, + topicUri, + "", + OC_OBSERVABLE, + {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC}, + {DEFAULT_INTERFACE}, + deviceName + ))); } +#endif const std::vector>& Resources() const { return m_resources; } - private: - std::string ConvertOCAddrToString(OCSecureType sec, int secureport) - { - uint16_t port; - std::ostringstream os; - - if(sec== OCSecureType::IPv4) - { - os<<"coap://"; - } - else if(sec == OCSecureType::IPv4Secure) - { - os<<"coaps://"; - } - else - { - oclog() << "ConvertOCAddrToString(): invalid SecureType"<(a)<<"."<(b) - <<"."<(c)<<"."<(d); - - if(sec == OCSecureType::IPv4Secure && secureport>0 && secureport<=65535) - { - port = static_cast(secureport); - } - else if(sec == OCSecureType::IPv4 && 0==OCDevAddrToPort(&m_address, &port)) - { - // nothing to do, this is a successful case - } - else - { - oclog() << "ConvertOCAddrToString() : Invalid Port" - <(port); - - return os.str(); - } - - void LoadFromJson(std::stringstream& json) - { - cereal::JSONInputArchive archive(json); - - std::vector resources; - archive(cereal::make_nvp(OC::Key::OCKEY, resources)); - - m_resources.clear(); - - for(const auto& res : resources) - { - try - { - if(res.loaded()) - { - m_resources.push_back(std::shared_ptr( - new OCResource(m_clientWrapper, - ConvertOCAddrToString(res.secureType(),res.port()), - res.m_uri, res.m_serverId, m_connectivityType, res.observable(), - res.resourceTypes(), res.interfaces()))); - } - - } - catch(ResourceInitException& e) - { - oclog() << "listenCallback(): failed to create resource: " << e.what() - << std::flush; - } - } - } std::vector> m_resources; - std::weak_ptr m_clientWrapper; - OCDevAddr m_address; - OCConnectivityType m_connectivityType; }; } -